How to solve TypeError: conv2d() received an invalid combination of arguments - got (map, Parameter, NoneType, tuple, tuple, tuple, int)

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.

I have tried to unsqueeze the metaitem like pred = model(metaitem.unsqueeze())
but I get AttributeError: 'map' object has no attribute 'unsqueeze'

You would have to pass valid tensors to the model instead of the map.

1 Like

Hello @ptrblck

I think I was able now to pass valid tensors. Here is my updated code:

for imname, metaitem in metadata_test.items():

        imname = Image.open(imname)
        convert_tensor = transforms.ToTensor()
        imname = convert_tensor(imname)

        imname = imname.unsqueeze(2) 
        imname = imname.permute(1, 0, 2, 3)
        pred = model(imname)
        
      
        
        pred.argmax(dim=1)
        pred[:,4].sum().backward()
        
        
        
        
        gradients = model.get_gradient()
     
        pooled_gradients = torch.mean(gradients, dim=[0, 2, 3])
        pred = torch.unsqueeze(pred, dim=0)
        activations = model.get_activations(pred).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)

but I am getting this error:

RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 1, 500, 20] to have 3 channels, but got 1 channels instead

I am thinking its because I am getting the activations per channel (for grad-cam) that is why I only have 1 channel in the size. Any advice for me? Thank you.

The error seems to be raised from the forward pass while it seems you are using the activations later in your code so I would guess your input image has a single channel while 3 are expected.