Recent

Author Topic: Conscious Artificial Intelligence Keras to CAI  (Read 1159 times)

Dzandaa

  • Sr. Member
  • ****
  • Posts: 250
  • From C# to Lazarus
Conscious Artificial Intelligence Keras to CAI
« on: October 08, 2022, 03:38:02 pm »
Hi everybody.

Using Lazarus 2.0.12 in Windows and Linux.

I try to translate a little keras python  test to CAI:

Code: Python  [Select][+][-]
  1. import cv2
  2. import keras
  3. from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
  4. import numpy as np
  5. import  matplotlib.pyplot as plt
  6. import tensorflow as tf
  7.  
  8. img = cv2.imread("Funny_Ben.jpg")
  9. img = cv2.resize(img, (224, 224))
  10.  
  11. model = keras.Sequential()
  12.  
  13. # Input
  14. # 224x224x3
  15.  
  16. # Block 1
  17. model.add(Conv2D(64, kernel_size=(3, 3), padding= "same", activation="relu", input_shape=(224, 224, 3)))
  18. # 224x224x64
  19. model.add(Conv2D(64, kernel_size=(3, 3), padding= "same", activation="relu"))
  20. # 224x224x64
  21. model.add(MaxPooling2D((2, 2), strides=(2,2)))
  22. # 112x112x64
  23.  
  24. model.build()
  25. model.summary()
  26.  
  27. # Result
  28.  
  29. result = model.predict(np.array([img]))
  30.  
  31. # Feature list
  32. for i in range(64):
  33.     feature_img = result[0, :, :, i]
  34.     ax: object = plt.subplot(8, 8, i+1)
  35.     ax.set_xticks([])
  36.     ax.set_yticks([])
  37.     plt.imshow(feature_img, cmap="gray")
  38. plt.show()
  39.  

what I have so far:

Code: Pascal  [Select][+][-]
  1. OpenPicture: TOpenPictureDialog;  
  2. InputVolume, PredictedVolume: TNNetVolume;
  3. ImgInput: TImage;
  4. NN: TNNet;  
  5.  
  6.  
  7. procedure TestForm.BTestClick(Sender: TObject);
  8. var
  9.    Res: Boolean;
  10.    TmpVolume: TNNetVolume;
  11. begin
  12.   InputVolume := TNNetVolume.Create();
  13.   PredictedVolume := TNNetVolume.Create(1);
  14.  
  15.   if(OpenPicture.Execute) then
  16.   begin
  17.     TmpVolume := TNNetVolume.Create();
  18.     Res := LoadImageFromFileIntoVolume(OpenPicture.FileName, TmpVolume);
  19.     if(Res) then
  20.     begin
  21.      ImgInput.Width := 224;
  22.      ImgInput.Height := 224;
  23.      InputVolume.CopyResizing(TmpVolume, 224, 224);
  24.      LoadVolumeIntoTImage(InputVolume, ImgInput);   // OK
  25.  
  26.      NN := TNNet.Create();
  27.    
  28.      NN.AddLayer(TNNetInput.Create(224, 224, 3)); // 224x224x3 Input Image
  29.    
  30.      // Block 1
  31.    
  32.      // 224x224x3
  33.    
  34.      NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}0, {Stride=}0));
  35.      // 224x224x64
  36.      NN.AddLayer(TNNetConvolutionReLU.Create({Features=}64, {FeatureSize=}3, {Padding=}1, {Stride=}1));
  37.      // 224x224x64
  38.      NN.AddLayer(TNNetMaxPool.Create(2));                                                                      
  39.      // 112x112x64
  40.      
  41.      NN.SetLearningRate(0.01,0.8);
  42.      
  43.      NN.Compute(InputVolume);
  44.      NN.GetOutput(PredictedVolume);
  45.      
  46.      // Here I want to display the Channels list from PredictedVolume in a TImage, but I don't know how...    
  47.            
  48.              
  49.     end;
  50.    
  51.     TmpVolume.Free;
  52.   end;
  53.   InputVolume.Free;
  54.   PredictedVolume.Free;
  55.   NN.Free;
  56. end;                                  
  57.  

I don't know if it is the right way to do that...

Any Help?

Thank you.



« Last Edit: October 10, 2022, 03:28:14 pm by Dzandaa »
Dzandaa

schuler

  • Full Member
  • ***
  • Posts: 229
Re: Conscious Artificial Intelligence Keras to CAI
« Reply #1 on: October 09, 2022, 04:38:22 pm »
Hello @Dzandaa,
I presume that you intend to plot each channel of the activation map (not the features - neuronal weights).

To everyone else reading this post, if you do not know what an activation map is, you can have a look at https://towardsdatascience.com/activation-maps-for-deep-learning-models-in-a-few-lines-of-code-ed9ced1e8d21 .

@Dzandaa, on your Python code, you are extracting a channel of the activation map: feature_img = result[0, :, :, i] .

As this has the "channel last" notation, i is the channel.

This is what I would do in Pascal:

To produce an image, you need 3 channels (RGB). So, I would first produce a volume with 3 channels by copying the same channel with neuralvolume TVolume.CopyChannels(Original: TVolume; aChannels: array of integer) . In your code, it would look like this:

Code: Pascal  [Select][+][-]
  1. ImageVolume.CopyChannels(PredictedVolume, [i,i,i]);

As you have a ReLU, you won't have negative values for us to care about.

Then, you'll need to scale the volume to [0, 255]:

Code: Pascal  [Select][+][-]
  1. ImageVolume.NormalizeMax(255);

Then, to transform the ImageVolume into an image, you can call either SaveImageFromVolumeIntoFile or LoadVolumeIntoImage from neuraldatasets unit.

Alternatively, if your form has a TImage, you can call LoadVolumeIntoTImage from the unit neuralvolumev instead of calling SaveImageFromVolumeIntoFile or LoadVolumeIntoImage .

If, instead of plotting channels, you really intend to plot features, you can look at https://sourceforge.net/p/cai/svncode/HEAD/tree/trunk/lazarus/experiments/visualCifar10BatchUpdate/ . In this source code example, the features of the first layer are plotted.

Does this reply solve the question?
« Last Edit: October 09, 2022, 04:56:41 pm by schuler »

Dzandaa

  • Sr. Member
  • ****
  • Posts: 250
  • From C# to Lazarus
Re: Conscious Artificial Intelligence Keras to CAI
« Reply #2 on: October 09, 2022, 07:18:09 pm »
Hi Joao Paulos,

Thank you very much.

This is just what I want, I will try it tomorrow.

Dzandaa

Dzandaa

  • Sr. Member
  • ****
  • Posts: 250
  • From C# to Lazarus
Re: Conscious Artificial Intelligence Keras to CAI
« Reply #3 on: October 10, 2022, 04:35:03 pm »
Hi,

It's working!!!

Thank you very much.

next Step OpenCL :)

B->
Dzandaa

 

TinyPortal © 2005-2018