-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodel.py
147 lines (121 loc) · 4.4 KB
/
model.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from module import *
class Generator32(nn.Module):
r"""
ResNet backbone generator for SNGAN.
Attributes:
nz (int): Noise dimension for upsampling.
ngf (int): Variable controlling generator feature map sizes.
bottom_width (int): Starting width for upsampling generator output to an image.
"""
def __init__(self, nz=128, ngf=256, bottom_width=4):
super().__init__()
self.l1 = nn.Linear(nz, (bottom_width ** 2) * ngf)
self.unfatten = nn.Unflatten(1, (ngf, bottom_width, bottom_width))
self.block2 = GBlock(ngf, ngf, upsample=True)
self.block3 = GBlock(ngf, ngf, upsample=True)
self.block4 = GBlock(ngf, ngf, upsample=True)
self.b5 = nn.BatchNorm2d(ngf)
self.c5 = nn.Conv2d(ngf, 3, 3, 1, padding=1)
self.activation = nn.ReLU(True)
nn.init.xavier_uniform_(self.l1.weight.data, 1.0)
nn.init.xavier_uniform_(self.c5.weight.data, 1.0)
def forward(self, x):
h = self.l1(x)
h = self.unfatten(h)
h = self.block2(h)
h = self.block3(h)
h = self.block4(h)
h = self.b5(h)
h = self.activation(h)
h = self.c5(h)
y = torch.tanh(h)
return y
class Discriminator32(nn.Module):
r"""
ResNet backbone discriminator for SNGAN.
Attributes:
ndf (int): Variable controlling discriminator feature map sizes.
"""
def __init__(self, ndf=128):
super().__init__()
self.block1 = DBlockOptimized(3, ndf)
self.block2 = DBlock(ndf, ndf, downsample=True)
self.block3 = DBlock(ndf, ndf, downsample=False)
self.block4 = DBlock(ndf, ndf, downsample=False)
self.l5 = SNLinear(ndf, 1)
self.activation = nn.ReLU(True)
nn.init.xavier_uniform_(self.l5.weight.data, 1.0)
def forward(self, x):
h = x
h = self.block1(h)
h = self.block2(h)
h = self.block3(h)
h = self.block4(h)
h = self.activation(h)
h = torch.sum(h, dim=(2, 3))
y = self.l5(h)
return y
class Generator64(nn.Module):
r"""
ResNet backbone generator for SNGAN.
Attributes:
nz (int): Noise dimension for upsampling.
ngf (int): Variable controlling generator feature map sizes.
bottom_width (int): Starting width for upsampling generator output to an image.
"""
def __init__(self, nz=128, ngf=1024, bottom_width=4):
super().__init__()
self.l1 = nn.Linear(nz, (bottom_width ** 2) * ngf)
self.unfatten = nn.Unflatten(1, (ngf, bottom_width, bottom_width))
self.block2 = GBlock(ngf, ngf >> 1, upsample=True)
self.block3 = GBlock(ngf >> 1, ngf >> 2, upsample=True)
self.block4 = GBlock(ngf >> 2, ngf >> 3, upsample=True)
self.block5 = GBlock(ngf >> 3, ngf >> 4, upsample=True)
self.b6 = nn.BatchNorm2d(ngf >> 4)
self.c6 = nn.Conv2d(ngf >> 4, 3, 3, 1, padding=1)
self.activation = nn.ReLU(True)
nn.init.xavier_uniform_(self.l1.weight.data, 1.0)
nn.init.xavier_uniform_(self.c6.weight.data, 1.0)
def forward(self, x):
h = self.l1(x)
h = self.unfatten(h)
h = self.block2(h)
h = self.block3(h)
h = self.block4(h)
h = self.block5(h)
h = self.b6(h)
h = self.activation(h)
h = self.c6(h)
y = torch.tanh(h)
return y
class Discriminator64(nn.Module):
r"""
ResNet backbone discriminator for SNGAN.
Attributes:
ndf (int): Variable controlling discriminator feature map sizes.
"""
def __init__(self, ndf=1024):
super().__init__()
self.block1 = DBlockOptimized(3, ndf >> 4)
self.block2 = DBlock(ndf >> 4, ndf >> 3, downsample=True)
self.block3 = DBlock(ndf >> 3, ndf >> 2, downsample=True)
self.block4 = DBlock(ndf >> 2, ndf >> 1, downsample=True)
self.block5 = DBlock(ndf >> 1, ndf, downsample=True)
self.l6 = SNLinear(ndf, 1)
self.activation = nn.ReLU(True)
nn.init.xavier_uniform_(self.l6.weight.data, 1.0)
def forward(self, x):
h = x
h = self.block1(h)
h = self.block2(h)
h = self.block3(h)
h = self.block4(h)
h = self.block5(h)
h = self.activation(h)
h = torch.sum(h, dim=(2, 3))
y = self.l6(h)
return y