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

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

Collaboration diagram for hog.HogClassifier:
Collaboration graph

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ __init__()

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.

Parameters
num_classesNumber of document classes
classifierType of classifier to use ("logistic_regression" or "svm")
hog_paramsParameters for HOG feature extraction
classifier_paramsParameters for the classifier
deviceDevice to use for computation ('cuda' or 'cpu')
batch_sizeBatch size for training
num_workersNumber 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

Member Function Documentation

◆ evaluate()

hog.HogClassifier.evaluate (   self,
  val_loader,
  tb_logger = None,
  step = None 
)

Evaluate the model on validation set.

Parameters
val_loaderValidation DataLoader
tb_loggerTensorBoard logger instance for metrics visualization
stepCurrent step for logging
Returns
Validation accuracy and loss
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

◆ extract_hog_features()

np.ndarray hog.HogClassifier.extract_hog_features (   self,
np.ndarray  image 
)

Extract HOG features from an image.

Parameters
imagePreprocessed image as a numpy array
Returns
HOG features 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

◆ load()

None hog.HogClassifier.load (   self,
str  path,
  logger = None 
)

Load a trained model from disk.

Parameters
pathPath to the saved model
loggerOptional logger for logging
Load a trained model from disk.

Args:
    path: Path to the saved model
    logger: Optional logger for logging

◆ preprocess_image()

np.ndarray hog.HogClassifier.preprocess_image (   self,
Union[Image.Image, torch.Tensor, np.ndarray]  image 
)

Preprocess an image for HOG feature extraction.

Parameters
imageImage as PIL Image, PyTorch tensor, or numpy array
Returns
Preprocessed image as a numpy array (uint8, grayscale)
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)

◆ save()

None hog.HogClassifier.save (   self,
str  path 
)

Save the trained model to disk.

Parameters
pathPath to save the model
Save the trained model to disk.

Args:
    path: Path to save the model
    logger: Optional logger for logging

◆ train_model()

hog.HogClassifier.train_model (   self,
  train_loader,
  val_loader = None,
  tb_logger = None,
**  kwargs 
)

Train the HOG-based classifier using DataLoader objects.

Parameters
train_loaderTraining DataLoader
val_loaderValidation DataLoader
tb_loggerTensorBoard logger instance for metrics visualization
**kwargsAdditional training parameters
Returns
Validation accuracy or training accuracy
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

Member Data Documentation

◆ batch_size

hog.HogClassifier.batch_size

◆ block_size

hog.HogClassifier.block_size

◆ block_stride

hog.HogClassifier.block_stride

◆ cell_size

hog.HogClassifier.cell_size

◆ classifier_type

hog.HogClassifier.classifier_type

◆ device

hog.HogClassifier.device

◆ hog

hog.HogClassifier.hog

◆ hog_params

hog.HogClassifier.hog_params

◆ model

hog.HogClassifier.model

◆ nbins

hog.HogClassifier.nbins

◆ num_classes

hog.HogClassifier.num_classes

◆ num_workers

hog.HogClassifier.num_workers

◆ transform

hog.HogClassifier.transform

◆ win_size

hog.HogClassifier.win_size

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