def visualize2(pretrained_model, prep_img, target_class, file_name_to_export="test"): # 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')
def visualize(pretrained_model, prep_img, target_class, file_name_to_export="test.png"): # Guided backprop GBP = GuidedBackprop(pretrained_model) # Get gradients guided_grads = GBP.generate_gradients(prep_img, target_class) # 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')
# Average it out smooth_grad = smooth_grad / param_n return smooth_grad 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) VBP = VanillaBackprop(pretrained_model) # GBP = GuidedBackprop(pretrained_model) # if you want to use GBP dont forget to # change the parametre in generate_smooth_grad param_n = 50 param_sigma_multiplier = 4 smooth_grad = generate_smooth_grad(VBP, # ^This parameter prep_img, target_class, param_n, param_sigma_multiplier) # Save colored gradients save_gradient_images(smooth_grad, file_name_to_export + '_SmoothGrad_color') # Convert to grayscale grayscale_smooth_grad = convert_to_grayscale(smooth_grad) # Save grayscale gradients save_gradient_images(grayscale_smooth_grad, file_name_to_export + '_SmoothGrad_gray') print('Smooth grad completed')
model_output = self.model(input_image) # Zero grads self.model.zero_grad() # Target for backprop one_hot_output = torch.FloatTensor(1, model_output.size()[-1]).zero_() one_hot_output[0][target_class] = 1 # Backward pass 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__': # 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')
# [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 dataset = ds.ClipArt("assets") prep_img, target_class = dataset.__getitem__(0) file_name_to_export = "test.png" pretrained_model = None # Guided backprop GBP = GuidedBackprop(pretrained_model) # Get gradients guided_grads = GBP.generate_gradients(prep_img, target_class) # 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')