I followed this tutorial to implement grad-cam
https://medium.com/@stepanulyanin/grad-cam-for-resnet152-network-784a1d65f3
to a subset of a dataset of multiple images. Here is the relevant parts of my training code:
Here is the subset:
dataset_test = VOC_ocv(
voc_folder, year='2007', image_set='test',
download=True, transforms=transforms_voc_ocv_eval)
metadata_test = read_metadata(dataset_test)
metadata_test = dict(list(metadata_test.items())[:500])
and here is the relevant parts of my evaluation code:
for imname, metaitem in metadata_test.items():
imname, metaitem = map(lambda x: x.to(device), (imname, metaitem))
pred = model(metaitem)
pred[:,4].sum().backward()
gradients = model.get_gradient()
pooled_gradients = torch.mean(gradients, dim=[0, 2, 3])
activations = model.get_activations(metaitem).detach()
for i in range(2048):
activations[:, i, :, :] *= pooled_gradients[i]
heatmap = torch.mean(activations, dim=1).squeeze()
heatmap = np.maximum(heatmap, 0)
heatmap /= torch.max(heatmap)
heatmap = torch.mean(heatmap, dim=-1)
However, I am getting this error:
imname, metaitem = map(lambda x: x.to(device), (imname, metaitem))
AttributeError: 'str' object has no attribute 'to'
I tried to edit the map lambda as :
imname = map(lambda x: x.to(device), (metaitem))
and I am not sure if I solved it because I got another error:
return F.conv2d(input, weight, bias, self.stride,
TypeError: conv2d() received an invalid combination of arguments - got (map, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
that traces back to this code:
pred = model(metaitem)
Any advice for me? Thank you in advance.