-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
82 lines (74 loc) · 2.7 KB
/
models.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
import torch.nn as nn
# custom weights initialization called on ``netG`` and ``netD``
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
class discriminator(nn.Module):
def __init__(self, nc, ndf):
super(discriminator, self).__init__()
self.main = nn.Sequential(
# input_size: nc x 64 x 64
nn.Conv2d(nc, ndf, 4, stride=2, padding=1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# size: ndf x 32 x 32
nn.Conv2d(ndf, ndf * 2, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# size: (ndf*2, 32, 32)
nn.Conv2d(ndf * 2, ndf * 4, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# size: (ndf*4, 8, 8)
nn.Conv2d(ndf * 4, ndf * 8, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# size: (ndf*8, 4, 4)
nn.Conv2d(ndf * 8, 1, 4, stride=1, padding=0, bias=False),
nn.Sigmoid()
)
def forward(self, x):
'''
:param x: 输入进来的虚假图片
:return: 主要是判断为真的概率
'''
return self.main(x)
class generator(nn.Module):
def __init__(self, nz, ngf, nc):
'''
:param nz: 噪声向量的通道数
:param ngf: generator中特征通道数
:param nc: 训练数据的通道数
'''
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),
# (ngf*8, 4, 4)
nn.ConvTranspose2d(ngf*8, ngf*4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf*4),
nn.ReLU(True),
# (ngf*4, 8, 8)
nn.ConvTranspose2d(ngf*4, ngf*2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf*2),
nn.ReLU(True),
# (ngf * 2, 16, 16)
nn.ConvTranspose2d(ngf*2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# (ngf, 32, 32)
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# (nc, 64, 64)
)
def forward(self, x):
'''
:param x: 噪声
:return: 虚构之后的图片
'''
x = self.main(x)
return x