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

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 | |
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.
| 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.
| num_classes | Number of document classes |
| 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) |
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)
| layout.LayoutLMv3Classifier.evaluate | ( | self, | |
| val_loader, | |||
tb_logger = None, |
|||
step = None |
|||
| ) |
Evaluate the model on validation set.
| val_loader | Validation DataLoader |
| tb_logger | TensorBoard logger instance for metrics visualization |
| step | Current step for logging |
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
| 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.
| image | Image to classify |
| words | List of OCR words (optional) |
| boxes | List of bounding boxes for words (optional) |
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
| None layout.LayoutLMv3Classifier.load | ( | self, | |
| str | path | ||
| ) |
Load a trained model from disk.
| path | Path to the saved model |
Load a trained model from disk.
Args:
path: Path to the saved model
| 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.
| image | Image to classify |
| words | List of OCR words (optional) |
| boxes | List of bounding boxes for words (optional) |
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
| 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.
| 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) |
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
| None layout.LayoutLMv3Classifier.save | ( | self, | |
| str | path | ||
| ) |
Save the trained model and processor to disk.
| path | Path to save the model |
Save the trained model and processor to disk.
Args:
path: Path to save the model
| layout.LayoutLMv3Classifier.train_model | ( | self, | |
| train_loader, | |||
val_loader = None, |
|||
tb_logger = None, |
|||
| ** | kwargs | ||
| ) |
Train the model using DataLoader objects.
| train_loader | Training DataLoader |
| val_loader | Validation DataLoader |
| tb_logger | Optional logger for TensorBoard logging |
| **kwargs | Additional training parameters |
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
| layout.LayoutLMv3Classifier.apply_ocr |
| layout.LayoutLMv3Classifier.criterion |
| layout.LayoutLMv3Classifier.device |
| layout.LayoutLMv3Classifier.learning_rate |
| layout.LayoutLMv3Classifier.max_length |
| layout.LayoutLMv3Classifier.model |
| layout.LayoutLMv3Classifier.model_name |
| layout.LayoutLMv3Classifier.num_classes |
| layout.LayoutLMv3Classifier.num_epochs |
| layout.LayoutLMv3Classifier.optimizer |
| layout.LayoutLMv3Classifier.processor |
| layout.LayoutLMv3Classifier.weight_decay |