Overview
TheCustomCNNBuilder class enables building custom CNN architectures from declarative layer configurations. It provides a flexible way to construct CNNs by specifying layers, filters, activations, and other parameters in a configuration dictionary.
Classes
CustomCNNBuilder
Builder class that constructs CNN models from configuration.app/models/pytorch/cnn_builder.py:13
Constructor
Model configuration dictionary containing:
num_classes: Number of output classescnn_config: CNN-specific configuration withlayerslist
Methods
build() -> nn.Module
Build the CNN model from configuration.
Built PyTorch CNN model (CustomCNN instance)
ValueError: If configuration validation fails
get_parameters_count() -> tuple[int, int]
Get parameter counts for the model.
Total number of parameters
Number of trainable parameters
validate_config() -> bool
Validate CNN configuration.
Returns
True if configuration is valid- Base validation (num_classes > 0)
cnn_configmust be presentlayerslist must not be empty- Must contain at least one Conv2D layer
- Must contain at least one transition layer (Flatten or GlobalAvgPool)
- Must contain at least one Dense layer
CustomCNN
PyTorch model class built from layer stack.app/models/pytorch/cnn_builder.py:72
Constructor
List of layer configurations from layer_stack
Number of output classes
Number of input channels (3 for RGB images)
Input image size (assumes square images)
Methods
forward(x: torch.Tensor) -> torch.Tensor
Forward pass through the network.
Input tensor of shape
(batch, channels, height, width)Output logits of shape
(batch, num_classes)get_feature_extractor() -> nn.Sequential
Extract the feature extraction layers (convolutional part).
Sequential module containing all feature extraction layers
Supported Layer Types
Conv2D
Convolutional layer with activation. Parameters:Number of output filters/channels
Size of the convolution kernel
Activation function:
"relu", "leaky_relu", "gelu", "swish", "none"Padding mode:
"same" or "valid"MaxPooling2D
Max pooling layer.Size of the pooling window
AveragePooling2D
Average pooling layer.Size of the pooling window
BatchNorm
Batch normalization layer. Example:Dropout
Dropout layer (2D for features, 1D for classifier).Dropout rate (0-1)
Flatten
Flatten spatial dimensions. Required before Dense layers. Example:GlobalAvgPool
Global average pooling. Alternative to Flatten, reduces parameters. Example:Dense
Fully connected layer with activation.Number of output units
Activation function
Activation Functions
Supported activation functions (case-sensitive):"relu"- ReLU activation"leaky_relu"- Leaky ReLU with alpha=0.1"gelu"- Gaussian Error Linear Unit"swish"- Swish/SiLU activation"none"- Identity (no activation)
Complete Example
See Also
- BaseModel - Base model interface
- Transfer Learning - Pre-trained models
- Transformers - Vision Transformers