I'm trying to visualize important regions for a classification task with CNN.
I'm using VGG16 + my own top layers (A global average pooling layer and a Dense layer)
model_vgg16_conv = VGG16(weights='imagenet', include_top=False, input_shape=(100, 100, 3))
model = models.Sequential()
model.add(model_vgg16_conv)
model.add(Lambda(global_average_pooling, output_shape=global_average_pooling_shape))
model.add(Dense(4, activation = 'softmax', init='uniform'))
After compiling and fitting the model I'm trying to use Grad-CAM for a new image:
image = cv2.imread("data/example_images/test.jpg")
# Resize to 100x100
image = resize(image,(100,100),anti_aliasing=True, mode='constant')
# Because it's a grey scale image extend the dimensions
image = np.repeat(image.reshape(1,100, 100, 1), 3, axis=3)
class_weights = model.get_layer("dense_1").get_weights()[0]
final_conv_layer = model.get_layer("vgg16").get_layer("block5_conv3")
input1 = model.get_layer("vgg16").layers[0].input
output1 = model.get_layer("dense_1").output
get_output = K.function([input1], [final_conv_layer.output, output1])
After that I'm executing
[conv_outputs, predictions] = get_output([image])
Leading to the following error:
InvalidArgumentError: You must feed a value for placeholder tensor 'vgg16_input' with dtype float and shape [?,100,100,3] [[{{node vgg16_input}}]] [[dense_1/Softmax/_233]]
Additional information
def global_average_pooling(x):
return K.mean(x, axis = (2, 3))
def global_average_pooling_shape(input_shape):
return input_shape[0:2]
Model summary:
Layer (type) Output Shape Param #
=================================================================
vgg16 (Model) (None, 3, 3, 512) 14714688
_________________________________________________________________
lambda_1 (Lambda) (None, 3) 0
_________________________________________________________________
dense_1 (Dense) (None, 4) 16
=================================================================
Total params: 14,714,704
Trainable params: 16
Non-trainable params: 14,714,688
VGG-Model Summary:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 100, 100, 3) 0
...
I'm new to Grad-CAM and I'm not sure if I'm just overseeing something or if I misunderstood the entire concept.