Skip to content

Commit

Permalink
Main commit
Browse files Browse the repository at this point in the history
commit for entire Image Generation using GANs  project including structured files.
  • Loading branch information
chiragHimself committed Jun 24, 2024
1 parent 6390347 commit 871005a
Show file tree
Hide file tree
Showing 29 changed files with 167 additions and 0 deletions.
131 changes: 131 additions & 0 deletions ImageGen using GANs/dcgan_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils

# Setting some hyperparameters
batchSize = 64
imageSize = 64

# Creating the transformations
transform = transforms.Compose([
transforms.Resize(imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])

# Loading the dataset
dataset = dset.CIFAR10(root='./data', download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batchSize, shuffle=True, num_workers=2)

# Defining the weights_init function that takes as input a neural network m and that will initialize all its weights.
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)

# Defining the generator
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(100, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
nn.Tanh()
)

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

# Creating the generator
netG = Generator()
netG.apply(weights_init)

# Defining the discriminator
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(3, 64, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(256, 512, 4, 2, 1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(512, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)

def forward(self, input):
return self.main(input).view(-1)

# Creating the discriminator
netD = Discriminator()
netD.apply(weights_init)

# Training the DCGANs
criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters(), lr=0.0002, betas=(0.5, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=0.0002, betas=(0.5, 0.999))

for epoch in range(25):
for i, data in enumerate(dataloader, 0):

# 1st Step: Updating the weights of the neural network of the discriminator
netD.zero_grad()

# Training the discriminator with a real image of the dataset
real, _ = data
input = real
target = torch.ones(input.size(0))
output = netD(input)
errD_real = criterion(output, target)

# Training the discriminator with a fake image generated by the generator
noise = torch.randn(input.size(0), 100, 1, 1)
fake = netG(noise)
target = torch.zeros(input.size(0))
output = netD(fake.detach())
errD_fake = criterion(output, target)

# Backpropagating the total error
errD = errD_real + errD_fake
errD.backward()
optimizerD.step()

# 2nd Step: Updating the weights of the neural network of the generator
netG.zero_grad()
target = torch.ones(input.size(0))
output = netD(fake)
errG = criterion(output, target)
errG.backward()
optimizerG.step()

# 3rd Step: Printing the losses and saving the real images and the generated images of the minibatch every 100 steps
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f' % (epoch, 25, i, len(dataloader), errD.item(), errG.item()))
if i % 100 == 0:
vutils.save_image(real, '%s/real_samples.png' % "./results", normalize=True)
fake = netG(noise)
vutils.save_image(fake.data, '%s/fake_samples_epoch_%03d.png' % ("./results", epoch), normalize=True)
36 changes: 36 additions & 0 deletions ImageGen using GANs/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# DCGAN: Deep Convolutional Generative Adversarial Network

Welcome to the Image generation using GANs, a deep convolutional generative adversarial network implemented in PyTorch! This project is designed to generate realistic images from random noise using the power of deep learning.

![DCGAN Image](https://github.com/chiragHimself/dcgan_random/raw/main/results/fake_samples_epoch_024.png)

## Overview
- **Project Name**: ImageGen using GANs
- **Description**: A deep convolutional generative adversarial network to generate realistic images.
- **Framework**: PyTorch 2.2.1
- **Training Device**: RTX 3050 Ti with CUDA 11.2
- **IDE**: Spyder (can be run on other IDEs and Google Colab)

## Dependencies
- `torch`
- `torch.nn`
- `torch.optim`
- `torch.utils.data`
- `torchvision.datasets`
- `torchvision.transforms`
- `torchvision.utils`

## Training Data
The training data for this project is obtained from the CIFAR-10 open dataset. It is downloaded to a local directory named `data`, where the training is conducted.

## Training Details
- **Epochs**: 25
- **Training Time**: Approximately 4 hours
- **Result**: various batch png's are included in the repository, showcasing the generated images after each epoch. Please note that this file will be overwritten if you run the code in your IDE.

## Generated Images
Here are some samples of the generated images produced by the DCGAN model:

![Generated Image 1](https://github.com/chiragHimself/dcgan_random/blob/main/results/fake_samples_epoch_000.png)
![Generated Image 2](https://github.com/chiragHimself/dcgan_random/blob/main/results/fake_samples_epoch_006.png)
![Generated Image 3](https://github.com/chiragHimself/dcgan_random/blob/main/results/fake_samples_epoch_024.png)
Binary file added ImageGen using GANs/results/.DS_Store
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ImageGen using GANs/results/real_samples.png

0 comments on commit 871005a

Please sign in to comment.