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

I am getting the following error on console:

on line 23, in generate_discrete_label
** input = input.view(1, label_nc, size[2], size[3])**
RuntimeError: shape ‘[1, 14, 256, 192]’ is invalid for input of size 147456

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.

But based on your error, it seems like you are trying to reshape view to invalid dimensions. The shape you provided [1,14,256,192] 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 256x192 by this point.

Thanks Karthik for your response. OK, I have removed the code and just mentioned the error.
I was mentioned the earlier code for understandability of situation, anyway.

My main function here to calling:

def generate_discrete_label(inputs, label_nc, onehot=True, encode=True):
pred_batch = []
size = inputs.size()
for input in inputs:
input = input.view(1, label_nc, size[2], size[3])
pred = np.squeeze(input.data.max(1)[1].cpu().numpy(), axis=0)
pred_batch.append(pred)

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)

====================

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.

**# -- 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)

could you print few things in above code?

label_nc:14
size dim torch.Size([1, 3, 256, 192])
input size BEFORE: torch.Size([3, 256, 192])

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