0

I am trying to make a neural network from a flow diagram. It is necessary for my analysis to translate this network into a code. Could you help me if I'm doing anything wrong. Here is the diagram. The author used binary classification but I'm doing multiple so ignore that one. I'm a kind a new to building CNN and this is all I could come up with different sources from the internet.

Neural Network for wound image classification

import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, Concatenate,Dense,Flatten
from tensorflow.keras.models import Sequential
from keras.layers import BatchNormalization

model_1=Sequential()
#First Stacked
model_1.add(Conv2D(filters=64,kernel_size=7,stride=(2,2),activation='relu',input_shape=(128,128,1)))
model_1.add(BatchNormalization())
model_1.add(LeakyReLU(alpha=0.1))
layer_1=Conv2D(filters=32,kernel_size=3,stride=(1,1),activation='relu')(model_1)
layer_2=Conv2D(filters=64,kernel_size=5,stride=(1,1),activation='relu')(model_1)
layer_3=Conv2D(filters=128,kernel_size=5,stride=(1,1),activation='relu')(model_1)
concatenate_1 = keras.layers.concatenate([layer_1, layer_2,layer_3], axis=1)
#Second Stacked
concatenate_1.add(Conv2D(filters=64,kernel_size=1,stride=(1,1),activation='relu')
concatenate_1.add(BatchNormalization())
concatenate_1.add(LeakyReLU(alpha=0.1))
concatenate_1.add(MaxPooling2D((2, 2), strides=(2, 2), padding='same'))
layer_1=Conv2D(filters=32,kernel_size=1,stride=(1,1),activation='relu')(concatenate_1)
layer_2=Conv2D(filters=64,kernel_size=3,stride=(1,1),activation='relu')(concatenate_1)
layer_3=Conv2D(filters=128,kernel_size=5,stride=(1,1),activation='relu')(concatenate_1)
concatenate_2 = keras.layers.concatenate([layer_1, layer_2,layer_3], axis=1)
#Third Stacked
concatenate_2.add(Conv2D(filters=64,kernel_size=1,stride=(1,1),activation='relu')
concatenate_2.add(BatchNormalization())
concatenate_2.add(LeakyReLU(alpha=0.1))
concatenate_2.add(MaxPooling2D((2, 2), strides=(2, 2), padding='same'))
layer_1=Conv2D(filters=32,kernel_size=1,stride=(1,1),activation='relu')(concatenate_2)
layer_2=Conv2D(filters=64,kernel_size=3,stride=(1,1),activation='relu')(concatenate_2)
layer_3=Conv2D(filters=128,kernel_size=5,stride=(1,1),activation='relu')(concatenate_2)
concatenate_3 = keras.layers.concatenate([layer_1, layer_2,layer_3], axis=1)
#Final
concatenate_3.add(Conv2D(filters=64,kernel_size=1,stride=(1,1),activation='relu')
concatenate_3.add(BatchNormalization())
concatenate_3.add(LeakyReLU(alpha=0.1))
concatenate_3.add(MaxPooling2D((2, 2), strides=(2, 2), padding='same'))
concatenate_3=Flatten()(concatenate_3)
model_dfu_spnet=Dense(200, activation='relu')(concatenate_3)
mode_dfu_spnet.add(Dropout(0.3,activation='softmax'))

1 Answers1

0

Concatenate() is done by doing Concatenate(**args)([layers])

keras.layers.concatenate([layer_1, layer_2,layer_3], axis=1)

should be (note the capitalization)

keras.layers.Concatenate(axis=1)([layer_1, layer_2,layer_3])
# axis=1 is default, so you can just do
# keras.layers.Concatenate()([layer_1, layer_2,layer_3])

Then do the same for the other Concatenate().

I'm not sure what you want to do with this:

model_dfu_spnet=Dense(200, activation='relu')(concatenate_3)

But following the picture, that layer should have 32 neurons (seems kinda small for that but idk...)

model_dfu_spnet=Dense(32, activation='relu')(concatenate_3)

You don't put the activation function on Droupout

mode_dfu_spnet.add(Dropout(0.3,activation='softmax'))

but you probably want it on another Dense layer, also with the number of classes as the neurons.

mode_dfu_spnet.add(Dropout(0.3))
mode_dfu_spnet.add(Dense(num_of_classes, activation="softmax", name="visualized_layer"))

I'm not used to doing Sequential models with Concatenate, usually Functional but it shouldn't be any different.

Djinn
  • 663
  • 5
  • 12
  • First of all thank you so much for making those relevant corrections. Could you please point out me sources where I can learn this through functional way. I will be extremely thankful. Thanks. Again thanks for checking the entire thing. – Azhan Ahmed Jul 07 '22 at 05:10
  • https://www.tensorflow.org/guide/keras/functional – Djinn Jul 07 '22 at 06:24
  • Thank you, while running it is throwing an error: File "", line 17 concatenate_1.add(BatchNormalization()) ^ SyntaxError: invalid syntax – Azhan Ahmed Jul 08 '22 at 16:28
  • Can you update your question with the problem code? SyntaxError can be something as simple as missing parentheses. – Djinn Jul 08 '22 at 20:16
  • And also the full error with trace? – Djinn Jul 08 '22 at 20:24
  • Here is the updated one: https://stackoverflow.com/questions/72916747/making-a-convolutional-neural-network-from-a-flow-diagram-update-1 – Azhan Ahmed Jul 08 '22 at 20:38