label = data['label'].to(device) label_ID = data['label_ID'] # get the most likely prediction of the model pred = net(img) pred_label = net(img).argmax(dim=1) acc_hist.update(pred_label.cpu().numpy(), label.cpu().numpy()) pred = pred[:, 1] # back-propagation with the logit of the 2nd class which represents a success # get the gradient of the output with respect to the parameters of the model pred.backward() # pull the gradients out of the model gradients = net.get_activations_gradient() # pool the gradients across the channels pooled_gradients = torch.mean(gradients, dim=(0, 2, 3, 4)) # get the activations of the last convolutional layer activations = net.get_activations(img).detach() # weight the channels by corresponding gradients for i in range(128): activations[:, i, :, :] *= pooled_gradients[i] # average the channels of the activations heatmap = torch.mean(activations, dim=1).squeeze() # relu on top of the heatmap heatmap = np.maximum(heatmap.cpu().numpy(), 0) # save the heatmap if label_ID[0].find('B_b') != -1: sumed_heatmap_B_b.append(heatmap) elif label_ID[0].find('Z_b') != -1: sumed_heatmap_Z_b.append(heatmap) elif label_ID[0].find('B_n') != -1: sumed_heatmap_B_n.append(heatmap) elif label_ID[0].find('B_r') != -1: