Class ResNetConfiguration
- Namespace
- AiDotNet.Configuration
- Assembly
- AiDotNet.dll
Configuration options for ResNet (Residual Network) neural network architectures.
public class ResNetConfiguration
- Inheritance
-
ResNetConfiguration
- Inherited Members
Remarks
This configuration class provides all the settings needed to instantiate a ResNet network. It follows the AiDotNet pattern where users provide minimal configuration and the library supplies sensible defaults.
For Beginners: ResNet networks are deep convolutional neural networks that use skip connections to enable training of very deep architectures. This configuration lets you choose which ResNet variant to use (ResNet18, ResNet34, ResNet50, ResNet101, or ResNet152), set the number of output classes for your classification task, and optionally customize the input image dimensions and other parameters.
Typical Usage:
var config = new ResNetConfiguration(ResNetVariant.ResNet50, numClasses: 1000);
var architecture = config.ToArchitecture<float>();
var network = new ResNetNetwork<float>(architecture);
Constructors
ResNetConfiguration(ResNetVariant, int, int, int, int, bool, bool)
Initializes a new instance of the ResNetConfiguration class.
public ResNetConfiguration(ResNetVariant variant, int numClasses, int inputHeight = 224, int inputWidth = 224, int inputChannels = 3, bool includeClassifier = true, bool zeroInitResidual = true)
Parameters
variantResNetVariantThe ResNet variant to use.
numClassesintThe number of output classes for classification.
inputHeightintThe height of input images (default: 224).
inputWidthintThe width of input images (default: 224).
inputChannelsintThe number of input channels (default: 3 for RGB).
includeClassifierboolWhether to include the classifier layers (default: true).
zeroInitResidualboolWhether to use zero-initialization for residual branches (default: true).
Exceptions
- ArgumentOutOfRangeException
Thrown when numClasses is less than or equal to 0, or when image dimensions are invalid.
Properties
BaseChannels
Gets the base channel counts for each stage.
public int[] BaseChannels { get; }
Property Value
- int[]
Remarks
For BasicBlock: actual channels = base channels For BottleneckBlock: actual channels = base channels * 4 (due to expansion)
BlockCounts
Gets the block counts for each of the 4 stages based on the variant.
public int[] BlockCounts { get; }
Property Value
- int[]
Remarks
ResNet architectures have 4 stages after the initial convolution:
- Stage 1 (conv2_x): 56x56 spatial, 64/256 channels
- Stage 2 (conv3_x): 28x28 spatial, 128/512 channels
- Stage 3 (conv4_x): 14x14 spatial, 256/1024 channels
- Stage 4 (conv5_x): 7x7 spatial, 512/2048 channels
Expansion
Gets the expansion factor for the blocks.
public int Expansion { get; }
Property Value
Remarks
BasicBlock has expansion 1, BottleneckBlock has expansion 4.
IncludeClassifier
Gets whether to include the fully connected classifier layer.
public bool IncludeClassifier { get; }
Property Value
Remarks
Default is true. Set to false when using ResNet as a feature extractor for transfer learning.
For Beginners: The classifier is the final part of the network that produces class predictions. When using ResNet for transfer learning or as a feature extractor, you may want to remove the classifier and add your own custom layers.
InputChannels
Gets the number of input channels.
public int InputChannels { get; }
Property Value
Remarks
Default is 3 for RGB images. Use 1 for grayscale images.
For Beginners: Color images typically have 3 channels (Red, Green, Blue). Grayscale images have 1 channel. Most pre-trained ResNet models expect 3-channel inputs.
InputHeight
Gets the height of input images in pixels.
public int InputHeight { get; }
Property Value
Remarks
Default is 224, which is the standard ImageNet input size.
For Beginners: ResNet networks were designed for 224x224 images. While you can use different sizes, 224x224 is recommended for best results, especially when using pre-trained weights.
InputShape
Gets the computed input shape as [channels, height, width].
public int[] InputShape { get; }
Property Value
- int[]
InputWidth
Gets the width of input images in pixels.
public int InputWidth { get; }
Property Value
Remarks
Default is 224, which is the standard ImageNet input size.
NumClasses
Gets the number of output classes for classification.
public int NumClasses { get; }
Property Value
Remarks
For Beginners: This is the number of categories your model will classify images into. For example, if you're classifying cats vs dogs, this would be 2. For ImageNet, it's 1000. For CIFAR-10, it's 10.
NumConvLayers
Gets the total number of convolutional layers in the network.
public int NumConvLayers { get; }
Property Value
NumWeightLayers
Gets the total number of weight layers (conv + FC).
public int NumWeightLayers { get; }
Property Value
TotalInputSize
Gets the total number of input features (channels * height * width).
public int TotalInputSize { get; }
Property Value
UseAutodiff
Gets or sets whether to use automatic differentiation for backpropagation.
public bool UseAutodiff { get; set; }
Property Value
Remarks
Default is false, which uses the optimized manual backward implementation.
For Beginners: Backpropagation is how neural networks learn from their mistakes. Autodiff automatically computes gradients, which is more flexible but slightly slower. The manual implementation is optimized for ResNet's specific architecture.
UsesBottleneck
Gets whether this variant uses BasicBlock (ResNet18/34) or BottleneckBlock (ResNet50/101/152).
public bool UsesBottleneck { get; }
Property Value
Variant
Gets the ResNet variant to use.
public ResNetVariant Variant { get; }
Property Value
Remarks
For Beginners: The variant determines how deep the network is. ResNet50 is the most commonly used variant. Deeper variants (ResNet101, ResNet152) have more capacity but require more computational resources.
ZeroInitResidual
Gets whether to use zero-initialization for the last batch normalization in each residual block.
public bool ZeroInitResidual { get; }
Property Value
Remarks
Default is true. This technique (from "Bag of Tricks" paper) improves training stability by initializing the last BN layer in each residual branch to zero, making the initial residual branch act as an identity.
For Beginners: This is a training trick that helps the network start learning more effectively. When enabled, each residual block initially acts like an identity function, making the network easier to optimize.
Methods
CreateForCIFAR(ResNetVariant, int)
Creates a configuration optimized for CIFAR-10/CIFAR-100 datasets.
public static ResNetConfiguration CreateForCIFAR(ResNetVariant variant, int numClasses)
Parameters
variantResNetVariantThe ResNet variant to use.
numClassesintThe number of output classes (10 for CIFAR-10, 100 for CIFAR-100).
Returns
- ResNetConfiguration
A ResNetConfiguration optimized for CIFAR datasets.
Remarks
For Beginners: CIFAR images are 32x32 pixels, which is smaller than ImageNet's 224x224. This configuration adjusts the input size accordingly. Note that for CIFAR-sized images, a modified ResNet architecture is typically used that removes the initial 7x7 conv and pooling.
CreateForTesting(int)
Creates a minimal ResNet configuration optimized for fast test execution.
public static ResNetConfiguration CreateForTesting(int numClasses)
Parameters
numClassesintThe number of output classes.
Returns
- ResNetConfiguration
A minimal ResNet configuration for testing.
Remarks
Uses ResNet18 (smallest variant) with 32x32 input resolution, resulting in minimal network construction time suitable for unit tests. Construction time is typically under 50ms.
CreateLightweight(int)
Creates a lightweight configuration using ResNet18.
public static ResNetConfiguration CreateLightweight(int numClasses)
Parameters
numClassesintThe number of output classes.
Returns
- ResNetConfiguration
A ResNetConfiguration for ResNet18.
Remarks
For Beginners: Use this when you need faster training and inference, or when working with smaller datasets where larger models might overfit.
CreateResNet50(int)
Creates a new configuration for ResNet50.
public static ResNetConfiguration CreateResNet50(int numClasses)
Parameters
numClassesintThe number of output classes.
Returns
- ResNetConfiguration
A ResNetConfiguration for ResNet50.
Remarks
For Beginners: This is the recommended ResNet configuration for most use cases. ResNet50 provides a good balance of accuracy and computational efficiency.