-
Notifications
You must be signed in to change notification settings - Fork 46
/
models.py
110 lines (101 loc) · 4.21 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
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
import torch
import torch.nn as nn
class Discriminator(nn.Module):
"""
Discriminator module:
Especially for Celeb-A dataset, otherwise cls vector size will vary.
Args:
Image Tensor (-1,3,128,128)
Output:
src (float): Probability between 0 and 1, discriminates whether source is real or fake.
cls (tensor, shape(c_dims,)): Returns class-wise probability, similar to that of AC-GAN (Goodfellow et.al).
"""
def __init__(self,c_dims):
super().__init__()
self.input_layer=nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=64,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.hidden1=nn.Sequential(
nn.Conv2d(in_channels=64,out_channels=128,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.hidden2=nn.Sequential(
nn.Conv2d(in_channels=128,out_channels=256,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.hidden3=nn.Sequential(
nn.Conv2d(in_channels=256,out_channels=512,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.hidden4=nn.Sequential(
nn.Conv2d(in_channels=512,out_channels=1024,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.hidden5=nn.Sequential(
nn.Conv2d(in_channels=1024,out_channels=2048,kernel_size=(4,4),stride=2,padding=1),
nn.LeakyReLU()
)
self.src=nn.Sequential(
nn.Conv2d(in_channels=2048,out_channels=1,kernel_size=(3,3),stride=1,padding=1,bias=False)
)
self.cls=nn.Sequential(
nn.Conv2d(in_channels=2048,out_channels=c_dims,kernel_size=(1,1),stride=1,padding=0,bias=False)
)
def forward(self,x):
bsize=x.size(0)
x=self.input_layer(x)
x=self.hidden1(x)
x=self.hidden2(x)
x=self.hidden3(x)
x=self.hidden4(x)
x=self.hidden5(x)
src=self.src(x)
cls=self.cls(x).squeeze()
return src,cls
class Generator(nn.Module):
"""
Generator module:
Args:
x : Image Tensor (-1,3,128,128)
c : Label Tensor (-1,c_dims)
Output:
Image Tensor (-1,3,128,128)
"""
def __init__(self,c_dims):
super().__init__()
self.down_sample=nn.Sequential(
nn.Conv2d(in_channels=3+c_dims,out_channels=64,kernel_size=(7,7),stride=1,padding=3,bias=False),
nn.InstanceNorm2d(64,affine=True,track_running_stats=True),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64,out_channels=128,kernel_size=(4,4),stride=2,padding=1,bias=False),
nn.InstanceNorm2d(128,affine=True,track_running_stats=True),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128,out_channels=256,kernel_size=(4,4),stride=2,padding=1,bias=False),
nn.InstanceNorm2d(256,affine=True,track_running_stats=True),
nn.ReLU(inplace=True),
)
bottle_neck=[]
for _ in range(2):
bottle_neck.append(nn.Conv2d(in_channels=256,out_channels=256,kernel_size=(3,3),stride=1,padding=1,bias=False))
bottle_neck.append(nn.InstanceNorm2d(256,affine=True,track_running_stats=True))
bottle_neck.append(nn.ReLU(inplace=True))
self.bottleneck=nn.Sequential(*bottle_neck)
self.up_sample=nn.Sequential(
nn.ConvTranspose2d(in_channels=256,out_channels=128,kernel_size=(4,4),stride=2,padding=1,bias=False),
nn.InstanceNorm2d(128,affine=True,track_running_stats=True),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=128,out_channels=64,kernel_size=(4,4),stride=2,padding=1,bias=False),
nn.InstanceNorm2d(64,affine=True,track_running_stats=True),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=64,out_channels=3,kernel_size=(7,7),stride=1,padding=3,bias=False),
nn.Tanh(),
)
def forward(self,x,c):
c=c.view(c.size(0),c.size(1),1,1).float()
c=c.repeat(1,1,x.size(2),x.size(3))
x=torch.cat((x,c),dim=1)
x=self.down_sample(x)
x=self.bottleneck(x)
x=self.up_sample(x)
return x