Struggling with a DCGAN while trying to train my dataset

joel2: <p>ValueError: Using a target size (torch.Size([16, 1])) that is different to the input size (torch.Size([3, 512, 512])) is deprecated. Please ensure they have the same size.</p>

ptrblck: <p>This error is usually raised in <code>nn.BCEWithLogitsLoss</code> is a shape mismatch between the target and model output is detected.<br>
The current error also indicates that the number of elements in these tensors is different so you would have to check how the loss should be calculated given these different tensors.</p>

joel2: <p>Thank you it has worked now am having</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x786432 and 100x8192) when i run my training module</p>

ptrblck: <p>This error is most likely raised in a linear layer so you would need to make sure the activation tensor is flattened to the desired shape and then adapt the <code>in_features</code> of the linear layer if needed. Based on the error message you might need to change the number of input features from <code>100</code> to <code>786432</code>.</p>

joel2: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a> let me try that and revert shortly hope your still online at that time</p>

joel2: <p>##########################</p>
<h3>
<a name="model-1" class="anchor" href="#model-1"></a>MODEL</h3>
<p>##########################<br>
class Discriminator(nn.Module):</p>
<pre><code>def __init__(self):
    super(Discriminator, self).__init__()
    self.conv = nn.Sequential(
        #
        # input size color_channels x image_height x image_width
        #
        nn.Conv2d(3, 32,
                  kernel_size=4, stride=2, padding=1),
        nn.BatchNorm2d(32),
        nn.LeakyReLU(inplace=True),
        
        #
        # size: num_feat_maps_dis x 32 x 32
        #              
        nn.Conv2d(32, 64,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(64),
        nn.LeakyReLU(inplace=True),
        #
        # size: num_feat_maps_dis*2 x 16 x 16
        #   
        nn.Conv2d(64, 128,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(128),
        nn.LeakyReLU(inplace=True),
        nn.Conv2d(128, 512,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(512),
        nn.LeakyReLU(inplace=True),
        #
        # size: num_feat_maps_dis*4 x 8 x 8
        #   
        
    )
    self.fc = nn.Sequential(
        # reshape input, 128 -&gt; 1
        nn.Linear(512, 1),
        
        nn.Sigmoid(),
        #
        # size: num_feat_maps_dis*8 x 4 x 4
        #   
        #nn.Conv2d(4096, 1,
                 # kernel_size=4, stride=1, padding=0),
        
        # size: 1 x 1 x 1
       nn.Flatten(),
        
        
        )

    
def forward(self, x):
    y = self.conv(x)
    y = y.view(y.size(0), -1)
    fc = self.fc
    y = self.fc(y)
    return y
</code></pre>
<p>class Generator(nn.Module):</p>
<pre><code>def __init__(self, input_size=524288):
    super(Generator, self).__init__()
    self.fc = nn.Sequential(
        nn.Linear(input_size, 4*4*32),
        nn.ReLU(),
    )
    
    self.conv = nn.Sequential(
        nn.ConvTranspose2d(input_size, 512, 
                           kernel_size=4, stride=1, padding=0,
                           bias=False),
        nn.BatchNorm2d(512),
        nn.LeakyReLU(inplace=True),
        #
        # size if latent_dim=100: num_feat_maps_gen*8 x 4 x 4
        #
        nn.ConvTranspose2d(512, 128, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(128),
        nn.LeakyReLU(inplace=True),
        #
        # size if latent_dim=100: num_feat_maps_gen*4 x 8 x 8
        #
        nn.ConvTranspose2d(128, 64, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(64),
        nn.LeakyReLU(inplace=True),
        #
        # size if latent_dim=100: num_feat_maps_gen*2 x 16 x 16
        #
        nn.ConvTranspose2d(64, 32, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(32),
        nn.LeakyReLU(inplace=True),
        #
        # size if latent_dim=100: num_feat_maps_gen x 32 x 32
        #
        nn.ConvTranspose2d(32,3, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        #
        # size: color_channels x 64 x 64
        #  
        nn.Tanh(),

    )
def forward(self, x):
    x = x.view(x.size(0), -1)
    y = self.fc(x)
    y = y.view(y.size(0),32,4,4 )
    y = self.conv(y)
    return y
</code></pre>

joel2: <p>When i run that i run out memory and it crashes and still get that error below<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (16x524288 and 512x1)</p>

joel2: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> when you say flatten to the desired shape and then adapt the <code>in_features</code> of the linear layer if needed,could you elaborate for me from my shared code i dont understand what you meant,thank you</p>

ptrblck: <p>I don’t think your current code fixes the original issue, as you’ve changed the <code>in_channels</code> of the transposed conv layer to <code>524288</code>, which would create a huge <code>weight</code> parameter of 16GB.</p>
<p>Could you post a minimal, executable code snippet which reproduces the error before you’ve tried to fix it, please?</p>

joel2: <p>okay let me change it back to 512*512 and share</p>

joel2: <p>class Discriminator(nn.Module):</p>
<pre><code>def __init__(self):
    super(Discriminator, self).__init__()
    self.conv = nn.Sequential(
      
        nn.Conv2d(3, 512,
                  kernel_size=4, stride=2, padding=1),
        #nn.BatchNorm2d(32),
        nn.LeakyReLU(inplace=True),
        
                     
        nn.Conv2d(512, 1024,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(1024),
        nn.LeakyReLU(inplace=True),
       

        nn.Conv2d(1024, 2048,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(2048),
        nn.LeakyReLU(inplace=True),
       
                    
        nn.Conv2d(2048, 4096,
                  kernel_size=4, stride=2, padding=1,
                  bias=False),        
        nn.BatchNorm2d(4096),
        nn.LeakyReLU(inplace=True),
        
    

        
       
        nn.Conv2d(4096, 1,
                 kernel_size=4, stride=1, padding=0),
    )
    self.fc = nn.Sequential(
       
        nn.Linear(512, 1),

        
        
        )

    
def forward(self, x):
    y = self.conv(x)
    y = y.view(y.size(0), -1)
    fc = self.fc
    y = self.fc(y)
    return y
</code></pre>
<p>class Generator(nn.Module):</p>
<pre><code>def __init__(self, input_size=100):
    super(Generator, self).__init__()
    self.fc = nn.Sequential(
        nn.Linear(input_size, 4*4*512),
        nn.ReLU(),
    )
    
    self.conv = nn.Sequential(
        nn.ConvTranspose2d(input_size, 4096, 
                           kernel_size=4, stride=1, padding=0,
                           bias=False),
        nn.BatchNorm2d(4096),
        nn.LeakyReLU(inplace=True),
       
        
        nn.ConvTranspose2d(4096, 2048, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(2048),
        nn.LeakyReLU(inplace=True),
        
        nn.ConvTranspose2d(2048, 1024, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(1024),
        nn.LeakyReLU(inplace=True),
        
        nn.ConvTranspose2d(1024, 512, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
        nn.BatchNorm2d(512),
        nn.LeakyReLU(inplace=True),
        #
        #
        nn.ConvTranspose2d(512,3, 
                           kernel_size=4, stride=2, padding=1,
                           bias=False),
     
        
        nn.Tanh(),

    )
def forward(self, x):
    x = x.view(x.size(0), -1)
    y = self.fc(x)
    y = y.view(y.size(0),512,4,4 )
    y = self.conv(y)
    return y
</code></pre>

joel2: <p>the is the code i had before i tried to fix the issue now when i run it again i run out of memory</p>

ptrblck: <p>Your code works fine if you fix the shape mismatch in <code>Discriminator.fc</code> to have <code>in_features=16</code>:</p>
<pre><code class="lang-python">g = Generator(input_size=512)
d = Discriminator()

x = torch.randn(2, 512)
out = g(x)
out = d(out)

print(out.shape)
# torch.Size([2, 1])
</code></pre>