How to change the DCGAN so that it accepts 128x128 image?

Leandro_Francia: <p>So basically, i am using the tutorial to test out DCGAN and right now i am using 400 images to generate but all images are in 128x128 after preprocessing and i want to stay that way because of quality, is there a way to change this? this is my attempt.</p>
<pre><code class="lang-auto"># Generator Code
    class Generator(nn.Module):
        def __init__(self, ngpu):
            super(Generator, self).__init__()
            self.ngpu = ngpu
            self.main = nn.Sequential(
                # input is Z, going into a convolution
                nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
                nn.BatchNorm2d(ngf * 8),
                nn.ReLU(True),
                # state size. ``(ngf*8) x 4 x 4``
                nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ngf * 4),
                nn.ReLU(True),
                # state size. ``(ngf*4) x 8 x 8``
                nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ngf * 2),
                nn.ReLU(True),
                # state size. ``(ngf*2) x 16 x 16``
                nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ngf),
                nn.ReLU(True),
                # state size. ``(ngf) x 32 x 32``
                nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
                nn.Tanh()
                # state size. ``(nc) x 64 x 64``
            )

        def forward(self, input):
            return self.main(input)

    # Create the generator
    netG = Generator(ngpu).to(device)

    # Handle multi-GPU if desired
    if (device.type == 'cuda') and (ngpu &gt; 1):
        netG = nn.DataParallel(netG, list(range(ngpu)))

    # Apply the ``weights_init`` function to randomly initialize all weights
    #  to ``mean=0``, ``stdev=0.02``.
    netG.apply(weights_init)

    # Print the model
    print(netG)

        # Discriminator Code
    class Discriminator(nn.Module):
        def __init__(self, ngpu):
            super(Discriminator, self).__init__()
            self.ngpu = ngpu
            self.main = nn.Sequential(
                # input is (nc) x 128 x 128
                nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf) x 64 x 64
                nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ndf * 2),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*2) x 32 x 32
                nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ndf * 4),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*4) x 16 x 16
                nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
                nn.BatchNorm2d(ndf * 8),
                nn.LeakyReLU(0.2, inplace=True),
                # state size. (ndf*8) x 8 x 8
                nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
                nn.Sigmoid()
            )

        def forward(self, input):
            return self.main(input)

    # Create the Discriminator
    netD = Discriminator(ngpu).to(device)
</code></pre>

ptrblck: <p>Are you getting any errors or is the model not training? In the latter case you might need to check the “Progressive Growing of GANs” paper which explains how they achieved high resolution outputs by progressively adding more layers during the training.</p>

Leandro_Francia: <p>I am getting errors when I set the image size to 128 but it runs when it is 64. I should look at adding more layers while training but I’m still new to GAN so probably gonna take a while.</p>

stherdy: <p>I implemented a working version for a 128x128 GAN, modified from the tutorial here:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/stefanherdy/segment-anything-microseg/blob/main/gan_128.py">
  <header class="source">

      <a href="https://github.com/stefanherdy/segment-anything-microseg/blob/main/gan_128.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/stefanherdy/segment-anything-microseg/blob/main/gan_128.py" target="_blank" rel="noopener nofollow ugc">stefanherdy/segment-anything-microseg/blob/main/gan_128.py</a></h4>


      <pre><code class="lang-py"># -*- coding: utf-8 -*-

import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

</code></pre>



  This file has been truncated. <a href="https://github.com/stefanherdy/segment-anything-microseg/blob/main/gan_128.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>You can use the generator architecture:</p>
<p>class Generator(nn.Module):<br>
def <strong>init</strong>(self, ngpu):<br>
super(Generator, self).<strong>init</strong>()<br>
self.ngpu = ngpu<br>
self.main = nn.Sequential(<br>
nn.ConvTranspose2d(     nz, ngf * 16, 4, 1, 0, bias=False),<br>
nn.BatchNorm2d(ngf * 16),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(ngf * 16, ngf * 8, 4, 2, 1, bias=False),<br>
nn.BatchNorm2d(ngf * 8),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),<br>
nn.BatchNorm2d(ngf * 4),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),<br>
nn.BatchNorm2d(ngf * 2),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),<br>
nn.BatchNorm2d(ngf),<br>
nn.ReLU(True),<br>
nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),<br>
nn.Tanh()<br>
)</p>
<p>I hope that helps!<br>
All the best,<br>
Stefan</p>