Image classification Using CNN

Question:

I am working on breast cancer classification. I found this online code to train my pre-processed outputs on it. The results was awful but I didn’t understand the code, I want to train my own model but I don’t how to replace my own code with this one.

Any help would be appreciated.

in_model = tf.keras.applications.DenseNet121(input_shape=(224,224,3),
                                               include_top=False,
                                               weights='imagenet',classes = 2)
in_model.trainable = False
inputs = tf.keras.Input(shape=(224,224,3))
x = in_model(inputs)
flat = Flatten()(x)
dense_1 = Dense(4096,activation = 'relu')(flat)
dense_2 = Dense(4096,activation = 'relu')(dense_1)
prediction = Dense(2,activation = 'softmax')(dense_2)
in_pred = Model(inputs = inputs,outputs = prediction)

Asked By: Eda

||

Answers:

You forgot to preprocess_input:

Note: each Keras Application expects a specific kind of input preprocessing. For DenseNet, call tf.keras.applications.densenet.preprocess_input on your inputs before passing them to the model.

inputs = tf.keras.Input(shape=(224,224,3))
x = tf.keras.applications.densenet.preprocess_input(inputs)  # HERE
x = in_model(x)

You can also try to use the default top-net by setting include_top=True or create the same top net:

x = layers.GlobalAveragePooling2D(name="avg_pool")(x)
x = layers.Dense(2, activation='softmax', name='predictions')(x)
Answered By: Corralien

#This is a Deep Learning model using Keras.
#the CNN model:

 in_model = tf.keras.applications.DenseNet121(input_shape=(224,224,3),
                                               include_top=False,
                                               weights='imagenet',classes = 2)

#First, to all, you need to creates a CNN DenseNet121 model with pre-trained #ImageNet weights. input_shape specifies the shape of the input images to the model. #include_top=False specifies that we don’t want to include the last fully-connected #layer in the model. This is because we want to replace the last layer with our own #layers for our specific task. weights=’imagenet’ specifies that we want to use pre-#trained weights from the ImageNet dataset. Finally, classes = 2 specifies the #number of output classes for our specific task.

in_model.trainable = False

#The model freezes the weights of the pre-trained model, so they will not be updated #during training. This is because we only want to train the new layers that we add #to the model.

inputs = tf.keras.Input(shape=(224,224,3)) 

the input layer of the model is defined as shape=(224,224,3) specifies the shape of #the input images.

x = in_model(inputs)  

#Now is applied the pre-trained model to the input images to extract features.

flat = Flatten()(x)  

flattens the output of the pre-trained model into a 1-dimensional array, #so it can be used as input to the fully-connected layers that we will add next.

#Now, in the next two lines add two fully-connected layers with 4096 units each and #ReLU activation functions. These layers are added to learn more complex features #from the flattened output of the pre-trained model.

  dense_1 = Dense(4096,activation = 'relu')(flat)
  dense_2 = Dense(4096,activation = 'relu')(dense_1)

#The next step involves to adds the output layer of the model. It’s a fully-#connected layer with 2 units (one for each output class) and a softmax activation #function. This layer will output the predicted class probabilities for each input #image.

prediction = Dense(2,activation = 'softmax')(dense_2)

#Finally, you create the final model by defining the input and output layers. #inputs and prediction are the input and output layers that we defined earlier. The #resulting in_pred model is a Keras Model object that can be trained on data for a #specific classification task.

 in_pred = Model(inputs = inputs,outputs = prediction)