Skip to content

Instantly share code, notes, and snippets.

@RaphaelMeudec
Created July 18, 2019 08:55
Show Gist options
  • Save RaphaelMeudec/7985b0c5eb720a29021d52b0a0be549a to your computer and use it in GitHub Desktop.
Save RaphaelMeudec/7985b0c5eb720a29021d52b0a0be549a to your computer and use it in GitHub Desktop.
Perform occlusion sensitivity with Tensorflow 2.0
import numpy as np
import tensorflow as tf
# Create function to apply a grey patch on an image
def apply_grey_patch(image, top_left_x, top_left_y, patch_size):
patched_image = np.array(image, copy=True)
patched_image[top_left_y:top_left_y + patch_size, top_left_x:top_left_x + patch_size, :] = 127.5
return patched_image
# Load image
IMAGE_PATH = './cat.jpg'
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(224, 224))
img = tf.keras.preprocessing.image.img_to_array(img)
# Instantiate model
model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', include_top=True)
CAT_CLASS_INDEX = 281 # Imagenet tabby cat class index
PATCH_SIZE = 40
sensitivity_map = np.zeros((img.shape[0], img.shape[1]))
# Iterate the patch over the image
for top_left_x in range(0, img.shape[0], PATCH_SIZE):
for top_left_y in range(0, img.shape[1], PATCH_SIZE):
patched_image = apply_grey_patch(img, top_left_x, top_left_y, PATCH_SIZE)
predicted_classes = model.predict(np.array([patched_image]))[0]
confidence = predicted_classes[CAT_CLASS_INDEX]
# Save confidence for this specific patched image in map
sensitivity_map[
top_left_y:top_left_y + PATCH_SIZE,
top_left_x:top_left_x + PATCH_SIZE,
] = confidence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment