forked from zilunzhang/StreetCLIP-Repoduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_img2gps.py
307 lines (279 loc) · 14.9 KB
/
eval_img2gps.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import pandas as pd
from PIL import Image
import open_clip
import torch
import os
import numpy as np
import argparse
from torch.utils.data import DataLoader
from tqdm import tqdm
from helper import build_model, Image2GPSDataset, calculate_metric, get_name_predict_gt, calculate_metric_new
# Country list from https://arxiv.org/pdf/2302.00275.pdf, Appendix B.3.1
country_candidates_list_from_streetclip = [
"Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
"Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria",
"Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", "Chad",
"Chile", "China", "Colombia", "Comoros", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
"Côte d'Ivoire", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
"East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji",
"Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India",
"Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan",
"Kenya", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
"Lithuania", "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta", "Marshall Islands", "Mauritania",
"Mauritius", "Mexico", "Micronesia", "Moldova", "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar",
"Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "North Korea",
"North Macedonia", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Panama", "Papua New Guinea", "Paraguay",
"Peru", "Philippines", "Poland", "Portugal", "Qatar", "Republic of the Congo", "Romania", "Russia", "Rwanda",
"Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia",
"Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Korea", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland",
"Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam",
"Yemen", "Zambia", "Zimbabwe"
]
# mismatch between names in country list from the paper and names in SimpleMaps-Basic database
# (https://simplemaps.com/data/world-cities)
streetclip2simplemap = {
"Bahamas": "The Bahamas",
"Czech Republic": "Czechia",
"Democratic Republic of the Congo": "Congo (Kinshasa)",
"East Timor": "Timor-Leste",
"Gambia": "The Gambia",
"Micronesia": "Federated States of Micronesia",
"North Macedonia": "Macedonia",
"Republic of the Congo": "Congo (Brazzaville)"
}
def eval_geolocation_openclip(img_dir, model_name, model_path, country_list, simple_map_df, num_worker, batch_size, topk_city_per_country=30):
def country2city(country):
if country != "Palestine":
cities_df = simple_map_df[simple_map_df["country"] == country]
else:
# "Palestine" does not exist in the database from SimpleMaps.
# For the purpose of creating text context for Palestine in order to make CLIP-like models work,
# I combine two regions ("Gaza Strip" and "West Bank") from the SimpleMaps to represent the Palestine.
# There is no political meaning behind the data processing. I stand with UN's resolution.
cities_1 = simple_map_df[simple_map_df["country"] == "Gaza Strip"]
cities_2 = simple_map_df[simple_map_df["country"] == "West Bank"]
cities_df = pd.concat([cities_1, cities_2])
cities_df_sorted = cities_df.sort_values("population", ascending=False)
city_names = cities_df_sorted["city_ascii"].tolist()[:topk_city_per_country]
return city_names
device = "cuda" if torch.cuda.is_available() else "cpu"
model, img_preprocess = build_model(
model_name,
model_path,
device,
use_streetclip=False
)
tokenizer = open_clip.tokenize
img2gps_dataset = Image2GPSDataset(img_dir, img_preprocess)
img2gps_dataloader = DataLoader(
img2gps_dataset,
num_workers=num_worker,
batch_size=batch_size,
pin_memory=True,
shuffle=False
)
# Stage 1: Linear prob a country
country_text_prompt = ["A Street View photo in {}.".format(country) for country in country_list]
img2country_sim_list = []
img_name_list = []
image_normed_features_list = []
for index, (img_names, imgs) in tqdm(enumerate(img2gps_dataloader)):
text = tokenizer(country_text_prompt)
imgs = imgs.to(device)
text = text.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(imgs)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
image_normed_features_list.append(image_features)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1).cpu()
img2country_sim_list.append(text_probs)
img_name_list.extend(img_names)
img2country_sim = torch.cat(img2country_sim_list).numpy()
image_normed_features = torch.cat(image_normed_features_list)
img2country_argmax = np.argmax(img2country_sim, 1)
selected_country = [country_list[index] for index in img2country_argmax]
selected_country = [streetclip2simplemap[country_name] if country_name in streetclip2simplemap else country_name for country_name in selected_country]
# Stage 2: Linear prob a city
result_dict = dict()
selected_city = [country2city(country) for country in selected_country]
for i, city_list in tqdm(enumerate(selected_city)):
city_text_prompt = ["A Street View photo from {}.".format(city) for city in city_list]
text = tokenizer(city_text_prompt)
text = text.to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
text_features = model.encode_text(text)
text_features /= text_features.norm(dim=-1, keepdim=True)
img_name = img_name_list[i]
img_normed_feat = image_normed_features[i].unsqueeze(0)
country_name = selected_country[i]
text_probs = (100.0 * img_normed_feat @ text_features.T).softmax(dim=-1).cpu().numpy()
img2city_argmax = np.argmax(text_probs, 1)[0]
select_city_name = city_list[img2city_argmax]
lat = simple_map_df[simple_map_df["city_ascii"] == select_city_name]["lat"].tolist()[0]
lng = simple_map_df[simple_map_df["city_ascii"] == select_city_name]["lng"].tolist()[0]
result_dict[img_name] = [country_name, select_city_name, lat, lng]
return result_dict
def eval_geolocation_streetclip(img_dir, model_name, model_path, country_list, simple_map_df, num_worker, batch_size, topk_city_per_country=30):
def country2city(country):
if country != "Palestine":
cities_df = simple_map_df[simple_map_df["country"] == country]
else:
# "Palestine" does not exist in the database from SimpleMaps.
# For the purpose of creating text context for Palestine in order to make CLIP-like models work,
# I combine two regions ("Gaza Strip" and "West Bank") from the SimpleMaps to represent the Palestine.
# There is no political meaning behind the data processing. I stand with UN's resolution.
cities_1 = simple_map_df[simple_map_df["country"] == "Gaza Strip"]
cities_2 = simple_map_df[simple_map_df["country"] == "West Bank"]
cities_df = pd.concat([cities_1, cities_2])
cities_df_sorted = cities_df.sort_values("population", ascending=False)
city_names = cities_df_sorted["city_ascii"].tolist()[:topk_city_per_country]
return city_names
device = "cuda" if torch.cuda.is_available() else "cpu"
model, img_preprocess = build_model(
model_name,
model_path,
device,
use_streetclip=True
)
img_names = os.listdir(img_dir)
img_pils = [Image.open(os.path.join(img_dir, img_name)) for img_name in img_names]
img_pils = [img_pils[i:i + batch_size] for i in range(0, len(img_pils), batch_size)]
# Stage 1: Linear prob a country
country_text_prompt = ["A Street View photo in {}.".format(country) for country in country_list]
img2country_sim_list = []
for i, img_pil in tqdm(enumerate(img_pils)):
inputs = img_preprocess(text=country_text_prompt, images=img_pil, return_tensors="pt", padding=True)
inputs['pixel_values'] = inputs['pixel_values'].to(device)
inputs['input_ids'] = inputs['input_ids'].to(device)
inputs['attention_mask'] = inputs['attention_mask'].to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image.softmax(dim=1).cpu() # this is the image-text similarity score
img2country_sim_list.append(logits_per_image)
img2country_sim = torch.cat(img2country_sim_list).numpy()
img2country_argmax = np.argmax(img2country_sim, 1)
selected_country = [country_list[index] for index in img2country_argmax]
selected_country = [streetclip2simplemap[country_name] if country_name in streetclip2simplemap else country_name for country_name in selected_country]
# Stage 2: Linear prob a city
img_pils = [item for img_pil in img_pils for item in img_pil]
result_dict = dict()
selected_city = [country2city(country) for country in selected_country]
for i, city_list in tqdm(enumerate(selected_city)):
city_text_prompt = ["A Street View photo from {}.".format(city) for city in city_list]
img_pil = img_pils[i]
inputs = img_preprocess(text=city_text_prompt, images=img_pil, return_tensors="pt", padding=True)
inputs['pixel_values'] = inputs['pixel_values'].to(device)
inputs['input_ids'] = inputs['input_ids'].to(device)
inputs['attention_mask'] = inputs['attention_mask'].to(device)
with torch.no_grad(), torch.cuda.amp.autocast():
outputs = model(**inputs)
text_probs = outputs.logits_per_image.softmax(dim=1).cpu().numpy() # this is the image-text similarity score
img_name = img_names[i]
country_name = selected_country[i]
img2city_argmax = np.argmax(text_probs, 1)[0]
select_city_name = city_list[img2city_argmax]
lat = simple_map_df[simple_map_df["city_ascii"] == select_city_name]["lat"].tolist()[0]
lng = simple_map_df[simple_map_df["city_ascii"] == select_city_name]["lng"].tolist()[0]
result_dict[img_name] = [country_name, select_city_name, lat, lng]
return result_dict
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-name", default="ViT-B-32", type=str,
help="ViT-B-32 or ViT-L-14-336 or ViT-H-14",
)
parser.add_argument(
"--ckpt-path", default="/media/zilun/mx500/2-year-work/MLLM_geoguesser/StreetCLIP", type=str,
help="Path to ViT-B-32.pt",
)
parser.add_argument(
"--random-seed", default=3407, type=int,
help="random seed",
)
parser.add_argument(
"--test-dataset-dir",
# default="./data/img2gps3k_dataset/image",
default="./data/img2gps_dataset/image",
type=str,
help="test dataset dir",
)
parser.add_argument(
"--gps-mat-path",
# default="./data/img2gps3k_dataset/img2gps3k_gps.mat",
default="./data/img2gps_dataset/img2gps_gps.mat",
type=str,
help="gps path",
)
parser.add_argument(
"--imgname-mat-path",
# default="./data/img2gps3k_dataset/img2gps3k_image_names.mat",
default="./data/img2gps_dataset/img2gps_image_names.mat",
type=str,
help="imgname path",
)
parser.add_argument(
"--imgname-coord-path",
# default="./data/img2gps3k_dataset/img2gps3k_dataset_2997.csv",
default="./data/img2gps_dataset/img2gps_dataset_237.csv",
type=str,
help="path of img2gps_dataset_237.csv/img2gps3k_dataset_2997.csv",
)
parser.add_argument(
"--workers", default=8, type=int,
help="number of workers",
)
parser.add_argument(
"--batch-size", default=300, type=int,
help="batch size",
)
parser.add_argument(
"--city-coord-csv", default="./data/simplemaps_worldcities_basicv1.76/worldcities.csv", type=str,
help="batch size",
)
args = parser.parse_args()
args.device = "cuda" if torch.cuda.is_available() else "cpu"
print(args)
# convert_mat2csv(args)
# d = gps_distance(32.845269, -117.273988, 45.433703, 12.339298)
gt_coord_df = pd.read_csv(args.imgname_coord_path)
city_coord_df = pd.read_csv(args.city_coord_csv)
print(len(country_candidates_list_from_streetclip), city_coord_df.shape)
countries = city_coord_df["country"].tolist()
print(len(set(countries)))
print(list(set(countries)))
for country in country_candidates_list_from_streetclip:
if country not in countries:
print("{} not in the country list from df".format(country))
result_dict_georsclip = eval_geolocation_openclip(
args.test_dataset_dir,
args.model_name,
args.ckpt_path,
country_candidates_list_from_streetclip,
city_coord_df,
args.workers,
args.batch_size,
topk_city_per_country=30
)
georsclip_img_name_list, georsclip_predict_gps_list, georsclip_gt_gps_list = get_name_predict_gt(result_dict_georsclip, gt_coord_df)
calculate_metric(georsclip_img_name_list, georsclip_predict_gps_list, georsclip_gt_gps_list)
result_dict_streetclip = eval_geolocation_streetclip(
args.test_dataset_dir,
args.model_name,
args.ckpt_path,
country_candidates_list_from_streetclip,
city_coord_df,
args.workers,
args.batch_size,
topk_city_per_country=30
)
streetclip_img_name_list, streetclip_predict_gps_list, streetclip_gt_gps_list = get_name_predict_gt(result_dict_streetclip, gt_coord_df)
calculate_metric(streetclip_img_name_list, streetclip_predict_gps_list, streetclip_gt_gps_list)
if __name__ == "__main__":
main()