Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. # CAM
  2. import cv2
  3. from PIL import Image
  4. import tensorflow as tf
  5. from tensorflow.python.keras import backend as K
  6.  
  7. def grad_cam_keras(model, im, class_select, layer, image_size, preproc_fn, alpha=0.6, filter_threshold=0.5):
  8. """GradCAM method for visualizing input saliency.
  9. Args:
  10. model: keras model
  11. im: single image (with only RGB, [H,W,C])
  12. class_select: class to show
  13. layer: layer name
  14. image_size: tuple of image H,W
  15. preproc_fn: preprocessing function
  16. alpha: alpha
  17. Returns:
  18. gradient-class-activation-map
  19. """
  20. H, W = image_size[0], image_size[1]
  21. image = im.copy()
  22. if len(image) != 4:
  23. image = image[np.newaxis, :,:,:]
  24. image_original = image[0].astype("uint8")
  25. image = preproc_fn(image.astype("float32"))
  26.  
  27. if class_select is None:
  28. output = model.predict(image)
  29. y_c = model.output[0, output.argmax()]
  30. print("Class-selected by prediction: {}".format(output.argmax()))
  31. else:
  32. y_c = model.output[0, class_select]
  33.  
  34. conv_output = model.get_layer(layer).output
  35. grads = K.gradients(y_c, conv_output)[0]
  36.  
  37. gradient_function = K.function([model.input],
  38. [conv_output, grads])
  39. with tf.device("/gpu:0"):
  40. output, grads_val = gradient_function([image])
  41. output, grads_val = output[0, :], grads_val[0, :, :, :]
  42.  
  43. weights = np.mean(grads_val, axis=(0, 1))
  44. cam = np.dot(output, weights)
  45.  
  46. # Process CAM
  47. cam = cv2.resize(cam, (H, W), cv2.INTER_LINEAR)
  48. cam = np.maximum(cam, 0)
  49. cam = cam / cam.max()
  50.  
  51. # Filter
  52. cam[cam<filter_threshold] = 0
  53.  
  54. # apply colormap
  55. mapping = cv2.applyColorMap(np.uint8(255 * (1-cam)), cv2.COLORMAP_JET)
  56. mapping = np.concatenate((mapping, ((mapping.max(axis=-1) - 128 )*255*alpha)[:,:,np.newaxis]), axis = -1)
  57.  
  58. background = Image.fromarray(image_original)
  59. foreground = Image.fromarray(mapping.astype('uint8'))
  60. background.paste(foreground, (0, 0), foreground)
  61.  
  62. return cam, background
  63.  
  64.  
  65.  
  66. ## Usage
  67. cam_map, raw_img = grad_cam_keras(model=model,
  68. im=x_array[idx], ## raw image array (H,W,3)
  69. class_select=None,
  70. layer="conv5_block3_out", #"post_relu",
  71. image_size=tuple(model.input.shape.as_list()[1:3]), # (H,W)
  72. preproc_fn=preproc_fn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement