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=35) # Generate cam mask cam = gcv2.generate_cam(prep_img, target_class) print('Grad cam completed') # Guided backprop GBP = GuidedBackprop(pretrained_model, prep_img, target_class) # Get gradients guided_grads = GBP.generate_gradients() 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')
model_output.backward(gradient=one_hot_output.to(device), retain_graph=True) # Convert Pytorch variable to numpy array # [0] to get rid of the first channel (1,3,224,224) gradients_as_arr = self.gradients.data.cpu().numpy()[0] # print(gradients_as_arr.shape) return gradients_as_arr if __name__ == '__main__': # Get params target_example = 1 # Snake (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\ get_example_params(target_example) # Vanilla backprop VBP = VanillaBackprop(pretrained_model) # Generate gradients vanilla_grads = VBP.generate_gradients(prep_img, target_class) # Save colored gradients save_gradient_images(vanilla_grads, file_name_to_export + '_Vanilla_BP_color') # Convert to grayscale grayscale_vanilla_grads = convert_to_grayscale(vanilla_grads) # Save grayscale gradients save_gradient_images(grayscale_vanilla_grads, file_name_to_export + '_Vanilla_BP_gray') print('Vanilla backprop completed')
model_output.backward(gradient=one_hot_output) # Convert Pytorch variable to numpy array # [0] to get rid of the first channel (1,3,224,224) gradients_as_arr = self.gradients.data.numpy()[0] return gradients_as_arr if __name__ == '__main__': target_example = 0 # Snake (original_image, prep_img, target_class, file_name_to_export, pretrained_model) = \ get_params(target_example) # Guided backprop GBP = GuidedBackprop(pretrained_model, prep_img, target_class) # Get gradients guided_grads = GBP.generate_gradients() # Save colored gradients save_gradient_images(guided_grads, file_name_to_export + '_Guided_BP_color') # Convert to grayscale grayscale_guided_grads = convert_to_grayscale(guided_grads) # Save grayscale gradients save_gradient_images(grayscale_guided_grads, file_name_to_export + '_Guided_BP_gray') # Positive and negative saliency maps pos_sal, neg_sal = get_positive_negative_saliency(guided_grads) save_gradient_images(pos_sal, file_name_to_export + '_pos_sal') save_gradient_images(neg_sal, file_name_to_export + '_neg_sal') print('Guided backprop completed')
def main(): act = ['gilpin', 'bracco', 'harmon', 'baldwin', 'hader', 'carell'] print("Please select part to display output for:\n" + "\tFirst Layer \n\tWhole Network") question = 'whole network' # raw_input() if question.lower() == "first layer": man = MyAlexNet() layer1 = man.features[0] # Show the weights of the first layer of the neural network plt.figure(figsize=(5, 5)) for i in range(64): ax = plt.subplot(8, 8, i + 1) ax.set_xticklabels([]) ax.set_yticklabels([]) plt.axis('off') img = (np.rollaxis(layer1.weight.data[i].numpy(), 0, 3) + 1) / 2 plt.imshow(img) plt.tight_layout() plt.subplots_adjust(wspace=0.02, hspace=0.02) plt.savefig('layer_1_weights.png') plt.show() alexnet = torch.nn.Sequential(man.features[0]) images = [] for a in act: images.append(load_data(a)) images = Variable( torch.from_numpy(np.vstack(images)).type(dtype_float)) activations = np.rollaxis(alexnet(images).data.numpy(), 1, 0) # Visualize regions of most activations plot_most_activated(activations, images.data.numpy(), 11, 4, (8, 8), image_pad=2, save_as='layer1_activate.png') # Visualize around regions of most activations plot_most_activated(activations, images.data.numpy(), 11, 4, (8, 8), image_pad=2, view_pad=10, save_as='layer1_activate_border.png') elif question.lower() == 'whole network': print('Loading Pretrained Model') model = FaceModel() print("\tLoaded Pretrained Model") try: mosts = [] for a in act: mosts.append(process_image("most_{}.png".format(a))) except IOError: mosts = prototypical_images(model, act) print("\tLoaded Images") m = model for i, img in enumerate(mosts): print("Working on {} of 6".format(i + 1)) # Vanilla Backpropagation model = deepcopy(m) VBP = VanillaBackprop(model, img.unsqueeze(0), i) vanilla_grads = VBP.generate_gradients() save_gradient_images(vanilla_grads, act[i] + '_Vanilla_BP_color') grayscale_vanilla_grads = convert_to_grayscale(vanilla_grads) save_gradient_images(grayscale_vanilla_grads, act[i] + '_Vanilla_BP_gray') # Guided backpropagation model = deepcopy(m) GBP = GuidedBackprop(model, img.unsqueeze(0), i) guided_grads = GBP.generate_gradients() save_gradient_images(guided_grads, act[i] + '_Guided_BP_colour') grayscale_guided_grads = convert_to_grayscale(guided_grads) save_gradient_images(grayscale_guided_grads, act[i] + '_Guided_BP_gray') pos_sal, neg_sal = get_positive_negative_saliency(guided_grads) save_gradient_images(pos_sal, act[i] + '_pos_sal') save_gradient_images(neg_sal, act[i] + '_neg_sal') # GradCAM and Guided GradCAM orig_im = cv2.imread('most_{}.png'.format(act[i])) for j in range(10): model = deepcopy(m) grad_cam = GradCam(model, target_layer=j) cam = grad_cam.generate_cam(img.unsqueeze(0), 0) save_class_activation_on_image(orig_im, cam, act[i] + '_layer' + str(j)) cam_gb = guided_grad_cam(cam, guided_grads) save_gradient_images(cam_gb, act[i] + '_layer' + str(j) + '_GGrad_Cam') grayscale_cam_gb = convert_to_grayscale(cam_gb) save_gradient_images( grayscale_cam_gb, act[i] + '_layer' + str(j) + '_GGrad_Cam_gray') # Layer Visualization for j in range(10): model = deepcopy(m) inverted_representation = InvertedRepresentation(model) try: inverted_representation.generate_inverted_image_specific_layer( img.unsqueeze(0), 277, act[i], j) except: continue # Class Specific Samples model = deepcopy(m) csig = ClassSpecificImageGeneration(model, i) csig.generate(act[i])
""" Created on Wed Jun 19 17:12:04 2019 @author: Utku Ozbulak - github.com/utkuozbulak """ from pytorch_cnn_visualizations.src.misc_functions import (get_example_params, convert_to_grayscale, save_gradient_images) from pytorch_cnn_visualizations.src.vanilla_backprop import VanillaBackprop # from guided_backprop import GuidedBackprop # To use with guided backprop # from integrated_gradients import IntegratedGradients # To use with integrated grads 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) # Vanilla backprop VBP = VanillaBackprop(pretrained_model) # Generate gradients vanilla_grads = VBP.generate_gradients(prep_img, target_class) grad_times_image = vanilla_grads[0] * prep_img.detach().numpy()[0] # Convert to grayscale grayscale_vanilla_grads = convert_to_grayscale(grad_times_image) # Save grayscale gradients save_gradient_images(grayscale_vanilla_grads, file_name_to_export + '_Vanilla_grad_times_image_gray') print('Grad times image completed.')
# Generate xbar images xbar_list = self.generate_images_on_linear_path(input_image, steps) # Initialize an iamge composed of zeros integrated_grads = np.zeros(input_image.size()) for xbar_image in xbar_list: # Generate gradients from xbar images single_integrated_grad = self.generate_gradients( xbar_image.to(device), target_class, device) # Add rescaled grads from xbar images integrated_grads = integrated_grads + single_integrated_grad / steps # [0] to get rid of the first channel (1,3,224,224) return integrated_grads[0] 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) # Vanilla backprop IG = IntegratedGradients(pretrained_model) # Generate gradients integrated_grads = IG.generate_integrated_gradients( prep_img, target_class, 100) # Convert to grayscale grayscale_integrated_grads = convert_to_grayscale(integrated_grads) # Save grayscale gradients save_gradient_images(grayscale_integrated_grads, file_name_to_export + '_Integrated_G_gray') print('Integrated gradients completed.')