Hi I am trying to use the same code as mentioned in one of the post of this discussion forum for visualizing feature map but I am kind of getting this error "hook_result = hook(self, input, result)
TypeError: ‘NoneType’ object is not callable"
Below is the code
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
model.dc.register_forward_hook(get_activation('dc'))
dataiter = iter(trainloader)
images,labels = dataiter.next()
imshow(images[0],labels[0])
print(images[0].unsqueeze_(0).shape)
output = model(images[0].unsqueeze_(0))
print(output.shape)
act = activation['dc'].squeeze()
fig, ax = plt.subplots(act.size(0))
for idx in range(act.size(0)):
ax[idx].imshow(act[idx])
Model
class NormalCNN(nn.Module):
def __init__(self, args, classes):
super(NormalCNN, self).__init__()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.args = args
self.class_num = 10
self.classes = classes
resnet18 = models.resnet18(pretrained=True)
self.backbone = nn.Sequential(*list(resnet18.children())[0:5])
self.dc = nn.Conv2d(64, 1 , kernel_size = 1, stride=1, padding=1)
self.mlp = nn.Sequential(
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 10))
### ----------------------------------------------
def forward(self, imgs):
v = self.backbone(imgs)
v_out = self.dc(v)
v_out = v_out/8
out = nn.Upsample(size=(8, 8), mode='bilinear')(v_out)
out = out.view(-1,1*8*8)
cls_scores = self.mlp(out)
return cls_scores # Dim: [batch_size, 10]