-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnets.py
178 lines (152 loc) · 4.95 KB
/
nets.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
import efficientnet_pytorch
import torch
from torch import nn
from torch.nn import functional as F
class Normalize(nn.Module):
def __init__(self, power=2):
super(Normalize, self).__init__()
self.power = power
def forward(self, x):
norm = x.pow(self.power).sum(1, keepdim=True).pow(1. / self.power)
out = x.div(norm)
return out
class LinearClassifier(nn.Module):
def __init__(self, in_features, out_features):
super(LinearClassifier, self).__init__()
self.net = nn.Sequential(
nn.Linear(in_features, 100),
nn.BatchNorm1d(100),
nn.ReLU(),
nn.Linear(100, out_features),
)
def forward(self, x):
return self.net(x)
class HalfAlexNet(nn.Module):
def __init__(self, in_channel, feat_dim, pool_type):
super(HalfAlexNet, self).__init__()
self.pool_type = pool_type
self.conv_block_1 = nn.Sequential(
nn.Conv2d(in_channel, 48, 3, 1, 1, bias=False), # 64 -> 64
nn.BatchNorm2d(48),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2), # 64 -> 31
)
self.conv_block_2 = nn.Sequential(
nn.Conv2d(48, 96, 3, 1, 1, bias=False), # 31 -> 31
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2), # 31 -> 15
)
self.conv_block_3 = nn.Sequential(
nn.Conv2d(96, 192, 3, 1, 1, bias=False), # 15 -> 15
nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
)
self.conv_block_4 = nn.Sequential(
nn.Conv2d(192, 192, 3, 1, 1, bias=False), # 15 -> 15
nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
)
self.conv_block_5 = nn.Sequential(
nn.Conv2d(192, 96, 3, 1, 1, bias=False), # 15 -> 15
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2), # 15 -> 7
)
self.fc6 = nn.Sequential(
nn.Linear(96 * 7 * 7, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(inplace=True),
)
self.fc7 = nn.Sequential(
nn.Linear(2048, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(inplace=True),
)
self.fc8 = nn.Sequential(
nn.Linear(2048, feat_dim)
)
self.l2norm = Normalize(2)
def pool_flatten(self, x, pool_size):
if self.pool_type == 'max':
x = F.adaptive_max_pool2d(x, (pool_size, pool_size))
elif self.pool_type == 'avg':
x = F.adaptive_avg_pool2d(x, (pool_size, pool_size))
else:
raise NotImplementedError()
x = x.view(x.shape[0], -1)
return x
def forward(self, x, layer):
x = self.conv_block_1(x)
if layer == 1:
x = self.pool_flatten(x, pool_size=15)
return x
x = self.conv_block_2(x)
if layer == 2:
x = self.pool_flatten(x, pool_size=10)
return x
x = self.conv_block_3(x)
if layer == 3:
x = self.pool_flatten(x, pool_size=5)
return x
x = self.conv_block_4(x)
if layer == 4:
x = self.pool_flatten(x, pool_size=5)
return x
x = self.conv_block_5(x)
x = torch.flatten(x, start_dim=1)
if layer == 5:
return x
x = self.fc6(x)
if layer == 6:
return x
x = self.fc7(x)
if layer == 7:
return x
x = self.fc8(x)
x = self.l2norm(x)
return x
def output_dim(self, layer):
if layer == 1:
pool_size = 15
n_channels = 48
elif layer == 2:
pool_size = 10
n_channels = 96
elif layer == 3:
pool_size = 5
n_channels = 192
elif layer == 4:
pool_size = 5
n_channels = 192
elif layer == 5:
pool_size = 7
n_channels = 96
elif layer == 6:
return 2048
elif layer == 7:
return 2048
else:
raise NotImplementedError()
return n_channels * pool_size * pool_size
class EfficientNet(nn.Module):
def __init__(self, in_channel, feat_dim):
super(EfficientNet, self).__init__()
self.in_channels = in_channel
self.feat_dim = feat_dim
self.encoder = efficientnet_pytorch.EfficientNet.from_name('efficientnet-b0', in_channels=in_channel, num_classes=feat_dim)
self.l2norm = Normalize(2)
self.flatten = nn.Sequential(nn.AdaptiveAvgPool2d(1), nn.Flatten())
def forward(self, x, layer):
if layer == 15:
x = self.encoder.extract_features(x)
x = self.flatten(x)
return x
x = self.encoder(x)
x = self.l2norm(x)
return x
def output_dim(self, layer):
if layer == 15:
return 1280
else:
return self.feat_dim