-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblocks.py
164 lines (131 loc) · 5.51 KB
/
blocks.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
import math
import os
import torch
class StraightThrough(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return torch.sign(x)
@staticmethod
def backward(ctx, grad):
return grad.clamp(-1., 1.)
class STLayer(torch.nn.Module):
def __init__(self):
super(STLayer, self).__init__()
self.func = StraightThrough.apply
def forward(self, x):
return self.func(x)
class Linear(torch.nn.Module):
""" linear layer with optional batch normalization. """
def __init__(self, in_features, out_features, std=None, batch_norm=False, gain=None):
super(Linear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = torch.nn.Parameter(torch.Tensor(out_features, in_features))
self.bias = torch.nn.Parameter(torch.Tensor(out_features))
if batch_norm:
self.batch_norm = torch.nn.BatchNorm1d(num_features=self.out_features)
if std is not None:
self.weight.data.normal_(0., std)
self.bias.data.normal_(0., std)
else:
# defaults to linear activation
if gain is None:
gain = 1
stdv = math.sqrt(gain / self.weight.size(1))
self.weight.data.normal_(0., stdv)
self.bias.data.zero_()
def forward(self, x):
x = torch.nn.functional.linear(x, self.weight, self.bias)
if hasattr(self, "batch_norm"):
x = self.batch_norm(x)
return x
def extra_repr(self):
return "in_features={}, out_features={}".format(self.in_features, self.out_features)
class MLP(torch.nn.Module):
""" multi-layer perceptron with batch norm option """
def __init__(self, layer_info, activation=torch.nn.ReLU(), std=None, batch_norm=False, indrop=None, hiddrop=None):
super(MLP, self).__init__()
layers = []
in_dim = layer_info[0]
for i, unit in enumerate(layer_info[1:-1]):
if i == 0 and indrop:
layers.append(torch.nn.Dropout(indrop))
elif i > 0 and hiddrop:
layers.append(torch.nn.Dropout(hiddrop))
layers.append(Linear(in_features=in_dim, out_features=unit, std=std, batch_norm=batch_norm, gain=2))
layers.append(activation)
in_dim = unit
layers.append(Linear(in_features=in_dim, out_features=layer_info[-1], batch_norm=False))
self.layers = torch.nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
def load(self, path, name):
state_dict = torch.load(os.path.join(path, name+".ckpt"))
self.load_state_dict(state_dict)
def save(self, path, name):
dv = self.layers[-1].weight.device
if not os.path.exists(path):
os.makedirs(path)
torch.save(self.cpu().state_dict(), os.path.join(path, name+".ckpt"))
self.train().to(dv)
class ConvBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, std=None, bias=True,
batch_norm=False):
super(ConvBlock, self).__init__()
self.block = [torch.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=bias)]
if batch_norm:
self.block.append(torch.nn.BatchNorm2d(out_channels))
self.block.append(torch.nn.ReLU())
if std is not None:
self.block[0].weight.data.normal_(0., std)
self.block[0].bias.data.normal_(0., std)
self.block = torch.nn.Sequential(*self.block)
def forward(self, x):
return self.block(x)
class Flatten(torch.nn.Module):
def __init__(self, dims):
super(Flatten, self).__init__()
self.dims = dims
def forward(self, x):
dim = 1
for d in self.dims:
dim *= x.shape[d]
return x.reshape(-1, dim)
def extra_repr(self):
return "dims=[" + ", ".join(list(map(str, self.dims))) + "]"
class Avg(torch.nn.Module):
def __init__(self, dims):
super(Avg, self).__init__()
self.dims = dims
def forward(self, x):
return x.mean(dim=self.dims)
def extra_repr(self):
return "dims=[" + ", ".join(list(map(str, self.dims))) + "]"
def build_encoder(opts, level):
if level == 1:
code_dim = opts["code1_dim"]
else:
code_dim = opts["code2_dim"]
if opts["cnn"]:
L = len(opts["filters"+str(level)])-1
stride = 2
encoder = []
for i in range(L):
encoder.append(ConvBlock(in_channels=opts["filters"+str(level)][i],
out_channels=opts["filters"+str(level)][i+1],
kernel_size=3, stride=1, padding=1, batch_norm=opts["batch_norm"]))
encoder.append(ConvBlock(in_channels=opts["filters"+str(level)][i+1],
out_channels=opts["filters"+str(level)][i+1],
kernel_size=3, stride=stride, padding=1, batch_norm=opts["batch_norm"]))
encoder.append(Avg([2, 3]))
encoder.append(MLP([opts["filters"+str(level)][-1], code_dim]))
encoder.append(STLayer())
else:
encoder = [
Flatten([1, 2, 3]),
MLP([[opts["size"]**2] + [opts["hidden_dim"]]*opts["depth"] + [code_dim]],
batch_norm=opts["batch_norm"]),
STLayer()]
encoder = torch.nn.Sequential(*encoder)
return encoder