maflobra

OpenCV Heatmap Function

Jun 18th, 2019
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. def plot_conv_heat_map(model_prediction, input_layer, conv_layer, image_data, image_path):
  2.     # Get the gradient of the winner class with regard to the output of the (last) conv. layer
  3.     grads = K.gradients(model_prediction, conv_layer.output)[0]
  4.     pooled_grads = K.mean(grads, axis=(0, 1, 2))
  5.  
  6.     # Get values of pooled grads and model conv. layer output as Numpy arrays
  7.     iterate = K.function([input_layer], [pooled_grads, conv_layer.output[0]])
  8.     pooled_grads_value, conv_layer_output_value = iterate([image_data])
  9.  
  10.     # Multiply each channel in the feature-map array by "how important this channel is"
  11.     for i in range(pooled_grads_value.shape[0]):
  12.         conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
  13.  
  14.     # The channel-wise mean of the resulting feature map is the heatmap of the class activation
  15.     heatmap = np.mean(conv_layer_output_value, axis=-1)
  16.     heatmap = np.maximum(heatmap, 0)
  17.     max_heat = np.max(heatmap)
  18.     if max_heat == 0:
  19.         print("Heatmap generation failed, can't divide by zero!")
  20.         return
  21.     heatmap /= max_heat
  22.  
  23.     # Show heatmap in original (conv. layer) size
  24.     #plt.matshow(heatmap)
  25.     #plt.show()
  26.     #return
  27.  
  28.     # Load image via CV2
  29.     img = cv2.imread(image_path)
  30.  
  31.     # Resize heatmap to original image size, normalize it, convert to RGB, apply color map
  32.     heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
  33.     heatmap = cv2.normalize(heatmap, heatmap, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
  34.     heatmap = cv2.applyColorMap(np.uint8(255 * (255 - heatmap)), cv2.COLORMAP_JET)
  35.    
  36.     # Crate final image (combo of original pic + heatmap)
  37.     superimposed = heatmap * 0.5 + img * 0.5
  38.     cv2.imshow('Heatmap', _deprocess_tensor_to_image(superimposed))
  39.     cv2.waitKey(0)
  40.  
  41. def _deprocess_tensor_to_image(tensor):
  42.     # Normalize the tensor: centers on 0, ensures that std is 0.1
  43.     tensor -= tensor.mean()
  44.     tensor /= (tensor.std()) + 1e-5
  45.     tensor *= 0.1
  46.  
  47.     # Clip to [0,1]
  48.     tensor += 0.5
  49.     tensor = np.clip(tensor, 0, 1)
  50.  
  51.     # Converts to an RGB array
  52.     tensor *= 255
  53.     tensor = np.clip(tensor, 0, 255).astype("uint8")
  54.  
  55.     return tensor
Advertisement
Add Comment
Please, Sign In to add comment