forked from GNAYUOHZ/ReID-MGN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
162 lines (121 loc) · 4.96 KB
/
main.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
import os
import numpy as np
from scipy.spatial.distance import cdist
from tqdm import tqdm
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import torch
from torch.optim import lr_scheduler
from opt import opt
from data import Data
from network import MGN
from loss import Loss
from utils.get_optimizer import get_optimizer
from utils.extract_feature import extract_feature
from utils.metrics import mean_ap, cmc, re_ranking
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class Main():
def __init__(self, model, loss, data):
self.train_loader = data.train_loader
self.test_loader = data.test_loader
self.query_loader = data.query_loader
self.testset = data.testset
self.queryset = data.queryset
self.model = model.to('cuda')
self.loss = loss
self.optimizer = get_optimizer(model)
self.scheduler = lr_scheduler.MultiStepLR(self.optimizer, milestones=opt.lr_scheduler, gamma=0.1)
def train(self):
self.scheduler.step()
self.model.train()
for batch, (inputs, labels) in enumerate(self.train_loader):
inputs = inputs.to('cuda')
labels = labels.to('cuda')
self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.loss(outputs, labels)
loss.backward()
self.optimizer.step()
def evaluate(self):
self.model.eval()
print('extract features, this may take a few minutes')
qf = extract_feature(self.model, tqdm(self.query_loader)).numpy()
gf = extract_feature(self.model, tqdm(self.test_loader)).numpy()
def rank(dist):
r = cmc(dist, self.queryset.ids, self.testset.ids, self.queryset.cameras, self.testset.cameras,
separate_camera_set=False,
single_gallery_shot=False,
first_match_break=True)
m_ap = mean_ap(dist, self.queryset.ids, self.testset.ids, self.queryset.cameras, self.testset.cameras)
return r, m_ap
######################### re rank##########################
q_g_dist = np.dot(qf, np.transpose(gf))
q_q_dist = np.dot(qf, np.transpose(qf))
g_g_dist = np.dot(gf, np.transpose(gf))
dist = re_ranking(q_g_dist, q_q_dist, g_g_dist)
r, m_ap = rank(dist)
print('[With Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
.format(m_ap, r[0], r[2], r[4], r[9]))
#########################no re rank##########################
dist = cdist(qf, gf)
r, m_ap = rank(dist)
print('[Without Re-Ranking] mAP: {:.4f} rank1: {:.4f} rank3: {:.4f} rank5: {:.4f} rank10: {:.4f}'
.format(m_ap, r[0], r[2], r[4], r[9]))
def vis(self):
self.model.eval()
gallery_path = data.testset.imgs
gallery_label = data.testset.ids
# Extract feature
print('extract features, this may take a few minutes')
query_feature = extract_feature(model, tqdm([(torch.unsqueeze(data.query_image, 0), 1)]))
gallery_feature = extract_feature(model, tqdm(data.test_loader))
# sort images
query_feature = query_feature.view(-1, 1)
score = torch.mm(gallery_feature, query_feature)
score = score.squeeze(1).cpu()
score = score.numpy()
index = np.argsort(score) # from small to large
index = index[::-1] # from large to small
# # Remove junk images
# junk_index = np.argwhere(gallery_label == -1)
# mask = np.in1d(index, junk_index, invert=True)
# index = index[mask]
# Visualize the rank result
fig = plt.figure(figsize=(16, 4))
ax = plt.subplot(1, 11, 1)
ax.axis('off')
plt.imshow(plt.imread(opt.query_image))
ax.set_title('query')
print('Top 10 images are as follow:')
for i in range(10):
img_path = gallery_path[index[i]]
print(img_path)
ax = plt.subplot(1, 11, i + 2)
ax.axis('off')
plt.imshow(plt.imread(img_path))
ax.set_title(img_path.split('/')[-1][:9])
fig.savefig("show.png")
print('result saved to show.png')
if __name__ == '__main__':
data = Data()
model = MGN()
loss = Loss()
main = Main(model, loss, data)
if opt.mode == 'train':
for epoch in range(1, opt.epoch + 1):
print('\nepoch', epoch)
main.train()
if epoch % 50 == 0:
print('\nstart evaluate')
main.evaluate()
os.makedirs('weights', exist_ok=True)
torch.save(model.state_dict(), ('weights/model_{}.pt'.format(epoch)))
if opt.mode == 'evaluate':
print('start evaluate')
model.load_state_dict(torch.load(opt.weight))
main.evaluate()
if opt.mode == 'vis':
print('visualize')
model.load_state_dict(torch.load(opt.weight))
main.vis()