DAPPER (Document Analysis, Processing, and Pattern Extraction Repository)
Statistical Learning Final Project for Document Classification
 
Loading...
Searching...
No Matches
cnn.CNNClassifier Class Reference
Collaboration diagram for cnn.CNNClassifier:
Collaboration graph

Public Member Functions

 __init__ (self, int num_classes, int input_channels=3, float learning_rate=0.001, float weight_decay=1e-4, float dropout_rate=0.3, Optional[str] device=None, int num_epochs=10)
 
torch.Tensor preprocess_image (self, Union[Image.Image, torch.Tensor, np.ndarray] image, bool for_training=False)
 
 train_model (self, train_loader, val_loader=None, tb_logger=None, **kwargs)
 
int inference (self, Union[Image.Image, torch.Tensor, np.ndarray] image)
 
np.ndarray predict_proba (self, Union[Image.Image, torch.Tensor, np.ndarray] image)
 
 evaluate (self, val_loader, tb_logger=None, step=None)
 
None save (self, str path)
 
None load (self, str path)
 

Public Attributes

 num_classes
 
 input_channels
 
 learning_rate
 
 weight_decay
 
 dropout_rate
 
 num_epochs
 
 device
 
 model
 
 optimizer
 
 criterion
 
 transform
 
 test_transform
 

Detailed Description

A classifier using a Convolutional Neural Network (CNN) for image classification.

Constructor & Destructor Documentation

◆ __init__()

cnn.CNNClassifier.__init__ (   self,
int  num_classes,
int   input_channels = 3,
float   learning_rate = 0.001,
float   weight_decay = 1e-4,
float   dropout_rate = 0.3,
Optional[str]   device = None,
int   num_epochs = 10 
)
Initialize the CNN classifier.

Args:
    num_classes: Number of classes to classify
    input_channels: Number of input channels (1 for grayscale, 3 for RGB)
    learning_rate: Learning rate for the optimizer
    weight_decay: L2 regularization parameter
    dropout_rate: Dropout rate for regularization
    device: Device to use for training ('cuda' or 'cpu')
    num_epochs: Number of training epochs

Member Function Documentation

◆ evaluate()

cnn.CNNClassifier.evaluate (   self,
  val_loader,
  tb_logger = None,
  step = None 
)
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 cnn.CNNClassifier.inference (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image 
)
Run inference on a single image.

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

◆ load()

None cnn.CNNClassifier.load (   self,
str  path 
)
Load a trained model from disk.

Args:
    path: Path to the saved model

◆ predict_proba()

np.ndarray cnn.CNNClassifier.predict_proba (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image 
)
Get class probabilities for an image.

Args:
    image: Image to classify
    
Returns:
    Array of class probabilities

◆ preprocess_image()

torch.Tensor cnn.CNNClassifier.preprocess_image (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image,
bool   for_training = False 
)
Preprocess an image for the CNN.

Args:
    image: Image as PIL Image, PyTorch tensor, or numpy array
    for_training: Whether to use training augmentations
    
Returns:
    Preprocessed image as a PyTorch tensor

◆ save()

None cnn.CNNClassifier.save (   self,
str  path 
)
Save the trained model to disk.

Args:
    path: Path to save the model

◆ train_model()

cnn.CNNClassifier.train_model (   self,
  train_loader,
  val_loader = None,
  tb_logger = None,
**  kwargs 
)
Train the model using DataLoader objects.

Args:
    train_loader: Training DataLoader
    val_loader: Validation DataLoader
    tb_logger: Logger instance for TensorBoard logging
    **kwargs: Additional training parameters
    
Returns:
    Validation accuracy or training accuracy

Member Data Documentation

◆ criterion

cnn.CNNClassifier.criterion

◆ device

cnn.CNNClassifier.device

◆ dropout_rate

cnn.CNNClassifier.dropout_rate

◆ input_channels

cnn.CNNClassifier.input_channels

◆ learning_rate

cnn.CNNClassifier.learning_rate

◆ model

cnn.CNNClassifier.model

◆ num_classes

cnn.CNNClassifier.num_classes

◆ num_epochs

cnn.CNNClassifier.num_epochs

◆ optimizer

cnn.CNNClassifier.optimizer

◆ test_transform

cnn.CNNClassifier.test_transform

◆ transform

cnn.CNNClassifier.transform

◆ weight_decay

cnn.CNNClassifier.weight_decay

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