Editing
Generative Adversarial Networks
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Building a simple DCGAN for MNIST digit generation:''' <syntaxhighlight lang="python"> import torch import torch.nn as nn # Generator: noise z β fake image class Generator(nn.Module): def __init__(self, latent_dim=100): super().__init__() self.net = nn.Sequential( nn.Linear(latent_dim, 256), nn.LeakyReLU(0.2), nn.BatchNorm1d(256), nn.Linear(256, 512), nn.LeakyReLU(0.2), nn.BatchNorm1d(512), nn.Linear(512, 784), # 28Γ28 image nn.Tanh() # Output range [-1, 1] ) def forward(self, z): return self.net(z).view(-1, 1, 28, 28) # Discriminator: image β real/fake probability class Discriminator(nn.Module): def __init__(self): super().__init__() self.net = nn.Sequential( nn.Flatten(), nn.Linear(784, 512), nn.LeakyReLU(0.2), nn.Dropout(0.3), nn.Linear(512, 256), nn.LeakyReLU(0.2), nn.Linear(256, 1), nn.Sigmoid() ) def forward(self, x): return self.net(x) G = Generator(); D = Discriminator() opt_G = torch.optim.Adam(G.parameters(), lr=2e-4, betas=(0.5, 0.999)) opt_D = torch.optim.Adam(D.parameters(), lr=2e-4, betas=(0.5, 0.999)) criterion = nn.BCELoss() def train_step(real_images, latent_dim=100): batch_size = real_images.size(0) real_labels = torch.ones(batch_size, 1) fake_labels = torch.zeros(batch_size, 1) # Train Discriminator z = torch.randn(batch_size, latent_dim) fake_images = G(z).detach() # detach: don't backprop into G yet loss_D = criterion(D(real_images), real_labels) + \ criterion(D(fake_images), fake_labels) opt_D.zero_grad(); loss_D.backward(); opt_D.step() # Train Generator z = torch.randn(batch_size, latent_dim) loss_G = criterion(D(G(z)), real_labels) # G wants D to say "real" opt_G.zero_grad(); loss_G.backward(); opt_G.step() return loss_D.item(), loss_G.item() </syntaxhighlight> ; GAN application landscape : '''Face generation''' β StyleGAN3 (NVIDIA) β photorealistic faces at 1024px : '''Image-to-image''' β Pix2Pix (paired), CycleGAN (unpaired) : '''Super resolution''' β SRGAN, ESRGAN β upscale low-res images : '''Video synthesis''' β Vid2Vid, StyleGAN-V : '''Data augmentation''' β Generate synthetic training data for rare classes : '''Medical imaging''' β Synthesize rare pathology images for training classifiers </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information