Home Artificial Intelligence “The Birth of Artificial Creativity: How GANs are Redefining the Boundaries of Digital Art, and Shaping the Way forward for Creative Industries!” Index What could occur if the Turing test had one other artificial intelligence as judge as a substitute of a human? Architecture of GAN The characters within the story The History of GANs The right way to implement a GAN with Pytorch THE DATASET The goal of this GAN Possible Future scenarios Thanks

“The Birth of Artificial Creativity: How GANs are Redefining the Boundaries of Digital Art, and Shaping the Way forward for Creative Industries!” Index What could occur if the Turing test had one other artificial intelligence as judge as a substitute of a human? Architecture of GAN The characters within the story The History of GANs The right way to implement a GAN with Pytorch THE DATASET The goal of this GAN Possible Future scenarios Thanks

2
“The Birth of Artificial Creativity: How GANs are Redefining the Boundaries of Digital Art, and Shaping the Way forward for Creative Industries!”
Index
What could occur if the Turing test had one other artificial intelligence as judge as a substitute of a human?
Architecture of GAN
The characters within the story
The History of GANs
The right way to implement a GAN with Pytorch
THE DATASET
The goal of this GAN
Possible Future scenarios
Thanks

Fig: Architecture of GAN Img Ref: Google Developers
picture generated by dall-e 2note the paint brush

The Story

Ian Goodfellow https://en.wikipedia.org/wiki/File:Ian_Goodfellow.jpg
PICTURE GENERATED BY DALL-E 2

The right way to Create a GAN in PyTorch: A Step-by-Step Guide with CIFAR-10 Dataset

import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.nn as nn
import torch.optim as optim
import torchvision.utils as vutils
import matplotlib.pyplot as plt
transform = transforms.Compose([transforms.Resize(64),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])

train_set = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

batch_size = 128
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2)

class Generator(nn.Module):
def __init__(self, nz, ngf, nc):
super(Generator, self).__init__()
self.predominant = nn.Sequential(
nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
)

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

class Discriminator(nn.Module):
def __init__(self, ndf, nc):
super(Discriminator, self).__init__()
self.predominant = nn.Sequential(
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)

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

nz = 100 
ngf = 64
ndf = 64
nc = 3
# Initialize the generator and discriminator networks
netG = Generator(nz, ngf, nc)
netD = Discriminator(ndf, nc).to(device)

# define the loss function
criterion = nn.BCELoss() # Funzione di perdita utilizzata per addestrare la GAN

# define the optimizers.
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))

# Definition of GAN parameters
img_list = []
fixed_noise = torch.randn(64, nz, 1, 1, device=device)

num_epochs = 100
batch_size = 256
lr = 0.0002
real_label = 1
fake_label = 0

# Defining the dataloader for the image dataset
dataloader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True)

# Defining the loss function and optimisers for the 2 networks
criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(0.5, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(0.5, 0.999))

# Move the GAN network to the GPU, if available
netG.to(device)

# GAN training
for epoch in range(num_epochs):
for i, data in enumerate(dataloader, 0):
# discriminator training
netD.zero_grad()
real_images = data[0].to(device) # move the actual picture to GPU
b_size = real_images.size(0)
label = torch.full((b_size,), real_label, device=device).float()

output = netD(real_images)
errD_real = criterion(output, label)
errD_real.backward()
D_x = output.mean().item()

noise = torch.randn(b_size, nz, 1, 1, device=device, dtype=torch.float32)

fake = netG(noise)
label.fill_(real_label)

output = netD(fake.detach())
errD_fake = criterion(output, label)
errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()

# Generator training
netG.zero_grad()
label.fill_(real_label)
output = netD(fake)
errG = criterion(output, label)
errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()

# print of the result
if i % 100 == 0:
print('[%d/%d][%d/%d]tLoss_D: %.4ftLoss_G: %.4ftD(x): %.4ftD(G(z)): %.4f / %.4f'
% (epoch, num_epochs, i, len(dataloader),
errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))

# Adds generated images to the list
if (i % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)):
with torch.no_grad():
fake_images = netG(fixed_noise).detach().cpu()
img_list.append(vutils.make_grid(fake_images, padding=2, normalize=True))

#Saving a picture generated at the tip of every epoch
with torch.no_grad():
fixed_noise = fixed_noise.to(device) # Move the noise to the GPU
fake = netG(fixed_noise).detach().cpu()
img_list.append(vutils.make_grid(fake, padding=2, normalize=True))
# Generate and save images with the trained generator

plt.imshow(img_list[-1].permute(1, 2, 0))
plt.show()

PICTURE GENERATED BY DALL-E 2

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here