ptrblck:

dataroot = "/content"
workers = 4 
batch_size = 32
image_size = 299
nc = 3 #rgb
nz = 598
ngf = 299
ndf = 299
num_epochs = 2 
lr = 0.001
beta1 = 0.5
ngpu = 1
num_show = 2
n_class = 120

outf = '/content/drive/MyDrive/output_result'
if not os.path.exists(outf):
    os.mkdir(outf)



class Generator(nn.Module):

    def __init__(self, ngpu, nz=nz, ngf=ngf, nc=nc, n_class=n_class):

        super(Generator, self).__init__()
        self.ngpu = ngpu
        self.ReLU = nn.ReLU(True)
        self.Tanh = nn.Tanh()
        self.conv1 = nn.ConvTranspose2d(nz+n_class, ngf * 8, 4, 1, 0, bias=False)
        self.BatchNorm1 = nn.BatchNorm2d(ngf * 8)

        self.conv2 = nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False)
        self.BatchNorm2 = nn.BatchNorm2d(ngf * 4)

        self.conv3 = nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False)
        self.BatchNorm3 = nn.BatchNorm2d(ngf * 2)

        self.conv4 = nn.ConvTranspose2d(ngf * 2, ngf * 1, 4, 2, 1, bias=False)
        self.BatchNorm4 = nn.BatchNorm2d(ngf * 1)

        self.conv5 = nn.ConvTranspose2d(ngf * 1, nc, 4, 2, 1, bias=False)

        self.apply(weights_init)


    def forward(self, input):

        x = self.conv1(input)
        x = self.BatchNorm1(x)
        x = self.ReLU(x)

        x = self.conv2(x)
        x = self.BatchNorm2(x)
        x = self.ReLU(x)

        x = self.conv3(x)
        x = self.BatchNorm3(x)
        x = self.ReLU(x)

        x = self.conv4(x)
        x = self.BatchNorm4(x)
        x = self.ReLU(x)

        x = self.conv5(x)
        output = self.Tanh(x)
        return output

# Create the generator
netG = Generator(ngpu).to(device)
# Apply the weights_init function to randomly initialize all weights
netG.apply(weights_init)
# Print the model
print(netG)


class Discriminator(nn.Module):

    def __init__(self, ngpu, ndf=ndf, nc=nc, n_class=n_class):

        super(Discriminator, self).__init__()
        self.LeakyReLU = nn.LeakyReLU(0.2, inplace=True)
        self.conv1 = nn.Conv2d(nc, ndf, 4, 2, 1, bias=False)
        self.conv2 = nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False)
        self.BatchNorm2 = nn.BatchNorm2d(ndf * 2)
        self.conv3 = nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False)
        self.BatchNorm3 = nn.BatchNorm2d(ndf * 4)
        self.conv4 = nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False)
        self.BatchNorm4 = nn.BatchNorm2d(ndf * 8)
        self.conv5 = nn.Conv2d(ndf * 8, ndf * 1, 4, 1, 0, bias=False)
        self.disc_linear = nn.Linear(ndf * 1, 1)
        self.aux_linear = nn.Linear(ndf * 1, n_class)
        self.softmax = nn.Softmax()
        self.sigmoid = nn.Sigmoid()
        self.ndf = ndf
        self.apply(weights_init)

    def forward(self, input):

        x = self.conv1(input)
        x = self.LeakyReLU(x)

        x = self.conv2(x)
        x = self.BatchNorm2(x)
        x = self.LeakyReLU(x)

        x = self.conv3(x)
        x = self.BatchNorm3(x)
        x = self.LeakyReLU(x)

        x = self.conv4(x)
        x = self.BatchNorm4(x)
        x = self.LeakyReLU(x)

        x = self.conv5(x)
        x = x.view(-1, self.ndf * 1)
        c = self.aux_linear(x)
        c = self.softmax(c)
        s = self.disc_linear(x)
        s = self.sigmoid(s)
        return s,c


# Create the Discriminator
netD = Discriminator(ngpu).to(device)
# Apply the weights_init function to randomly initialize all weights
netD.apply(weights_init)
# Print the model
print(netD)

# Training Loop

# Lists to keep track of progress
G_losses = []
D_losses = []
fake_img_list=[]
iters = 0

print("Starting Training Loop...")
for epoch in range(num_epochs):
    for i, data in enumerate(dataloader,0):
        # prepare real image and label
        real_label = data[1].to(device) 
        real_image = data[0].to(device) 
        b_size = real_label.size(0)    
        
        # prepare fake image and label
        fake_label = torch.randint(n_class, (b_size,), dtype=torch.long, device=device) 
        noise = torch.randn(b_size, nz, 1, 1, device=device).squeeze(0)
        noise = concat_noise_label(noise, real_label, device) 
        fake_image = netG(noise) 
        
        
        # target
        real_target = torch.full((b_size,), r_label, device=device, dtype=torch.float) 
        fake_target = torch.full((b_size,), f_label, device=device, dtype=torch.float) 

        real_target = real_target.view(-1,1)
        fake_target = fake_target.view(-1,1)
        
        #-----------------------
        # Update Discriminator
        #-----------------------
        netD.zero_grad()
        
        # train with real
        s_output, c_output = netD(real_image) 
        s_errD_real = s_criterion(s_output, real_target)  
        c_errD_real = c_criterion(c_output, real_label)  
        errD_real = s_errD_real + c_errD_real
        errD_real.backward()
        D_x = s_output.data.mean()


        # train with fake
        s_output,c_output = netD(fake_image.detach())
        s_errD_fake = s_criterion(s_output, fake_target)  # realfake
        c_errD_fake = c_criterion(c_output, real_label)  # class
        errD_fake = s_errD_fake + c_errD_fake
        errD_fake.backward()
        D_G_z1 = s_output.data.mean()
        
        errD = s_errD_real + s_errD_fake
        optimizerD.step()        

        #-----------------------
        # Update Generator
        #-----------------------
        netG.zero_grad()
        
        s_output,c_output = netD(fake_image)
        s_errG = s_criterion(s_output, real_target)  # realfake
        c_errG = c_criterion(c_output, real_label)  # class
        errG = s_errG + c_errG
        errG.backward()
        D_G_z2 = s_output.data.mean()
        
        optimizerG.step()

        # Save Losses for plotting later
        G_losses.append(errG.item())
        D_losses.append(errD.item())

        
        iters += 1

    # scheduler.step(errD.item())

  
    
    print('[%d/%d][%d/%d]\nLoss_D: %.4f\tLoss_G: %.4f\nD(x): %.4f\tD(G(z)): %.4f / %.4f'
          % (epoch+1, num_epochs, i+1, len(dataloader),
             errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
    
    show_generated_img(num_show)
  
   
    # --------- save fake image  ----------
    fake_image = netG(fixed_noise_label) 
    vutils.save_image(fake_image.detach(), '{}/fake_samples_epoch_{:03d}.png'.format(outf, epoch + 1),
                    normalize=True, nrow=5)
    fake_img_list.append(fake_image) 
  




Thank you for your response.
I’m working with acgan to generate image from noise.I am trying to save the images of mine as per the size of input image(299x299).For giving image_size =64 it works perfectly but when it’s changed the training part gives error.