-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval.py
162 lines (128 loc) · 4.96 KB
/
eval.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
import torch
from torch.utils.data import DataLoader
import torchvision.transforms as T
from tqdm import tqdm
import argparse
from vpr_model import VPRModel
from utils.validation import get_validation_recalls
# Dataloader
from dataloaders.val.NordlandDataset import NordlandDataset
from dataloaders.val.MapillaryDataset import MSLS
from dataloaders.val.PittsburghDataset import PittsburghDataset
from dataloaders.val.SPEDDataset import SPEDDataset
VAL_DATASETS = ['MSLS', 'pitts30k_test', 'pitts250k_test', 'Nordland', 'SPED']
def input_transform(image_size=None):
MEAN=[0.485, 0.456, 0.406]; STD=[0.229, 0.224, 0.225]
if image_size:
return T.Compose([
T.Resize(image_size, interpolation=T.InterpolationMode.BILINEAR),
T.ToTensor(),
T.Normalize(mean=MEAN, std=STD)
])
else:
return T.Compose([
T.ToTensor(),
T.Normalize(mean=MEAN, std=STD)
])
def get_val_dataset(dataset_name, image_size=None):
dataset_name = dataset_name.lower()
transform = input_transform(image_size=image_size)
if 'nordland' in dataset_name:
ds = NordlandDataset(input_transform=transform)
elif 'msls_test' in dataset_name:
ds = MSLSTest(input_transform=transform)
elif 'msls' in dataset_name:
ds = MSLS(input_transform=transform)
elif 'pitts' in dataset_name:
ds = PittsburghDataset(which_ds=dataset_name, input_transform=transform)
elif 'sped' in dataset_name:
ds = SPEDDataset(input_transform=transform)
else:
raise ValueError
num_references = ds.num_references
num_queries = ds.num_queries
ground_truth = ds.ground_truth
return ds, num_references, num_queries, ground_truth
def get_descriptors(model, dataloader, device):
descriptors = []
with torch.no_grad():
with torch.autocast(device_type='cuda', dtype=torch.float16):
for batch in tqdm(dataloader, 'Calculating descritptors...'):
imgs, labels = batch
output = model(imgs.to(device)).cpu()
descriptors.append(output)
return torch.cat(descriptors)
def load_model(ckpt_path):
model = VPRModel(
backbone_arch='dinov2_vitb14',
backbone_config={
'num_trainable_blocks': 4,
'return_token': True,
'norm_layer': True,
},
agg_arch='SALAD',
agg_config={
'num_channels': 768,
'num_clusters': 64,
'cluster_dim': 128,
'token_dim': 256,
},
)
model.load_state_dict(torch.load(ckpt_path)['state_dict'])
model = model.eval()
model = model.to('cuda')
print(f"Loaded model from {ckpt_path} Successfully!")
return model
def parse_args():
parser = argparse.ArgumentParser(
description="Eval VPR model",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
# Model parameters
parser.add_argument("--ckpt_path", type=str, required=True, default=None, help="Path to the checkpoint")
# Datasets parameters
parser.add_argument(
'--val_datasets',
nargs='+',
default=VAL_DATASETS,
help='Validation datasets to use',
choices=VAL_DATASETS,
)
parser.add_argument('--image_size', nargs='*', default=None, help='Image size (int, tuple or None)')
parser.add_argument('--batch_size', type=int, default=512, help='Batch size')
args = parser.parse_args()
# Parse image size
if args.image_size:
if len(args.image_size) == 1:
args.image_size = (args.image_size[0], args.image_size[0])
elif len(args.image_size) == 2:
args.image_size = tuple(args.image_size)
else:
raise ValueError('Invalid image size, must be int, tuple or None')
args.image_size = tuple(map(int, args.image_size))
return args
if __name__ == '__main__':
torch.backends.cudnn.benchmark = True
args = parse_args()
model = load_model(args.ckpt_path)
for val_name in args.val_datasets:
val_dataset, num_references, num_queries, ground_truth = get_val_dataset(val_name, args.image_size)
val_loader = DataLoader(val_dataset, num_workers=16, batch_size=args.batch_size, shuffle=False, pin_memory=True)
print(f'Evaluating on {val_name}')
descriptors = get_descriptors(model, val_loader, 'cuda')
print(f'Descriptor dimension {descriptors.shape[1]}')
r_list = descriptors[ : num_references]
q_list = descriptors[num_references : ]
print('total_size', descriptors.shape[0], num_queries + num_references)
preds = get_validation_recalls(
r_list=r_list,
q_list=q_list,
k_values=[1, 5, 10, 15, 20, 25],
gt=ground_truth,
print_results=True,
dataset_name=val_name,
faiss_gpu=False,
testing=False,
)
del descriptors
print('========> DONE!\n\n')