-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-test.py
167 lines (118 loc) · 4.94 KB
/
run-test.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
# Evaluation
# Acknowledgement: the code is based on Siddhant Kapil's repo on LA-Transformer
from __future__ import print_function
import os
import faiss
import numpy as np
from PIL import Image
from tqdm.notebook import tqdm
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
from torchvision import models
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data import DataLoader, Dataset
from final.ReID_Slice_Cosine import SliceNet as ReidModel
weights_file = "final/models/final_model_Slice_Cosine_100.pth"
from utils import get_id
from metrics import rank1, rank5, calc_ap
# ### Set feature volume sizes (height, width, depth)
# TODO: update with your model's feature length
batch_size = 1
H, W, D = 1, 1, 128 # for dummymodel we have feature volume 7x7x2048
# ### Load Model
# TODO: Uncomment the following lines to load the Implemented and trained Model
#save_path = "<model weight path>"
#model = ReidModel(num_classes=C)
#model.load_state_dict(torch.load(save_path), strict=False)
#model.eval()
# TODO: Comment out the dummy model
model = ReidModel(numClasses=62,inference=True)
model.load_state_dict(torch.load(weights_file,map_location=torch.device('cpu')), strict=False)
model.eval()
# ### Data Loader for query and gallery
# TODO: For demo, we have resized to 224x224 during data augmentation
# You are free to use augmentations of your own choice
transform_query_list = [
transforms.Resize((128, 48), interpolation=3),
transforms.ToTensor(),
transforms.Normalize([0.4726, 0.4468, 0.4811], [0.2765, 0.2747, 0.2764])
]
transform_gallery_list = [
transforms.Resize(size=(128, 48), interpolation=3), #Image.BICUBIC
transforms.ToTensor(),
transforms.Normalize([0.4726, 0.4468, 0.4811], [0.2765, 0.2747, 0.2764])
]
data_transforms = {
'query': transforms.Compose( transform_query_list ),
'gallery': transforms.Compose(transform_gallery_list),
}
image_datasets = {}
data_dir = "data/val"
image_datasets['query'] = datasets.ImageFolder(os.path.join(data_dir, 'query'),
data_transforms['query'])
image_datasets['gallery'] = datasets.ImageFolder(os.path.join(data_dir, 'gallery'),
data_transforms['gallery'])
query_loader = DataLoader(dataset = image_datasets['query'], batch_size=batch_size, shuffle=False )
gallery_loader = DataLoader(dataset = image_datasets['gallery'], batch_size=batch_size, shuffle=False)
class_names = image_datasets['query'].classes
# ### Extract Features
def extract_feature(dataloaders):
features = torch.FloatTensor()
count = 0
idx = 0
for data in tqdm(dataloaders):
img, label = data
# Uncomment if using GPU for inference
#img, label = img.cuda(), label.cuda()
output = model(img) # (B, D, H, W) --> B: batch size, HxWxD: feature volume size
n, c, h, w = img.size()
count += n
features = torch.cat((features, output.detach().cpu()), 0)
idx += 1
return features
# Extract Query Features
query_feature= extract_feature(query_loader)
# Extract Gallery Features
gallery_feature = extract_feature(gallery_loader)
# Retrieve labels
gallery_path = image_datasets['gallery'].imgs
query_path = image_datasets['query'].imgs
gallery_cam,gallery_label = get_id(gallery_path)
query_cam,query_label = get_id(query_path)
# ## Concat Averaged GELTs
concatenated_query_vectors = []
for query in tqdm(query_feature):
fnorm = torch.norm(query, p=2, dim=0, keepdim=True)#*np.sqrt(H*W)
query_norm = query.div(fnorm.expand_as(query))
concatenated_query_vectors.append(query_norm.view((-1)))
concatenated_gallery_vectors = []
for gallery in tqdm(gallery_feature):
fnorm = torch.norm(gallery, p=2, dim=0, keepdim=True)#*np.sqrt(H*W)
gallery_norm = gallery.div(fnorm.expand_as(gallery))
concatenated_gallery_vectors.append(gallery_norm.view((-1)))
# ## Calculate Similarity using FAISS
index = faiss.IndexIDMap(faiss.IndexFlatIP(H*W*D))
index.add_with_ids(np.array([t.numpy() for t in concatenated_gallery_vectors]),np.array(gallery_label))
def search(query: str, k=1):
encoded_query = query.unsqueeze(dim=0).numpy()
top_k = index.search(encoded_query, k)
return top_k
# ### Evaluate
rank1_score = 0
rank5_score = 0
ap = 0
count = 0
for query, label in zip(concatenated_query_vectors, query_label):
count += 1
label = label
output = search(query, k=10)
rank1_score += rank1(label, output)
rank5_score += rank5(label, output)
print("Correct: {}, Total: {}, Incorrect: {}".format(rank1_score, count, count-rank1_score), end="\r")
ap += calc_ap(label, output)
print("Rank1: %.3f, Rank5: %.3f, mAP: %.3f"%(rank1_score/len(query_feature),
rank5_score/len(query_feature),
ap/len(query_feature)))