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

Jace_Dang: <p>class RTCNN(nn.Module):<br>
def <strong>init</strong>(self, args):<br>
super(RTCNN, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>File “C:\Users\DIC\Meta-SR-Pytorch\model\metartcnn.py”, line 117, in forward<br>
output = PW_conv4.reshape(2,64,50,50)<br>
RuntimeError: shape ‘[2, 64, 50, 50]’ is invalid for input of size 48384</p>
<p>Really need help to figure out where did I do wrong.</p>

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

Jace_Dang: <p>I update a more complete error message and my code. very appreciative if you could help me out with it. Thanks</p>
<p>class Pos2Weight(nn.Module):<br>
def <strong>init</strong>(self,inC, kernel_size=3, outC=3):<br>
super(Pos2Weight,self).<strong>init</strong>()<br>
self.inC = inC<br>
self.kernel_size=kernel_size<br>
self.outC = outC<br>
self.meta_block=nn.Sequential(<br>
nn.Linear(3,256),<br>
nn.ReLU(inplace=True),<br>
nn.Linear(256,self.kernel_size<em>self.kernel_size</em>self.inC*self.outC)<br>
)<br>
def forward(self,x):</p>
<pre><code>    output = self.meta_block(x)
    return output
</code></pre>
<p>class RTCNN(nn.Module):<br>
def <strong>init</strong>(self, args):<br>
super(RTCNN, self).<strong>init</strong>()</p>
<pre><code>    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
</code></pre>
<p>Traceback (most recent call last):<br>
File “main.py”, line 31, in <br>
main()<br>
File “main.py”, line 25, in main<br>
t.train()<br>
File “C:\Users\DIC\Meta-SR-Pytorch\trainer.py”, line 168, in train<br>
sr = self.model(lr, idx_scale, scale_coord_map)<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\DIC\Meta-SR-Pytorch\model_<em>init</em>_.py”, line 54, in forward<br>
return self.model(x,pos_mat)<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\data_parallel.py”, line 114, in forward<br>
outputs = self.parallel_apply(replicas, inputs, kwargs)<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\data_parallel.py”, line 124, in parallel_apply<br>
return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\parallel_apply.py”, line 65, in parallel_apply<br>
raise output<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\parallel\parallel_apply.py”, line 41, in _worker<br>
output = module(*input, **kwargs)<br>
File “D:\3rd_semester\anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\DIC\Meta-SR-Pytorch\model\metartcnn.py”, line 135, in forward<br>
out = torch.matmul(cols,local_weight).permute(0,1,4,2,3)<br>
RuntimeError: The size of tensor a (6048) must match the size of tensor b (2500) at non-singleton dimension 2</p>