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

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

Collaboration diagram for lsnet.LSNetClassifier:
Collaboration graph

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.
 

Public Attributes

 num_classes
 
 model_size
 
 dropout_rate
 
 pretrained
 
 freeze_backbone
 
 learning_rate
 
 weight_decay
 
 num_epochs
 
 batch_size
 
 num_workers
 
 pin_memory
 
 shuffle
 
 drop_last
 
 device
 
 model
 
 optimizer
 
 scheduler
 
 criterion
 
 transform
 

Protected Member Functions

 _load_pretrained_weights (self)
 Load pre-trained weights for the model.
 
 _freeze_backbone (self)
 Freeze the backbone of the model.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ __init__()

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.

Parameters
num_classesNumber of classes to classify
model_sizeSize of the LSNet model ('t' for tiny, 's' for small, 'b' for base)
learning_rateLearning rate for the optimizer
weight_decayWeight decay for regularization
deviceDevice to use for training ('cuda' or 'cpu')
num_epochsNumber of training epochs
batch_sizeBatch size for training
num_workersNumber of workers for data loading
pretrainedWhether to use pre-trained weights
freeze_backboneWhether to freeze the backbone
configConfiguration 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

Member Function Documentation

◆ _freeze_backbone()

lsnet.LSNetClassifier._freeze_backbone (   self)
protected

Freeze the backbone of the model.

Freeze the backbone of the model.

◆ _load_pretrained_weights()

lsnet.LSNetClassifier._load_pretrained_weights (   self)
protected

Load pre-trained weights for the model.

Load pre-trained weights for the model.

◆ evaluate()

lsnet.LSNetClassifier.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

◆ inference()

int lsnet.LSNetClassifier.inference (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image 
)

Run inference on a single image.

Parameters
imageImage to classify
Returns
Predicted class label
Run inference on a single image.

Args:
    image: Image to classify
    
Returns:
    Predicted class label

◆ load()

None lsnet.LSNetClassifier.load (   self,
str  path 
)

Load the model from disk.

Parameters
pathPath to load the model from
Load the model from disk.

Args:
    path: Path to load the model from

◆ predict_proba()

np.ndarray lsnet.LSNetClassifier.predict_proba (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image 
)

Get class probabilities for an image.

Parameters
imageImage to classify
Returns
Class probabilities as a numpy array
Get class probabilities for an image.

Args:
    image: Image to classify
    
Returns:
    Class probabilities as a numpy array

◆ save()

None lsnet.LSNetClassifier.save (   self,
str  path 
)

Save the model to disk.

Parameters
pathPath to save the model to
Save the model to disk.

Args:
    path: Path to save the model to

◆ train_model()

lsnet.LSNetClassifier.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_loggerTensorBoard logger instance for metrics visualization
**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: TensorBoard logger instance for metrics visualization
    **kwargs: Additional training parameters
    
Returns:
    Validation accuracy or training accuracy

Member Data Documentation

◆ batch_size

lsnet.LSNetClassifier.batch_size

◆ criterion

lsnet.LSNetClassifier.criterion

◆ device

lsnet.LSNetClassifier.device

◆ drop_last

lsnet.LSNetClassifier.drop_last

◆ dropout_rate

lsnet.LSNetClassifier.dropout_rate

◆ freeze_backbone

lsnet.LSNetClassifier.freeze_backbone

◆ learning_rate

lsnet.LSNetClassifier.learning_rate

◆ model

lsnet.LSNetClassifier.model

◆ model_size

lsnet.LSNetClassifier.model_size

◆ num_classes

lsnet.LSNetClassifier.num_classes

◆ num_epochs

lsnet.LSNetClassifier.num_epochs

◆ num_workers

lsnet.LSNetClassifier.num_workers

◆ optimizer

lsnet.LSNetClassifier.optimizer

◆ pin_memory

lsnet.LSNetClassifier.pin_memory

◆ pretrained

lsnet.LSNetClassifier.pretrained

◆ scheduler

lsnet.LSNetClassifier.scheduler

◆ shuffle

lsnet.LSNetClassifier.shuffle

◆ transform

lsnet.LSNetClassifier.transform

◆ weight_decay

lsnet.LSNetClassifier.weight_decay

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