Skip to content

Instantly share code, notes, and snippets.

@nfsrules
Last active April 19, 2018 12:05
Show Gist options
  • Save nfsrules/29f9df48a2d9b0ecbc385a0c162487b2 to your computer and use it in GitHub Desktop.
Save nfsrules/29f9df48a2d9b0ecbc385a0c162487b2 to your computer and use it in GitHub Desktop.
Script to preprocess input image and get a feature map
def pre_process(img):
""" Prepare image to be feed to a Keras convolutive model.
img: 3-channel Numpy array image of size.
Returns a resized and smoothed 250x70 RGB image.
"""
# Crop relevant area from input image
img = crop(img,600,1080,0,1800)
# Resize to model's input size (250,70)
img = cv2.resize(img,(250,70), interpolation = cv2.INTER_LANCZOS4)#INTER_CUBIC)
# Smooth image with mean filter
kernel = np.ones((2,2),np.float32)/4
img = cv2.filter2D(img,-1,kernel)
return img
# Save input image ROI
cropped_img = crop(imgs[0],600,1080,0,1800)
# Transform original image to model's input size (250, 70, 3)
dst = pre_process(imgs[0])
# Get desired feature map (2nd convolutive layer)
layer_name = model.layers[6].name
# Input image to Keras model. Don't forget the mini-batch dimension
activations = get_activations(model,np.expand_dims(dst, axis=0),
print_shape_only=True,
layer_name=layer_name)
# Normalize heatmap to pixels from (0 - 255)
target_map = norm_pixels(activations[0][0][:,:,1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment