-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackbone.py
133 lines (109 loc) · 4.15 KB
/
backbone.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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torch.utils.model_zoo as model_zoo
resnet50_url = 'https://download.pytorch.org/models/resnet50-19c8e357.pth'
class Network_D(nn.Module):
def __init__(self, *args, **kwargs):
super(Network_D, self).__init__(*args, **kwargs)
resnet50 = torchvision.models.resnet50()
self.conv1 = resnet50.conv1
self.bn1 = resnet50.bn1
self.relu = resnet50.relu
self.maxpool = resnet50.maxpool
self.layer1 = create_layer(64, 64, 3, stride=1)
self.layer2 = create_layer(256, 128, 4, stride=2)
self.layer3 = create_layer(512, 256, 6, stride=2)
self.layer4 = create_layer(1024, 512, 3, stride=1)
self.bn2 = nn.BatchNorm1d(2048)
self.dp = nn.Dropout(0.5)
self.fc = nn.Linear(in_features=2048, out_features=1024, bias=True)
self.bn3 = nn.BatchNorm1d(1024)
# load pretrained weights and initialize added weight
pretrained_state = model_zoo.load_url(resnet50_url)
state_dict = self.state_dict()
for k, v in pretrained_state.items():
if 'fc' in k:
continue
state_dict.update({k: v})
self.load_state_dict(state_dict)
nn.init.kaiming_normal_(self.fc.weight, a=1)
nn.init.constant_(self.fc.bias, 0)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = F.avg_pool2d(x, x.size()[2:]).view(x.size()[:2])
x = self.bn2(x)
x = self.dp(x)
x = self.fc(x)
embd = self.bn3(x)
if not self.training:
embd_norm = torch.norm(embd, 2, 1, True).clamp(min=1e-12).expand_as(embd)
embd = embd / embd_norm
return embd
class Bottleneck(nn.Module):
def __init__(self, in_chan, mid_chan, stride=1, stride_at_1x1=False, *args, **kwargs):
super(Bottleneck, self).__init__(*args, **kwargs)
stride1x1, stride3x3 = (stride, 1) if stride_at_1x1 else (1, stride)
out_chan = 4 * mid_chan
self.conv1 = nn.Conv2d(in_chan, mid_chan, kernel_size=1, stride=stride1x1,
bias=False)
self.bn1 = nn.BatchNorm2d(mid_chan)
self.conv2 = nn.Conv2d(mid_chan, mid_chan, kernel_size=3, stride=stride3x3,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(mid_chan)
self.conv3 = nn.Conv2d(mid_chan, out_chan, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(out_chan)
self.relu = nn.ReLU(inplace=True)
self.downsample = None
if in_chan != out_chan or stride != 1:
self.downsample = nn.Sequential(
nn.Conv2d(in_chan, out_chan, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_chan))
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample == None:
residual = x
else:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
def create_layer(in_chan, mid_chan, b_num, stride):
out_chan = mid_chan * 4
blocks = [Bottleneck(in_chan, mid_chan, stride=stride),]
for i in range(1, b_num):
blocks.append(Bottleneck(out_chan, mid_chan, stride=1))
return nn.Sequential(*blocks)
if __name__ == '__main__':
intensor = torch.randn(10, 3, 256, 128)
net = Network_D()
out = net(intensor)
print(out.shape)
params = list(net.parameters())
optim = torch.optim.Adam(params, lr = 1e-3, weight_decay = 5e-4)
lr = 3
optim.defaults['lr'] = 4
for param_group in optim.param_groups:
param_group['lr'] = lr
print(param_group.keys())
print(param_group['lr'])
print(optim.defaults['lr'])
print(optim.defaults.keys())
print(net)