Shape '[1, 14, 256, 192]' is invalid for input of size 147456

NomanMK007: <p>I am getting the following error on console:</p>
<p><strong>on line 23, in generate_discrete_label</strong><br>
**    input = input.view(1, label_nc, size[2], size[3])**<br>
<strong>RuntimeError: shape ‘[1, 14, 256, 192]’ is invalid for input of size 147456</strong></p>

Karthik_Ganesan: <p>You don’t need to provide all your code (unless someone specifically asks for it). Its very rare anyone is going to go through all of it.</p>
<p>But based on your error, it seems like you are trying to reshape view to invalid dimensions. The shape you provided <code>[1,14,256,192]</code> has 688,128 elements but you are trying to reshape it to 147456 elements. You have to see what the actual dimensions if input are before the reshape. Maybe your input is no longer <code>256x192</code> by this point.</p>

NomanMK007: <p>Thanks Karthik for your response. OK, I have removed the code and just mentioned the error.<br>
I was mentioned the earlier code for understandability of situation, anyway.</p>
<p><strong>My main function here to calling:</strong></p>
<p>def generate_discrete_label(inputs, label_nc, onehot=True, encode=True):<br>
pred_batch = []<br>
size = inputs.size()<br>
for input in inputs:<br>
input = input.view(1, label_nc, size[2], size[3])<br>
pred = np.squeeze(input.data.max(1)[1].cpu().numpy(), axis=0)<br>
pred_batch.append(pred)</p>
<pre><code>pred_batch = np.array(pred_batch)
pred_batch = torch.from_numpy(pred_batch)
label_map = []
for p in pred_batch:
    p = p.view(1, 256, 192)
    label_map.append(p)
label_map = torch.stack(label_map, 0)
if not onehot:
    return label_map.float().cuda()
size = label_map.size()
oneHot_size = (size[0], label_nc, size[2], size[3])
input_label = torch.cuda.FloatTensor(torch.Size(oneHot_size)).zero_()
input_label = input_label.scatter_(1, label_map.data.long().cuda(), 1.0)
</code></pre>
<p>====================</p>
<p>I am passing an png edited image through following custom code, but it’s giving shape error as mentioned earlier. please give me a solution that what can I do now, thanks.</p>
<pre><code>**# -- Start custom code**
    tmp_arm_label = "Data_preprocessing/test/msw.png"
    img = Image.open(tmp_arm_label).convert('RGB')
    imgArray = np.array(img)
    
    to_tensor = transforms.ToTensor()
    tensor = to_tensor(img)
    arm_label = tensor.unsqueeze(0)
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    arm_label = arm_label.to(device)
    **# -- End custom code**
    
    arm_label = self.sigmoid(arm_label)
    CE_loss = self.cross_entropy2d(arm_label, (label * (1 - clothes_mask)).transpose(0, 1)[0].long()) * 10
    
    armlabel_map = generate_discrete_label(arm_label.detach(), 14, False)
    dis_label = generate_discrete_label(arm_label.detach(), 14)</code></pre>

swap: <p>could you print few things in above code?</p>
<aside class="quote no-group quote-modified" data-username="NomanMK007" data-post="3" data-topic="154202">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/0ea827/48.png" class="avatar"> NomanMK007:</div>
<blockquote>
<p>print(f’label_nc:{label_nc}‘)<br>
size = inputs.size()<br>
print(f’size dim {size}’)<br>
for input in inputs:<br>
print(f’input size BEFORE: {input.shape}')<br>
input = input.view(1, label_nc, size[2], size[3])</p>
</blockquote>
</aside>

NomanMK007: <aside class="quote no-group quote-modified" data-username="NomanMK007" data-post="3" data-topic="154202">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/0ea827/48.png" class="avatar"> NomanMK007:</div>
<blockquote>
<p>print(f’label_nc:{label_nc}’)<br>
size = inputs.size()<br>
print(f’size dim {size}’)<br>
for input in inputs:<br>
print(f’input size BEFORE: {input.shape}’)<br>
input = input.view(1, label_nc, size[2], size[3])</p>
</blockquote>
</aside>
<p>label_nc:14<br>
size dim torch.Size([1, 3, 256, 192])<br>
input size BEFORE: torch.Size([3, 256, 192])</p>

swap: <p>If your initial shape is (3,256,192), you can’t reshape “input” to (1,14,256,192) dimension.</p>