forked from yanghongji2007/cross_view_localization_L2LTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
147 lines (100 loc) · 4.23 KB
/
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
import os
import torch
import torch.nn as nn
import numpy as np
from utils.data_utils import get_loader
from torch.utils.data import DataLoader
from tqdm import tqdm
# from apex import amp
import scipy.io as scio
import torch.nn.functional as F
import argparse
from models.model_crossattn import VisionTransformer, CONFIGS
#from utils.data_utils import get_loader
#from utils.dataloader_act import TestDataloader
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
def validate(dist_array, top_k):
accuracy = 0.0
data_amount = 0.0
for i in range(dist_array.shape[0]):
gt_dist = dist_array[i,i]
prediction = np.sum(dist_array[:, i] < gt_dist)
if prediction < top_k:
accuracy += 1.0
data_amount += 1.0
accuracy /= data_amount
return accuracy
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument("--name", required=True,
help="Name of this run. Used for monitoring.")
parser.add_argument("--dataset", choices=["CVUSA", "CVACT"], default="CVUSA",
help="Which downstream task.")
parser.add_argument("--model_type", choices=["ViT-B_16", "ViT-B_32", "ViT-L_16",
"ViT-L_32", "ViT-H_14", "R50-ViT-B_16", "R50-ViT-B_32"],
default="R50-ViT-B_16",
help="Which variant to use.")
parser.add_argument("--polar", type=int,choices=[1,0],
default=1,
help="polar transform or not")
parser.add_argument("--dataset_dir", default="output", type=str,
help="The dataset path.")
parser.add_argument("--output_dir", default="output", type=str,
help="The output directory where checkpoints will be written.")
parser.add_argument("--img_size", default=(128, 512), type=int,
help="Resolution size")
parser.add_argument("--img_size_sat", default=(128, 512), type=int,
help="Resolution size")
parser.add_argument("--eval_batch_size", default=32, type=int,
help="Total batch size for eval.")
args = parser.parse_args()
print(args)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
args.n_gpu = torch.cuda.device_count()
args.device = device
config = CONFIGS[args.model_type]
model_grd = VisionTransformer(config, args.img_size)
model_sat = VisionTransformer(config, args.img_size_sat)
print("loading model form ", os.path.join(args.output_dir,'model_grd_checkpoint.pth'))
state_dict = torch.load(os.path.join(args.output_dir,'model_checkpoint.pth'))
model_grd.load_state_dict(state_dict['model_grd'])
model_sat.load_state_dict(state_dict['model_sat'])
if args.dataset == 'CVUSA':
from utils.dataloader_usa import TestDataloader
elif args.dataset == 'CVACT':
from utils.dataloader_act import TestDataloader
testset = TestDataloader(args)
test_loader = DataLoader(testset,
batch_size=args.eval_batch_size,
shuffle=False,
num_workers=4)
model_grd.to(device)
model_sat.to(device)
sat_global_descriptor = np.zeros([8884, 768])
grd_global_descriptor = np.zeros([8884, 768])
val_i =0
model_grd.eval()
model_sat.eval()
with torch.no_grad():
for step, batch in enumerate(tqdm(test_loader)):
x_grd, x_sat = batch
if step == 1:
print(x_grd.shape, x_sat.shape)
x_grd=x_grd.to(args.device)
x_sat=x_sat.to(args.device)
grd_global = model_grd(x_grd)
sat_global = model_sat(x_sat)
sat_global_descriptor[val_i: val_i + sat_global.shape[0], :] = sat_global.detach().cpu().numpy()
grd_global_descriptor[val_i: val_i + grd_global.shape[0], :] = grd_global.detach().cpu().numpy()
val_i += sat_global.shape[0]
print(' compute accuracy')
dist_array = 2.0 - 2.0 * np.matmul(sat_global_descriptor, grd_global_descriptor.T)
top1_percent = int(dist_array.shape[0] * 0.01) + 1
val_accuracy = np.zeros((1, top1_percent))
print('start')
for i in tqdm(range(top1_percent)):
val_accuracy[0, i] = validate(dist_array, i)
print('top1', ':', val_accuracy[0, 1])
print('top5', ':', val_accuracy[0, 5])
print('top10', ':', val_accuracy[0, 10])
print('top1%', ':', val_accuracy[0, -1])