【发布时间】:2019-10-03 11:25:42
【问题描述】:
我正在尝试使用此模块 (https://github.com/utkuozbulak/pytorch-cnn-visualizations) 来可视化网络在我的图像中看到的内容,并对其进行了编辑以满足我的需要。但是,我得到一个错误,我无法解决这个问题。 (AttributeError: 'GoogLeNet' 对象没有属性 'features')
最初,该模块使用 misc_functions.py 脚本来处理图像(获取 RGB、转换为灰度、调整大小等),但由于我使用的网络已经完成了这些,我对模块进行了一些编辑,以便它不使用这些功能。
您可以在下面找到我尝试使用的代码和我用来训练的网络
"""
Created on Thu Oct 26 11:19:58 2017
@author: Utku Ozbulak - github.com/utkuozbulak
"""
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.optim as optim
from torch import nn
import torchvision
from torch.utils.data import DataLoader, random_split
from torchvision.datasets import ImageFolder
from torchvision import transforms
from torchvision import models
from PIL import Image
from misc_functions import save_gradient_images
class VanillaBackprop():
"""
Produces gradients generated with vanilla back propagation from the image
"""
def __init__(self, model):
self.model = model
self.gradients = None
# Put model in evaluation mode
self.model.eval()
# Hook the first layer to get the gradient
self.hook_layers()
def hook_layers(self):
def hook_function(module, grad_in, grad_out):
self.gradients = grad_in[0]
# Register hook to the first layer
first_layer = list(self.model.features._modules.items())[0][1]
first_layer.register_backward_hook(hook_function)
def generate_gradients(self, input_image, target_class):
# Forward
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
#Use simple pre-trained ResNet
net = models.googlenet(pretrained=True)
#manually replace last layer for different num_classes (necessary for pretrained models)
net.fc = torch.nn.Linear(net.fc.in_features, 4)
#to gpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net = net.to(device)
net.load_state_dict(torch.load("/home/serce/scratch/titan_googlenet_epoch_100.pt")["model_state_dict"])
net.eval()
# dataLoader with transforms
tr = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor()
])
ds = ImageFolder('/scratch/serce/chromatin_texture/dna_sir160-163_128/sted/', transform=tr)
def get_example_params(example_index):
"""
Gets used variables for almost all visualizations, like the image, model etc.
Args:
example_index (int): Image id to use from examples
returns:
original_image (numpy arr): Original image read from the file
prep_img (numpy_arr): Processed image
target_class (int): Target class for the image
file_name_to_export (string): File name to export the visualizations
pretrained_model(Pytorch model): Model to use for the operations
"""
# Pick one of the examples
img_path = ds[example_index][0]
target_class = ds[example_index][1]
file_name_to_export = "example{}".format(example_index)
# Read image
original_image = img_path
# Process image
prep_img = original_image
# Define model
pretrained_model = net
return (original_image,
prep_img,
target_class,
file_name_to_export,
pretrained_model)
if __name__ == '__main__':
# Get params
target_example = 0
(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 = vanilla_grads
# Save grayscale gradients
save_gradient_images(grayscale_vanilla_grads, file_name_to_export + '_Vanilla_BP_gray')
print('Vanilla backprop completed')
我不知道输出应该是什么样子,但我正在尝试获取原始图片的图像并查看神经网络看到的内容。
【问题讨论】:
-
你确定
net有features吗?顺便说一句,函数从其范围之外获取输入是一种不好的做法(get_example_params采用在全局范围内定义的net。
标签: python machine-learning conv-neural-network backpropagation