'Class activation heatmap InceptionV3 transfer learning
I have used transfer learning (imagenet weights) and trained InceptionV3 to recognize two classes of images. The code looks like
InceptionV3_model = InceptionV3(input_shape=(150,150,3),weights='imagenet', include_top=False)
for layer in InceptionV3_model.layers[:249]:
layer.trainable = False
for layer in InceptionV3_model.layers[249:]:
layer.trainable = True
InceptionV3_last_output = InceptionV3_model.output
InceptionV3_maxpooled_output = Flatten()(InceptionV3_last_output)
InceptionV3_x = Dense(1024, activation='relu')(InceptionV3_maxpooled_output)
InceptionV3_x = Dropout(0.5)(InceptionV3_x)
InceptionV3_x = Dense(2, activation='softmax')(InceptionV3_x)
InceptionV3_x_final_model = Model(inputs=InceptionV3_model.input,outputs=InceptionV3_x)
InceptionV3_x_final_model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy',metrics=['accuracy'])
number_of_epochs = inception_epoch
inception_filepath = 'inceptionv3_'+'-saved-model-{epoch:02d}-loss-{loss:.2f}.hdf5'
inception_checkpoint = tf.keras.callbacks.ModelCheckpoint(inception_filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
inception_early_stopping = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=10)
inceptionv3_history = InceptionV3_x_final_model.fit(train_generator, epochs = number_of_epochs, validation_data = validation_generator,callbacks=[inception_checkpoint,inception_early_stopping],verbose=1)
do_history_stuff(inceptionv3_history, 'inceptionv3_model', True)
then i get the predictions using
def mode(my_list):
ct = Counter(my_list)
max_value = max(ct.values())
return ([key for key, value in ct.items() if value == max_value])
true_value = []
inception_pred = []
for folder in os.listdir(seg_test_folders):
test_image_ids = os.listdir(os.path.join(seg_test_folders,folder))
for image_id in test_image_ids[:int(len(test_image_ids))]:
path = os.path.join(seg_test_folders,folder,image_id)
true_value.append(validation_generator.class_indices[folder])
img = cv2.resize(cv2.imread(path),(150,150))
#img = cv2.imread(path)
img_normalized = img/255
#Inception
inception_image_prediction = np.argmax(inception_best_model.predict(np.array([img_normalized])))
inception_pred.append(inception_image_prediction)
I am trying to use gradcam to visualize a heatmap to see where the network focuses but it doesnt work. I am trying to use Chollet's guide but I am a newbee and I dont know how to match it to my code. Can you please help to customize the gradcam code? I cannot find the penultimate layer of my model and I cannnot generate the heatmap that matches one of my own images as predicted by my model. The code I am trying to use is https://github.com/Abhijit-2592/Keras-custom-callbacks/blob/master/how%20to%20use%20grad-cam%20in%20inceptionv3_copy.ipynb. THis code uses the generic Inception_v3 and not my finetuned version. Can you please help match this code with mine?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
