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

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:

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

How can I solve the problem?

Discriminator Code:

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


Generator Code:

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

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).

I added more details to the question

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

The full code of the discriminator and generator are added to the question with DIM = 64

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

This is the error details:

RuntimeError                              Traceback (most recent call last)
<ipython-input-41-b5e01faa9c48> in <module>
     13         fake_imgs = G(z)
     14 
---> 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:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

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

<ipython-input-9-ed9aa1621bc7> in extract_feature(self, input)
     32     def extract_feature(self, input):
     33         output = input.contiguous()
---> 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

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.

Input shape: torch.Size([16, 128])
 Output Shape: torch.Size([16, 3, 64, 64])
 def extract_feature(self, input):
        output = input.contiguous()

is the shape of this output ? because from your error this output variable is not

[16, 3, 64, 64]

this is the output of ```
output = input.contiguous()

input: torch.Size([16, 128])
torch.Size([16, 8192])

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

there is no other solution to crop images before process them to transforms to solve this problem?

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

okay thank you for responses