LSNet-based classifier for document classification. More...

Public Member Functions | |
| __init__ (self, Optional[int] num_classes=None, str model_size='t', float learning_rate=0.001, float weight_decay=0.0001, Optional[str] device=None, int num_epochs=100, int batch_size=64, int num_workers=4, bool pretrained=False, bool freeze_backbone=False, Optional[Dict[str, Any]] config=None) | |
| Constructor for LSNetClassifier. | |
| train_model (self, train_loader, val_loader=None, tb_logger=None, **kwargs) | |
| Train the model using DataLoader objects. | |
| int | inference (self, Union[Image.Image, torch.Tensor, np.ndarray] image) |
| Run inference on a single image. | |
| np.ndarray | predict_proba (self, Union[Image.Image, torch.Tensor, np.ndarray] image) |
| 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 model to disk. | |
| None | load (self, str path) |
| Load the model from disk. | |
Protected Member Functions | |
| _load_pretrained_weights (self) | |
| Load pre-trained weights for the model. | |
| _freeze_backbone (self) | |
| Freeze the backbone of the model. | |
LSNet-based classifier for document classification.
This class implements a document classifier using the LSNet model, providing methods for training, evaluation, and inference.
A classifier using the LSNet model for image classification.
| lsnet.LSNetClassifier.__init__ | ( | self, | |
| Optional[int] | num_classes = None, |
||
| str | model_size = 't', |
||
| float | learning_rate = 0.001, |
||
| float | weight_decay = 0.0001, |
||
| Optional[str] | device = None, |
||
| int | num_epochs = 100, |
||
| int | batch_size = 64, |
||
| int | num_workers = 4, |
||
| bool | pretrained = False, |
||
| bool | freeze_backbone = False, |
||
| Optional[Dict[str, Any]] | config = None |
||
| ) |
Constructor for LSNetClassifier.
| num_classes | Number of classes to classify |
| model_size | Size of the LSNet model ('t' for tiny, 's' for small, 'b' for base) |
| 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 |
| batch_size | Batch size for training |
| num_workers | Number of workers for data loading |
| pretrained | Whether to use pre-trained weights |
| freeze_backbone | Whether to freeze the backbone |
| config | Configuration dictionary containing model and training parameters |
Initialize the LSNet classifier.
Can be initialized either with direct parameters or with a config dictionary.
If config is provided, it takes precedence over direct parameters.
Args:
num_classes: Number of classes to classify
model_size: Size of the LSNet model ('t' for tiny, 's' for small, 'b' for base)
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
batch_size: Batch size for training
num_workers: Number of workers for data loading
pretrained: Whether to use pre-trained weights
freeze_backbone: Whether to freeze the backbone
config: Configuration dictionary containing model and training parameters
|
protected |
Freeze the backbone of the model.
Freeze the backbone of the model.
|
protected |
Load pre-trained weights for the model.
Load pre-trained weights for the model.
| lsnet.LSNetClassifier.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
| int lsnet.LSNetClassifier.inference | ( | self, | |
| Union[Image.Image, torch.Tensor, np.ndarray] | image | ||
| ) |
Run inference on a single image.
| image | Image to classify |
Run inference on a single image.
Args:
image: Image to classify
Returns:
Predicted class label
| None lsnet.LSNetClassifier.load | ( | self, | |
| str | path | ||
| ) |
Load the model from disk.
| path | Path to load the model from |
Load the model from disk.
Args:
path: Path to load the model from
| np.ndarray lsnet.LSNetClassifier.predict_proba | ( | self, | |
| Union[Image.Image, torch.Tensor, np.ndarray] | image | ||
| ) |
Get class probabilities for an image.
| image | Image to classify |
Get class probabilities for an image.
Args:
image: Image to classify
Returns:
Class probabilities as a numpy array
| None lsnet.LSNetClassifier.save | ( | self, | |
| str | path | ||
| ) |
Save the model to disk.
| path | Path to save the model to |
Save the model to disk.
Args:
path: Path to save the model to
| lsnet.LSNetClassifier.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 | TensorBoard logger instance for metrics visualization |
| **kwargs | Additional training parameters |
Train the model using DataLoader objects.
Args:
train_loader: Training DataLoader
val_loader: Validation DataLoader
tb_logger: TensorBoard logger instance for metrics visualization
**kwargs: Additional training parameters
Returns:
Validation accuracy or training accuracy
| lsnet.LSNetClassifier.batch_size |
| lsnet.LSNetClassifier.criterion |
| lsnet.LSNetClassifier.device |
| lsnet.LSNetClassifier.drop_last |
| lsnet.LSNetClassifier.dropout_rate |
| lsnet.LSNetClassifier.freeze_backbone |
| lsnet.LSNetClassifier.learning_rate |
| lsnet.LSNetClassifier.model |
| lsnet.LSNetClassifier.model_size |
| lsnet.LSNetClassifier.num_classes |
| lsnet.LSNetClassifier.num_epochs |
| lsnet.LSNetClassifier.num_workers |
| lsnet.LSNetClassifier.optimizer |
| lsnet.LSNetClassifier.pin_memory |
| lsnet.LSNetClassifier.pretrained |
| lsnet.LSNetClassifier.scheduler |
| lsnet.LSNetClassifier.shuffle |
| lsnet.LSNetClassifier.transform |
| lsnet.LSNetClassifier.weight_decay |