-
Notifications
You must be signed in to change notification settings - Fork 19
/
reIDmodel_others.py
165 lines (137 loc) · 5.22 KB
/
reIDmodel_others.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
import torch
import torch.nn as nn
from torch.nn import init
from torchvision import models
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
# #####################################################################
def weights_init_kaiming(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')
# init.normal_(m.weight.data, 0, 0.001)
init.zeros_(m.bias.data)
elif classname.find('BatchNorm1d') != -1:
init.normal_(m.weight.data, 1.0, 0.01)
init.zeros_(m.bias.data)
def weights_init_classifier(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
init.normal_(m.weight.data, 0, 0.001)
init.zeros_(m.bias.data)
# Defines the new fc layer and classification layer
# |--Linear--|--bn--|--relu--|--Linear--|
class FeatureBlock(nn.Module):
def __init__(self, input_dim, low_dim, dropout=0.5, relu=True):
super(FeatureBlock, self).__init__()
feat_block = []
feat_block += [nn.Linear(input_dim, low_dim)]
feat_block += [nn.BatchNorm1d(low_dim)]
feat_block = nn.Sequential(*feat_block)
feat_block.apply(weights_init_kaiming)
self.feat_block = feat_block
def forward(self, x):
x = self.feat_block(x)
return x
class ClassBlock(nn.Module):
def __init__(self, input_dim, class_num, dropout=0.5, relu=True):
super(ClassBlock, self).__init__()
classifier = []
if relu:
classifier += [nn.LeakyReLU(0.1)]
if dropout:
classifier += [nn.Dropout(p=dropout)]
classifier += [nn.Linear(input_dim, class_num)]
classifier = nn.Sequential(*classifier)
classifier.apply(weights_init_classifier)
self.classifier = classifier
def forward(self, x):
x = self.classifier(x)
return x
# Define the ResNet18-based Model
class visible_net_resnet(nn.Module):
def __init__(self, arch='resnet18'):
super(visible_net_resnet, self).__init__()
if arch == 'resnet18':
model_ft = models.resnet18(pretrained=True)
elif arch == 'resnet50':
model_ft = models.resnet50(pretrained=True)
# avg pooling to global pooling
model_ft.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.visible = model_ft
self.dropout = nn.Dropout(p=0.5)
def forward(self, x):
x = self.visible.conv1(x)
x = self.visible.bn1(x)
x = self.visible.relu(x)
x = self.visible.maxpool(x)
x = self.visible.layer1(x)
x = self.visible.layer2(x)
x = self.visible.layer3(x)
x = self.visible.layer4(x)
x = self.visible.avgpool(x)
x = x.view(x.size(0), x.size(1))
# x = self.dropout(x)
return x
class thermal_net_resnet(nn.Module):
def __init__(self, arch='resnet18'):
super(thermal_net_resnet, self).__init__()
if arch == 'resnet18':
model_ft = models.resnet18(pretrained=True)
elif arch == 'resnet50':
model_ft = models.resnet50(pretrained=True)
# avg pooling to global pooling
model_ft.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.thermal = model_ft
self.dropout = nn.Dropout(p=0.5)
def forward(self, x):
x = self.thermal.conv1(x)
x = self.thermal.bn1(x)
x = self.thermal.relu(x)
x = self.thermal.maxpool(x)
x = self.thermal.layer1(x)
x = self.thermal.layer2(x)
x = self.thermal.layer3(x)
x = self.thermal.layer4(x)
x = self.thermal.avgpool(x)
x = x.view(x.size(0), x.size(1))
# x = self.dropout(x)
return x
class baseline_IR(nn.Module):
def __init__(self, class_num = 10, low_dim = 512, droprate=0.5, arch='resnet50'):
super(baseline_IR, self).__init__()
if arch == 'resnet18':
self.visible_net = visible_net_resnet(arch=arch)
self.thermal_net = thermal_net_resnet(arch=arch)
pool_dim = 512
elif arch == 'resnet50':
self.visible_net = visible_net_resnet(arch=arch)
self.thermal_net = thermal_net_resnet(arch=arch)
pool_dim = 2048
self.feature = FeatureBlock(pool_dim, low_dim, dropout=droprate)
self.classifier = ClassBlock(low_dim, class_num, dropout=droprate)
self.l2norm = Normalize(2)
def forward(self, x1, x2, modal=0):
if modal == 0:
x1 = self.visible_net(x1)
x2 = self.thermal_net(x2)
x = torch.cat((x1, x2), 0)
elif modal == 1:
x = self.visible_net(x1)
elif modal == 0:
x = self.thermal_net(x2)
y = self.feature(x)
out = self.classifier(y)
if self.training:
return out, self.l2norm(y), self.l2norm(y)
else:
return self.l2norm(x), self.l2norm(y), 0