This repository contains pre-trained neural network models for the CAI neural API.
You can icrease the resolution of your own images with this code and its pre-trained model. After compiling the super resolution code, you will be able to increase the resolution of your own images via command line:
#SuperResolution -i street.png -o street2.png
The parameter -i
defines the input file while -o
defines the output file. You can find more details at this link.
Dataset | Source Code | Input Size | Trained Model | Parameters | Test Accuracy |
---|---|---|---|---|---|
Malaria | source | 64x64x3 | Malaria-20230720 | 192K | 95.63% |
Colorectal Cancer | source | 64x64x3 | Colorectal-20230720 | 202K | 94.26% |
Plant Leaf Disease (Plant Village) |
source | 64x64x3 | SimplePlantLeafDisease-20230720 | 252K | 99.03% |
The simplest way to load a trained model and classify an image is:
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;
The above source code is located at TestPlantLeafDiseaseTrainedModelOneImage.pas.
If you would like to test against the actual training dataset, you can follow this example: TestPlantLeafDiseaseTrainedModel.pas.
In the case that you need more control on how your image is classified, you can look at this more detailed example:
procedure ClassifyOneImage;
var
NN: TNNet;
ImageFileName: string;
NeuralFit: TNeuralImageFit;
vInputImage, vOutput: TNNetVolume;
InputSizeX, InputSizeY, NumberOfClasses: integer;
begin
WriteLn('Loading Neural Network...');
NN := TNNet.Create;
NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
NN.DebugStructure();
InputSizeX := NN.Layers[0].Output.SizeX;
InputSizeY := NN.Layers[0].Output.SizeY;
NumberOfClasses := NN.GetLastLayer().Output.Size;
NeuralFit := TNeuralImageFit.Create;
vInputImage := TNNetVolume.Create();
vOutput := TNNetVolume.Create(NumberOfClasses);
ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
WriteLn('Loading image: ',ImageFileName);
if LoadImageFromFileIntoVolume(
ImageFileName, vInputImage, InputSizeX, InputSizeY,
{EncodeNeuronalInput=}csEncodeRGB) then
begin
WriteLn('Classifying the image:', ImageFileName);
vOutput.Fill(0);
NeuralFit.ClassifyImage(NN, vInputImage, vOutput);
WriteLn('The image belongs to the class of images: ', vOutput.GetClass());
end
else
begin
WriteLn('Failed loading image: ',ImageFileName);
end;
vInputImage.Free;
vOutput.Free;
NeuralFit.Free;
NN.Free;
end;
The trained neural network (model) is loaded with
NN := TNNet.Create;
NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
The input image size is found from the loaded model with:
InputSizeX := NN.Layers[0].Output.SizeX;
InputSizeY := NN.Layers[0].Output.SizeY;
The number of classes is found from the loaded model with:
NumberOfClasses := NN.GetLastLayer().Output.Size;
The image is loaded, resized and scaled from [0,255] to [-2,+2] with:
ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
WriteLn('Loading image: ',ImageFileName);
if LoadImageFromFileIntoVolume(
ImageFileName, vInputImage, InputSizeX, InputSizeY,
{EncodeNeuronalInput=}csEncodeRGB) then
The NN is run with plenty of tricks specific for computer vision with:
NeuralFit.ClassifyImage(NN, vInputImage, vOutput);
The output of the neural network is placed at vOutput
. The actual predicted class can be found with:
vOutput.GetClass()