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

Wrapper class for training and using the EAML model. More...

Collaboration diagram for eaml.EAMLClassifier:
Collaboration graph

Public Member Functions

 __init__ (self, int num_classes, int vocab_size, int embedding_dim=100, int word_hidden_dim=50, int sent_hidden_dim=50, int image_channels=3, int image_feature_dim=128, Tuple[int, int] image_size=(224, 224), float dropout=0.5, float learning_rate=0.001, Optional[str] device=None, int num_epochs=10, bool use_ocr_text=False)
 Constructor for EAMLClassifier class.
 
torch.Tensor preprocess_image (self, Union[Image.Image, np.ndarray] image)
 Preprocess an image for the EAML model.
 
 train_model (self, train_loader, val_loader=None, tb_logger=None, save_dir="models", patience=5, **kwargs)
 Train the EAML model.
 
 evaluate (self, data_loader, tb_logger=None, step=None)
 Evaluate the model on a dataset.
 
int inference (self, torch.Tensor docs, Union[Image.Image, np.ndarray] image)
 Run inference on a single text/image pair.
 
np.ndarray predict_proba (self, torch.Tensor docs, Union[Image.Image, np.ndarray] image)
 Get class probabilities for a single text/image pair.
 
None save (self, str path, Optional[int] epoch=None, Optional[float] val_accuracy=None, Optional[float] train_accuracy=None, scaler=None)
 Save the trained model and relevant info.
 
None load (self, str path, map_location=None)
 Load a trained model and optimizer state.
 

Public Attributes

 num_classes
 
 vocab_size
 
 embedding_dim
 
 word_hidden_dim
 
 sent_hidden_dim
 
 image_channels
 
 image_feature_dim
 
 image_size
 
 dropout
 
 learning_rate
 
 num_epochs
 
 use_ocr_text
 
 device
 
 model
 
 optimizer
 
 criterion
 
 transform
 

Detailed Description

Wrapper class for training and using the EAML model.

This class provides a complete interface for training, evaluating, and using the EAML model for document classification.

Wrapper class for training and using the EAML model.
Mirrors the structure of CNNClassifier.

Constructor & Destructor Documentation

◆ __init__()

eaml.EAMLClassifier.__init__ (   self,
int  num_classes,
int  vocab_size,
int   embedding_dim = 100,
int   word_hidden_dim = 50,
int   sent_hidden_dim = 50,
int   image_channels = 3,
int   image_feature_dim = 128,
Tuple[int, int]   image_size = (224, 224),
float   dropout = 0.5,
float   learning_rate = 0.001,
Optional[str]   device = None,
int   num_epochs = 10,
bool   use_ocr_text = False 
)

Constructor for EAMLClassifier class.

Parameters
num_classesNumber of document classes
vocab_sizeSize of the vocabulary for text embedding
embedding_dimDimension of the word embeddings
word_hidden_dimDimension of the word-level GRU hidden state
sent_hidden_dimDimension of the sentence-level GRU hidden state
image_channelsNumber of input image channels
image_feature_dimDimension of the image feature vector
image_sizeTuple of (height, width) for image resizing
dropoutDropout rate for regularization
learning_rateLearning rate for the optimizer
deviceDevice to use for training ('cuda' or 'cpu')
num_epochsNumber of training epochs
use_ocr_textWhether to use OCR text from the dataset

Member Function Documentation

◆ evaluate()

eaml.EAMLClassifier.evaluate (   self,
  data_loader,
  tb_logger = None,
  step = None 
)

Evaluate the model on a dataset.

Parameters
data_loaderDataLoader for evaluation
tb_loggerLogger instance for TensorBoard logging
stepCurrent step for logging
Returns
Tuple of (accuracy, loss)
Evaluate the model.

◆ inference()

int eaml.EAMLClassifier.inference (   self,
torch.Tensor  docs,
Union[Image.Image, np.ndarray]  image 
)

Run inference on a single text/image pair.

Parameters
docsInput tensor of shape (num_sentences, max_sent_length)
imageImage as PIL Image or numpy array
Returns
Predicted class label
Run inference on a single text/image pair.

◆ load()

None eaml.EAMLClassifier.load (   self,
str  path,
  map_location = None 
)

Load a trained model and optimizer state.

Parameters
pathPath to the saved model
map_locationDevice to map the model to
Load a trained model and optimizer state.

◆ predict_proba()

np.ndarray eaml.EAMLClassifier.predict_proba (   self,
torch.Tensor  docs,
Union[Image.Image, np.ndarray]  image 
)

Get class probabilities for a single text/image pair.

Parameters
docsInput tensor of shape (num_sentences, max_sent_length)
imageImage as PIL Image or numpy array
Returns
Array of class probabilities
Get class probabilities for a single text/image pair.

◆ preprocess_image()

torch.Tensor eaml.EAMLClassifier.preprocess_image (   self,
Union[Image.Image, np.ndarray]  image 
)

Preprocess an image for the EAML model.

Parameters
imageImage as PIL Image or numpy array
Returns
Preprocessed image as a PyTorch tensor
Preprocesses a single image.

◆ save()

None eaml.EAMLClassifier.save (   self,
str  path,
Optional[int]   epoch = None,
Optional[float]   val_accuracy = None,
Optional[float]   train_accuracy = None,
  scaler = None 
)

Save the trained model and relevant info.

Parameters
pathPath to save the model
epochCurrent epoch number
val_accuracyValidation accuracy
train_accuracyTraining accuracy
scalerGradient scaler for mixed precision training
Save the trained model and relevant info.

◆ train_model()

eaml.EAMLClassifier.train_model (   self,
  train_loader,
  val_loader = None,
  tb_logger = None,
  save_dir = "models",
  patience = 5,
**  kwargs 
)

Train the EAML model.

Parameters
train_loaderTraining DataLoader
val_loaderValidation DataLoader
tb_loggerLogger instance for TensorBoard logging
save_dirDirectory to save model checkpoints
patienceNumber of epochs to wait before early stopping
**kwargsAdditional training parameters
Returns
Best validation accuracy or training accuracy
Train the EAML model.

Member Data Documentation

◆ criterion

eaml.EAMLClassifier.criterion

◆ device

eaml.EAMLClassifier.device

◆ dropout

eaml.EAMLClassifier.dropout

◆ embedding_dim

eaml.EAMLClassifier.embedding_dim

◆ image_channels

eaml.EAMLClassifier.image_channels

◆ image_feature_dim

eaml.EAMLClassifier.image_feature_dim

◆ image_size

eaml.EAMLClassifier.image_size

◆ learning_rate

eaml.EAMLClassifier.learning_rate

◆ model

eaml.EAMLClassifier.model

◆ num_classes

eaml.EAMLClassifier.num_classes

◆ num_epochs

eaml.EAMLClassifier.num_epochs

◆ optimizer

eaml.EAMLClassifier.optimizer

◆ sent_hidden_dim

eaml.EAMLClassifier.sent_hidden_dim

◆ transform

eaml.EAMLClassifier.transform

◆ use_ocr_text

eaml.EAMLClassifier.use_ocr_text

◆ vocab_size

eaml.EAMLClassifier.vocab_size

◆ word_hidden_dim

eaml.EAMLClassifier.word_hidden_dim

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