Recent

Author Topic: Conscious Artificial Intelligence - Project Update  (Read 9054 times)

zulof

  • Newbie
  • Posts: 4
Re: Conscious Artificial Intelligence - Project Update
« Reply #30 on: November 12, 2019, 11:05:27 am »
Could/Should this be included in online package manager?

m.abudrais

  • New Member
  • *
  • Posts: 47
Re: Conscious Artificial Intelligence - Project Update
« Reply #31 on: November 22, 2019, 08:14:54 am »
thank you for making this good library.
I am absolute beginner in NN.
i run the CIFAR-10 example (for  50 epochs only)   and  the SimpleImageClassifier.nn is generated.
then I tried to Classify some image  by this code:
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   NN: THistoricalNets;
  4.   O1: array of TNeuralFloat;
  5.   pOutPut, pInput: TNNetVolume;
  6.   k, y, x: integer;
  7.   OK: extended;
  8.   img: TTinyImage;
  9. begin
  10.   Image1.Picture.LoadFromFile('C:\Users\moh\Desktop\dog1.png');
  11.   NN := THistoricalNets.Create();
  12.   NN.AddLayer([TNNetInput.Create(32, 32, 3),
  13.     TNNetConvolutionLinear.Create(64, 5, 2, 1, 1).InitBasicPatterns(),
  14.     TNNetMaxPool.Create(4), TNNetConvolutionReLU.Create(64, 3, 1, 1, 1),
  15.     TNNetConvolutionReLU.Create(64, 3, 1, 1, 1),
  16.     TNNetConvolutionReLU.Create(64, 3, 1, 1, 1),
  17.     TNNetConvolutionReLU.Create(64, 3, 1, 1, 1), TNNetDropout.Create(0.5),
  18.     TNNetMaxPool.Create(2), TNNetFullConnectLinear.Create(10),
  19.     TNNetSoftMax.Create()]);
  20.   NN.LoadDataFromFile('SimpleImageClassifier.nn');
  21.   for y := 0 to 31 do
  22.   begin
  23.     for x := 0 to 31 do
  24.     begin
  25.       img.B[y, x] := Red(Image1.Canvas.Pixels[y, x]);
  26.       img.g[y, x] := Green(Image1.Canvas.Pixels[y, x]);
  27.       img.B[y, x] := Blue(Image1.Canvas.Pixels[y, x]);
  28.     end;
  29.   end;
  30.   pInput := TNNetVolume.Create(32, 32, 3, 1);
  31.   pOutPut := TNNetVolume.Create(10, 1, 1, 1);
  32.   LoadTinyImageIntoNNetVolume(img, pInput);
  33.   NN.Compute(pInput);
  34.   NN.GetOutput(pOutPut);
  35.   OK := 0.0;
  36.   for k := 0 to 9 do
  37.   begin
  38.     OK := OK + pOutPut.Raw[k];
  39.     WriteLn(pOutPut.Raw[k]);
  40.   end;
  41.   WriteLn();
  42.   WriteLn(OK);
  43. end;  
i tried image for a dog and car from the same data set but i always get 1 in pOutPut.Raw[0]!
can you please add  example  to show how to use Classifier after training.
« Last Edit: November 22, 2019, 08:16:33 am by m.abudrais »

schuler

  • Full Member
  • ***
  • Posts: 136
Re: Conscious Artificial Intelligence - Project Update
« Reply #32 on: November 25, 2019, 04:56:02 pm »
Please forgive me for taking so long to reply.

First of all, you can completely remove "AddLayer" call as CAI stores both architecture and weights into the same nn file.

Then, you need to add RgbImgToNeuronalInput as follows:

Code: Pascal  [Select]
  1. LoadTinyImageIntoNNetVolume(img, pInput);
  2. pInput.RgbImgToNeuronalInput(csEncodeRGB);

If the above doesn't work, I'll try to compile at my end.

As you have SoftMax, you can print output class probabilities with:
Code: Pascal  [Select]
  1. pOutPut.Print();

You can get the inferred output class with:
Code: Pascal  [Select]
  1. WriteLn('Inferred Class: ', csTinyImageLabel[pOutPut.GetClass()]);

m.abudrais

  • New Member
  • *
  • Posts: 47
Re: Conscious Artificial Intelligence - Project Update
« Reply #33 on: November 25, 2019, 07:40:52 pm »
thank you for replying.
Quote
First of all, you can completely remove "AddLayer" call as CAI stores both architecture and weights into the same nn file.

I used the wrong function LoadDataFromFile, the correct one is LoadFromFile.
now it works :).
this is the correct code if any one is interesting
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   NN: THistoricalNets;
  4.   O1: array of TNeuralFloat;
  5.   pOutPut, pInput: TNNetVolume;
  6.   k, y, x: integer;
  7.   OK: extended;
  8.   img: TTinyImage;
  9. begin
  10.   NN := THistoricalNets.Create();
  11.   NN.LoadFromFile('SimpleImageClassifier.nn');
  12.   for y := 0 to 31 do
  13.   begin
  14.     for x := 0 to 31 do
  15.     begin
  16.       img.R[y, x] := Red(Image1.Canvas.Pixels[y, x]);
  17.       img.g[y, x] := Green(Image1.Canvas.Pixels[y, x]);
  18.       img.B[y, x] := Blue(Image1.Canvas.Pixels[y, x]);
  19.     end;
  20.   end;
  21.   pInput := TNNetVolume.Create(32, 32, 3, 1);
  22.   pOutPut := TNNetVolume.Create(10, 1, 1, 1);
  23.   LoadTinyImageIntoNNetVolume(img, pInput);
  24.   pInput.RgbImgToNeuronalInput(csEncodeRGB);
  25.   NN.Compute(pInput);
  26.  
  27.   WriteLn('Inferred Class: ', csTinyImageLabel[pOutPut.GetClass()]);  
  28.  
I moved the image loading to TForm1.FormCreate,the image don't load when I load it TForm1.Button1Click functon !!.
thank you again for making this good library.

schuler

  • Full Member
  • ***
  • Posts: 136
Re: Conscious Artificial Intelligence - Project Update
« Reply #34 on: November 25, 2019, 09:55:28 pm »
@m.abudrais
Are you placing your code on a public repo?

Anyway, just realized that you might be able to replace both for loops by using the neuralvolumev https://github.com/joaopauloschuler/neural-api/blob/master/neural/neuralvolumev.pas unit:

Code: Pascal  [Select]
  1. /// Loads a Picture into a Volume
  2. procedure LoadPictureIntoVolume(LocalPicture: TPicture; Vol:TNNetVolume); {$IFDEF Release} inline; {$ENDIF}
  3.  
  4. /// Loads a Bitmat into a Volume
  5. procedure LoadBitmapIntoVolume(LocalBitmap: TBitmap; Vol:TNNetVolume);

The code will look like:
Code: Pascal  [Select]
  1. LoadPictureIntoVolume(Image1.Picture, pInput);
  2. pInput.RgbImgToNeuronalInput(csEncodeRGB);

In the case that the input image isn't 32x32, you can resize it (via copying) with:
Code: Pascal  [Select]
  1. TVolume.CopyResizing(Original: TVolume; NewSizeX, NewSizeY: integer);

Last idea: given that you have a trained NN, you could call this:
Code: Pascal  [Select]
  1. procedure TNeuralImageFit.ClassifyImage(pNN: TNNet; pImgInput, pOutput: TNNetVolume);
« Last Edit: November 25, 2019, 10:16:01 pm by schuler »

m.abudrais

  • New Member
  • *
  • Posts: 47
Re: Conscious Artificial Intelligence - Project Update
« Reply #35 on: November 26, 2019, 04:58:28 pm »
Quote
Are you placing your code on a public repo?
I have just uploaded the code to:
https://github.com/mabudrais/CAI-NEURAL-API-Test
your library has many useful functions, I think more examples may be added to show that.

schuler

  • Full Member
  • ***
  • Posts: 136
Re: Conscious Artificial Intelligence - Project Update
« Reply #36 on: December 06, 2019, 06:57:25 pm »
:) Hello :)

It's usually very hard to understand neuron by neuron how a neural network dedicated to image classification internally works. One technique 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 .

In this technique, an arbitrary neuron is required to activate and then the same backpropagation method used for learning is applied to an input image producing an image that this neuron expects to see. I'm happy to inform that we have a working example purely coded in FPC/Lazarus at: https://github.com/joaopauloschuler/neural-api/tree/master/examples/GradientAscent

To be able to run this example, you'll need to load an already trained neural network file and then select the layer you intend to visualise. I've attached an example from the last layer (can you see any pattern?) of a neural network trained to classify CIFAR-10 dataset. Can you find patterns for horse, deer, ship, car or truck?

:) Long life to Pascal :)
« Last Edit: December 06, 2019, 07:01:36 pm by schuler »