Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def plot_conv_heat_map(model_prediction, input_layer, conv_layer, image_data, image_path):
- # Get the gradient of the winner class with regard to the output of the (last) conv. layer
- grads = K.gradients(model_prediction, conv_layer.output)[0]
- pooled_grads = K.mean(grads, axis=(0, 1, 2))
- # Get values of pooled grads and model conv. layer output as Numpy arrays
- iterate = K.function([input_layer], [pooled_grads, conv_layer.output[0]])
- pooled_grads_value, conv_layer_output_value = iterate([image_data])
- # Multiply each channel in the feature-map array by "how important this channel is"
- for i in range(pooled_grads_value.shape[0]):
- conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
- # The channel-wise mean of the resulting feature map is the heatmap of the class activation
- heatmap = np.mean(conv_layer_output_value, axis=-1)
- heatmap = np.maximum(heatmap, 0)
- max_heat = np.max(heatmap)
- if max_heat == 0:
- print("Heatmap generation failed, can't divide by zero!")
- return
- heatmap /= max_heat
- # Show heatmap in original (conv. layer) size
- #plt.matshow(heatmap)
- #plt.show()
- #return
- # Load image via CV2
- img = cv2.imread(image_path)
- # Resize heatmap to original image size, normalize it, convert to RGB, apply color map
- heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
- heatmap = cv2.normalize(heatmap, heatmap, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
- heatmap = cv2.applyColorMap(np.uint8(255 * (255 - heatmap)), cv2.COLORMAP_JET)
- # Crate final image (combo of original pic + heatmap)
- superimposed = heatmap * 0.5 + img * 0.5
- cv2.imshow('Heatmap', _deprocess_tensor_to_image(superimposed))
- cv2.waitKey(0)
- def _deprocess_tensor_to_image(tensor):
- # Normalize the tensor: centers on 0, ensures that std is 0.1
- tensor -= tensor.mean()
- tensor /= (tensor.std()) + 1e-5
- tensor *= 0.1
- # Clip to [0,1]
- tensor += 0.5
- tensor = np.clip(tensor, 0, 1)
- # Converts to an RGB array
- tensor *= 255
- tensor = np.clip(tensor, 0, 255).astype("uint8")
- return tensor
Advertisement
Add Comment
Please, Sign In to add comment