
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 | |
A classifier using a Convolutional Neural Network (CNN) for image classification.
| 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
| 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
| 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
| None cnn.CNNClassifier.load | ( | self, | |
| str | path | ||
| ) |
Load a trained model from disk.
Args:
path: Path to the saved model
| 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
| 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
| None cnn.CNNClassifier.save | ( | self, | |
| str | path | ||
| ) |
Save the trained model to disk.
Args:
path: Path to save the 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
| cnn.CNNClassifier.criterion |
| cnn.CNNClassifier.device |
| cnn.CNNClassifier.dropout_rate |
| cnn.CNNClassifier.input_channels |
| cnn.CNNClassifier.learning_rate |
| cnn.CNNClassifier.model |
| cnn.CNNClassifier.num_classes |
| cnn.CNNClassifier.num_epochs |
| cnn.CNNClassifier.optimizer |
| cnn.CNNClassifier.test_transform |
| cnn.CNNClassifier.transform |
| cnn.CNNClassifier.weight_decay |