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

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. | |
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.
| 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.
| num_classes | Number of document classes |
| vocab_size | Size of the vocabulary for text embedding |
| embedding_dim | Dimension of the word embeddings |
| word_hidden_dim | Dimension of the word-level GRU hidden state |
| sent_hidden_dim | Dimension of the sentence-level GRU hidden state |
| image_channels | Number of input image channels |
| image_feature_dim | Dimension of the image feature vector |
| image_size | Tuple of (height, width) for image resizing |
| dropout | Dropout rate for regularization |
| learning_rate | Learning rate for the optimizer |
| device | Device to use for training ('cuda' or 'cpu') |
| num_epochs | Number of training epochs |
| use_ocr_text | Whether to use OCR text from the dataset |
| eaml.EAMLClassifier.evaluate | ( | self, | |
| data_loader, | |||
tb_logger = None, |
|||
step = None |
|||
| ) |
Evaluate the model on a dataset.
| data_loader | DataLoader for evaluation |
| tb_logger | Logger instance for TensorBoard logging |
| step | Current step for logging |
Evaluate the model.
| int eaml.EAMLClassifier.inference | ( | self, | |
| torch.Tensor | docs, | ||
| Union[Image.Image, np.ndarray] | image | ||
| ) |
Run inference on a single text/image pair.
| docs | Input tensor of shape (num_sentences, max_sent_length) |
| image | Image as PIL Image or numpy array |
Run inference on a single text/image pair.
| None eaml.EAMLClassifier.load | ( | self, | |
| str | path, | ||
map_location = None |
|||
| ) |
Load a trained model and optimizer state.
| path | Path to the saved model |
| map_location | Device to map the model to |
Load a trained model and optimizer state.
| 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.
| docs | Input tensor of shape (num_sentences, max_sent_length) |
| image | Image as PIL Image or numpy array |
Get class probabilities for a single text/image pair.
| torch.Tensor eaml.EAMLClassifier.preprocess_image | ( | self, | |
| Union[Image.Image, np.ndarray] | image | ||
| ) |
Preprocess an image for the EAML model.
| image | Image as PIL Image or numpy array |
Preprocesses a single image.
| 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.
| path | Path to save the model |
| epoch | Current epoch number |
| val_accuracy | Validation accuracy |
| train_accuracy | Training accuracy |
| scaler | Gradient scaler for mixed precision training |
Save the trained model and relevant info.
| eaml.EAMLClassifier.train_model | ( | self, | |
| train_loader, | |||
val_loader = None, |
|||
tb_logger = None, |
|||
save_dir = "models", |
|||
patience = 5, |
|||
| ** | kwargs | ||
| ) |
Train the EAML model.
| train_loader | Training DataLoader |
| val_loader | Validation DataLoader |
| tb_logger | Logger instance for TensorBoard logging |
| save_dir | Directory to save model checkpoints |
| patience | Number of epochs to wait before early stopping |
| **kwargs | Additional training parameters |
Train the EAML model.
| eaml.EAMLClassifier.criterion |
| eaml.EAMLClassifier.device |
| eaml.EAMLClassifier.dropout |
| eaml.EAMLClassifier.embedding_dim |
| eaml.EAMLClassifier.image_channels |
| eaml.EAMLClassifier.image_feature_dim |
| eaml.EAMLClassifier.image_size |
| eaml.EAMLClassifier.learning_rate |
| eaml.EAMLClassifier.model |
| eaml.EAMLClassifier.num_classes |
| eaml.EAMLClassifier.num_epochs |
| eaml.EAMLClassifier.optimizer |
| eaml.EAMLClassifier.sent_hidden_dim |
| eaml.EAMLClassifier.transform |
| eaml.EAMLClassifier.use_ocr_text |
| eaml.EAMLClassifier.vocab_size |
| eaml.EAMLClassifier.word_hidden_dim |