forked from nashory/pggan-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_layers.py
187 lines (150 loc) · 7.53 KB
/
custom_layers.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
from PIL import Image
import copy
from torch.nn.init import kaiming_normal, calculate_gain
# same function as ConcatTable container in Torch7.
class ConcatTable(nn.Module):
def __init__(self, layer1, layer2):
super(ConcatTable, self).__init__()
self.layer1 = layer1
self.layer2 = layer2
def forward(self,x):
y = [self.layer1(x), self.layer2(x)]
return y
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
return x.view(x.size(0), -1)
class fadein_layer(nn.Module):
def __init__(self, config):
super(fadein_layer, self).__init__()
self.alpha = 0.0
def update_alpha(self, delta):
self.alpha = self.alpha + delta
self.alpha = max(0, min(self.alpha, 1.0))
# input : [x_low, x_high] from ConcatTable()
def forward(self, x):
return torch.add(x[0].mul(1.0-self.alpha), x[1].mul(self.alpha))
# https://github.com/github-pengge/PyTorch-progressive_growing_of_gans/blob/master/models/base_model.py
class minibatch_std_concat_layer(nn.Module):
def __init__(self, averaging='all'):
super(minibatch_std_concat_layer, self).__init__()
self.averaging = averaging.lower()
if 'group' in self.averaging:
self.n = int(self.averaging[5:])
else:
assert self.averaging in ['all', 'flat', 'spatial', 'none', 'gpool'], 'Invalid averaging mode'%self.averaging
self.adjusted_std = lambda x, **kwargs: torch.sqrt(torch.mean((x - torch.mean(x, **kwargs)) ** 2, **kwargs) + 1e-8)
def forward(self, x):
shape = list(x.size())
target_shape = copy.deepcopy(shape)
vals = self.adjusted_std(x, dim=0, keepdim=True)
if self.averaging == 'all':
target_shape[1] = 1
vals = torch.mean(vals, dim=1, keepdim=True)
elif self.averaging == 'spatial':
if len(shape) == 4:
vals = mean(vals, axis=[2,3], keepdim=True) # torch.mean(torch.mean(vals, 2, keepdim=True), 3, keepdim=True)
elif self.averaging == 'none':
target_shape = [target_shape[0]] + [s for s in target_shape[1:]]
elif self.averaging == 'gpool':
if len(shape) == 4:
vals = mean(x, [0,2,3], keepdim=True) # torch.mean(torch.mean(torch.mean(x, 2, keepdim=True), 3, keepdim=True), 0, keepdim=True)
elif self.averaging == 'flat':
target_shape[1] = 1
vals = torch.FloatTensor([self.adjusted_std(x)])
else: # self.averaging == 'group'
target_shape[1] = self.n
vals = vals.view(self.n, self.shape[1]/self.n, self.shape[2], self.shape[3])
vals = mean(vals, axis=0, keepdim=True).view(1, self.n, 1, 1)
vals = vals.expand(*target_shape)
return torch.cat([x, vals], 1)
def __repr__(self):
return self.__class__.__name__ + '(averaging = %s)' % (self.averaging)
class pixelwise_norm_layer(nn.Module):
def __init__(self):
super(pixelwise_norm_layer, self).__init__()
self.eps = 1e-8
def forward(self, x):
return x / (torch.mean(x**2, dim=1, keepdim=True) + self.eps) ** 0.5
# for equaliaeed-learning rate.
class equalized_conv2d(nn.Module):
def __init__(self, c_in, c_out, k_size, stride, pad, initializer='kaiming', bias=False):
super(equalized_conv2d, self).__init__()
self.conv = nn.Conv2d(c_in, c_out, k_size, stride, pad, bias=False)
if initializer == 'kaiming': kaiming_normal(self.conv.weight, a=calculate_gain('conv2d'))
elif initializer == 'xavier': xavier_normal(self.conv.weight)
conv_w = self.conv.weight.data.clone()
self.bias = torch.nn.Parameter(torch.FloatTensor(c_out).fill_(0))
self.scale = (torch.mean(self.conv.weight.data ** 2)) ** 0.5
self.conv.weight.data.copy_(self.conv.weight.data/self.scale)
def forward(self, x):
x = self.conv(x.mul(self.scale))
return x + self.bias.view(1,-1,1,1).expand_as(x)
class equalized_deconv2d(nn.Module):
def __init__(self, c_in, c_out, k_size, stride, pad, initializer='kaiming'):
super(equalized_deconv2d, self).__init__()
self.deconv = nn.ConvTranspose2d(c_in, c_out, k_size, stride, pad, bias=False)
if initializer == 'kaiming': kaiming_normal(self.deconv.weight, a=calculate_gain('conv2d'))
elif initializer == 'xavier': xavier_normal(self.deconv.weight)
deconv_w = self.deconv.weight.data.clone()
self.bias = torch.nn.Parameter(torch.FloatTensor(c_out).fill_(0))
self.scale = (torch.mean(self.deconv.weight.data ** 2)) ** 0.5
self.deconv.weight.data.copy_(self.deconv.weight.data/self.scale)
def forward(self, x):
x = self.deconv(x.mul(self.scale))
return x + self.bias.view(1,-1,1,1).expand_as(x)
class equalized_linear(nn.Module):
def __init__(self, c_in, c_out, initializer='kaiming'):
super(equalized_linear, self).__init__()
self.linear = nn.Linear(c_in, c_out, bias=False)
if initializer == 'kaiming': kaiming_normal(self.linear.weight, a=calculate_gain('linear'))
elif initializer == 'xavier': torch.nn.init.xavier_normal(self.linear.weight)
linear_w = self.linear.weight.data.clone()
self.bias = torch.nn.Parameter(torch.FloatTensor(c_out).fill_(0))
self.scale = (torch.mean(self.linear.weight.data ** 2)) ** 0.5
self.linear.weight.data.copy_(self.linear.weight.data/self.scale)
def forward(self, x):
x = self.linear(x.mul(self.scale))
return x + self.bias.view(1,-1).expand_as(x)
# ref: https://github.com/github-pengge/PyTorch-progressive_growing_of_gans/blob/master/models/base_model.py
class generalized_drop_out(nn.Module):
def __init__(self, mode='mul', strength=0.4, axes=(0,1), normalize=False):
super(generalized_drop_out, self).__init__()
self.mode = mode.lower()
assert self.mode in ['mul', 'drop', 'prop'], 'Invalid GDropLayer mode'%mode
self.strength = strength
self.axes = [axes] if isinstance(axes, int) else list(axes)
self.normalize = normalize
self.gain = None
def forward(self, x, deterministic=False):
if deterministic or not self.strength:
return x
rnd_shape = [s if axis in self.axes else 1 for axis, s in enumerate(x.size())] # [x.size(axis) for axis in self.axes]
if self.mode == 'drop':
p = 1 - self.strength
rnd = np.random.binomial(1, p=p, size=rnd_shape) / p
elif self.mode == 'mul':
rnd = (1 + self.strength) ** np.random.normal(size=rnd_shape)
else:
coef = self.strength * x.size(1) ** 0.5
rnd = np.random.normal(size=rnd_shape) * coef + 1
if self.normalize:
rnd = rnd / np.linalg.norm(rnd, keepdims=True)
rnd = Variable(torch.from_numpy(rnd).type(x.data.type()))
if x.is_cuda:
rnd = rnd.cuda()
return x * rnd
def __repr__(self):
param_str = '(mode = %s, strength = %s, axes = %s, normalize = %s)' % (self.mode, self.strength, self.axes, self.normalize)
return self.__class__.__name__ + param_str