Hi,
I am trying to write a code to visualize the maximum feature response of a fine-tuned vgg16 network. However, I have the following error while using the autograd function:
TypeError: expected tuple, but hook returned 'Tensor'
Here is a shortened version of the code:
def relu_backward_deconv_hook ( module, grad_input, grad_output):
return F.relu(grad_output[0])
def equip_model_deconv (model):
for m in model.modules():
if isinstance(m, nn.ReLU):
m.register_backward_hook(relu_backward_deconv_hook)
def grad_view(model, image_name):
to_tensor = transforms.ToTensor()
img = to_tensor(PIL.Image.open(image_name))
img = 0.5 + 0.5 * (img - img.mean()) / img.std()
if torch.cuda.is_available():
img = img.cuda()
input = Variable(img.view(1, img. size(0), img.size(1), img.size(2)), requires_grad=True)
output = model(input)
result = torch.autograd.grad(output.max(), input)
result = result.data / result.data.max() + 0.5
return result
model = model_fine_tuned
model.eval()
model = model.features
equip_model_deconv(model)
result = grad_view(model, image)
It would be really nice of you to help me in solving this issue.
Thanks in advance,
best wishes.