RuntimeError: shape '[2, 64, 50, 50]' is invalid for input of size 48384

class RTCNN(nn.Module):
def init(self, args):
super(RTCNN, self).init()

    self.args = args
    self.scale_idx = 0

    scale = args.scale[0]
    
    self.conv1 = nn.Conv2d(3, 32, kernel_size=(3,3), stride=(1,1), padding=1, bias=False)
    self.DW_conv1 = DepthwiseConv2d(32, depth_multiplier=1, kernel_size=(1,5))
    self.PW_conv1 = nn.Conv2d(32, 16, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    self.DW_conv2 = DepthwiseConv2d(16, depth_multiplier=1, kernel_size=(1,5))
    self.PW_conv2 = nn.Conv2d(16, 32, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    # self.add1 = nn.Sequential(nn.Conv2d(3,32,3,1,1), nn.Conv2d(16,32,1,1,1))
    # self.add1 = torch.add(self.conv1, self.PW_conv2)
    self.DW_conv3 = DepthwiseConv2d(32, depth_multiplier=1, kernel_size=(3,3))
    self.PW_conv3 = nn.Conv2d(32, 16, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    self.DW_conv4 = DepthwiseConv2d(16, depth_multiplier=1, kernel_size=(3,3))
    self.PW_conv4 = nn.Conv2d(16, 4, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    # self.pixel_shuffle = LambdaLayer(lambda x:space_to_depth(x,2))
    # self.Yc = LambdaLayer(self.PW_conv4)
    # self.Yn = nn.Upsample(scale_factor=2, mode='nearest')
    # self.Y = nn.Sequential(self.Yc, self.Yn)
    ## position to weight
    self.P2W = Pos2Weight(inC=64)

def repeat_x(self, x):
    scale_int = math.ceil(self.scale)
    N, C, H, W = x.size()
    x = x.view(N, C, H, 1, W, 1)

    x = torch.cat([x] * scale_int, 3)
    x = torch.cat([x] * scale_int, 5).permute(0, 3, 5, 1, 2, 4)

    return x.contiguous().view(-1, C, H, W)


def forward(self, x, pos_mat):
    # x = F.relu(self.conv1(x))
    conv1 = self.conv1(x)
    relu1 = F.relu(conv1)
    DW_conv1 = self.DW_conv1(relu1)
    PW_conv1 = self.PW_conv1(DW_conv1)
    relu2 = F.relu(PW_conv1)
    DW_conv2 = self.DW_conv2(relu2)
    PW_conv2 = self. PW_conv2(DW_conv2)
    add1 = torch.cat((conv1, PW_conv2), dim=2)
    residual = F.relu(add1)
    relu3 = F.relu(residual)
    DW_conv3 = self.DW_conv3(relu3)
    PW_conv3 = self.PW_conv3(DW_conv3)
    relu4 = F.relu(PW_conv3)
    DW_conv4 = self.DW_conv4(relu4)
    PW_conv4 = self.PW_conv4(DW_conv4)

    local_weight = self.P2W(pos_mat.view(pos_mat.size(1),-1)) 
    up_x = self.repeat_x(PW_conv4 )     ### the output is (N*r*r,inC,inH,inW)
  
  
    # N*r^2 x [inC * kH * kW] x [inH * inW]
    cols = nn.functional.unfold(up_x, 3,padding=1)
    scale_int = math.ceil(self.scale)
    cols = cols.contiguous().view(cols.size(0)//(scale_int**2),scale_int**2, cols.size(1), cols.size(2), 1).permute(0,1, 3, 4, 2).contiguous()
    local_weight = local_weight.contiguous().view(x.size(2),scale_int, x.size(3),scale_int,-1,3).permute(1,3,0,2,4,5).contiguous()
    local_weight = local_weight.contiguous().view(scale_int**2, x.size(2)*x.size(3),-1, 3)
    out = torch.matmul(cols,local_weight).permute(0,1,4,2,3)
    out = out.contiguous().view(x.size(0),scale_int,scale_int,3,x.size(2),x.size(3)).permute(0,3,4,1,5,2)
    out = out.contiguous().view(x.size(0),3, scale_int*x.size(2),scale_int*x.size(3))
    out = self.add_mean(out)
    return out

File “C:\Users\DIC\Meta-SR-Pytorch\model\metartcnn.py”, line 117, in forward
output = PW_conv4.reshape(2,64,50,50)
RuntimeError: shape ‘[2, 64, 50, 50]’ is invalid for input of size 48384

Really need help to figure out where did I do wrong.

Based on the error message it seems that you are trying to reshape PW_conv4 to a shape of [2, 64, 50, 50], which is invalid given that PW_conv4 contains 48384 elements.
Could you explain how you’ve calculated the shape for the reshape operation and then check where the size mismatch is coming from?

I update a more complete error message and my code. very appreciative if you could help me out with it. Thanks

class Pos2Weight(nn.Module):
def init(self,inC, kernel_size=3, outC=3):
super(Pos2Weight,self).init()
self.inC = inC
self.kernel_size=kernel_size
self.outC = outC
self.meta_block=nn.Sequential(
nn.Linear(3,256),
nn.ReLU(inplace=True),
nn.Linear(256,self.kernel_sizeself.kernel_sizeself.inC*self.outC)
)
def forward(self,x):

    output = self.meta_block(x)
    return output

class RTCNN(nn.Module):
def init(self, args):
super(RTCNN, self).init()

    self.args = args
    self.scale_idx = 0

    scale = args.scale[0]
    
    self.conv1 = nn.Conv2d(3, 32, kernel_size=(3,3), stride=(1,1), padding=1, bias=False)
    self.DW_conv1 = DepthwiseConv2d(32, depth_multiplier=1, kernel_size=(1,5))
    self.PW_conv1 = nn.Conv2d(32, 16, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    self.DW_conv2 = DepthwiseConv2d(16, depth_multiplier=1, kernel_size=(1,5))
    self.PW_conv2 = nn.Conv2d(16, 32, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    # self.add1 = nn.Sequential(nn.Conv2d(3,32,3,1,1), nn.Conv2d(16,32,1,1,1))
    # self.add1 = torch.add(self.conv1, self.PW_conv2)
    self.DW_conv3 = DepthwiseConv2d(32, depth_multiplier=1, kernel_size=(3,3))
    self.PW_conv3 = nn.Conv2d(32, 16, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    self.DW_conv4 = DepthwiseConv2d(16, depth_multiplier=1, kernel_size=(3,3))
    self.PW_conv4 = nn.Conv2d(16, 4, kernel_size=(1,1), stride=(1,1), padding=1, bias=False)
    # self.pixel_shuffle = LambdaLayer(lambda x:space_to_depth(x,2))
    # self.Yc = LambdaLayer(self.PW_conv4)
    # self.Yn = nn.Upsample(scale_factor=2, mode='nearest')
    # self.Y = nn.Sequential(self.Yc, self.Yn)
    ## position to weight
    self.P2W = Pos2Weight(inC=64)

def repeat_x(self, x):
    scale_int = math.ceil(self.scale)
    N, C, H, W = x.size()
    x = x.view(N, C, H, 1, W, 1)

    x = torch.cat([x] * scale_int, 3)
    x = torch.cat([x] * scale_int, 5).permute(0, 3, 5, 1, 2, 4)

    return x.contiguous().view(-1, C, H, W)


def forward(self, x, pos_mat):
    # x = F.relu(self.conv1(x))
    conv1 = self.conv1(x)
    relu1 = F.relu(conv1)
    DW_conv1 = self.DW_conv1(relu1)
    PW_conv1 = self.PW_conv1(DW_conv1)
    relu2 = F.relu(PW_conv1)
    DW_conv2 = self.DW_conv2(relu2)
    PW_conv2 = self. PW_conv2(DW_conv2)
    add1 = torch.cat((conv1, PW_conv2), dim=2)
    residual = F.relu(add1)
    relu3 = F.relu(residual)
    DW_conv3 = self.DW_conv3(relu3)
    PW_conv3 = self.PW_conv3(DW_conv3)
    relu4 = F.relu(PW_conv3)
    DW_conv4 = self.DW_conv4(relu4)
    PW_conv4 = self.PW_conv4(DW_conv4)
    # output = F.softmax(PW_conv4, dim=1)

    # output = PW_conv4.reshape(2,64,50,50)
    # print(output.shape)
    print('----')
    # Yn = self.Yn(input_shape)
    # Y = self.Y(Y)
    # out1 = F.relu(Y)
    local_weight = self.P2W(pos_mat.view(pos_mat.size(1),-1)) 
    up_x = self.repeat_x(PW_conv4)     ### the output is (N*r*r,inC,inH,inW)
    print(up_x.shape)
    print('........')
    # N*r^2 x [inC * kH * kW] x [inH * inW]
    cols = nn.functional.unfold(up_x, 3,padding=1)
    scale_int = math.ceil(self.scale)
    cols = cols.contiguous().view(cols.size(0)//(scale_int**2),scale_int**2, cols.size(1), cols.size(2), 1).permute(0,1, 3, 4, 2).contiguous()
    local_weight = local_weight.contiguous().view(x.size(2),scale_int, x.size(3),scale_int,-1,3).permute(1,3,0,2,4,5).contiguous()
    local_weight = local_weight.contiguous().view(scale_int**2, x.size(2)*x.size(3),-1, 3)
    print(cols.shape)
    print(local_weight.shape)
    out = torch.matmul(cols,local_weight).permute(0,1,4,2,3)
    out = out.contiguous().view(x.size(0),scale_int,scale_int,3,x.size(2),x.size(3)).permute(0,3,4,1,5,2)
    out = out.contiguous().view(x.size(0),3, scale_int*x.size(2),scale_int*x.size(3))
    out = self.add_mean(out)
    return out

Traceback (most recent call last):
File “main.py”, line 31, in
main()
File “main.py”, line 25, in main
t.train()
File “C:\Users\DIC\Meta-SR-Pytorch\trainer.py”, line 168, in train
sr = self.model(lr, idx_scale, scale_coord_map)
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in call
result = self.forward(*input, **kwargs)
File “C:\Users\DIC\Meta-SR-Pytorch\model_init_.py”, line 54, in forward
return self.model(x,pos_mat)
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in call
result = self.forward(*input, **kwargs)
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\data_parallel.py”, line 114, in forward
outputs = self.parallel_apply(replicas, inputs, kwargs)
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\data_parallel.py”, line 124, in parallel_apply
return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\parallel_apply.py”, line 65, in parallel_apply
raise output
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\parallel_apply.py”, line 41, in _worker
output = module(*input, **kwargs)
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in call
result = self.forward(*input, **kwargs)
File “C:\Users\DIC\Meta-SR-Pytorch\model\metartcnn.py”, line 135, in forward
out = torch.matmul(cols,local_weight).permute(0,1,4,2,3)
RuntimeError: The size of tensor a (6048) must match the size of tensor b (2500) at non-singleton dimension 2