Home

Awesome

CAI NEURAL API VERSIONDOI

<img align="right" src="docs/cai.png" height="192"> CAI NEURAL API is a pascal based deep learning neural network API optimized for AVX, AVX2 and AVX512 instruction sets plus OpenCL capable devices including AMD, Intel and NVIDIA. This API has been tested under Windows and Linux.

This project is a subproject from a bigger and older project called CAI and is sister to Keras based K-CAI NEURAL API. You can find trained neural network models in the pre-trained-neural-api-networks repository.

Intro Videos

Watch the videoWatch the videoWatch the video
Basics of Neural Networks in Pascal - Loading and SavingNeural Networks for Absolute Beginners! Learning a Simple FunctionCoding a Neural Network in Pascal that Learns to Calculate the Hypotenuse

Why Pascal?

Prerequisites

You'll need Lazarus development environment. If you have an OpenCL capable device, you'll need its OpenCL drivers. Many examples use the CIFAR-10 dataset. You'll also find examples for the CIFAR-100, MNIST, Fashion MNIST and the Places365-Standard Small images 256x256 dataset.

Will It Work with Delphi?

This project is Lazarus based. That said, as of release v2.0.0, a number of units do compile with Delphi and you can create and run neural networks with Delphi. You'll be able to compile these units with Delphi: neuralvolume, neuralnetwork, neuralab, neuralabfun, neuralbit, neuralbyteprediction, neuralcache, neuraldatasets, neuralgeneric, neuralplanbuilder, Neural OpenCL, Neural Threading and neuralfit.

Installation

Clone this project, add the neural folder to your Lazarus unit search path and you'll be ready to go!

A.I. Powered Support

You can get A.I. powered help from these tools:

Documentation

In this readme file, you’ll find information about:

Easy Examples First Please!

Watch the video

You can click on the image above to watch the video.

Assuming that you would like to train a neural network to learn a function that has 2 inputs and one output, you could start with something like this:

    NN.AddLayer([
      TNNetInput.Create(2),
      TNNetFullConnectReLU.Create(32),
      TNNetFullConnectReLU.Create(32),
      TNNetFullConnectLinear.Create(1)
    ]);

The example above has 2 inputs (TNNetInput), 2 dense layers (TNNetFullConnectReLU) with 32 neurons each and one output (TNNetFullConnectLinear).

You can learn more about how to build and train simple neural networks at the following source code examples:

Loading and Saving Neural Networks

Loading is very easy:

    NN := TNNet.Create;
    NN.LoadFromFile('MyTrainedNeuralNetwork.nn');

Saving is as easy:

    NN.SaveToFile('MyTrainedNeuralNetwork.nn');

Simple Image Classification Examples

CIFAR-10 Image Classification Example

The CIFAR-10 dataset is a well-known collection of images commonly used to train machine learning and computer vision algorithms. It was created by the Canadian Institute for Advanced Research (CIFAR). It contains 60K 32x32 color images. The images are classified into 10 different classes, with 6,000 images per class. The classes represent airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. Despite its relatively low resolution and small size, CIFAR-10 can be challenging for models to achieve high accuracy, making it a good dataset for testing advancements in machine learning techniques.

Follows a source code example for the CIFAR-10 image classification:

NN := TNNet.Create();
NN.AddLayer([
  TNNetInput.Create(32, 32, 3), //32x32x3 Input Image
  TNNetConvolutionReLU.Create({Features=}16, {FeatureSize=}5, {Padding=}0, {Stride=}1, {SuppressBias=}0),
  TNNetMaxPool.Create({Size=}2),
  TNNetConvolutionReLU.Create({Features=}32, {FeatureSize=}5, {Padding=}0, {Stride=}1, {SuppressBias=}0),
  TNNetMaxPool.Create({Size=}2),
  TNNetConvolutionReLU.Create({Features=}32, {FeatureSize=}5, {Padding=}0, {Stride=}1, {SuppressBias=}0),
  TNNetFullConnectReLU.Create({Neurons=}32),
  TNNetFullConnectLinear.Create(NumClasses),
  TNNetSoftMax.Create()
]);

CreateCifar10Volumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes);

WriteLn('Neural Network will minimize error with:');
WriteLn(' Layers: ', NN.CountLayers());
WriteLn(' Neurons:', NN.CountNeurons());
WriteLn(' Weights:', NN.CountWeights());

NeuralFit := TNeuralImageFit.Create;
NeuralFit.InitialLearningRate := fLearningRate;
NeuralFit.Inertia := fInertia;
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, NumClasses, {batchsize}128, {epochs}100);

These examples train a neural network to classify images in classes such as: image has a cat, image has a dog, image has an airplane...

You can save and load trained models (neural networks) with TNNet.SaveToFile and TNNet.LoadFromFile. The file format is portable meaning that you can train on CPU and run on GPU or train in AMD and run on ARM as examples. The following code shows a simple example for image classification loading a pre-trained model:

  procedure ClassifyOneImageSimple;
  var
    NN: TNNet;
    ImageFileName: string;
    NeuralFit: TNeuralImageFit;
  begin
    WriteLn('Loading Neural Network...');
    NN := TNNet.Create;
    NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
    NeuralFit := TNeuralImageFit.Create;
    ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
    WriteLn('Processing image: ', ImageFileName);
    WriteLn(
      'The class of the image is: ',
      NeuralFit.ClassifyImageFromFile(NN, ImageFileName)
    );
    NeuralFit.Free;
    NN.Free;
  end;  

Youtube Videos

Watch the videoWatch the videoWatch the video
Basics of Neural Networks in Pascal - Loading and SavingNeural Networks for Absolute Beginners! Learning a Simple FunctionCoding a Neural Network in Pascal that Learns to Calculate the Hypotenuse
Watch the videoWatch the videoWatch the video
Pre-trained Neural Networks & Transfer Learning with Pascal's CAI Neural APICoding a Neural Network in Pascal that Learns the OR Boolean OperationA Dive into Identity Shortcut Connection - The ResNet building block
Watch the videoWatch the videoWatch the video
Increasing Image Resolution with Neural NetworksUltra Fast Single Precision Floating Point ComputingAVX and AVX2 Code Optimization

Some videos make referrence to uvolume unit. The current neuralvolume unit used to be called uvolume. This is why it's mentioned.

Advanced Examples

Although these examples require deeper understanding about neural networks, they are very interesting:

There are also some older code examples that you can look at.

Volumes

Volumes behave like dynamically created arrays. They are the main array like structure used by this API. TNNetVolume class allows you to create volumes that can be accessed as 1D, 2D or 3D arrays and be operated with Advanced Vector Extensions (AVX) - Single Instruction Multiple Data (SIMD) instruction set. The usual way to create a volume is:

constructor Create(pSizeX, pSizeY, pDepth: integer; c: T = 0);

You can access the data as 1D or 3D with:

property Raw[x: integer]: T read GetRaw write SetRaw;
property Data[x, y, d: integer]: T read Get write Store; default;

Your code will look like this:

// Usage Examples
vInput := TNNetVolume.Create(32, 32, 3);
vInput[1, 1, 1] := 1;
vInput[2, 2, 2] := vInput[1, 1, 1] + 1;
vInput.Raw[10] := 5;

vInput.RandomizeGaussian();
WriteLn('Avg: ', vInput.GetAvg());
WriteLn('Variance: ', vInput.GetVariance());
WriteLn('Std Dev: ', vInput.GetStdDeviation());

WriteLn('Multiplying by 10');
vInput.Mul(10);
WriteLn('Avg: ', vInput.GetAvg());
WriteLn('Variance: ', vInput.GetVariance());
WriteLn('Std Dev: ', vInput.GetStdDeviation());

As examples, you can add, subtract, multiply and calculate dot products with:

procedure Add(Original: TNNetVolume); overload;
procedure Sub(Original: TNNetVolume); overload;
procedure Mul(Value: Single); overload;
function DotProduct(Original: TNNetVolume): TNeuralFloat; overload;

In the case that you need the raw position or raw pointer to an element of the volume, you can get with:

function GetRawPos(x, y, d: integer): integer; overload;
function GetRawPos(x, y: integer): integer; overload;
function GetRawPtr(x, y, d: integer): pointer; overload;
function GetRawPtr(x, y: integer): pointer; overload;
function GetRawPtr(x: integer): pointer; overload;

You can easily operate volumes with OpenCL via TEasyOpenCLV:

  TEasyOpenCLV = class (TEasyOpenCL)
    public
      function CreateBuffer(flags: cl_mem_flags; V: TNNetVolume): cl_mem; overload;
      function CreateInputBuffer(V: TNNetVolume): cl_mem; overload;
      function CreateHostInputBuffer(V: TNNetVolume): cl_mem; overload;
      function CreateOutputBuffer(V: TNNetVolume): cl_mem; overload;
      function CreateBuffer(V: TNNetVolume): cl_mem;  overload;

      function WriteBuffer(buffer: cl_mem; V: TNNetVolume; blocking: cl_bool = CL_FALSE): integer;
      function ReadBuffer(buffer: cl_mem; V: TNNetVolume; blocking: cl_bool = CL_TRUE): integer;

      function CreateAndWriteBuffer(V: TNNetVolume; var buffer: cl_mem): integer; overload;
      function CreateAndWriteBuffer(V: TNNetVolume): cl_mem; overload;
      function CreateWriteSetArgument(V: TNNetVolume; kernel:cl_kernel; arg_index: cl_uint): cl_mem;
      function CreateOutputSetArgument(V: TNNetVolume; kernel:cl_kernel; arg_index: cl_uint): cl_mem;
  end;

Volume Pairs, Volume Lists and Volume Pair Lists

Volumes can be organized in pairs:

  /// Implements a pair of volumes
  TNNetVolumePair = class(TObject)
    protected
      FA: TNNetVolume;
      FB: TNNetVolume;
    public
      constructor Create(); overload;
      constructor Create(pA, pB: TNNetVolume); overload;
      constructor CreateCopying(pA, pB: TNNetVolume); overload;

      destructor Destroy(); override;

      property A:TNNetVolume read FA;
      property B:TNNetVolume read FB;
      property I:TNNetVolume read FA;
      property O:TNNetVolume read FB;
  end;

Depending on the problem that you are trying to solve, modelling the training with pairs or pair lists might be helpful. Typically, a pair will be (input, desired output). This is how volume lists and volume pair lists have been implemented:

TNNetVolumeList = class (specialize TFPGObjectList<TNNetVolume>
TNNetVolumePairList = class (specialize TFPGObjectList<TNNetVolumePair>)

Neural Network Layers

The layered structure of artificial neural networks is inspired by the organization of the human brain and nervous system. In the human brain, information processing occurs in a hierarchical manner. Sensory inputs are first processed by lower-level neurons, which extract simple features. These features are then passed on to deeper neurons that combine them to recognize more complex patterns. This hierarchical processing is mirrored in artificial neural networks through the use of stacked layers.

Biological neurons are connected to each other through synapses, forming complex networks. Similarly, in artificial neural networks, neurons in one layer are connected to neurons in the next layer, mimicking this interconnected structure. Biological neurons fire (activate) based on a non-linear response to their inputs. This non-linearity is crucial for the brain's ability to learn complex patterns. In artificial neural networks, we use non-linear activation functions (such as ReLU) to introduce this non-linearity. Different regions of the brain specialize in processing different types of information. For instance, the visual cortex has layers specialized for detecting edges, shapes, and complex objects. This specialization is reflected in artificial neural networks, where different layers can learn to recognize different levels of abstraction.

In the context of artificial neural networks, we can see this biologically-inspired layered approach implemented. For example:

NN := TNNet.Create();
NN.AddLayer([
  TNNetInput.Create(32, 32, 3),
  TNNetConvolutionLinear.Create({neurons=}16, {featuresize}3, {padding}1, {stride}1),
  TNNetReLU6.Create()
]);

This code snippet demonstrates the creation of a neural network with an input layer and a convolutional layer followed by a ReLU6 activation. This structure is inspired by the visual cortex in the brain, where neurons respond to specific patterns in their receptive fields, similar to how convolutional layers operate. The CAI Neural API also supports the creation of more complex, biologically-inspired architectures. These architectures are designed with multiple layers of different types, mirroring the complex structure of the brain.

Artificial neural networks with multiple layers and specialized structures are inspired by the hierarchical and specialized nature of biological neural processing. It's important to note that while artificial neural networks are inspired by biological neural networks, they are highly simplified models. The human brain is far more complex, with various types of neurons, complex connectivity patterns, and mechanisms we don't yet fully understand. However, the layered structure in artificial neural networks has proven to be a powerful approach for solving complex problems in machine learning, inspired by the remarkable capabilities of biological neural networks.

Input Layer

The input layer serves as the gateway to the entire network. It's like the sensory organs of our brain, receiving information from the outside world. Without an input layer, the neural network would have no way to receive and interpret the initial data, making it impossible to perform any meaningful computations or learning tasks. The TNNetInput class implements the input layer.

Fully Connected (Dense) Layers

Fully connected layers, also known as dense layers, are a fundamental component of neural networks. In these layers, every neuron is connected to every neuron in the previous layer, allowing for comprehensive information processing across the entire network.

In the context of the CAI Neural API, fully connected layers are represented by various classes derived from TNNetFullConnect. These layers play a crucial role in transforming input data and learning complex patterns.

The computation process in a fully connected layer involves:

  1. Multiplying input values by the layer's weights.
  2. Adding bias terms (if not suppressed).
  3. Applying an activation function (if present).

Key types of fully connected layers include:

  1. TNNetFullConnectLinear: a basic fully connected layer without an activation function. It performs a linear transformation of the input data.
  2. TNNetFullConnectReLU: incorporates the Rectified Linear Unit (ReLU) activation function. ReLU introduces non-linearity by outputting the input for positive values and zero for negative values, helping the network learn complex patterns.
  3. TNNetFullConnectSigmoid: applies the sigmoid activation function to the layer's output. Sigmoid squashes the output between 0 and 1, useful for binary classification tasks.

Fully connected layers are typically used in neural network architectures as:

  1. Hidden layers for processing and transforming features.
  2. Output layers for producing final predictions.

For example, in the provided context, we see a simple neural network structure:

NN := TNNet.Create();
NN.AddLayer([
  TNNetInput.Create(2),       // Input layer with 2 inputs
  TNNetFullConnect.Create(2), // Hidden fully connected layer with 2 neurons
  TNNetFullConnect.Create(1)  // Output fully connected layer with 1 neuron  
]);
Layer NameInput/Output DimensionsActivationDescription
TNNetFullConnectLinear1D, 2D, or 3DNoneFully connected layer without an activation function (linear).
TNNetFullConnect1D, 2D, or 3DtanhFully connected layer with tanh as the default activation function.
TNNetFullConnectReLU1D, 2D, or 3DReLUFully connected layer with ReLU activation.
TNNetFullConnectSigmoid1D, 2D, or 3DSigmoidFully connected layer with Sigmoid activation.
TNNet.AddGroupedFullConnect1D, 2D, or 3DOptionalAdds a grouped fully connected layer, inspired by TNNet.AddGroupedConvolution.

Convolutional Layers

Neurons, filters, and kernels are often used as synonyms in the context of neural networks, particularly in convolutional neural networks (CNNs). They are closely related concepts that are used interchangeably. Here's why:

In practice, when implementing CNNs, the terms "filter" and "kernel" are more commonly used than "neuron" when referring to the convolutional layers. However, the underlying concept of a computational unit that processes input and produces output remains the same across these terms.

Convolutional layers are fundamental building blocks in neural networks, particularly in the field of computer vision and image processing. They are designed to automatically and adaptively learn spatial hierarchies of features from input data, such as images.

In the context of the CAI Neural API, convolutional layers are implemented as classes derived from TNNetConvolutionAbstract. This abstract base class provides the core functionality for convolutional operations.

The structure of a convolutional layer typically includes:

  1. Input: A multi-dimensional array (usually 3D for images: width, height, and channels).
  2. Kernels (or filters): small matrices of weights that slide over the input.
  3. Feature maps: the output produced by applying the kernels to the input.

Key parameters of convolutional layers include:

The CAI Neural API offers several types of convolutional layers:

  1. TNNetConvolution: the standard convolutional layer.
  2. TNNetConvolutionLinear: a convolutional layer without an activation function.
  3. TNNetConvolutionReLU: a convolutional layer with a ReLU activation function.

Convolutional layers are crucial in neural networks because they:

  1. Automatically learn hierarchical features from data.
  2. Maintain spatial relationships in the input.
  3. Reduce the number of parameters compared to fully connected layers.
  4. Enable the network to be translation-invariant.

In practice, convolutional layers are often used in combination with other layer types, such as pooling layers (e.g., TNNetMaxPool) and normalization layers (e.g., TNNetMovingStdNormalization), to create powerful neural network architectures for tasks like image classification, object detection, and segmentation.

Here's a brief example of how to create a convolutional layer using the CAI Neural API:

NN := TNNet.Create();
NN.AddLayer([
  TNNetInput.Create(32, 32, 3),  // Input layer for 32x32 RGB images
  TNNetConvolutionLinear.Create(
    {Features=}64,     // Number of output features
    {FeatureSize=}5,   // 5x5 kernel size
    {Padding=}2,       // Padding of 2 pixels
    {Stride=}1,        // Stride of 1 pixel
    {SuppressBias=}1   // Suppress bias
  ),
  TNNetReLU6.Create()  // Activation function
]);

This example creates a convolutional layer with 64 features, a 5x5 kernel size, padding of 2, and a stride of 1, followed by a ReLU6 activation function.

These are tha available convolutional layers in CAI:

Layer NameInput/Output DimensionsActivationDescription
TNNetConvolutionLinear1D, 2D, or 3DNoneLinear convolutional layer without activation. Useful for intermediate layers.
TNNetConvolution1D, 2D, or 3DtanhStandard convolutional layer. Versatile for feature extraction in tasks like image recognition.
TNNetConvolutionReLU1D, 2D, or 3DReLUConvolutional layer with ReLU activation. Helps mitigate vanishing gradient problem.
TNNetConvolutionSwish1D, 2D, or 3DSwishConvolutional layer with Swish activation. Performs better than ReLU in some cases.
TNNetConvolutionHardSwish1D, 2D, or 3DHard SwishConvolutional layer with Hard Swish activation. It is similar to swish but it's faster.
TNNetConvolutionSharedWeights1D, 2D, or 3Dsame as linked layerConvolutional layer that uses the weights from another layer
TNNetPointwiseConvLinear1D, 2D, or 3DNoneLinear 1x1 convolution. Useful for channel mixing without spatial operations.
TNNetPointwiseConvReLU1D, 2D, or 3DReLU1x1 convolution with ReLU. Efficient for channel-wise dimensionality reduction or expansion.
TNNetPointwiseConv1D, 2D, or 3Dtanh1x1 convolution. Useful for autoencoding architectures.
TNNetDepthwiseConvLinear1D, 2D, or 3DNoneLinear depthwise convolution. Useful when additional non-linearity is not required.
TNNetDepthwiseConv1D, 2D, or 3DtanhDepthwise convolution with tanh activation. Reduces computational cost by processing each channel separately.
TNNetDepthwiseConvReLU1D, 2D, or 3DReLUDepthwise convolution with ReLU activation. Combines depthwise efficiency with the benefits of ReLU.
TNNet.AddSeparableConvLinear1D, 2D, or 3DNoneAdds a linear separable convolution. Useful for lightweight models with reduced parameter count.
TNNet.AddSeparableConvReLU1D, 2D, or 3DReLUAdds a separable convolution with ReLU. Combines depthwise and pointwise for efficient feature extraction.
TNNet.AddConvOrSeparableConv1D, 2D, or 3DOptionalAdds standard or separable convolution. Supports optional ReLU and normalization for versatile design.
TNNet.AddGroupedConvolution1D, 2D, or 3DOptionalAdds a grouped convolution. Allows efficient parallel processing of input channels.

Grouped pointwise convolutions are an interesting and efficient variant of standard convolutions in neural networks. Grouped pointwise convolutions are a type of convolution operation where the input channels are divided into groups, and each group is processed separately. This is particularly useful for 1x1 convolutions (pointwise) where the spatial dimensions are not affected. The grouped approach can significantly reduce the number of parameters in a neural network as shown in the papers Grouped Pointwise Convolutions Reduce Parameters in Convolutional Neural Networks and An Enhanced Scheme for Reducing the Complexity of Pointwise Convolutions in CNNs for Image Classification Based on Interleaved Grouped Filters without Divisibility Constraints. By reducing parameters, these convolutions can make models more efficient in terms of computation and memory usage. These convolutions can be combined with other techniques like normalization and intergroup connections. This flexibility allows for the creation of more sophisticated network designs. Grouped pointwise convolutions are particularly useful in efficient network designs, such as mobile or edge computing applications where resource constraints are significant. They allow for maintaining model expressivity while reducing computational requirements.

The grouped pointwise convolutional layers are:

Layer NameInput/Output DimensionsActivationDescription
TNNetGroupedPointwiseConvLinear1D, 2D, or 3DNoneLinear 1x1 grouped convolution. Useful for channel mixing without spatial operations.
TNNetGroupedPointwiseConvReLU1D, 2D, or 3DReLU1x1 grouped convolution with ReLU. Efficient for channel-wise dimensionality reduction or expansion.
TNNetGroupedPointwiseConvHardSwish1D, 2D, or 3DHard Swish1x1 grouped convolution wish fast hard swish activation function.

Locally Connected Layers

A locally connected layer is a type of neural network layer that shares some similarities with convolutional layers but has some distinct characteristics:

Layer NameInput/Output DimensionsActivationDescription
TNNetLocalConnectLinear1D, 2D, or 3DNoneLocally connected layer with ReLU activation.
TNNetLocalConnect1D, 2D, or 3DtanhLocally connected layer with htan as the default activation function.
TNNetLocalConnectReLU1D, 2D, or 3DReLULocally connected layer with ReLU activation.

Min / Max / Avg Pools

Max, min, and avg poolings are downsampling techniques used in neural networks, particularly in convolutional neural networks (CNNs). Let's explore each of these pooling types as implemented in the CAI Neural API:

  1. Max Pooling (TNNetMaxPool): Max pooling selects the maximum value from a defined region of the input.
  1. Min Pooling (TNNetMinPool): Min pooling selects the minimum value from a defined region of the input.
  1. Average Pooling (TNNetAvgPool): Average pooling calculates the average value of a defined region of the input.

Unique pooling variants in the API:

Backpropagation in pooling layers: During backpropagation, pooling layers distribute the gradient differently:

The CAI Neural API implements these backpropagation methods in the respective Backpropagate() functions of each pooling class.

Deconvolution (Upsampling) counterparts: The API also provides deconvolution or upsampling layers, which can be seen as the inverse operations of pooling:

These layers are crucial in architectures like autoencoders or in tasks requiring upsampling, such as image segmentation.

When to use each pooling type:

Layer NameInput/Output DimensionsDescription
TNNetAvgPool1D, 2D, or 3DAverage pooling layer for reducing spatial dimensions.
TNNetMaxPool1D, 2D, or 3DMax pooling layer for reducing spatial dimensions.
TNNetMinPool1D, 2D, or 3DMin pooling layer for reducing spatial dimensions.
TNNet.AddMinMaxPool1D, 2D, or 3DPerforms both min and max pooling, then concatenates the results.
TNNet.AddAvgMaxPool1D, 2D, or 3DPerforms both average and max pooling, then concatenates the results.

The CAI Neural API also provides specialized versions:

Layer NameInput/Output DimensionsDescription
TNNetAvgChannel2D or 3D (output: 1D)Calculates the average value per channel.
TNNetMaxChannel2D or 3D (output: 1D)Calculates the maximum value per channel.
TNNetMinChannel2D or 3D (output: 1D)Calculates the minimum value per channel.
TNNet.AddMinMaxChannel1D, 2D, or 3DPerforms both min and max channel operations, then concatenates the results.
TNNet.AddAvgMaxChannel1D, 2D, or 3DPerforms both average and max channel operations, then concatenates the results.

Trainable Normalization Layers Allowing Faster Learning/Convergence

Normalization layers may offer:

The available normalization techniques are:

Layer NameInput/Output DimensionsDescription
TNNetChannelZeroCenter1D, 2D, or 3DTrainable zero-centering normalization.
TNNetMovingStdNormalization1D, 2D, or 3DTrainable standard deviation normalization.
TNNetChannelStdNormalization1D, 2D, or 3DTrainable per-channel standard deviation normalization.
TNNet.AddMovingNorm1D, 2D, or 3DPossible replacement for batch normalization.
TNNet.AddChannelMovingNorm1D, 2D, or 3DPossible replacement for batch normalization, applied per channel.

Non Trainable and per Sample Normalization Layers

Normalization layers (TNNetLayerMaxNormalization, TNNetLayerStdNormalization, TNNetLocalResponseNorm2D, TNNetLocalResponseNormDepth) help stabilize training and can improve model performance by managing the scale and distribution of activations. They are particularly useful in deep networks where the scale of values can change dramatically between layers.

TNNetLayerMaxNormalization normalizes based on the maximum value, while TNNetLayerStdNormalization uses standard deviation. These are particularly useful when you want to normalize the activations within a specific range or distribution without learning any parameters. They can be applied to various network architectures and are especially helpful when dealing with varying scales of input features.

TNNetLocalResponseNorm2D and TNNetLocalResponseNormDepth implement types of local Response Normalization (LRN). LRN is inspired by lateral inhibition in real neurons. It's particularly useful in Convolutional Neural Networks (CNNs) for image processing tasks. You may use it in scenarios where you want to create competition amongst neuron outputs in the same layer.

TNNetLocalResponseNorm2D is applied across nearby kernel maps at the same spatial position, while TNNetLocalResponseNormDepth normalizes across the depth dimension. These layers can help in increasing the generalization capability of the model, reducing the chances of overfitting and enhancing the model's ability to detect high-frequency features with a big response.

Random layers (TNNetRandomMulAdd, TNNetChannelRandomMulAdd) serve as powerful regularization techniques, helping to prevent overfitting and improve the model's ability to generalize. They can be especially beneficial when working with limited datasets or when you want your model to be robust to small variations in input.

Layer NameInput/Output DimensionsDescription
TNNetLayerMaxNormalization1D, 2D, or 3DNon-trainable max normalization per layer.
TNNetLayerStdNormalization1D, 2D, or 3DNon-trainable standard deviation normalization per layer.
TNNetLocalResponseNorm2D2D or 3DNon-trainable local response normalization for 2D or 3D input.
TNNetLocalResponseNormDepth2D or 3DNon-trainable local response normalization with depth normalization.
TNNetRandomMulAdd1D, 2D, or 3DAdds random multiplication and random bias (shift).
TNNetChannelRandomMulAdd1D, 2D, or 3DAdds random multiplication and random bias (shift) per channel.

These layers provide various tools for normalization, regularization, and introducing controlled variability in neural networks. The choice of which layers to use and where to place them in your network architecture depends on the specific problem you're trying to solve, the characteristics of your data, and the behavior you want to encourage in your model.

Concatenation, Summation and Reshaping Layers

These layers are essential for creating flexible and powerful neural network architectures. Let's break them down:

  1. Concatenation Layers: There are two main types of concatenation layers in the CAI Neural API: a. TNNetConcat:

    • This layer concatenates outputs from multiple layers along the depth dimension.
    • It's designed to work with layers that have the same spatial dimensions (X and Y sizes).
    • Usage: It's particularly useful when you want to combine features from different processing paths in your network. b. TNNetDeepConcat:
    • This layer also concatenates outputs from multiple layers, but it's specifically optimized for the depth dimension.
    • It maintains separate arrays to track the depths of each layer and channel, allowing for efficient deep concatenation.
    • Usage: Ideal for creating architectures that process information in parallel and then combine the results.
  2. Summation Layer (TNNetSum):

    • This layer adds together the outputs of multiple layers element-wise.
    • It's designed to work with layers of the same size.
    • Usage: Commonly used in residual network (ResNet) style architectures, where it allows for skip connections that help mitigate the vanishing gradient problem and enable the training of very deep networks.

These layers provide several benefits in neural network design:

  1. Flexibility: they allow for the creation of complex, non-linear network topologies that can process information in parallel and then combine it in various ways.
  2. Feature Fusion: concatenation and summation layers enable the network to combine features from different processing streams, potentially capturing multi-scale or multi-aspect information.
  3. Skip Connections: summation layers are crucial for implementing skip connections, which are fundamental to many modern architectures like ResNets and DenseNets.
  4. Dimensionality Manipulation: the transposition layers allow for creative manipulations of data dimensions, which can be crucial for certain types of operations or for interfacing between different parts of a network.
  5. Custom Architectures: these layers provide the building blocks for designing novel network architectures tailored to specific tasks or data types.

By using these layers creatively, developers can build highly customized and efficient neural network architectures that are optimized for their specific use cases.

Layer NameInput/Output DimensionsDescription
TNNetConcat1D, 2D, or 3DConcatenates previous layers into a single layer.
TNNetDeepConcat1D, 2D, or 3DConcatenates previous layers along the depth axis. This is useful with DenseNet like architectures. Use TNNetDeepConcat instead of TNNetConcat if you need to add convolutions after concating layers.
TNNetIdentity1D, 2D, or 3DIdentity layer that passes the input unchanged.
TNNetIdentityWithoutBackprop1D, 2D, or 3DAllows the forward pass but prevents backpropagation.
TNNetReshape1D, 2D, or 3DReshapes the input into a different dimension.
TNNetSum1D, 2D, or 3DSums the outputs from previous layers, useful for ResNet-style networks.
TNNetUpsample3DUpsamples channels (depth) into spatial data, converting depth into spatial resolution. For example, a 128x128x256 activation map will be converted to 256x256x64. The number of channels is always divided by 4 while the resolution increases.

Split Channels

TNNetSplitChannels and TNNetSplitChannelEvery are specialized layer types in the CAI Neural API that allow for selective channel manipulation within neural networks.

  1. TNNetSplitChannels: This layer is designed to pick or split selected channels from the previous layer. It provides fine-grained control over which specific channels are passed on to subsequent layers in the network. Key features:

    • It can be created with a specific range of channels (ChannelStart and ChannelLen) or with an array of specific channel indices.

    Potential uses:

    • Feature selection: Allowing the network to focus on specific features represented by certain channels.
    • Creating multiple parallel paths in the network that process different subsets of the input channels.
    • Implementing attention-like mechanisms by selectively passing certain channels forward.
  2. TNNetSplitChannelEvery: This layer is a specialized version of TNNetSplitChannels. It splits channels at regular intervals.

    Potential uses:

    • Creating regular patterns of channel selection throughout the network.
    • Implementing a form of grouped convolutions or channel-wise operations.
    • Reducing the computational load by consistently selecting a subset of channels at regular intervals.

Both these layers offer powerful tools for manipulating the flow of information through the network's channels. They allow for the creation of more complex and efficient network architectures by providing fine control over which features (represented by channels) are processed in different parts of the network.

These layers could be particularly useful in scenarios where:

Layer NameInput/Output DimensionsDescription
TNNetSplitChannels2D or 3DSplits or copies channels from the input. This layer allows getting a subset of the input channels.
TNNetSplitChannelEvery2D or 3DSplits channels from the input every few channels. As example, this layer allows getting half (GetChannelEvery=2) or a third (GetChannelEvery=3) of the input channels.
TNNetInterleaveChannels2D or 3DIf you're using grouped convolutions in your network, TNNetInterleaveChannels could be particularly useful. It can help mix information between groups, allowing for more interaction between different feature groups.

Transposing Layers

The layers TNNetTransposeXD and TNNetTransposeYD are specialized layer types in the CAI Neural API that perform specific transposition operations on the input data. These transposition operations can be particularly useful in various neural network architectures and data processing pipelines:

These layers are implemented with both forward (Compute) and backward (Backpropagate) methods, indicating that they are fully integrated into the network's training process and can be used in the middle of a network, not just as preprocessing steps. This can be particularly valuable for researchers and practitioners working on novel network designs or dealing with unconventional data structures.

Layer NameInput/Output DimensionsDescription
TNNetTransposeXD2D or 3DIt transposes the X and Depth axes of the input data. It swaps the spatial dimension along the width (X-axis) with the channel or feature dimension (Depth axis).
TNNetTransposeYD2D or 3DIt transposes the Y and Depth axes of the input data. It swaps the spatial dimension along the height (Y-axis) with the channel or feature dimension (Depth axis).

Layers with Activation Functions and no Trainable Parameter

Activation functions are a fundamental component of neural networks. These functions play several crucial roles in neural networks:

The CAI Neural API supports various types of activation functions, as per the below table:

Layer NameInput/Output DimensionsActivationDescription
TNNetReLU1D, 2D, or 3DReLUApplies the ReLU activation function.
TNNetReLU61D, 2D, or 3DReLU6ReLU activation clipped at 6.
TNNetReLUL1D, 2D, or 3DReLULLeaky version of ReLU.
TNNetLeakyReLU1D, 2D, or 3DLeaky ReLUApplies a leaky ReLU activation function.
TNNetVeryLeakyReLU1D, 2D, or 3DVery Leaky ReLUApplies a very leaky ReLU activation function.
TNNetReLUSqrt1D, 2D, or 3DReLU SqrtReLU activation function with square root scaling.
TNNetSELU1D, 2D, or 3DSELUSelf-normalizing activation function.
TNNetSigmoid1D, 2D, or 3DSigmoidSigmoid activation function.
TNNetSoftMax1D, 2D, or 3DSoftMaxSoftMax activation function.
TNNetPointwiseSoftMax2D or 3D1x1 SoftMaxPointwise (1x1) SoftMax activation function.
TNNetPointwiseNorm2D or 3D1x1 NormPointwise (1x1) normalization.
TNNet.AddGroupedPointwiseSoftMax2D or 3DGr 1x1 NormGrouped pointwise (1x1) SoftMax.
TNNetSwish1D, 2D, or 3DSwishSwish activation function.
TNNetSwish61D, 2D, or 3DSwish 6Swish activation clipped at 6.
TNNetHardSwish1D, 2D, or 3DHard SwishHard version of Swish activation.
TNNetHyperbolicTangent1D, 2D, or 3DtanhHyperbolic tangent activation function.
TNNetPower1D, 2D, or 3DPowerApplies a power activation function.
TNNetMulByConstant1D, 2D, or 3D* CMultiplies the output by a constant.
TNNetNegate1D, 2D, or 3D* -1Multiplies the previous output by -1.
TNNetSignedSquareRoot1D, 2D, or 3DSSRSquare root of the input absolute value preserving the original sign. y = Sign(x) * Sqrt(Abs(x))
TNNetSignedSquareRoot11D, 2D, or 3DSSR1If Abs(x) < 1 then y = x, otherwise, y = Sign(x) * Sqrt(Abs(x)).
TNNetSignedSquareRootN1D, 2D, or 3DSSRNIf Abs(x) < N then y = x, otherwise, y = Sign(x) * Sqrt(Abs(x)-N+1)+N-1.

Trainable Bias (Shift) and Multiplication (Scaling) per Cell or Channel Allowing Faster Learning and Convergence

When TNNetCellBias is added after convolutional layers, it introduces a trainable bias to each output cell of the convolutional layer. This can have several effects on the neural network:

  1. Fine-tuning: TNNetCellBias allows for fine-tuning of the network's output by adding a learnable bias to each cell. This can help the network adjust its predictions more precisely.
  2. Increased flexibility: by adding a bias to each cell individually, the network gains additional parameters to optimize, potentially allowing it to learn more complex representations.
  3. Improved learning speed: placing this layer before and after convolutions can speed up learning. This is because it gives the network an additional way to adjust its output, potentially making it easier to find optimal solutions.
  4. Parameter increase: adding TNNetCellBias increases the number of trainable parameters in the network. While this can be beneficial for learning, it also increases the model's complexity and the risk of overfitting.

It's worth noting that the effectiveness of adding TNNetCellBias after convolutional layers can vary depending on the specific architecture and problem at hand. While it can potentially speed up learning and improve the network's flexibility, it's important to experiment and validate its impact on your particular use case. TNNetChannelBias adds a trainable bias to each channel in the output. It's like TNNetCellBias, but operating on entire channels instead of individual cells.

Layer NameInput/Output DimensionsDescription
TNNetCellBias1D, 2D, or 3DTrainable bias (shift) for each cell.
TNNetCellMul1D, 2D, or 3DTrainable multiplication (scaling) for each cell.
TNNetChannelBias1D, 2D, or 3DTrainable bias (shift) for each channel.
TNNetChannelMul1D, 2D, or 3DTrainable multiplication (scaling) for each channel.

Embedding Layers

TNNetEmbedding is designed to convert input tokens (usually represented as integers) into dense vector representations (embedding vectors). TNNetTokenAndPositionalEmbedding extends TNNetEmbedding by adding positional information to the token embeddings. This is crucial for transformer models that don't have an inherent notion of sequence order. Both layers are crucial for modern NLP tasks, especially when working with transformer-based models. They allow the network to work with text data by converting tokens into rich, informative vector representations that capture both semantic meaning and positional information. By using TNNetTokenAndPositionalEmbedding, you're equipping your model with the fundamental building blocks needed for advanced NLP tasks as it provides both embedding and positional encoding.

To illustrate how these layers might be used in practice, let's consider a simple example. Suppose you're building a language model for text generation. You could use these layers:

    FNN.AddLayer([
      TNNetInput.Create(csContextLen, 1, 1),
      TNNetTokenAndPositionalEmbedding.Create(csModelVocabSize, csEmbedDim, {EncodeZero=}1, {ScaleEmbedding=}0.02, {ScalePositional=}0.01)
    ]);

    for I := 1 to 2 do FNN.AddTransformerBlockCAI({Heads=}8, {IntermediateDim=}2048, {NoForward=}true, {HasNorm=}false);

    FNN.AddLayer([
      TNNetPointwiseConvLinear.Create(csModelVocabSize),
      TNNetPointwiseSoftMax.Create({SkipBackpropDerivative=}1)
    ]);

The above example resembles a simplified version of models like GPT (Generative Pre-trained Transformer). It's designed to process sequential data such as text generation tasks. The use of token and positional embeddings, followed by transformer blocks, is a standard approach in modern NLP models. The final pointwise convolution and softmax layers are typical for generating probability distributions over a vocabulary, which is common in language models. The number of transformer blocks (2) indicates that this is a lightweight model. The choice of parameters like embedding dimensions, number of heads, and intermediate dimensions would depend on the specific requirements of the task and computational constraints.

Layer NameInput/Output DimensionsDescription
TNNetEmbeddingInput: 1D integer tokens. Output: 2D (sequence_length x embedding_size).Converts input tokens into dense vector representations. Parameters include vocabulary size, embedding size, scaling factor, and whether to encode zero. Allows for training of embedding weights through backpropagation.
TNNetTokenAndPositionalEmbeddingInput: 1D integer tokens. Output: 2D (sequence_length x embedding_size)Extends TNNetEmbedding by adding positional information to token embeddings. This layer is crucial for transformer architectures.

Opposing Operations

Layer NameInput/Output DimensionsActivationDescription
TNNetDeLocalConnect1D, 2D, or 3DtanhOpposing operation to TNNetLocalConnect.
TNNetDeLocalConnectReLU1D, 2D, or 3DReLUOpposing operation to TNNetLocalConnectReLU.
TNNetDeconvolution1D, 2D, or 3DtanhOpposing operation to TNNetConvolution, also known as transposed convolution.
TNNetDeconvolutionReLU1D, 2D, or 3DReLUOpposing operation to convolution with ReLU activation (TNNetConvolutionReLU).
TNNetDeMaxPool1D, 2D, or 3DNoneOpposing operation to max pooling layer TNNetMaxPool.

Weight Initializers

This API implements popular weight initialization methods including He (Kaiming) and Glorot/Bengio (Xavier):

Data Augmentation Methods Implemented at TVolume

Closest Layer Types to Other APIs (work in progress)

NEURALKerasPyTorch
TNNetFullConnectlayers.Dense(activation='tanh')nn.Linear nn.Tanh()
TNNetFullConnectReLUlayers.Dense(activation='relu')nn.Linear nn.ReLU()
TNNetFullConnectLinearlayers.Dense(activation=None)nn.Linear
TNNetFullConnectSigmoidlayers.Dense(activation='sigmoid')nn.Linear nn.Sigmoid()
TNNetReLUactivations.relunn.ReLU()
TNNetLeakyReLUactivations.relu(alpha=0.01)nn.LeakyReLU(0.01)
TNNetVeryLeakyReLUactivations.relu(alpha=1/3)nn.LeakyReLU(1/3)
TNNetReLUSqrt
TNNetSELUactivations.selunn.SELU
TNNetSigmoidactivations.sigmoidnn.Sigmoid
TNNetSoftMaxactivations.softmaxnn.Softmax
TNNetHyperbolicTangentactivations.tanhnn.Tanh
TNNetPower
TNNetAvgPoollayers.AveragePooling2Dnn.AvgPool2d
TNNetMaxPoollayers.MaxPool2Dnn.MaxPool2d
TNNetMaxPoolPortablelayers.MaxPool2Dnn.MaxPool2d
TNNetMinPool
TNNet.AddMinMaxPool
TNNet.AddAvgMaxPool
TNNetAvgChannellayers.GlobalAveragePooling2Dnn.AvgPool2d
TNNetMaxChannellayers.GlobalMaxPool2Dnn.MaxPool2d
TNNetMinChannel
TNNet.AddMinMaxChannel
TNNet.AddAvgMaxChannelcai.layers.GlobalAverageMaxPooling2D
TNNetConcatlayers.Concatenate(axis=1)torch.cat
TNNetDeepConcatlayers.Concatenate(axis=3)torch.cat
TNNetIdentitynn.Identity
TNNetIdentityWithoutBackprop
TNNetReshapelayers.Reshapetorch.reshape
TNNetSplitChannelscai.layers.CopyChannels
TNNetSplitChannelEvery
TNNetSumlayers.Addtorch.add
TNNetCellMulByCelllayers.Multiply
TNNetChannelMulByLayerlayers.Multiply
TNNetUpsampletf.nn.depth_to_space

Adding Layers

You can add layers one by one or you can add an array of layers in one go. Follows an example adding layers one by one:

NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1));
NN.AddLayer(TNNetMaxPool.Create(2));

The next example shows how to add an array of layers that is equivalent to the above example:

NN.AddLayer([
  TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1),
  TNNetMaxPool.Create(2)
]);

Multi-path Architectures Support

Since 2017, this API supports multi-paths architectures. You can create multi-paths with AddLayerAfter method. For concatenating (merging) paths, you can call either TNNetConcat or TNNetDeepConcat. Follows an example:

// Creates The Neural Network
NN := TNNet.Create();
 
// This network splits into 2 paths and then is later concatenated
InputLayer := NN.AddLayer(TNNetInput.Create(32, 32, 3));
 
// First branch starting from InputLayer (5x5 features)
NN.AddLayerAfter(TNNetConvolutionReLU.Create({Features=}16, {FeatureSize=}5, {Padding=}2, {Stride=}1), InputLayer);
NN.AddLayer(TNNetMaxPool.Create(2));
NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1));
NN.AddLayer(TNNetMaxPool.Create(2));
EndOfFirstPath := NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}5, {Padding=}2, {Stride=}1));
 
// Another branch starting from InputLayer (3x3 features)
NN.AddLayerAfter(TNNetConvolutionReLU.Create({Features=}16, {FeatureSize=}3, {Padding=}1, {Stride=}1), InputLayer);
NN.AddLayer(TNNetMaxPool.Create(2));
NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1));
NN.AddLayer(TNNetMaxPool.Create(2));
EndOfSecondPath := NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1));
 
// Concats both branches into one branch.
NN.AddLayer(TNNetDeepConcat.Create([EndOfFirstPath, EndOfSecondPath]));
NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1));
NN.AddLayer(TNNetLayerFullConnectReLU.Create(64));
NN.AddLayer(TNNetLayerFullConnectReLU.Create(NumClasses));

These source code examples show AddLayerAfter:

You can find more about multi-path architectures at:

Dataset Support

These datasets can be easily loaded:

CIFAR-10

procedure CreateCifar10Volumes(out ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList);

Source code example: Simple CIFAR-10 Image Classifier

CIFAR-100

procedure CreateCifar100Volumes(out ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList);

Source code example: CAI Optimized DenseNet CIFAR-100 Image Classifier

MNIST and Fashion MNIST

procedure CreateMNISTVolumes(out ImgTrainingVolumes, ImgValidationVolumes,
  ImgTestVolumes: TNNetVolumeList;
  TrainFileName, TestFileName: string;
  Verbose:boolean = true;
  IsFashion:boolean = false);

Source code examples:

One Class per Folder with Image Classification

In the case that your dataset has one class per folder, you can call CreateVolumesFromImagesFromFolder for loading your data into RAM:

// change ProportionToLoad to a smaller number if you don't have enough RAM.
ProportionToLoad := 1;
WriteLn('Loading ', Round(ProportionToLoad*100), '% of the Plant leave disease dataset into memory.');
CreateVolumesFromImagesFromFolder
(
  ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes,
  {FolderName=}'plant', {pImageSubFolder=}'',
  {color_encoding=}csRGB{RGB},
  {TrainingProp=}0.9*ProportionToLoad,
  {ValidationProp=}0.05*ProportionToLoad,
  {TestProp=}0.05*ProportionToLoad,
  {NewSizeX=}128, {NewSizeY=}128
);

The example above shows how to load the dataset with 90% loaded into training and 5% loaded for each validation and testing. Images are being resized to 128x128.

Source code examples:

Is your Dataset too Big for RAM? You should use TNeuralImageLoadingFit.

In the case that your image classification dataset is too big to be stored in RAM, you can follow this example:

    FTrainingFileNames, FValidationFileNames, FTestFileNames: TFileNameList;
...
    ProportionToLoad := 1;
    CreateFileNameListsFromImagesFromFolder(
      FTrainingFileNames, FValidationFileNames, FTestFileNames,
      {FolderName=}'places_folder/train', {pImageSubFolder=}'',
      {TrainingProp=}0.9*ProportionToLoad,
      {ValidationProp=}0.05*ProportionToLoad,
      {TestProp=}0.05*ProportionToLoad
    );

Then, you can call a fitting method made specific for this:

NeuralFit := TNeuralImageLoadingFit.Create;
...
NeuralFit.FitLoading({NeuralNetworkModel}NN, {ImageSizeX}256, {ImageSizeY}256, FTrainingFileNames, FValidationFileNames, FTestFileNames, {BatchSize}256, {Epochs}100);

TNeuralImageLoadingFit.FitLoading has been tested with Places365-Standard Small images 256x256 with easy directory structure. You can follow this example:

Loading and Saving Images with Volumes

When loading an image from a file, the easiest and fastest method is calling LoadImageFromFileIntoVolume(ImageFileName:string; V:TNNetVolume). When loading from an TFPMemoryImage, you can load with LoadImageIntoVolume(M: TFPMemoryImage; Vol:TNNetVolume). For saving an image, the fastest method is SaveImageFromVolumeIntoFile(V: TNNetVolume; ImageFileName: string).

Fitting your Neural Network

The easiest way to train your neural network is utilizing unit neuralfit.pas. Inside this unit, you’ll find the class TNeuralImageFit that is used by many examples.

Image Classification

TNeuralImageFit has been designed for image classification tasks and can be called as follows:

procedure Fit(pNN: TNNet;
  pImgVolumes, pImgValidationVolumes, pImgTestVolumes: TNNetVolumeList;
  pNumClasses, pBatchSize, Epochs: integer);

Each volume should be provided with property tag that contains the corresponding class. TNeuralImageFit internally implements data augmentation techniques: flipping, making gray, cropping and resizing. These techniques can be controlled with:

property HasImgCrop: boolean read FHasImgCrop write FHasImgCrop;
property HasMakeGray: boolean read FHasMakeGray write FHasMakeGray;
property HasFlipX: boolean read FHasFlipX write FHasFlipX;
property HasFlipY: boolean read FHasFlipY write FHasFlipY;
property MaxCropSize: integer read FMaxCropSize write FMaxCropSize; 

Once you have a trained neural network, you can use an advanced classification procedure that will average the classification probability of the input image with its flipped and cropped versions. This process frequently gives a higher classification accuracy at the expense of internally running the very same neural network a number of times. This is how you can classify images:

procedure ClassifyImage(pNN: TNNet; pImgInput, pOutput: TNNetVolume);

In the case that you would like to look into TNeuralImageFit in more detail, the Simple CIFAR-10 Image Classifier example is a good starting point.

Training with Volume Pair Lists - TNeuralFit

In the case that your training, validation and testing data can be defined as volume pairs from input volume to output volume, the easiest way to train your neural network will be calling TNeuralFit. This class has the following fitting method:

procedure Fit(pNN: TNNet;
  pTrainingVolumes, pValidationVolumes, pTestVolumes: TNNetVolumePairList;
  pBatchSize, Epochs: integer);

Both AND, OR and XOR with neuralfit unit and hypotenuse function examples load volume pair lists for training.

Training with Volume Pairs - TNeuralDataLoadingFit

The TNeuralFit implementation has a limitation: your dataset needs to be placed into RAM. In the case that your dataset is too large for RAM, you can call TNeuralDataLoadingFit:

TNNetGetPairFn = function(Idx: integer; ThreadId: integer): TNNetVolumePair of object;
TNNetGet2VolumesProc = procedure(Idx: integer; ThreadId: integer; pInput, pOutput: TNNetVolume) of object;
  
TNeuralDataLoadingFit = class(TNeuralFitBase)
...
    procedure FitLoading(pNN: TNNet;
      TrainingCnt, ValidationCnt, TestCnt, pBatchSize, Epochs: integer;
      pGetTrainingPair, pGetValidationPair, pGetTestPair: TNNetGetPairFn); overload;
    procedure FitLoading(pNN: TNNet;
      TrainingCnt, ValidationCnt, TestCnt, pBatchSize, Epochs: integer;
      pGetTrainingProc, pGetValidationProc, pGetTestProc: TNNetGet2VolumesProc); overload;

The Hypotenuse with FitLoading example uses TNeuralDataLoadingFit so it creates training pairs on the fly.

TNeuralFitBase

TNeuralImageFit and TNeuralDataLoadingFit both descend from TNeuralFitBase. From TNeuralFitBase, you can define training properties:

property Inertia: single read FInertia write FInertia;
property InitialEpoch: integer read FInitialEpoch write FInitialEpoch;
property InitialLearningRate: single read FInitialLearningRate write FInitialLearningRate;
property LearningRateDecay: single read FLearningRateDecay write FLearningRateDecay;
property CyclicalLearningRateLen: integer read FCyclicalLearningRateLen write FCyclicalLearningRateLen;
property Momentum: single read FInertia write FInertia;
property L2Decay: single read FL2Decay write FL2Decay;
property FileNameBase: string read FFileNameBase write FFileNameBase;

You can also collect current statistics:

property CurrentEpoch: integer read FCurrentEpoch;
property CurrentStep: integer read FCurrentStep;
property CurrentLearningRate: single read FCurrentLearningRate;
property TestAccuracy: TNeuralFloat read FTestAccuracy;
property TrainingAccuracy: TNeuralFloat read FTrainingAccuracy;
property Running: boolean read FRunning;

Some events are available:

property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnAfterStep: TNotifyEvent read FOnAfterStep write FOnAfterStep;
property OnAfterEpoch: TNotifyEvent read FOnAfterEpoch write FOnAfterEpoch;

You can define your own learning rate schedule:

property CustomLearningRateScheduleFn: TCustomLearningRateScheduleFn read FCustomLearningRateScheduleFn write FCustomLearningRateScheduleFn;
property CustomLearningRateScheduleObjFn: TCustomLearningRateScheduleObjFn read FCustomLearningRateScheduleObjFn write FCustomLearningRateScheduleObjFn;

Got Too Many Console Messages?

TNeuralFitBase descends from TMObject that allows you to code your own message treatment:

property MessageProc: TGetStrProc read FMessageProc write FMessageProc;
property ErrorProc: TGetStrProc read FErrorProc write FErrorProc;

On your own code, you could something is:

MyFit.MessageProc := {$IFDEF FPC}@{$ENDIF}Self.MessageProc;
MyFit.ErrorProc := {$IFDEF FPC}@{$ENDIF}Self.ErrorProc;

If you don’t need any message at all, you can hide messages by calling:

procedure HideMessages();

You can also disable fitting verbosity with:

property Verbose: boolean read FVerbose write FVerbose;

Your code will look like this:

NeuralFit := TNeuralImageFit.Create;
...
NeuralFit.Verbose := false;
NeuralFit.HideMessages();

Parallel Computing - The neuralthread.pas

This API has easy to use, lightweight and platform independent parallel processing API methods.

As an example, assuming that you need to run a procedure 10 times in parallel, you can create 10 thread workers as follows:

FProcs := TNeuralThreadList.Create( 10 );

As an example, this is the procedure that we intend to run in parallel:

procedure MyClassName.RunNNThread(index, threadnum: integer);
begin
  WriteLn('This is thread ',index,' out of ',threadnum,' threads.');
end; 

Then, to run the procedure RunNNThread passed as parameter 10 times in parallel, do this:

FProcs.StartProc({$IFDEF FPC}@RunNNThread{$ELSE}RunNNThread{$ENDIF});

You can control the blocking mode (waiting threads to finish before the program continues) as per declaration:

procedure StartProc(pProc: TNeuralProc; pBlock: boolean = true);

Or, if you prefer, you can specifically say when to wait for threads to finish as per this example:

FProcs.StartProc({$IFDEF FPC}@RunNNThread{$ELSE}RunNNThread{$ENDIF}, false);
// insert your code here
FProcs.WaitForProc(); // waits until all threads are finished.

When you are done, you should call:

FProcs.Free; 

NLP

This NLP source code example shows a (hello world) small neural network trained on the Tiny Stories dataset. A more complex NLP example showing the implementation of the GPT-3 Small architecture is also available.

In short, this API supports:

Publications from the Author

In the case that you would like to know more about what the CAI's author is working at, here we go.

Optimizing the first layers of a convolutional neural network:

Optimizing deep layers of a convolutional neural network:

Publicações em Português:

Contributing

Pull requests are welcome. Having requests accepted might be hard.

Paid Support

In the case that you need help with your own A.I. project (Pascal, Python, or PHP), please feel free to contact the author of this API.

Citing this API

You can cite this API in BibTeX format with:

@software{cai_neural_api_2021_5810077,
  author       = {Joao Paulo Schwarz Schuler},
  title        = {CAI NEURAL API},
  month        = dec,
  year         = 2021,
  publisher    = {Zenodo},
  version      = {v1.0.6},
  doi          = {10.5281/zenodo.5810077},
  url          = {https://doi.org/10.5281/zenodo.5810077}
}