Too many values to unpack from tensor

username: mathwseg
cooked: <p>i got the shape of the image like</p>
<pre><code>torch.Size([1, 1, 3, 319, 256])
</code></pre>
<p>due to that, i got</p>
<pre><code> B, C, H, W = x.shape
ValueError: too many values to unpack (expected 4)
</code></pre>
<p>my code is</p>
<pre><code>inference_transform = T.Compose(
    [
        T.Resize(256),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),  # Imagenet
    ]
)
img = load_img("127535.jpg")        
img = inference_transform(img.convert("RGB"))    
img = img.to(DEVICE)         
img = img.reshape((1,img.shape[0], img.shape[1], img.shape[2]))   
img = img.unsqueeze(0)    
print(img.shape)    
output = model(img)
</code></pre>
<p>in which place I should change these dimensions ? or is there anything I can add to this code to make the dimensions right ?</p>

username: Sayed_Nadim
cooked: <p>Let’s go through your code.</p>
<pre><code class="lang-auto">img = inference_transform(img.convert("RGB")) 
</code></pre>
<p>So, you have loaded and transformed an image. Your image should have a shape of <code>channel, height, width</code>.</p>
<p>Now, let’s see here,</p>
<pre><code class="lang-auto">img = img.reshape((1,img.shape[0], img.shape[1], img.shape[2])) 
</code></pre>
<p>When you perform reshape operation <code>img.reshape((1,img.shape[0], img.shape[1], img.shape[2]))</code>, the shape becomes <code>1, channel, height, width</code>, where <code>1</code> at the beginning acts like the <code>batch</code>. Now, your image is a 4d tensor and has a shape of <code>batch, channel, height, width</code>.  Your image is now ready for your model, assuming it works with images with shape <code>batch, channel, height, width</code>.</p>
<p>However, here’s a problem,</p>
<pre><code class="lang-auto">img = img.unsqueeze(0)
</code></pre>
<p>When you perform <code>img.unsqueeze(0)</code>, it creates another dimension at 0th index. Now, your image has become a 5d tensor with shape <code>batch, number_of_frames, channel, height, width</code>.</p>
<p>So, when you do this,</p>
<pre><code class="lang-auto">B, C, H, W = x.shape
</code></pre>
<p>You are trying to unpack 5 values into 4 placeholders (Remember, x has 5 values now!).</p>
<p>So, just remove the reshape line or unsqueeze line and you should be ok.</p>
<pre><code class="lang-auto">img = img.reshape((1,img.shape[0], img.shape[1], img.shape[2]))   #&lt;------ this
img = img.unsqueeze(0) #&lt;------ or this
</code></pre>
<p>I have a crush on unsqueeze operation (haha), so I would suggest commenting out the <code>reshape</code> line.</p>
<p>Cheers!</p>

username: mathwseg
cooked: <p>Thanks for replying … i leave one of this lines but got another error</p>
<pre><code class="lang-auto">img = img.reshape((1,img.shape[0], img.shape[1], img.shape[2]))   #&lt;------ this
img = img.unsqueeze(0) #&lt;------ or this
</code></pre>
<p>but got another error</p>
<blockquote>
<p>output = model(img)<br>
File “/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/container.py”, line 119, in forward<br>
input = module(input)<br>
File “/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/user/.local/lib/python3.6/site-packages/torch/nn/modules/dropout.py”, line 58, in forward<br>
return F.dropout(input, self.p, self.training, self.inplace)<br>
File “/home/user/.local/lib/python3.6/site-packages/torch/nn/functional.py”, line 1076, in dropout<br>
return <em>VF.dropout</em>(input, p, training) if inplace else _VF.dropout(input, p, training)<br>
TypeError: dropout(): argument ‘input’ (position 1) must be Tensor, not tuple</p>
</blockquote>

username: Sayed_Nadim
cooked: <p>This error comes from the <code>model</code> code. The input to your <code>Dropout</code> layer is not a tensor, but a list or tuple. I can’t suggest anything without the code of <code>model</code> but I would suggest you to check the input of <code>Dropout</code> layer.</p>

username: mathwseg
cooked: <p>Thanks … i think the problem happen when tried to delete the head layer from the model using this line</p>
<blockquote>
<p>model = torch.nn.Sequential(*(list(model.children())[:-1]))</p>
</blockquote>
<p>when i comment out this line i got the tensor of the image without problem but i need to remove the classification layer as i need the features before classification … what i should do, please</p>

username: Sayed_Nadim
cooked: <p>Can you please print the shape of the output feature from the model?</p>

username: Sayed_Nadim
cooked: <p>Is there any list operation in the forward method? Or ar you returning more than one value from any method? I am not sure that removing the head is creating the issue.</p>

username: Sayed_Nadim
cooked: <p>I think your previous (deleted)  approach is ok. Can you try this?</p>
<pre><code class="lang-auto">class Identity(nn.Module):
    def __init__(self):
        super(Identity, self).__init__()
        
    def forward(self, x):
        return 

model.head = Identity()
</code></pre>
<p>See this <a href="https://discuss.pytorch.org/t/how-to-delete-layer-in-pretrained-model/17648/2">answer</a> .</p>

username: mathwseg
cooked: <p>Thanks but last question about this</p>
<pre><code class="lang-auto">inference_transform = T.Compose(
    [
        T.Resize(256),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),  # Imagenet
    ]
)
</code></pre>
<p>i got problem with size of images as i got</p>
<pre><code class="lang-auto">&gt;  return torch._C._nn.gelu(input)
&gt; RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 5.80 GiB total capacity; 3.83 GiB already allocated; 16.50 MiB free; 4.01 GiB reserved in total by PyTorch)
</code></pre>
<p>when i changed</p>
<blockquote>
<p>T.Resize(256)</p>
</blockquote>
<p>to</p>
<blockquote>
<p>T.Resize((224,224))</p>
</blockquote>
<p>another image have a problem . is there any setting for all images</p>

username: Sayed_Nadim
cooked: <p>In both cases, all images will be resized according to the provided image size. What issue are you facing with resize operation?</p>

username: mathwseg
cooked: <p>This</p>
<pre><code class="lang-auto">&gt;  return torch._C._nn.gelu(input)
&gt; RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB (GPU 0; 5.80 GiB total capacity; 3.83 GiB already allocated; 16.50 MiB free; 4.01 GiB reserved in total by PyTorch)
</code></pre>
<p>I put a counter for images so when I changed the number of resizing it keep going on but stop in some images</p>

username: Sayed_Nadim
cooked: <p>Can you please post your inference code or snippet? It shouldn’t give CUDA OOM error for resize operation. Also, can you try with smaller image size e.g., 64 and check whether you are getting CUDA OOM error?</p>