-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevaluateMA.py
159 lines (130 loc) · 4.91 KB
/
evaluateMA.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
import argparse
import scipy.io
import torch
import numpy as np
import os
from torchvision import datasets
import matplotlib
# matplotlib.use('agg')
import matplotlib.pyplot as plt
import json
from tqdm import tqdm
import math
#######################################################################
# Evaluate
parser = argparse.ArgumentParser(description='Demo')
# parser.add_argument('--query_index', default=10, type=int, help='test_image_index')
parser.add_argument(
'--root_dir', default='/home/dmmm/Dataset/DenseUAV/data_2022/', type=str, help='./test_data')
parser.add_argument('--mode', default="1", type=str,
help='1:drone->satellite 2:satellite->drone')
opts = parser.parse_args()
opts.config = os.path.join(opts.root_dir, "Dense_GPS_ALL.txt")
opts.test_dir = os.path.join(opts.root_dir, "test")
configDict = {}
with open(opts.config, "r") as F:
context = F.readlines()
for line in context:
splitLineList = line.split(" ")
configDict[splitLineList[0].split("/")[-2]] = [float(splitLineList[1].split("E")[-1]),
float(splitLineList[2].split("N")[-1])]
if opts.mode == "1":
gallery_name = 'gallery_satellite'
query_name = 'query_drone'
else:
gallery_name = 'gallery_drone'
query_name = 'query_satellite'
data_dir = opts.test_dir
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x)) for x in [
gallery_name, query_name]}
#####################################################################
# Show result
def imshow(path, title=None):
"""Imshow for Tensor."""
im = plt.imread(path)
plt.imshow(im)
if title is not None:
plt.title(title)
plt.pause(0.1) # pause a bit so that plots are updated
######################################################################
if opts.mode == "1":
result = scipy.io.loadmat('pytorch_result_1.mat')
else:
result = scipy.io.loadmat('pytorch_result_2.mat')
query_feature = torch.FloatTensor(result['query_f'])
query_label = result['query_label'][0]
gallery_feature = torch.FloatTensor(result['gallery_f'])
gallery_label = result['gallery_label'][0]
multi = os.path.isfile('multi_query.mat')
if multi:
m_result = scipy.io.loadmat('multi_query.mat')
mquery_feature = torch.FloatTensor(m_result['mquery_f'])
mquery_cam = m_result['mquery_cam'][0]
mquery_label = m_result['mquery_label'][0]
mquery_feature = mquery_feature.cuda()
query_feature = query_feature.cuda()
gallery_feature = gallery_feature.cuda()
#######################################################################
# sort the images and return topK index
def sort_img(qf, ql, gf, gl, K):
query = qf.view(-1, 1)
# print(query.shape)
score = torch.mm(gf, query)
score = score.squeeze(1).cpu()
score = score.numpy()
# predict index
index = np.argsort(score) # from small to large
index = index[::-1]
# index = index[0:2000]
# good index
query_index = np.argwhere(gl == ql)
# good_index = np.setdiff1d(query_index, camera_index, assume_unique=True)
junk_index = np.argwhere(gl == -1)
mask = np.in1d(index, junk_index, invert=True)
index = index[mask]
return index[:K]
def getLatitudeAndLongitude(imgPath):
if isinstance(imgPath, list):
posInfo = [configDict[p.split("/")[-2]] for p in imgPath]
else:
posInfo = configDict[imgPath.split("/")[-2]]
return posInfo
def latlog2meter(lata, loga, latb, logb):
# log 纬度 lat 经度
# EARTH_RADIUS = 6371.0
EARTH_RADIUS =6378.137
PI = math.pi
# // 转弧度
lat_a = lata * PI / 180
lat_b = latb * PI / 180
a = lat_a - lat_b
b = loga * PI / 180 - logb * PI / 180
dis = 2 * math.asin(
math.sqrt(math.pow(math.sin(a / 2), 2) + math.cos(lat_a) * math.cos(lat_b) * math.pow(math.sin(b / 2), 2)))
distance = EARTH_RADIUS * dis * 1000
return distance
def evaluate_MA(indexOfTop1, queryIndex):
query_path, _ = image_datasets[query_name].imgs[queryIndex]
galleryTopKPath = image_datasets[gallery_name].imgs[indexOfTop1][0]
# get position information including latitude and longitude
queryPosInfo = getLatitudeAndLongitude(query_path)
galleryTopKPosInfo = getLatitudeAndLongitude(galleryTopKPath)
# get real distance
distance_meter = latlog2meter(queryPosInfo[1],queryPosInfo[0],galleryTopKPosInfo[1],galleryTopKPosInfo[0])
return distance_meter
indexOfTopK_list = []
for i in range(len(query_label)):
indexOfTopK = sort_img(
query_feature[i], query_label[i], gallery_feature, gallery_label, 100)
indexOfTopK_list.append(indexOfTopK)
MA_dict = {}
for meter in tqdm(range(1,101,1)):
MA_K = 0
for i in range(len(query_label)):
MA_meter = evaluate_MA(indexOfTopK_list[i][0],i)
if MA_meter<meter:
MA_K+=1
MA_K = MA_K/len(query_label)
MA_dict[meter]=MA_K
with open("MA@K(1,100)", 'w') as F:
json.dump(MA_dict, F, indent=4)