Created
July 19, 2019 18:09
-
-
Save randmized/6c506086fb8e92518529c188ed22c558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#prepare gradcam | |
image = cv2.imread(args.image, 1) #1 color, 0 grayscale | |
""" | |
warning:: ensure slicing of imagepath is set to the right dimension, otherwise cam generation will fail. Probably with "NoneType" warning. | |
param: img_args_offset: set offset to image args | |
""" | |
#check image paths and enter offset | |
img_args_offset = 54 | |
print(args.image[img_args_offset:-4]) | |
#image network preprocessing | |
image = np.float32(cv2.resize(image, (HEIGHT, WIDTH))) | |
original = image | |
combined = image | |
cv2.imwrite('./evaluation/cam_output/{}-original.png'.format(args.image[img_args_offset:-4]), image) #save original for later comparison | |
image = preprocessing_function(image.reshape(1, HEIGHT, WIDTH, 3)) | |
#prepare layers for gradcam | |
layer_idx = visutils.find_layer_idx(finetune_model, 'dense_2') #last dense layer 310, other forum post says 312 (dense_2 = 313, dense_1 = 312(?)) | |
print("layer_idx: {}".format(layer_idx)) | |
# use explainer module | |
explainer = GradCam(finetune_model, layer=layer_idx) | |
exp_heatmap = explainer.explain(image, None) #target class 744, returns array | |
# We resize the heatmap to have the same size as the original image | |
exp_heatmap = cv2.resize(exp_heatmap, (c_original.shape[1], c_original.shape[0])) | |
# We convert the heatmap to RGB | |
#exp_heatmap = np.uint8(255 * exp_heatmap) | |
# We apply the heatmap to the original image | |
#exp_heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) | |
# 0.4 here is a heatmap intensity factor | |
#superimposed_img = exp_heatmap * 0.4 + c_original | |
# Save the image to disk | |
cv2.imwrite('./evaluation/cam_output/{}-blended_exp.png'.format(args.image[img_args_offset:-4]), exp_heatmap) | |
print("cam visualized") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment