File size: 6,068 Bytes
e1bcf3e
 
 
 
 
9bdb2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1bcf3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
license: cc
language:
- en
---
# Pre-trained Neural API Networks (Models)
This repository contains pre-trained neural network models for the [CAI neural API](https://github.com/joaopauloschuler/neural-api).

## Super resolution pre-trained neural network model
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:
```
#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](https://github.com/joaopauloschuler/neural-api/tree/master/examples/SuperResolution).

## Image classification pre-trained neural network models

| Dataset | Source Code | Input Size | Trained Model | Parameters    | Test Accuracy | 
|---------|-------------|------------|---------------|---------------|---------------|
| [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%|
| [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%|
| [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%|

### Using Trained Models for Image Classification

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](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SimplePlantLeafDisease/TestPlantLeafDiseaseTrainedModelOneImage.pas).

If you would like to test against the actual training dataset, you can follow this example:
[TestPlantLeafDiseaseTrainedModel.pas](https://github.com/joaopauloschuler/neural-api/blob/master/examples/SimplePlantLeafDisease/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()
```