-
Notifications
You must be signed in to change notification settings - Fork 0
/
lip_convnets.py
178 lines (155 loc) · 7.49 KB
/
lip_convnets.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from cayley_ortho_conv import Cayley, CayleyLinear
from block_ortho_conv import BCOP
from skew_ortho_conv import SOC
from custom_activations import *
from utils import conv_mapping, activation_mapping
class NormalizedLinear(nn.Linear):
def forward(self, X):
X = X.view(X.shape[0], -1)
weight_norm = torch.norm(self.weight, dim=1, keepdim=True)
self.lln_weight = self.weight/weight_norm
return F.linear(X, self.lln_weight if self.training else self.lln_weight.detach(), self.bias)
class LipBlock(nn.Module):
def __init__(self, in_planes, planes, conv_layer, activation_name, stride=1, kernel_size=3):
super(LipBlock, self).__init__()
self.conv = conv_layer(in_planes, planes*stride, kernel_size=kernel_size,
stride=stride, padding=kernel_size//2)
self.activation = activation_mapping(activation_name, planes*stride)
def forward(self, x):
x = self.activation(self.conv(x))
return x
class LipConvNet(nn.Module):
def __init__(self, conv_name, activation, init_channels=32, block_size=1,
num_classes=10, input_side=32, lln=False):
super(LipConvNet, self).__init__()
self.lln = lln
self.in_planes = 3
conv_layer = conv_mapping[conv_name]
assert type(block_size) == int
self.layer1 = self._make_layer(init_channels, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer2 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer3 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer4 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer5 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=1)
flat_size = input_side // 32
flat_features = flat_size * flat_size * self.in_planes
if self.lln:
self.last_layer = NormalizedLinear(flat_features, num_classes)
elif conv_name == 'cayley':
self.last_layer = CayleyLinear(flat_features, num_classes)
else:
self.last_layer = conv_layer(flat_features, num_classes,
kernel_size=1, stride=1)
def _make_layer(self, planes, num_blocks, conv_layer, activation,
stride, kernel_size):
strides = [1]*(num_blocks-1) + [stride]
kernel_sizes = [3]*(num_blocks-1) + [kernel_size]
layers = []
for stride, kernel_size in zip(strides, kernel_sizes):
layers.append(LipBlock(self.in_planes, planes, conv_layer, activation,
stride, kernel_size))
self.in_planes = planes * stride
return nn.Sequential(*layers)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.last_layer(x)
x = x.view(x.shape[0], -1)
return x
class ResLayer(nn.Module):
def __init__(self, in_planes, planes, num_blocks, conv_layer, activation, stride, kernel_size):
super(ResLayer, self).__init__()
self.num_blocks = num_blocks
if self.num_blocks == 1:
self.block = LipBlock(in_planes, planes, conv_layer, activation, stride, kernel_size)
else:
self.in_block = LipBlock(in_planes, planes, conv_layer, activation, stride=1, kernel_size=3)
hidden_blocks = []
for _ in range(num_blocks-2):
cur_block = LipBlock(planes, planes, conv_layer, activation, stride=1, kernel_size=3)
hidden_blocks.append(cur_block)
self.hidden_block = nn.Sequential(*hidden_blocks)
self.out_block = LipBlock(planes, planes, conv_layer, activation, stride, kernel_size)
self.res_lamda_logit = nn.Parameter(torch.FloatTensor([0.0]))
self.res_lamda_logit.requires_grad = False # fixed lambda
def forward(self, x):
if self.num_blocks == 1:
x = self.block(x)
else:
x = self.in_block(x)
for block in self.hidden_block:
ret = block(x)
res_lamda = torch.sigmoid(self.res_lamda_logit)
x = res_lamda*x+(1-res_lamda)*ret
x = self.out_block(x)
return x
class LipResNet(nn.Module):
def __init__(self, conv_name, activation, init_channels=32, block_size=1,
num_classes=10, input_side=32, lln=False):
super(LipResNet, self).__init__()
self.lln = lln
self.in_planes = 3
self.conv_name = conv_name
self.input_side = input_side
conv_layer = conv_mapping[conv_name]
assert type(block_size) == int
self.layer1 = self._make_layer(init_channels, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer2 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer3 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer4 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=3)
self.layer5 = self._make_layer(self.in_planes, block_size, conv_layer,
activation, stride=2, kernel_size=1)
flat_size = input_side // 32
flat_features = flat_size * flat_size * self.in_planes
if self.lln:
self.last_layer = NormalizedLinear(flat_features, num_classes)
elif conv_name == 'cayley':
self.last_layer = CayleyLinear(flat_features, num_classes)
else:
self.last_layer = conv_layer(flat_features, num_classes,
kernel_size=1, stride=1)
def _make_layer(self, planes, num_blocks, conv_layer, activation,
stride, kernel_size):
layer = ResLayer(self.in_planes, planes, num_blocks, conv_layer, activation, stride, kernel_size)
self.in_planes = planes * stride
return layer
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = x.view(x.shape[0], -1, 1, 1)
x = self.last_layer(x)
x = x.view(x.shape[0], -1)
return x
def frozen_w_ortho(self,):
assert self.conv_name == 'LOT'
n = self.input_side
for layer in [self.layer1, self.layer2, self.layer3, self.layer4, self.layer5]:
if layer.num_blocks == 1:
layer.block.conv.frozen_w_ortho(n)
else:
layer.in_block.conv.frozen_w_ortho(n)
for hidden in layer.hidden_block:
hidden.conv.frozen_w_ortho(n)
layer.out_block.conv.frozen_w_ortho(n)
n = n // 2
if not self.lln:
self.last_layer.frozen_w_ortho(n)