-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
156 lines (127 loc) · 5.98 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import os
import argparse
import torch
from torch.autograd import Variable
import torchvision.transforms as transforms
import random
from torch.utils.data import DataLoader
import logging
from torchvision import datasets
import torch.nn as nn
import torch.nn.functional as F
import torchvision.utils as vutils
# from torch.utils.tensorboard import SummaryWriter
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default='cifar10', help='cifar10 | lsun | mnist')
parser.add_argument('--dataroot', default='./data', help='path to data')
parser.add_argument('--batchSize', type=int, default=64, help='input batch size')
parser.add_argument('--imageSize', type=int, default=32, help='image size input')
parser.add_argument('--channels', type=int, default=3, help='number of channels')
parser.add_argument('--latentdim', type=int, default=100, help='size of latent vector')
parser.add_argument('--n_classes', type=int, default=10, help='number of classes in data set')
parser.add_argument('--epoch', type=int, default=200, help='number of epoch')
parser.add_argument('--lrate', type=float, default=0.0002, help='learning rate')
parser.add_argument('--beta', type=float, default=0.5, help='beta for adam optimizer')
parser.add_argument('--beta1', type=float, default=0.999, help='beta1 for adam optimizer')
parser.add_argument('--output', default='./output', help='folder to output images and model checkpoints')
parser.add_argument('--randomseed', type=int, help='seed')
opt = parser.parse_args()
img_shape = (opt.channels, opt.imageSize, opt.imageSize)
cuda = True if torch.cuda.is_available() else False
os.makedirs(opt.output, exist_ok=True)
if opt.randomseed is None:
opt.randomseed = random.randint(1, 10000)
random.seed(opt.randomseed)
torch.manual_seed(opt.randomseed)
# preprocessing for mnist, lsun, cifar10
if opt.dataset == 'mnist':
dataset = datasets.MNIST(root=opt.dataroot, train=True, download=True,
transform=transforms.Compose([transforms.Resize(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
elif opt.dataset == 'lsun':
dataset = datasets.LSUN(root=opt.dataroot, train=True, download=True,
transform=transforms.Compose([transforms.Resize(opt.imageSize),
transforms.CenterCrop(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))]))
elif opt.dataset == 'cifar10':
dataset = datasets.CIFAR10(root=opt.dataroot, train=True, download=True,
transform=transforms.Compose([transforms.Resize(opt.imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
assert dataset
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize, shuffle=True)
# building generator
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.label_embed = nn.Embedding(opt.n_classes, opt.n_classes)
self.depth = 128
def init(input, output, normalize=True):
layers = [nn.Linear(input, output)]
if normalize:
layers.append(nn.BatchNorm1d(output, 0.8))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.generator = nn.Sequential(
*init(opt.latentdim + opt.n_classes, self.depth),
*init(self.depth, self.depth * 2),
*init(self.depth * 2, self.depth * 4),
*init(self.depth * 4, self.depth * 8),
nn.Linear(self.depth * 8, int(np.prod(img_shape))),
nn.Tanh()
)
# torchcat needs to combine tensors
def forward(self, noise, labels):
gen_input = torch.cat((self.label_embed(labels), noise), -1)
img = self.generator(gen_input)
img = img.view(img.size(0), *img_shape)
return img
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.label_embed1 = nn.Embedding(opt.n_classes, opt.n_classes)
self.dropout = 0.4
self.depth = 512
def init(input, output, normalize=True):
layers = [nn.Linear(input, output)]
if normalize:
layers.append(nn.Dropout(self.dropout))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.discriminator = nn.Sequential(
*init(opt.n_classes+int(np.prod(img_shape)), self.depth, normalize=False),
*init(self.depth, self.depth),
*init(self.depth, self.depth),
nn.Linear(self.depth, 1),
nn.Sigmoid()
)
def forward(self, img, labels):
imgs = img.view(img.size(0),-1)
inpu = torch.cat((imgs, self.label_embed1(labels)), -1)
validity = self.discriminator(inpu)
return validity
generator = Generator()
discriminator = Discriminator()
# 加载已经训练好的模型参数
g_model_path = "./output/generator_epoch_199.pth" # 替换为训练好的模型的路径
d_model_path = "./output/discriminator_epoch_199.pth"
generator.load_state_dict(torch.load(g_model_path))
discriminator.load_state_dict(torch.load(d_model_path))
# 生成噪声和标签
noise = torch.randn(opt.batchSize, opt.latentdim)
labels = torch.randint(0, opt.n_classes, (opt.batchSize,))
# 将生成器设置为评估模式
generator.eval()
discriminator.eval()
# 使用生成器生成图像
with torch.no_grad():
generated_images = generator(noise, labels)
# 将生成的图像输入鉴别器并转换为类别概率
with torch.no_grad():
probabilities = discriminator(generated_images, labels)
save_path = os.path.join("tests", "test.png")
print("生成的图像的类别概率:", probabilities)
vutils.save_image(generated_images, save_path, normalize=True)