Maxbox Starter105 Classify Pretrained Model
Maxbox Starter105 Classify Pretrained Model
Maxbox Starter105 Classify Pretrained Model
Classify Cifar10
____________________________________________________________________________
maXbox Starter 105 – CIFAR-10 Image Classifier with
loading and testing a pre-trained model.
“Train your models during night – called night train ,-)”.
Now let’s have a look at the app/script below with individual images from
Cifar test data. For this, we wrote two useful functions. The first one
returns the label associated with predictions made by the model. The
second one accepts one image as an argument. Then it will show the image,
the prediction the model made and the actual class the image belongs to.
Also other probabilities are shown in the multi-classification grid:
Pic:1135_classifiergui2_tutor105.png
1/8
Specifically, models are comprised of small linear filters and the result
of applying filters called activation maps, or more generally, feature
maps. Looking at the following dataset, it will extract features in a
constant dot product, even though images has shadows or positioned with
various angle. It is important to note that filters acts as feature
detectors from the original input image, in our case 32*32 bitmaps.
The proper way to use a CNN doesn’t exists. The advice for ugly score is
to use a smaller learning rate or larger batch size for the weights that
are being fine-tuned and a higher one for the randomly initialized
weights (e.g. the ones in the softmax classifier) TNNetSoftMax. Pre-
trained weights (in ClassifyCNNModel_70.nn) are already good, they need
to be fine-tuned, not distorted.
Pic:1135_visualcifarset.png
The learning rate is the crucial hyper-parameter used during the training
of deep convolution neural networks (DCNN) to improve model accuracy;
By following these ways you can make a CNN model that has a validation
set accuracy of more than 95 % but the question is how specific or
relevant is this validation.
In our example, values smaller than 0.7 mean false while values bigger
than 0.7 mean true. This is called monopolar encoding. CAI also supports
bipolar encoding (-1, +1). Let's have a look directly into the source
code for the labels and the classify method:
procedure setClassifierLabels;
begin
2/8
cs10Labels[0]:= 'airplane';
cs10Labels[1]:= 'automobile';
cs10Labels[2]:= 'bird';
cs10Labels[3]:= 'cat';
cs10Labels[4]:= 'deer';
cs10Labels[5]:= 'dog';
cs10Labels[6]:= 'frog';
cs10Labels[7]:= 'horse';
cs10Labels[8]:= 'ship';
cs10Labels[9]:= 'truck';
end;
The main procedure to classify incoming images loads the model, decides
dropout or not (later more) and creates input- and output-volumes with
the shape of 32;32;3 or a 32x32x3 volume:
begin
NN:= THistoricalNets.create; //TNNet.Create();
NN.LoadFromFile(TRAINPATH);
label2.caption:= 'load: '+TRAINPATH;
if chkboxdrop.checked then
NN.EnableDropouts(true) else
NN.EnableDropouts(false);
pInput:= TNNetVolume.Create0(32, 32, 3, 1);
pOutPut:= TNNetVolume.Create0(10, 1, 1, 1);
LoadPictureIntoVolume(image1.picture, pinput);
pInput.RgbImgToNeuronalInput(csEncodeRGB);
NN.Compute65(pInput,0);
NN.GetOutput(pOutPut);
writeln('result get class type: '+itoa(pOutPut.GetClass()));
3/8
The *.nn file in TRAINPATH serves as a pre-trained file (FAvgWeight) to
classify/predict images we trained on. Also the CIFAR-10 classification
examples with experiments/testcnnalgo/testcnnalgo.lpr and a number of
CIFAR-10 classification examples are available on /experiments.
Imagine the accuracy goes up and the loss-function (error-rate) goes
down. The loss function is the bread & butter of modern machine learning;
it takes your algorithm from theoretical to practical and transforms
matrix multiplication into deep learning.
6) TNNetDropout:2;0;0;0;0;0;0;0
Pic:1135_classifiergui3_tutor105.png
4/8
Classify you get another result in small changes. The ship in the first
screen is classified with 15.2 now above with 17.1. This means that their
contribution to the activation of downstream neurons is temporally
removed on the forward pass, and any weight updates are not applied to
the neuron on the backward pass. If you want a comparable result,
deactivate the checkbox. So what's the advantage of dropout? You can
imagine that if neurons are randomly dropped out of a network during
training, other neurons will have to step in and complement the repres-
entation required to make predictions for those missing neurons.
The effect is that a CNN (or whatever deep learning nn) becomes less
sensitive to some specific weights of neurons. This in turn, results in a
network capable of better generalization and less likely to specialise
training data, means you get on the average with new or in training
unseen pictures a better result. For example we take a new picture out of
the known classification labels. For this we convert the picture at first
as a cifar 32*32 24-bit bitmap:
Pic:1135_resizebitmap_tutor105.png
Then we load the picture as *.bmp (just drop in the ./data directory and
try to classify an unknown class with unseen training, but and that's
sort of surprising, we get a result:
Pic:1135_dropout_tutor105.png
5/8
So the result is devastating and amazing too, somewhat between dog and
horse is the kind of bionics!
But this can be a baseline for similarities in a recommender system or
you can classify the age or sex of a person, enable also in a gender gap
research. In our model, a new dropout layer between TNNetConvolutionReLU
(activation layer) and the hidden layer TNNetMaxPool was added. You can
also make them visible, but its more art than science or more science
than fiction:
Pic:1135_dropout_tutor105.jpg
This visual technique above used to help with the understanding about
what individual neurons represent is called Gradient Ascent. You can find
more about gradient ascent at http://yosinski.com/deepvis.
Pic:lazarus_maxbox_classifier1.jpg
6/8
The FormCreate() can also be triggered with this few lines of code:
//FindAllFiles(ComboBox1.Items, 'csdata');
FindFiles(exepath+'data', '*.bmp',items);
writeln(items.text);
for t:= 1 to items.count-1 do
ComboBox1.Items.add(items[t]);
if ComboBox1.Items.Count > 0 then begin
ComboBox1.text:= ComboBox1.Items[0];
if FileExists(ComboBox1.text) then begin
Image1.Picture.LoadFromFile(ComboBox1.text);
Image2.Picture.LoadFromFile(ComboBox1.text);
label1.Caption:= extractfilename(ComboBox1.text);
end;
end;
end;
Pic:1135_classifier_box_tutor105.png
7/8
Conclusion:
The neural-api or CAI API (Conscious Artificial Intelligence) is some-
thing like TensorFlow for Pascal and is platform-independent open source
library for artificial intelligence or machine learning in the field of
speech recognition, image classification, OpenCL, big data, data science
and computer vision2.
https://github.com/joaopauloschuler/neural-api
https://sourceforge.net/projects/cai/files/
https://github.com/maxkleiner/neural-api
Reference:
As a Jupyter Notebook:
https://github.com/maxkleiner/maXbox/blob/master/EKON24_SimpleImageClassificatio
nCPU.ipynb
and the same in colab.research:
https://colab.research.google.com/github/maxkleiner/maXbox/blob/master/EKON24_Si
mpleImageClassificationCPU.ipynb
The whole package with app, script, tutorial, data and model:
https://github.com/maxkleiner/neural-
api/blob/master/examples/SimpleImageClassifier/MachineLearningPackage.zip
maXbox4exe maXbox4exe
Datum: 2023.02.12 20:27:42 8/8
+01'00'