Table of Contents

Class EfficientNetNetwork<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Implements the EfficientNet architecture with compound scaling.

public class EfficientNetNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations.

Inheritance
EfficientNetNetwork<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

EfficientNet (Tan & Le, 2019) introduced compound scaling, which uniformly scales network width, depth, and resolution using a principled approach. This achieves state-of-the-art accuracy with significantly fewer parameters than previous models.

Architecture overview (EfficientNet-B0 baseline):

Input (3x224x224)
  ↓
Stem: Conv 3x3, 32, stride 2 → BN → Swish
  ↓
Stage 1: MBConv1 (k3, c16, n1, s1, SE)
  ↓
Stage 2: MBConv6 (k3, c24, n2, s2, SE)
  ↓
Stage 3: MBConv6 (k5, c40, n2, s2, SE)
  ↓
Stage 4: MBConv6 (k3, c80, n3, s2, SE)
  ↓
Stage 5: MBConv6 (k5, c112, n3, s1, SE)
  ↓
Stage 6: MBConv6 (k5, c192, n4, s2, SE)
  ↓
Stage 7: MBConv6 (k3, c320, n1, s1, SE)
  ↓
Head: Conv 1x1, 1280 → BN → Swish → GlobalAvgPool → FC
Where k=kernel size, c=output channels, n=num layers, s=stride.

For Beginners: EfficientNet achieves excellent accuracy while being very efficient.

Key innovations:

  • Compound Scaling: Balances network width, depth, and resolution together
  • MBConv blocks: Mobile Inverted Bottleneck with Squeeze-and-Excitation
  • Swish activation: Smooth, self-gated activation function (x * sigmoid(x))
  • Neural Architecture Search (NAS): The baseline B0 was found via automated search

The scaling philosophy: increasing only one dimension (width/depth/resolution) quickly saturates accuracy. Compound scaling increases all three proportionally.

Constructors

EfficientNetNetwork(NeuralNetworkArchitecture<T>, EfficientNetConfiguration, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, double)

Initializes a new instance of the EfficientNetNetwork<T> class.

public EfficientNetNetwork(NeuralNetworkArchitecture<T> architecture, EfficientNetConfiguration configuration, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, double maxGradNorm = 1)

Parameters

architecture NeuralNetworkArchitecture<T>

The architecture defining the structure of the neural network.

configuration EfficientNetConfiguration

The EfficientNet-specific configuration.

optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

Optional optimizer for training (default: Adam).

lossFunction ILossFunction<T>

Optional loss function (default: based on task type).

maxGradNorm double

Maximum gradient norm for gradient clipping (default: 1.0).

Properties

InputResolution

Gets the input resolution for this variant.

public int InputResolution { get; }

Property Value

int

NumClasses

Gets the number of output classes.

public int NumClasses { get; }

Property Value

int

Variant

Gets the EfficientNet variant.

public EfficientNetVariant Variant { get; }

Property Value

EfficientNetVariant

Methods

Backward(Tensor<T>)

Performs backward propagation through the network.

public Tensor<T> Backward(Tensor<T> outputGradient)

Parameters

outputGradient Tensor<T>

The gradient of the loss with respect to the output.

Returns

Tensor<T>

The gradient of the loss with respect to the input.

Clone()

Creates a clone of the neural network.

public override IFullModel<T, Tensor<T>, Tensor<T>> Clone()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance that is a clone of this neural network.

Remarks

For most neural networks, Clone and DeepCopy perform the same function - creating a complete independent copy of the network. Some specialized networks might implement this differently.

For Beginners: This creates an identical copy of your neural network.

In most cases, this works the same as DeepCopy and creates a completely independent duplicate of your network. The duplicate will have the same structure and the same learned parameters, but changes to one won't affect the other.

CreateNewInstance()

Creates a new instance of the same type as this neural network.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance of the same neural network type.

Remarks

For Beginners: This creates a blank version of the same type of neural network.

It's used internally by methods like DeepCopy and Clone to create the right type of network before copying the data into it.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes and validates network-specific configuration data.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The binary reader to read from.

Remarks

This method performs validation-only deserialization. The serialized configuration values are read and compared against the current instance's configuration to ensure compatibility.

Design rationale: The network's layer structure is created during construction based on the configuration. Changing the configuration during deserialization would not recreate the layers, leading to an inconsistent state. Therefore, deserialization requires that the target instance was created with a matching configuration.

To load a model with a different configuration, create a new network instance with the desired configuration, then call NeuralNetworkBase<T>.Load on that instance.

Exceptions

InvalidDataException

Thrown when the serialized configuration does not match the current instance's configuration.

EfficientNetB0(int, int)

Creates an EfficientNet-B0 network (baseline model).

public static EfficientNetNetwork<T> EfficientNetB0(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int

The number of output classes.

inputChannels int

The number of input channels (default: 3 for RGB).

Returns

EfficientNetNetwork<T>

A configured EfficientNet-B0 network.

EfficientNetB1(int, int)

Creates an EfficientNet-B1 network.

public static EfficientNetNetwork<T> EfficientNetB1(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB2(int, int)

Creates an EfficientNet-B2 network.

public static EfficientNetNetwork<T> EfficientNetB2(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB3(int, int)

Creates an EfficientNet-B3 network.

public static EfficientNetNetwork<T> EfficientNetB3(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB4(int, int)

Creates an EfficientNet-B4 network.

public static EfficientNetNetwork<T> EfficientNetB4(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB5(int, int)

Creates an EfficientNet-B5 network.

public static EfficientNetNetwork<T> EfficientNetB5(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB6(int, int)

Creates an EfficientNet-B6 network.

public static EfficientNetNetwork<T> EfficientNetB6(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

EfficientNetB7(int, int)

Creates an EfficientNet-B7 network.

public static EfficientNetNetwork<T> EfficientNetB7(int numClasses = 1000, int inputChannels = 3)

Parameters

numClasses int
inputChannels int

Returns

EfficientNetNetwork<T>

ForTesting(int, int)

Creates a minimal EfficientNet network optimized for fast test execution.

public static EfficientNetNetwork<T> ForTesting(int numClasses = 10, int inputChannels = 3)

Parameters

numClasses int

The number of output classes.

inputChannels int

The number of input channels (default: 3 for RGB).

Returns

EfficientNetNetwork<T>

A minimal EfficientNet network for testing.

Remarks

Uses 32x32 input resolution with 1.0 width/depth multipliers, resulting in significantly fewer layers than standard variants. Construction time is typically under 50ms, compared to hundreds of ms for B0.

Forward(Tensor<T>)

Performs a forward pass through the network.

public Tensor<T> Forward(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor [C, H, W] or [B, C, H, W].

Returns

Tensor<T>

The output class logits.

GetLayer(int)

Gets the layer at the specified index.

public ILayer<T> GetLayer(int index)

Parameters

index int

Returns

ILayer<T>

GetModelMetadata()

Gets the metadata for this neural network model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the model.

InitializeLayers()

Initializes the layers of the neural network based on the architecture.

protected override sealed void InitializeLayers()

Remarks

For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.

Predict(Tensor<T>)

Makes a prediction using the neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input data to process.

Returns

Tensor<T>

The network's prediction.

Remarks

For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).

SerializeNetworkSpecificData(BinaryWriter)

Serializes network-specific data that is not covered by the general serialization process.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The BinaryWriter to write the data to.

Remarks

This method is called at the end of the general serialization process to allow derived classes to write any additional data specific to their implementation.

For Beginners: Think of this as packing a special compartment in your suitcase. While the main serialization method packs the common items (layers, parameters), this method allows each specific type of neural network to pack its own unique items that other networks might not have.

Train(Tensor<T>, Tensor<T>)

Trains the neural network on a single input-output pair.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input data.

expectedOutput Tensor<T>

The expected output for the given input.

Remarks

This method performs one training step on the neural network using the provided input and expected output. It updates the network's parameters to reduce the error between the network's prediction and the expected output.

For Beginners: This is how your neural network learns. You provide: - An input (what the network should process) - The expected output (what the correct answer should be)

The network then:

  1. Makes a prediction based on the input
  2. Compares its prediction to the expected output
  3. Calculates how wrong it was (the loss)
  4. Adjusts its internal values to do better next time

After training, you can get the loss value using the GetLastLoss() method to see how well the network is learning.

UpdateParameters(Vector<T>)

Updates the network's parameters with new values.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The new parameter values to set.

Remarks

For Beginners: During training, a neural network's internal values (parameters) get adjusted to improve its performance. This method allows you to update all those values at once by providing a complete set of new parameters.

This is typically used by optimization algorithms that calculate better parameter values based on training data.