-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresnetSEIR.py
184 lines (157 loc) · 6.88 KB
/
resnetSEIR.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
#!/usr/bin/env python
# encoding: utf-8
'''
@author: kevin
'''
import torch
from torch import nn
from collections import namedtuple
import time
cos_time = 0
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1, padding=0, bias=False)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1, padding=0, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
input = x
x = self.avg_pool(x)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return input * x
class BottleNeck_IR(nn.Module):
def __init__(self, in_channel, out_channel, stride):
super(BottleNeck_IR, self).__init__()
if in_channel == out_channel:
self.shortcut_layer = nn.MaxPool2d(1, stride)
else:
self.shortcut_layer = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=(1, 1), stride=stride, bias=False),
nn.BatchNorm2d(out_channel)
)
self.res_layer = nn.Sequential(nn.BatchNorm2d(in_channel),
nn.Conv2d(in_channel, out_channel, (3, 3), 1, 1, bias=False),
nn.BatchNorm2d(out_channel),
nn.PReLU(out_channel),
nn.Conv2d(out_channel, out_channel, (3, 3), stride, 1, bias=False),
nn.BatchNorm2d(out_channel))
def forward(self, x):
shortcut = self.shortcut_layer(x)
end_time = time.time()
res = self.res_layer(x)
#l_time = time.time() - end_time
#global cos_time
#cos_time += l_time
#print(l_time * 1000)
return shortcut + res
class BottleNeck_IR_SE(nn.Module):
def __init__(self, in_channel, out_channel, stride):
super(BottleNeck_IR_SE, self).__init__()
if in_channel == out_channel:
self.shortcut_layer = nn.MaxPool2d(1, stride)
else:
self.shortcut_layer = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=(1, 1), stride=stride, bias=False),
nn.BatchNorm2d(out_channel)
)
self.res_layer = nn.Sequential(nn.BatchNorm2d(in_channel),
nn.Conv2d(in_channel, out_channel, (3, 3), 1, 1, bias=False),
nn.BatchNorm2d(out_channel),
nn.PReLU(out_channel),
nn.Conv2d(out_channel, out_channel, (3, 3), stride, 1, bias=False),
nn.BatchNorm2d(out_channel),
SEModule(out_channel, 16))
def forward(self, x):
shortcut = self.shortcut_layer(x)
res = self.res_layer(x)
return shortcut + res
class Bottleneck(namedtuple('Block', ['in_channel', 'out_channel', 'stride'])):
'''A named tuple describing a ResNet block.'''
def get_block(in_channel, out_channel, num_units, stride=2):
return [Bottleneck(in_channel, out_channel, stride)] + [Bottleneck(out_channel, out_channel, 1) for i in range(num_units - 1)]
def get_blocks(num_layers):
if num_layers == 50:
blocks = [
get_block(in_channel=64, out_channel=64, num_units=3),
get_block(in_channel=64, out_channel=128, num_units=4),
get_block(in_channel=128, out_channel=256, num_units=14),
get_block(in_channel=256, out_channel=512, num_units=3)
]
elif num_layers == 100:
blocks = [
get_block(in_channel=64, out_channel=64, num_units=3),
get_block(in_channel=64, out_channel=128, num_units=13), # 13
get_block(in_channel=128, out_channel=256, num_units=30), # 30
get_block(in_channel=256, out_channel=512, num_units=3)
]
elif num_layers == 152:
blocks = [
get_block(in_channel=64, out_channel=64, num_units=3),
get_block(in_channel=64, out_channel=128, num_units=8),
get_block(in_channel=128, out_channel=256, num_units=36),
get_block(in_channel=256, out_channel=512, num_units=3)
]
elif num_layers == 200:
blocks = [
get_block(in_channel=64, out_channel=64, num_units=3),
get_block(in_channel=64, out_channel=128, num_units=30),
get_block(in_channel=128, out_channel=256, num_units=48),
get_block(in_channel=256, out_channel=512, num_units=6)# 6
]
return blocks
class SEResNet_IR(nn.Module):
def __init__(self, num_layers, drop_ratio=0.4, mode = 'ir',feat=False,num_classes=85742):
super(SEResNet_IR, self).__init__()
assert num_layers in [50, 100, 152, 200], 'num_layers should be 50, 100 or 152'
assert mode in ['ir', 'se_ir'], 'mode should be ir or se_ir'
blocks = get_blocks(num_layers)
if mode == 'ir':
unit_module = BottleNeck_IR
elif mode == 'se_ir':
unit_module = BottleNeck_IR_SE
self.feat = feat
self.input_layer = nn.Sequential(nn.Conv2d(3, 64, (3, 3), 1, 1, bias=False),
nn.BatchNorm2d(64),
nn.PReLU(64))
self.output_layer = nn.Sequential(nn.BatchNorm2d(512),
nn.Dropout(drop_ratio),
Flatten(),
nn.Linear(512 * 7 * 7, 512),
nn.BatchNorm1d(512))
modules = []
for block in blocks:
for bottleneck in block:
modules.append(
unit_module(bottleneck.in_channel,
bottleneck.out_channel,
bottleneck.stride))
self.body = nn.Sequential(*modules)
self.fc = nn.Linear(512, num_classes, bias=False) # k 要变这里
def forward(self, x,targets): #
x = self.input_layer(x)
x = self.body(x)
x = self.output_layer(x)
norm = torch.norm(x, 2, 1, True)
#x = torch.div(x, norm)
if self.feat:
return x
x = self.fc(x, norm, targets)
if isinstance(x, tuple):
cos_thetas, bad_grad = x
targets[bad_grad.squeeze(-1)] = -100 # ignore_index
#print(cos_time *1000)
return x
if __name__ == '__main__':
input = torch.Tensor(2, 3, 112, 112)
net = SEResNet_IR(100, num_classes = 85742, mode='ir')
print(net)
x = net(input)
print(x.shape)