# DL 2D - Train Object Classifier

This is an experimental module from the Xtra Library: https://www.thermofisher.com/software-em-3d-vis/xtra-library/.

This module extracts image patches around labeled objects, and trains a deep learning classification model.

It supports multiple neural network architectures with configurable training parameters including optimizer, learning rate scheduler, and early stopping. It leverages TensorBoard for monitoring training progress and supports transfer learning from pretrained weights or custom models.

Trained classification models are used with DL - Predict Object Class module.

## Connections

Input | Description
--- | ---
Data [required] | Label field containing classified object instances for training. Each label value or material represents a class. The notion of object is implicitly associated with the notion of connected components. The module Disconnect Touching Labels can help ensure this.
Input Grayscale Image [required] | Grayscale image stack from which training patches will be extracted around labeled objects.

## Parameters (Ports)

Parameter | Description
--- | ---
Console | Displays status messages and technical information.
Train/Validation Ratio | Fraction of slices used for training (remainder for validation). All objects in a slice are assigned together.
Options | Enable to randomly shuffle slices when splitting into training and validation sets.
Resample | Target pixel size for resampling images before training. Downsampling will reduce computational cost, but may affect accuracy. Should match the expected scale of discriminating features.
Margin around object [px] | Additional padding around each object when extracting patches. Provides context around objects.
ROI Type | Choose raw patches, faded patches (object gradually fades toward edges), or both for training.
Classes to Train | Select which classes from the label field to include in training.
Model Architecture | Neural network architecture to use. Selection is a tradeoff between computation resources, amount of training data, model size and accuracy. resnet18: Smallest ResNet, best for limited data (<500 objects/class) or fast training resnet34: Balanced option for small to medium datasets resnet50: Default choice, best balance of accuracy and speed for most applications resnet101: Deepest ResNet, highest accuracy with abundant data (>1000 objects/class) efficientnet_b0: Optimized for fast inference with minimal memory footprint efficientnet_b2: Balanced efficiency and accuracy for deployment convnext_tiny: Modern architecture, high accuracy for datasets >1000 objects/class convnext_small: Highest accuracy option, requires large datasets and substantial computational resources
Input Patch Size | Size in pixels of square patches fed to the network. Should fit the typical object size to avoid excessive downsampling. Larger values increase GPU memory and computational requirements.
Initial Weights | Start from random weights, pretrained ImageNet weights (require internet connection at first usage), or custom weights from a previous training for transfer learning.
Model Filename | Path to custom pretrained model file (.pth) when using custom initial weights.
Training Options | Select optimizer among the following options: Adam: Default adaptive optimizer, good general-purpose choice for most applications AdamW: Adam with weight decay regularization, better generalization and recommended for larger models SGD: Classic optimizer with momentum, slower but can achieve better final accuracy with careful tuning Select learning rate scheduler among the following options: ConstantLR: Fixed learning rate throughout training, simplest option for stable convergence OneCycleLR: Cyclical learning rate with warmup and decay, often fastest convergence (default) CosineAnnealingLR: Smooth cosine decay, good for fine-tuning and transfer learning ReduceLROnPlateau: Reduces learning rate when validation loss plateaus, adaptive and robust choice StepLR: Periodic learning rate reduction at fixed intervals, predictable decay schedule
Training Params | Number of epochs, early stopping patience (epochs without improvement before stopping), and batch size.
Base Learning Rate | Initial learning rate for training. Typical values range from 1e-5 to 1e-3.
Output Directory | Directory where trained model and TensorBoard logs will be saved.
Base Filename | Base name for output 3D model file and tensorboard logs.

## See also

DL - Predict Object Class

Disconnect Touching Labels

ObjectClassification_Tuto.pdf