schuler commited on
Commit
9bdb2a6
·
verified ·
1 Parent(s): cffdde2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +132 -3
README.md CHANGED
@@ -1,3 +1,132 @@
1
- ---
2
- license: lgpl-2.1
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Pre-trained Neural API Networks (Models)
2
+ This repository contains pre-trained neural network models for the [CAI neural API](https://github.com/joaopauloschuler/neural-api).
3
+
4
+ ## Super resolution pre-trained neural network model
5
+ You can icrease the resolution of your own images with this [code](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SuperResolution/SuperResolution.lpr) and its pre-trained [model](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SuperResolution/super-resolution-7-64-sep.nn). After compiling [the super resolution code](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SuperResolution/SuperResolution.lpr), you will be able to increase the resolution of your own images via command line:
6
+ ```
7
+ #SuperResolution -i street.png -o street2.png
8
+ ```
9
+
10
+ The parameter `-i` defines the input file while `-o` defines the output file. You can find more details at this [link](https://github.com/joaopauloschuler/neural-api/tree/master/examples/SuperResolution).
11
+
12
+ ## Image classification pre-trained neural network models
13
+
14
+ | Dataset | Source Code | Input Size | Trained Model | Parameters | Test Accuracy |
15
+ |---------|-------------|------------|---------------|---------------|---------------|
16
+ | [Malaria](https://www.tensorflow.org/datasets/catalog/malaria)|[source](https://github.com/joaopauloschuler/neural-api/blob/master/examples/MalariaImageClassification/MalariaImageClassification.pas)|64x64x3|[Malaria-20230720](https://github.com/joaopauloschuler/pre-trained-neural-api-networks/tree/main/image-classification/malaria)|192K|95.63%|
17
+ | [Colorectal Cancer](https://www.tensorflow.org/datasets/catalog/colorectal_histology)|[source](https://github.com/joaopauloschuler/neural-api/blob/master/examples/ColorectalImageClassification/ColorectalImageClassification.pas)|64x64x3|[Colorectal-20230720](https://github.com/joaopauloschuler/pre-trained-neural-api-networks/tree/main/image-classification/colorectal-cancer)|202K|94.26%|
18
+ | [Plant Leaf Disease <br/> (Plant Village)](https://www.tensorflow.org/datasets/catalog/plant_village)|[source](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SimplePlantLeafDisease/SimplePlantLeafDisease.pas)|64x64x3|[SimplePlantLeafDisease-20230720](https://github.com/joaopauloschuler/pre-trained-neural-api-networks/tree/main/image-classification/plant-leaf-disease)|252K|99.03%|
19
+
20
+ ### Using Trained Models for Image Classification
21
+
22
+ The simplest way to load a trained model and classify an image is:
23
+ ```
24
+ procedure ClassifyOneImageSimple;
25
+ var
26
+ NN: TNNet;
27
+ ImageFileName: string;
28
+ NeuralFit: TNeuralImageFit;
29
+ begin
30
+ WriteLn('Loading Neural Network...');
31
+ NN := TNNet.Create;
32
+ NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
33
+ NeuralFit := TNeuralImageFit.Create;
34
+ ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
35
+ WriteLn('Processing image: ', ImageFileName);
36
+ WriteLn(
37
+ 'The class of the image is: ',
38
+ NeuralFit.ClassifyImageFromFile(NN, ImageFileName)
39
+ );
40
+ NeuralFit.Free;
41
+ NN.Free;
42
+ end;
43
+ ```
44
+ The above source code is located at [TestPlantLeafDiseaseTrainedModelOneImage.pas](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SimplePlantLeafDisease/TestPlantLeafDiseaseTrainedModelOneImage.pas).
45
+
46
+ If you would like to test against the actual training dataset, you can follow this example:
47
+ [TestPlantLeafDiseaseTrainedModel.pas](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SimplePlantLeafDisease/TestPlantLeafDiseaseTrainedModel.pas).
48
+
49
+ In the case that you need more control on how your image is classified, you can look at this more detailed example:
50
+ ```
51
+ procedure ClassifyOneImage;
52
+ var
53
+ NN: TNNet;
54
+ ImageFileName: string;
55
+ NeuralFit: TNeuralImageFit;
56
+ vInputImage, vOutput: TNNetVolume;
57
+ InputSizeX, InputSizeY, NumberOfClasses: integer;
58
+ begin
59
+ WriteLn('Loading Neural Network...');
60
+ NN := TNNet.Create;
61
+ NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
62
+ NN.DebugStructure();
63
+ InputSizeX := NN.Layers[0].Output.SizeX;
64
+ InputSizeY := NN.Layers[0].Output.SizeY;
65
+ NumberOfClasses := NN.GetLastLayer().Output.Size;
66
+
67
+ NeuralFit := TNeuralImageFit.Create;
68
+ vInputImage := TNNetVolume.Create();
69
+ vOutput := TNNetVolume.Create(NumberOfClasses);
70
+
71
+ ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
72
+ WriteLn('Loading image: ',ImageFileName);
73
+
74
+ if LoadImageFromFileIntoVolume(
75
+ ImageFileName, vInputImage, InputSizeX, InputSizeY,
76
+ {EncodeNeuronalInput=}csEncodeRGB) then
77
+ begin
78
+ WriteLn('Classifying the image:', ImageFileName);
79
+ vOutput.Fill(0);
80
+ NeuralFit.ClassifyImage(NN, vInputImage, vOutput);
81
+ WriteLn('The image belongs to the class of images: ', vOutput.GetClass());
82
+ end
83
+ else
84
+ begin
85
+ WriteLn('Failed loading image: ',ImageFileName);
86
+ end;
87
+
88
+ vInputImage.Free;
89
+ vOutput.Free;
90
+ NeuralFit.Free;
91
+
92
+ NN.Free;
93
+ end;
94
+ ```
95
+
96
+ The trained neural network (model) is loaded with
97
+ ```
98
+ NN := TNNet.Create;
99
+ NN.LoadFromFile('SimplePlantLeafDisease-20230720.nn');
100
+ ```
101
+
102
+ The input image size is found from the loaded model with:
103
+ ```
104
+ InputSizeX := NN.Layers[0].Output.SizeX;
105
+ InputSizeY := NN.Layers[0].Output.SizeY;
106
+ ```
107
+
108
+ The number of classes is found from the loaded model with:
109
+ ```
110
+ NumberOfClasses := NN.GetLastLayer().Output.Size;
111
+ ```
112
+
113
+ The image is loaded, resized and scaled from [0,255] to [-2,+2] with:
114
+ ```
115
+ ImageFileName := 'plant/Apple___Black_rot/image (1).JPG';
116
+ WriteLn('Loading image: ',ImageFileName);
117
+
118
+ if LoadImageFromFileIntoVolume(
119
+ ImageFileName, vInputImage, InputSizeX, InputSizeY,
120
+ {EncodeNeuronalInput=}csEncodeRGB) then
121
+ ```
122
+
123
+ The NN is run with plenty of tricks specific for computer vision with:
124
+ ```
125
+ NeuralFit.ClassifyImage(NN, vInputImage, vOutput);
126
+ ```
127
+
128
+ The output of the neural network is placed at `vOutput`. The actual predicted class can be found with:
129
+ ```
130
+ vOutput.GetClass()
131
+ ```
132
+