Help with unevenly sized images in GAN models

username: anon_ymous
cooked: <p>I am trying to modify one of the example codes to have images of uneven ratios. I cannot find a solution anywhere on the internet. Can anyone help me? Here’s the full error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\\Downloads\.py", line 298, in &lt;module&gt;
    main()
  File "C:\Users\\Downloads\.py", line 209, in main
    errD_real = criterion(output, label)
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\loss.py", line 697, in forward
    return F.binary_cross_entropy(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\functional.py", line 3545, in binary_cross_entropy
    raise ValueError(
ValueError: Using a target size (torch.Size([71])) that is different to the input size (torch.Size([142])) is deprecated. Please ensure they have the same size.
</code></pre>

username: anon_ymous
cooked: <p>Also, before this, I tried to fix this error, and that seemed to cause the current problem</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\\Downloads\.py", line 298, in &lt;module&gt;
    main()
  File "C:\Users\\Downloads\.py", line 207, in main
    output = netD(real_cpu).view(-1)
             ^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\Downloads\.py", line 155, in forward
    return self.main(input)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\container.py", line 250, in forward
    input = module(input)
            ^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\conv.py", line 549, in forward
    return self._conv_forward(input, self.weight, self.bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\modules\conv.py", line 544, in _conv_forward
    return F.conv2d(
           ^^^^^^^^^
RuntimeError: Calculated padded input size per channel: (4 x 2). Kernel size: (4 x 4). Kernel size can't be greater than actual input size
</code></pre>

username: ptrblck
cooked: <p>Based on your description it seems you might have padded the input in the batch dimension or have used a <code>view</code> operation incorrectly which has then increased the batch size of the input. Check if this could be the case and post the model definition if not.</p>

username: anon_ymous
cooked: <p>I apologize for the fact that I’ve switched to a completely different model now, but I still have an issue.<br>
I am trying to change <a href="https://github.com/MatthewR2D2/Pytorch/blob/master/GANS/DeepConvGAN.ipynb" rel="noopener nofollow ugc">this model</a> to use RGB instead of grayscale, and I have the issue of the model refusing to make an image with 3 channels, giving this error when I try to save an image:</p>
<pre><code class="lang-auto">ValueError: cannot reshape array of size 21632 into shape (208,104,3)
</code></pre>
<p>Any help is appreciated</p>
<p>Edit: Here’s how the models are defined</p>
<pre><code class="lang-auto">class Discriminator(nn.Module):
        def __init__(self, input_size, hidden_layer, output_size):
            super(Discriminator, self).__init__()
            self.fc1 = nn.Linear(input_size, hidden_layer*4)
            self.fc2 = nn.Linear(hidden_layer*4, hidden_layer*2)
            self.fc3 = nn.Linear(hidden_layer*2, hidden_layer)
            self.fc4 = nn.Linear(hidden_layer, output_size)
            self.dropout = nn.Dropout(0.3)
        
        def forward(self,x):
            x = x.view(-1, (xdim//sizediv)*(ydim//sizediv))
            x = F.leaky_relu(self.fc1(x), 0.2) # Negative slope of 0.2
            x = self.dropout(x)
            x = F.leaky_relu(self.fc2(x), 0.2)
            x = self.dropout(x)
            x = F.leaky_relu(self.fc3(x), 0.2)
            x = self.dropout(x)
            out = self.fc4(x)
            return out

    class Generator(nn.Module):
        def __init__(self, input_size, hidden_layer, output_size):
            super(Generator, self).__init__()
            self.fc1 = nn.Linear(input_size, hidden_layer)
            self.fc2 = nn.Linear(hidden_layer, hidden_layer*2)
            self.fc3 = nn.Linear(hidden_layer*2, hidden_layer*4)
            self.fc4 = nn.Linear(hidden_layer*4, output_size)
            self.dropout = nn.Dropout(0.3)
        def forward(self,x):
            x = F.leaky_relu(self.fc1(x), 0.2)
            x = self.dropout(x)
            x = F.leaky_relu(self.fc2(x), 0.2)
            x = self.dropout(x)
            x = F.leaky_relu(self.fc3(x), 0.2)
            x = self.dropout(x)
            out = F.tanh(self.fc4(x))

            return out
</code></pre>

username: anon_ymous
cooked: <p>Another thing of note: Trying to change the size of the inputs and/or models leads to a different error:<br>
<code>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1152x21632 and 64896x640)</code></p>

username: Daniel_Gonzalez1
cooked: <p>Hi, how are you defining the input size/output size?, since you are using linear layers and 3 channel images it should be like HxWx3. Also, you should ensure that the RGB image is flattened before passing it to the model.</p>
<p>Daniel Gonzalez<br>
Embedded SW Engineer at RidgeRun</p>