CGAN自动生成动漫头像代码PyTorch
时间: 2024-01-15 11:04:02 浏览: 87
下面是一个使用PyTorch实现的CGAN自动生成动漫头像的代码示例:
```python
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
# Hyper Parameters
num_epochs = 200
batch_size = 100
learning_rate = 0.0002
# Image Preprocessing
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])
])
# MNIST Dataset
train_dataset = dsets.ImageFolder(root='./data', transform=transform)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
# Discriminator Model
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 4, 2, 1)
self.conv2 = nn.Conv2d(64, 128, 4, 2, 1)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 256, 4, 2, 1)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 512, 4, 2, 1)
self.bn4 = nn.BatchNorm2d(512)
self.conv5 = nn.Conv2d(512, 1, 4, 1, 0)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = nn.functional.leaky_relu(self.conv1(x), 0.2, inplace=True)
x = nn.functional.leaky_relu(self.bn2(self.conv2(x)), 0.2, inplace=True)
x = nn.functional.leaky_relu(self.bn3(self.conv3(x)), 0.2, inplace=True)
x = nn.functional.leaky_relu(self.bn4(self.conv4(x)), 0.2, inplace=True)
x = self.sigmoid(self.conv5(x))
return x
# Generator Model
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.linear = nn.Linear(100, 512 * 4 * 4)
self.bn1 = nn.BatchNorm2d(512)
self.deconv1 = nn.ConvTranspose2d(512, 256, 4, 2, 1)
self.bn2 = nn.BatchNorm2d(256)
self.deconv2 = nn.ConvTranspose2d(256, 128, 4, 2, 1)
self.bn3 = nn.BatchNorm2d(128)
self.deconv3 = nn.ConvTranspose2d(128, 64, 4, 2, 1)
self.bn4 = nn.BatchNorm2d(64)
self.deconv4 = nn.ConvTranspose2d(64, 3, 4, 2, 1)
self.tanh = nn.Tanh()
def forward(self, x):
x = nn.functional.relu(self.bn1(self.linear(x).view(-1, 512, 4, 4)))
x = nn.functional.relu(self.bn2(self.deconv1(x)))
x = nn.functional.relu(self.bn3(self.deconv2(x)))
x = nn.functional.relu(self.bn4(self.deconv3(x)))
x = self.tanh(self.deconv4(x))
return x
# Discriminator and Generator
D = Discriminator()
G = Generator()
# Loss Functions
criterion = nn.BCELoss()
# Optimizers
D_optimizer = torch.optim.Adam(D.parameters(), lr=learning_rate)
G_optimizer = torch.optim.Adam(G.parameters(), lr=learning_rate)
# Training
for epoch in range(num_epochs):
for i, (images, _) in enumerate(train_loader):
batch_size = images.size(0)
# Real Images
real_images = Variable(images)
real_labels = Variable(torch.ones(batch_size))
# Fake Images
z = Variable(torch.randn(batch_size, 100))
fake_images = G(z)
fake_labels = Variable(torch.zeros(batch_size))
# Train Discriminator
D_optimizer.zero_grad()
real_outputs = D(real_images)
real_loss = criterion(real_outputs, real_labels)
fake_outputs = D(fake_images)
fake_loss = criterion(fake_outputs, fake_labels)
D_loss = real_loss + fake_loss
D_loss.backward()
D_optimizer.step()
# Train Generator
G_optimizer.zero_grad()
z = Variable(torch.randn(batch_size, 100))
fake_images = G(z)
outputs = D(fake_images)
G_loss = criterion(outputs, real_labels)
G_loss.backward()
G_optimizer.step()
# Print Losses
if (i + 1) % 10 == 0:
print("Epoch [{}/{}], Step [{}/{}], D Loss: {:.4f}, G Loss: {:.4f}"
.format(epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, D_loss.data.item(), G_loss.data.item()))
# Save Generated Images
fake_images = G(z)
torchvision.utils.save_image(fake_images.data, './generated_images/{}.png'.format(epoch + 1), nrow=10)
```
在这个代码中,我们使用了PyTorch框架来实现CGAN模型。我们首先定义了一个Discriminator模型和一个Generator模型,并且使用BCELoss作为损失函数,使用Adam优化器来进行模型训练。在训练过程中,我们首先训练Discriminator模型,然后训练Generator模型,并且每个epoch保存生成的图片。
阅读全文