def vis_grad(model, class_index, layer, image_path, size=[224, 224]): original_image = cv2.imread(image_path, 1) #plt.imshow(original_image) #plt.show() prep_img = preprocess_image(original_image, size) file_name_to_export = 'model' + '_classindex_' + str( class_index) + '-layer_' + str(layer) # Grad cam gcv2 = GradCam(model, target_layer=layer) # Generate cam mask cam = gcv2.generate_cam(prep_img, class_index, size) print('Grad cam completed') # Guided backprop GBP = GuidedBackprop(model) # Get gradients guided_grads = GBP.generate_gradients(prep_img, class_index) print('Guided backpropagation completed') # Guided Grad cam cam_gb = guided_grad_cam(cam, guided_grads) #save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam') grayscale_cam_gb = convert_to_grayscale(cam_gb) #save_gradient_images(grayscale_cam_gb, file_name_to_export + '_GGrad_Cam_gray') print('Guided grad cam completed') cam_gb = trans(cam_gb) grayscale_cam_gb = trans(grayscale_cam_gb) return cam_gb, grayscale_cam_gb
def vis_gradcam(model, class_index, layer, image_path, size=[224, 224]): original_image = cv2.imread(image_path, 1) #plt.imshow(original_image) #plt.show() prep_img = preprocess_image(original_image, size) file_name_to_export = 'model' + '_classindex_' + str( class_index) + '-layer_' + str(layer) # Grad cam gcv2 = GradCam(model, target_layer=layer) # Generate cam mask cam = gcv2.generate_cam(prep_img, class_index, size) print('Grad cam completed') #save_class_activation_on_image(original_image, cam, file_name_to_export) img_with_heatmap, activation_heatmap = act_on_img(original_image, cam, size) return cam, activation_heatmap, img_with_heatmap
""" cam_gb = np.multiply(grad_cam_mask, guided_backprop_mask) return cam_gb if __name__ == '__main__': # Get params target_example = 0 # Snake (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\ get_example_params(target_example) # Grad cam # gcv2 = GradCam(pretrained_model, target_layer=11) gcv2 = GradCam(pretrained_model, target_layer=7) # Generate cam mask cam = gcv2.generate_cam(prep_img, target_class) print('Grad cam completed') # Guided backprop GBP = GuidedBackprop(pretrained_model) # Get gradients guided_grads = GBP.generate_gradients(prep_img, target_class) print('Guided backpropagation completed') # Guided Grad cam cam_gb = guided_grad_cam(cam, guided_grads) save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam') grayscale_cam_gb = convert_to_grayscale(cam_gb) save_gradient_images(grayscale_cam_gb, file_name_to_export + '_GGrad_Cam_gray') print('Guided grad cam completed')
# print(pretrained_model) probs, label = predict2(image_path, pretrained_model.to(device)) print(os.path.basename(image_path)) print(probs) # print(*label, sep=", ") print([idx_to_class[idx] for idx in label], sep=", ") print("-" * 60 + "\n") # gradcam original_image = PIL.Image.open(image_path).convert('RGB') prep_img = process_image(original_image).unsqueeze(0) crop_image = only_crop_image(original_image) target_class = label[0] file_name_to_export = "butterfly_gradcam" # Grad cam grad_cam = GradCam(pretrained_model, target_layer=target_layer) # Generate cam mask cam = grad_cam.generate_cam(prep_img, target_class) # Save mask save_class_activation_images( crop_image, cam, os.path.join('heatmaps', os.path.splitext(os.path.basename(model_dir))[0]), "{}_{}_{:.2f}".format( os.path.splitext(os.path.basename(image_path))[0], idx_to_class[label[0]], probs[0]))
guided_backprop_mask (np_arr):Guided backprop mask """ cam_gb = np.multiply(grad_cam_mask, guided_backprop_mask) return cam_gb if __name__ == '__main__': # Get params target_example = 0 # Snake (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\ get_params(target_example) # Grad cam gcv2 = GradCam(pretrained_model, target_layer=11) # Generate cam mask cam = gcv2.generate_cam(prep_img, target_class) print('Grad cam completed') # Guided backprop GBP = GuidedBackprop(pretrained_model) # Get gradients guided_grads = GBP.generate_gradients(prep_img, target_class) print('Guided backpropagation completed') # Guided Grad cam cam_gb = guided_grad_cam(cam, guided_grads) save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam') grayscale_cam_gb = convert_to_grayscale(cam_gb) save_gradient_images(grayscale_cam_gb, file_name_to_export + '_GGrad_Cam_gray') print('Guided grad cam completed')