Histogram of Oriented Gradients (HOG) classifier for document classification. More...

Public Member Functions | |
| __init__ (self, int num_classes, str classifier="logistic_regression", Optional[Dict[str, Any]] hog_params=None, Optional[Dict[str, Any]] classifier_params=None, Optional[str] device=None, int batch_size=32, int num_workers=4) | |
| Constructor for HogClassifier class. | |
| np.ndarray | preprocess_image (self, Union[Image.Image, torch.Tensor, np.ndarray] image) |
| Preprocess an image for HOG feature extraction. | |
| np.ndarray | extract_hog_features (self, np.ndarray image) |
| Extract HOG features from an image. | |
| train_model (self, train_loader, val_loader=None, tb_logger=None, **kwargs) | |
| Train the HOG-based classifier using DataLoader objects. | |
| evaluate (self, val_loader, tb_logger=None, step=None) | |
| Evaluate the model on validation set. | |
| None | save (self, str path) |
| Save the trained model to disk. | |
| None | load (self, str path, logger=None) |
| Load a trained model from disk. | |
Public Attributes | |
| num_classes | |
| classifier_type | |
| device | |
| batch_size | |
| num_workers | |
| hog_params | |
| model | |
| transform | |
| win_size | |
| block_size | |
| block_stride | |
| cell_size | |
| nbins | |
| hog | |
Histogram of Oriented Gradients (HOG) classifier for document classification.
This class implements a document classifier using HOG features with either Logistic Regression or SVM for classification. It provides a lightweight alternative to deep learning approaches.
A simple classifier using Histogram of Oriented Gradients (HOG) features with either Logistic Regression or SVM for classification.
| hog.HogClassifier.__init__ | ( | self, | |
| int | num_classes, | ||
| str | classifier = "logistic_regression", |
||
| Optional[Dict[str, Any]] | hog_params = None, |
||
| Optional[Dict[str, Any]] | classifier_params = None, |
||
| Optional[str] | device = None, |
||
| int | batch_size = 32, |
||
| int | num_workers = 4 |
||
| ) |
Constructor for HogClassifier class.
| num_classes | Number of document classes |
| classifier | Type of classifier to use ("logistic_regression" or "svm") |
| hog_params | Parameters for HOG feature extraction |
| classifier_params | Parameters for the classifier |
| device | Device to use for computation ('cuda' or 'cpu') |
| batch_size | Batch size for training |
| num_workers | Number of workers for data loading |
Initialize the HOG classifier.
Args:
num_classes: Number of classes to classify
classifier: Type of classifier to use ("logistic_regression" or "svm")
hog_params: Parameters for HOG feature extraction
classifier_params: Parameters for the classifier
device: Device to use for computation ('cuda' or 'cpu')
batch_size: Batch size for training
num_workers: Number of workers for data loading
| hog.HogClassifier.evaluate | ( | self, | |
| val_loader, | |||
tb_logger = None, |
|||
step = None |
|||
| ) |
Evaluate the model on validation set.
| val_loader | Validation DataLoader |
| tb_logger | TensorBoard logger instance for metrics visualization |
| step | Current step for logging |
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 and loss
| np.ndarray hog.HogClassifier.extract_hog_features | ( | self, | |
| np.ndarray | image | ||
| ) |
Extract HOG features from an image.
| image | Preprocessed image as a numpy array |
Extract HOG features from an image.
Args:
image: Preprocessed image as a numpy array
Returns:
HOG features as a numpy array
| None hog.HogClassifier.load | ( | self, | |
| str | path, | ||
logger = None |
|||
| ) |
Load a trained model from disk.
| path | Path to the saved model |
| logger | Optional logger for logging |
Load a trained model from disk.
Args:
path: Path to the saved model
logger: Optional logger for logging
| np.ndarray hog.HogClassifier.preprocess_image | ( | self, | |
| Union[Image.Image, torch.Tensor, np.ndarray] | image | ||
| ) |
Preprocess an image for HOG feature extraction.
| image | Image as PIL Image, PyTorch tensor, or numpy array |
Preprocess an image for HOG feature extraction.
Resizes, converts to grayscale, and ensures uint8 format.
Args:
image: Image as PIL Image, PyTorch tensor, or numpy array
Returns:
Preprocessed image as a numpy array (uint8, grayscale)
| None hog.HogClassifier.save | ( | self, | |
| str | path | ||
| ) |
Save the trained model to disk.
| path | Path to save the model |
Save the trained model to disk.
Args:
path: Path to save the model
logger: Optional logger for logging
| hog.HogClassifier.train_model | ( | self, | |
| train_loader, | |||
val_loader = None, |
|||
tb_logger = None, |
|||
| ** | kwargs | ||
| ) |
Train the HOG-based classifier using DataLoader objects.
| train_loader | Training DataLoader |
| val_loader | Validation DataLoader |
| tb_logger | TensorBoard logger instance for metrics visualization |
| **kwargs | Additional training parameters |
Train the HOG-based classifier 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
| hog.HogClassifier.batch_size |
| hog.HogClassifier.block_size |
| hog.HogClassifier.block_stride |
| hog.HogClassifier.cell_size |
| hog.HogClassifier.classifier_type |
| hog.HogClassifier.device |
| hog.HogClassifier.hog |
| hog.HogClassifier.hog_params |
| hog.HogClassifier.model |
| hog.HogClassifier.nbins |
| hog.HogClassifier.num_classes |
| hog.HogClassifier.num_workers |
| hog.HogClassifier.transform |
| hog.HogClassifier.win_size |