DAPPER (Document Analysis, Processing, and Pattern Extraction Repository)
Statistical Learning Final Project for Document Classification
 
Loading...
Searching...
No Matches
layout.LayoutLMv3Classifier Class Reference

LayoutLMv3-based document classifier for document understanding and classification. More...

Collaboration diagram for layout.LayoutLMv3Classifier:
Collaboration graph

Public Member Functions

 __init__ (self, int num_classes, str model_name="microsoft/layoutlmv3-base", float learning_rate=5e-5, float weight_decay=0.01, Optional[str] device=None, int num_epochs=10, bool apply_ocr=False, int max_length=512, Optional[str] processor_name=None)
 Constructor for LayoutLMv3Classifier class.
 
Dict[str, torch.Tensor] preprocess_image (self, Union[Image.Image, np.ndarray] image, Optional[List[str]] words=None, Optional[List[List[int]]] boxes=None)
 Preprocess an image for LayoutLMv3.
 
 train_model (self, train_loader, val_loader=None, tb_logger=None, **kwargs)
 Train the model using DataLoader objects.
 
int inference (self, Union[Image.Image, np.ndarray] image, Optional[List[str]] words=None, Optional[List[List[int]]] boxes=None)
 Run inference on a single image.
 
np.ndarray predict_proba (self, Union[Image.Image, np.ndarray] image, Optional[List[str]] words=None, Optional[List[List[int]]] boxes=None)
 Get class probabilities for an image.
 
 evaluate (self, val_loader, tb_logger=None, step=None)
 Evaluate the model on validation set.
 
None save (self, str path)
 Save the trained model and processor to disk.
 
None load (self, str path)
 Load a trained model from disk.
 

Public Attributes

 num_classes
 
 model_name
 
 learning_rate
 
 weight_decay
 
 num_epochs
 
 apply_ocr
 
 max_length
 
 device
 
 processor
 
 model
 
 optimizer
 
 criterion
 

Detailed Description

LayoutLMv3-based document classifier for document understanding and classification.

This class implements a document classifier using LayoutLMv3, a multimodal model that combines text and layout information for document understanding. It provides state-of-the-art performance for document classification tasks.

A document classifier using LayoutLMv3 for document understanding and classification.

Constructor & Destructor Documentation

◆ __init__()

layout.LayoutLMv3Classifier.__init__ (   self,
int  num_classes,
str   model_name = "microsoft/layoutlmv3-base",
float   learning_rate = 5e-5,
float   weight_decay = 0.01,
Optional[str]   device = None,
int   num_epochs = 10,
bool   apply_ocr = False,
int   max_length = 512,
Optional[str]   processor_name = None 
)

Constructor for LayoutLMv3Classifier class.

Parameters
num_classesNumber of document classes
model_nameName or path of the pre-trained LayoutLMv3 model
learning_rateLearning rate for the optimizer
weight_decayWeight decay for regularization
deviceDevice to use for training ('cuda' or 'cpu')
num_epochsNumber of training epochs
apply_ocrWhether to apply OCR within the processor
max_lengthMaximum sequence length for tokenization
processor_nameName of the processor to use (defaults to model_name if None)
Initialize the LayoutLMv3 classifier.

Args:
    num_classes: Number of classes to classify
    model_name: Name or path of the pre-trained LayoutLMv3 model
    learning_rate: Learning rate for the optimizer
    weight_decay: Weight decay for regularization
    device: Device to use for training ('cuda' or 'cpu')
    num_epochs: Number of training epochs
    apply_ocr: Whether to apply OCR within the processor
    max_length: Maximum sequence length for tokenization
    processor_name: Name of the processor to use (defaults to model_name if None)

Member Function Documentation

◆ evaluate()

layout.LayoutLMv3Classifier.evaluate (   self,
  val_loader,
  tb_logger = None,
  step = None 
)

Evaluate the model on validation set.

Parameters
val_loaderValidation DataLoader
tb_loggerTensorBoard logger instance for metrics visualization
stepCurrent step for logging
Returns
Validation accuracy and loss
Evaluate the model on validation set.

Args:
    val_loader: Validation DataLoader
    tb_logger: TensorBoard logger instance for metrics visualization
    step: Current step for logging
    
Returns:
    Validation accuracy and loss

◆ inference()

int layout.LayoutLMv3Classifier.inference (   self,
Union[Image.Image, np.ndarray]  image,
Optional[List[str]]   words = None,
Optional[List[List[int]]]   boxes = None 
)

Run inference on a single image.

Parameters
imageImage to classify
wordsList of OCR words (optional)
boxesList of bounding boxes for words (optional)
Returns
Predicted class label
Run inference on a single image.

Args:
    image: Image to classify
    words: List of OCR words (optional)
    boxes: List of bounding boxes for words (optional)
    
Returns:
    Predicted class label

◆ load()

None layout.LayoutLMv3Classifier.load (   self,
str  path 
)

Load a trained model from disk.

Parameters
pathPath to the saved model
Load a trained model from disk.

Args:
    path: Path to the saved model

◆ predict_proba()

np.ndarray layout.LayoutLMv3Classifier.predict_proba (   self,
Union[Image.Image, np.ndarray]  image,
Optional[List[str]]   words = None,
Optional[List[List[int]]]   boxes = None 
)

Get class probabilities for an image.

Parameters
imageImage to classify
wordsList of OCR words (optional)
boxesList of bounding boxes for words (optional)
Returns
Array of class probabilities
Get class probabilities for an image.

Args:
    image: Image to classify
    words: List of OCR words (optional)
    boxes: List of bounding boxes for words (optional)
    
Returns:
    Array of class probabilities

◆ preprocess_image()

Dict[str, torch.Tensor] layout.LayoutLMv3Classifier.preprocess_image (   self,
Union[Image.Image, np.ndarray]  image,
Optional[List[str]]   words = None,
Optional[List[List[int]]]   boxes = None 
)

Preprocess an image for LayoutLMv3.

Parameters
imageImage as PIL Image or numpy array
wordsList of OCR words (optional, if not using internal OCR)
boxesList of bounding boxes for words (optional, if not using internal OCR)
Returns
Preprocessed input dictionary for LayoutLMv3
Preprocess an image for LayoutLMv3.

Args:
    image: Image as PIL Image or numpy array
    words: List of OCR words (optional, if not using internal OCR)
    boxes: List of bounding boxes for words (optional, if not using internal OCR)
    
Returns:
    Preprocessed input dictionary for LayoutLMv3

◆ save()

None layout.LayoutLMv3Classifier.save (   self,
str  path 
)

Save the trained model and processor to disk.

Parameters
pathPath to save the model
Save the trained model and processor to disk.

Args:
    path: Path to save the model

◆ train_model()

layout.LayoutLMv3Classifier.train_model (   self,
  train_loader,
  val_loader = None,
  tb_logger = None,
**  kwargs 
)

Train the model using DataLoader objects.

Parameters
train_loaderTraining DataLoader
val_loaderValidation DataLoader
tb_loggerOptional logger for TensorBoard logging
**kwargsAdditional training parameters
Returns
Validation accuracy or training accuracy
Train the model using DataLoader objects.

Args:
    train_loader: Training DataLoader
    val_loader: Validation DataLoader
    tb_logger: Optional logger for TensorBoard logging
    **kwargs: Additional training parameters
    
Returns:
    Validation accuracy or training accuracy

Member Data Documentation

◆ apply_ocr

layout.LayoutLMv3Classifier.apply_ocr

◆ criterion

layout.LayoutLMv3Classifier.criterion

◆ device

layout.LayoutLMv3Classifier.device

◆ learning_rate

layout.LayoutLMv3Classifier.learning_rate

◆ max_length

layout.LayoutLMv3Classifier.max_length

◆ model

layout.LayoutLMv3Classifier.model

◆ model_name

layout.LayoutLMv3Classifier.model_name

◆ num_classes

layout.LayoutLMv3Classifier.num_classes

◆ num_epochs

layout.LayoutLMv3Classifier.num_epochs

◆ optimizer

layout.LayoutLMv3Classifier.optimizer

◆ processor

layout.LayoutLMv3Classifier.processor

◆ weight_decay

layout.LayoutLMv3Classifier.weight_decay

The documentation for this class was generated from the following file: