RuntimeError: shape '[-1, 3, 64, 64]' is invalid for input of size 120000

username: Najeh_Nafti
cooked: <p>I am using a GAN model and working with CT scans, and I would like to crop them with transforms buth with a different size the the image size. But I got this error:</p>
<pre><code class="lang-auto">RuntimeError: shape '[-1, 3, 64, 64]' is invalid for input of size 120000

</code></pre>
<p>How can I solve the problem?</p>
<p>Discriminator Code:</p>
<pre><code class="lang-auto">class GoodDiscriminator(nn.Module):
    def __init__(self, dim=DIM):
        super(GoodDiscriminator, self).__init__()

        self.dim = dim

        self.conv1 = MyConvo2d(3, self.dim, 3, he_init=False)
        self.rb1 = ResidualBlock(self.dim, 2*self.dim,
                                 3, resample='down', hw=DIM)
        self.rb2 = ResidualBlock(
            2*self.dim, 4*self.dim, 3, resample='down', hw=int(DIM/2))
        self.rb3 = ResidualBlock(
            4*self.dim, 8*self.dim, 3, resample='down', hw=int(DIM/4))
        self.rb4 = ResidualBlock(
            8*self.dim, 8*self.dim, 3, resample='down', hw=int(DIM/8))
        self.ln1 = nn.Linear(4*4*8*self.dim, 1)
        self.initialize()

    def initialize(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.LayerNorm):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight)
                nn.init.constant_(m.bias, 0)

    def extract_feature(self, input):
        output = input.contiguous()
        output = output.view(-1, 3, DIM, DIM)
        output = self.conv1(output)
        output = self.rb1(output)
        output = self.rb2(output)
        output = self.rb3(output)
        output = self.rb4(output)
        output = output.view(-1, 4*4*8*self.dim)
        return output

    def forward(self, input):
        output = self.extract_feature(input)
        output = self.ln1(output)
        output = output.view(-1)
        return output


</code></pre>
<p>Generator Code:</p>
<pre><code class="lang-auto">class GoodGenerator(nn.Module):
    def __init__(self, dim=DIM, output_dim=OUTPUT_DIM):
        super(GoodGenerator, self).__init__()

        self.dim = dim

        self.ln1 = nn.Linear(128, 4*4*8*self.dim)
        self.rb1 = ResidualBlock(8*self.dim, 8*self.dim, 3, resample='up')
        self.rb2 = ResidualBlock(8*self.dim, 4*self.dim, 3, resample='up')
        self.rb3 = ResidualBlock(4*self.dim, 2*self.dim, 3, resample='up')
        self.rb4 = ResidualBlock(2*self.dim, 1*self.dim, 3, resample='up')
        self.bn = nn.BatchNorm2d(self.dim)

        self.conv1 = MyConvo2d(1*self.dim, 3, 3)
        self.relu = nn.ReLU()
        self.tanh = nn.Tanh()
        self.initialize()

    def initialize(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight)
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.LayerNorm):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight)
                nn.init.constant_(m.bias, 0)

    def forward(self, input):
        output = self.ln1(input.contiguous())
        output = output.view(-1, 8*self.dim, 4, 4)
        output = self.rb1(output)
        output = self.rb2(output)
        output = self.rb3(output)
        output = self.rb4(output)

        output = self.bn(output)
        output = self.relu(output)
        output = self.conv1(output)
        output = self.tanh(output)
        # output = output.view(-1, OUTPUT_DIM)
        return output
</code></pre>

username: PURUSHOTTAM_BHARDWAJ
cooked: <p>can you provide more information and the piece of code you are trying?  this is basically input and output size mismatch. your input size should be multiple of (3X64X64).</p>

username: Najeh_Nafti
cooked: <p>I added more details to the question</p>

username: PURUSHOTTAM_BHARDWAJ
cooked: <p>Assuming that you are getting this error in 3rd line of Discriminator code as the details you shared are not enough.<br>
the size of output is 120000, which is not a multiple of 12288(1,3,64,64)(considering DIM to 64).</p>

username: Najeh_Nafti
cooked: <p>The full code of the discriminator and generator are added to the question with DIM = 64</p>

username: lelouedec
cooked: <p>Can you share the error details as where the error is experienced in the execution ?<br>
the error means your tensor is not big enough for the shape you are trying to reshape it to.<br>
(1,3,64,64) should be create from a 1D tensor of shape (12288)</p>

username: Najeh_Nafti
cooked: <p>This is the error details:</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-41-b5e01faa9c48&gt; in &lt;module&gt;
     13         fake_imgs = G(z)
     14 
---&gt; 15         real_validity = D(real_imgs)
     16         fake_validity = D(fake_imgs.detach())
     17 

~\anaconda3\envs\st\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-9-ed9aa1621bc7&gt; in forward(self, input)
     42 
     43     def forward(self, input):
---&gt; 44         output = self.extract_feature(input)
     45         output = self.ln1(output)
     46         output = output.view(-1)

&lt;ipython-input-9-ed9aa1621bc7&gt; in extract_feature(self, input)
     32     def extract_feature(self, input):
     33         output = input.contiguous()
---&gt; 34         output = output.view(-1, 3, DIM, DIM)
     35         output = self.conv1(output)
     36         output = self.rb1(output)

RuntimeError: shape '[-1, 3, 64, 64]' is invalid for input of size 120000

</code></pre>

username: lelouedec
cooked: <p>Can you print the shape of input : print(input.shape) and print(output.shape) ? it seems they are not of the shape you might think they are.</p>

username: Najeh_Nafti
cooked: <pre><code class="lang-auto">Input shape: torch.Size([16, 128])
 Output Shape: torch.Size([16, 3, 64, 64])
</code></pre>

username: lelouedec
cooked: <pre><code class="lang-auto"> def extract_feature(self, input):
        output = input.contiguous()
</code></pre>
<p>is the shape of this output ? because from your error this output variable is not</p>
<pre><code class="lang-auto">[16, 3, 64, 64]
</code></pre>

username: Najeh_Nafti
cooked: <p>this is the output of ```<br>
output = input.contiguous()</p>
<pre><code class="lang-auto">input: torch.Size([16, 128])
torch.Size([16, 8192])</code></pre>

username: lelouedec
cooked: <p>Okay so you cannot reshape output from this line to this shape. It’s shape should be something like<br>
[16,2,64,64].<br>
so either reshape it to that or make sure your input to extract_features is of the correct shape : [16,12288]</p>

username: Najeh_Nafti
cooked: <p>there is no other solution to crop images before process them to transforms to solve this problem?</p>

username: lelouedec
cooked: <p>The problem here is that the input you are passing to the discriminator doesn’t seem to be of the correct size.<br>
So without your loading function and printing the size of the tensor before going in the model then it is difficult to assess</p>

username: Najeh_Nafti
cooked: <p>okay thank you for responses</p>