self attention gan 的全部代码,包括数据载入、模型代码、训练代码以及测试代码
时间: 2023-06-12 09:08:04 浏览: 96
GAN系列的代码,包含各种GAN结构
以下是一个简单的 Self-Attention GAN 的代码,包括数据载入、模型、训练和测试代码。
首先,我们需要导入必要的库:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
```
接下来,我们需要定义一些超参数:
```python
batch_size = 128
image_size = 64
nz = 100
ngf = 64
ndf = 64
num_epochs = 5
lr = 0.0002
beta1 = 0.5
ngpu = 1
```
然后,我们需要定义数据载入器:
```python
dataset = datasets.ImageFolder(root='data',
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=4)
```
接下来,我们需要定义生成器和判别器:
```python
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.main = 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, 3, 4, 2, 1, bias=False),
nn.Tanh()
)
def forward(self, input):
return self.main(input)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(3, 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.main(input)
```
接下来,我们需要初始化生成器和判别器:
```python
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
netG = Generator().to(device)
netD = Discriminator().to(device)
if (device.type == 'cuda') and (ngpu > 1):
netG = nn.DataParallel(netG, list(range(ngpu)))
netD = nn.DataParallel(netD, list(range(ngpu)))
```
然后,我们需要定义损失函数和优化器:
```python
criterion = nn.BCELoss()
fixed_noise = torch.randn(64, nz, 1, 1, device=device)
real_label = 1
fake_label = 0
optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999))
```
最后,我们可以开始训练模型:
```python
for epoch in range(num_epochs):
for i, data in enumerate(dataloader, 0):
netD.zero_grad()
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, device=device)
output = netD(real_cpu).view(-1)
errD_real = criterion(output, label)
errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(b_size, nz, 1, 1, device=device)
fake = netG(noise)
label.fill_(fake_label)
output = netD(fake.detach()).view(-1)
errD_fake = criterion(output, label)
errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
label.fill_(real_label)
output = netD(fake).view(-1)
errG = criterion(output, label)
errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if i % 50 == 0:
print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f'
% (epoch, num_epochs, i, len(dataloader),
errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
if (epoch == 0) and (i == 0):
torchvision.utils.save_image(real_cpu, 'real_samples.png', normalize=True)
if i == len(dataloader) - 1:
with torch.no_grad():
fake = netG(fixed_noise).detach().cpu()
torchvision.utils.save_image(fake, 'fake_samples_epoch_%03d.png' % epoch, normalize=True)
```
测试代码:
```python
import matplotlib.pyplot as plt
import numpy as np
def show_generated_img_all():
n_images=5
rows = 1
cols = 5
noise = torch.randn(n_images, nz, 1, 1).to(device)
fake_images = netG(noise)
fig, axes = plt.subplots(rows, cols, figsize=(15,5))
for i in range(rows):
for j in range(cols):
idx = i*cols+j
axes[j].imshow(np.transpose(fake_images[idx].detach().cpu().numpy(), (1,2,0)))
axes[j].axis('off')
plt.show()
show_generated_img_all()
```
以上就是一个简单的 Self-Attention GAN 的代码。其中,数据载入器可以根据自己的数据集进行修改,超参数也可以根据需要进行调整。
阅读全文