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

peony: <p>I followed this tutorial to implement grad-cam<br>
<a href="https://medium.com/@stepanulyanin/grad-cam-for-resnet152-network-784a1d65f3" class="onebox" target="_blank" rel="noopener nofollow ugc">https://medium.com/@stepanulyanin/grad-cam-for-resnet152-network-784a1d65f3</a></p>
<p>to a subset of a dataset of multiple images. Here  is the relevant parts of my training code:</p>
<p>Here is the subset:</p>
<pre><code class="lang-auto">    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])
</code></pre>
<pre><code>and here is the relevant parts of my evaluation code:
</code></pre>
<pre><code class="lang-auto">  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)
</code></pre>
<p>However, I am getting this error:</p>
<pre><code class="lang-auto">imname, metaitem = map(lambda x: x.to(device), (imname, metaitem))

AttributeError: 'str' object has no attribute 'to'
</code></pre>
<p>I tried to edit the map lambda as :</p>
<p><code>imname = map(lambda x: x.to(device), (metaitem))</code></p>
<p>and I am not sure if I solved it because I got another error:</p>
<pre><code class="lang-auto"> 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)
</code></pre>
<p>that traces back to this code:</p>
<p><code>pred = model(metaitem)</code></p>
<p>Any advice for me? Thank you in advance.</p>

peony: <p>I have tried to unsqueeze the metaitem like<code> pred = model(metaitem.unsqueeze())</code><br>
but I get <code>AttributeError: 'map' object has no attribute 'unsqueeze'</code></p>

ptrblck: <p>You would have to pass valid tensors to the model instead of the <code>map</code>.</p>

peony: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I think I was able now to pass valid tensors. Here is my updated code:</p>
<p>for imname, metaitem  in metadata_test.items():</p>
<pre><code>        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)
</code></pre>
<p>but I am getting this error:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>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.</p>

ptrblck: <p>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.</p>