Class MobileNetV3Network<T>
- Namespace
- AiDotNet.NeuralNetworks
- Assembly
- AiDotNet.dll
Implements the MobileNetV3 architecture for efficient mobile inference.
public class MobileNetV3Network<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
TThe numeric type used for calculations.
- Inheritance
-
MobileNetV3Network<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
MobileNetV3 (Howard et al., 2019) builds on MobileNetV2 with three key improvements: 1. Hard-Swish activation: x * min(max(0, x+3), 6) / 6 - computationally efficient 2. Squeeze-and-Excitation blocks: Adaptive channel weighting 3. Efficient network head: Reduced computational cost in final layers
For Beginners: MobileNetV3 is the latest in the MobileNet family, optimized for both accuracy and latency on mobile devices.
Key innovations over V2:
- Hard-Swish: A faster activation function that works better with quantization
- SE blocks: Helps the network learn which channels are most important
- Network search: The architecture was found using neural architecture search (NAS)
- Two variants: "Large" for higher accuracy, "Small" for extreme efficiency
Constructors
MobileNetV3Network(NeuralNetworkArchitecture<T>, MobileNetV3Configuration, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, double)
Initializes a new instance of the MobileNetV3Network<T> class.
public MobileNetV3Network(NeuralNetworkArchitecture<T> architecture, MobileNetV3Configuration configuration, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, double maxGradNorm = 1)
Parameters
architectureNeuralNetworkArchitecture<T>The architecture defining the structure of the neural network.
configurationMobileNetV3ConfigurationThe MobileNetV3-specific configuration.
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>Optional optimizer for training (default: Adam).
lossFunctionILossFunction<T>Optional loss function (default: based on task type).
maxGradNormdoubleMaximum gradient norm for gradient clipping (default: 1.0).
Properties
NumClasses
Gets the number of output classes.
public int NumClasses { get; }
Property Value
Variant
Gets the MobileNetV3 variant used by this network.
public MobileNetV3Variant Variant { get; }
Property Value
Methods
Backward(Tensor<T>)
Performs backward propagation through the network.
public Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>
Returns
- Tensor<T>
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 network-specific data that was not covered by the general deserialization process.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReaderThe BinaryReader to read the data from.
Remarks
This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.
For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.
Forward(Tensor<T>)
Performs a forward pass through the network.
public Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>
Returns
- Tensor<T>
GetLayer(int)
Gets the layer at the specified index.
public ILayer<T> GetLayer(int index)
Parameters
indexint
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.
MobileNetV3Large(int, int)
Creates a MobileNetV3-Large network.
public static MobileNetV3Network<T> MobileNetV3Large(int numClasses = 1000, int inputChannels = 3)
Parameters
Returns
MobileNetV3Small(int, int)
Creates a MobileNetV3-Small network.
public static MobileNetV3Network<T> MobileNetV3Small(int numClasses = 1000, int inputChannels = 3)
Parameters
Returns
Predict(Tensor<T>)
Makes a prediction using the neural network.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<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
writerBinaryWriterThe 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
inputTensor<T>The input data.
expectedOutputTensor<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:
- Makes a prediction based on the input
- Compares its prediction to the expected output
- Calculates how wrong it was (the loss)
- 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
parametersVector<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.