|
| 1 | +import faiss |
| 2 | +import torch |
| 3 | +import numpy as np |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +import pandas as pd |
| 7 | +import ast |
| 8 | +import itertools |
| 9 | +from PIL import Image |
| 10 | +from geopy.distance import geodesic |
| 11 | +from transformers import CLIPImageProcessor, CLIPModel |
| 12 | +from utils.utils import MP16Dataset, im2gps3kDataset, yfcc4kDataset |
| 13 | +from torch.utils.data import DataLoader |
| 14 | +from tqdm import tqdm |
| 15 | +from torch.utils.data import Dataset, DataLoader |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +def build_index(args): |
| 19 | + if args.index == 'g3': |
| 20 | + model = torch.load('./checkpoints/g3.pth', map_location='cuda:0') |
| 21 | + model.requires_grad_(False) |
| 22 | + vision_processor = model.vision_processor |
| 23 | + dataset = MP16Dataset(vision_processor = model.vision_processor, text_processor = None) |
| 24 | + index_flat = faiss.IndexFlatIP(768*3) |
| 25 | + dataloader = DataLoader(dataset, batch_size=1024, shuffle=False, num_workers=16, pin_memory=True, prefetch_factor=3) |
| 26 | + model.eval() |
| 27 | + t= tqdm(dataloader) |
| 28 | + for i, (images, texts, longitude, latitude) in enumerate(t): |
| 29 | + images = images.to(args.device) |
| 30 | + vision_output = model.vision_model(images)[1] |
| 31 | + image_embeds = model.vision_projection(vision_output) |
| 32 | + image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True) |
| 33 | + |
| 34 | + image_text_embeds = model.vision_projection_else_1(model.vision_projection(vision_output)) |
| 35 | + image_text_embeds = image_text_embeds / image_text_embeds.norm(p=2, dim=-1, keepdim=True) |
| 36 | + |
| 37 | + image_location_embeds = model.vision_projection_else_2(model.vision_projection(vision_output)) |
| 38 | + image_location_embeds = image_location_embeds / image_location_embeds.norm(p=2, dim=-1, keepdim=True) |
| 39 | + |
| 40 | + image_embeds = torch.cat([image_embeds, image_text_embeds, image_location_embeds], dim=1) |
| 41 | + index_flat.add(image_embeds.cpu().detach().numpy()) |
| 42 | + |
| 43 | + faiss.write_index(index_flat, f'./index/{args.index}.index') |
| 44 | + |
| 45 | +def search_index(args, index, topk): |
| 46 | + print('start searching...') |
| 47 | + if args.dataset == 'im2gps3k': |
| 48 | + if args.index == 'g3': |
| 49 | + model = torch.load('./checkpoints/g3.pth', map_location='cuda:0') |
| 50 | + model.requires_grad_(False) |
| 51 | + vision_processor = model.vision_processor |
| 52 | + dataset = im2gps3kDataset(vision_processor = vision_processor, text_processor = None) |
| 53 | + dataloader = DataLoader(dataset, batch_size=256, shuffle=False, num_workers=16, pin_memory=True, prefetch_factor=5) |
| 54 | + test_images_embeds = np.empty((0, 768*3)) |
| 55 | + model.eval() |
| 56 | + print('generating embeds...') |
| 57 | + t = tqdm(dataloader) |
| 58 | + for i, (images, texts, longitude, latitude) in enumerate(t): |
| 59 | + images = images.to(args.device) |
| 60 | + vision_output = model.vision_model(images)[1] |
| 61 | + image_embeds = model.vision_projection(vision_output) |
| 62 | + image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True) |
| 63 | + |
| 64 | + image_text_embeds = model.vision_projection_else_1(model.vision_projection(vision_output)) |
| 65 | + image_text_embeds = image_text_embeds / image_text_embeds.norm(p=2, dim=-1, keepdim=True) |
| 66 | + |
| 67 | + image_location_embeds = model.vision_projection_else_2(model.vision_projection(vision_output)) |
| 68 | + image_location_embeds = image_location_embeds / image_location_embeds.norm(p=2, dim=-1, keepdim=True) |
| 69 | + |
| 70 | + image_embeds = torch.cat([image_embeds, image_text_embeds, image_location_embeds], dim=1) |
| 71 | + test_images_embeds = np.concatenate([test_images_embeds, image_embeds.cpu().detach().numpy()], axis=0) |
| 72 | + print(test_images_embeds.shape) |
| 73 | + test_images_embeds = test_images_embeds.reshape(-1, 768*3) |
| 74 | + print('start searching NN...') |
| 75 | + D, I = index.search(test_images_embeds, topk) |
| 76 | + print(I) |
| 77 | + return D, I |
| 78 | + elif args.dataset == 'yfcc4k': |
| 79 | + if args.index == 'g3': |
| 80 | + model = torch.load('./checkpoints/g3.pth', map_location='cuda:0') |
| 81 | + model.requires_grad_(False) |
| 82 | + vision_processor = model.vision_processor |
| 83 | + dataset = yfcc4kDataset(vision_processor = vision_processor, text_processor = None) |
| 84 | + dataloader = DataLoader(dataset, batch_size=256, shuffle=False, num_workers=16, pin_memory=True, prefetch_factor=5) |
| 85 | + test_images_embeds = np.empty((0, 768*3)) |
| 86 | + model.eval() |
| 87 | + print('generating embeds...') |
| 88 | + t = tqdm(dataloader) |
| 89 | + for i, (images, texts, longitude, latitude) in enumerate(t): |
| 90 | + images = images.to(args.device) |
| 91 | + vision_output = model.vision_model(images)[1] |
| 92 | + image_embeds = model.vision_projection(vision_output) |
| 93 | + image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True) |
| 94 | + |
| 95 | + image_text_embeds = model.vision_projection_else_1(model.vision_projection(vision_output)) |
| 96 | + image_text_embeds = image_text_embeds / image_text_embeds.norm(p=2, dim=-1, keepdim=True) |
| 97 | + |
| 98 | + image_location_embeds = model.vision_projection_else_2(model.vision_projection(vision_output)) |
| 99 | + image_location_embeds = image_location_embeds / image_location_embeds.norm(p=2, dim=-1, keepdim=True) |
| 100 | + |
| 101 | + image_embeds = torch.cat([image_embeds, image_text_embeds, image_location_embeds], dim=1) |
| 102 | + test_images_embeds = np.concatenate([test_images_embeds, image_embeds.cpu().detach().numpy()], axis=0) |
| 103 | + print(test_images_embeds.shape) |
| 104 | + test_images_embeds = test_images_embeds.reshape(-1, 768*3) |
| 105 | + print('start searching NN...') |
| 106 | + D, I = index.search(test_images_embeds, topk) |
| 107 | + return D, I |
| 108 | + |
| 109 | +class GeoImageDataset(Dataset): |
| 110 | + def __init__(self, dataframe, img_folder, topn, vision_processor, database_df, I): |
| 111 | + self.dataframe = dataframe |
| 112 | + self.img_folder = img_folder |
| 113 | + self.topn = topn |
| 114 | + self.vision_processor = vision_processor |
| 115 | + self.database_df = database_df |
| 116 | + self.I = I |
| 117 | + |
| 118 | + def __len__(self): |
| 119 | + return len(self.dataframe) |
| 120 | + |
| 121 | + def __getitem__(self, idx): |
| 122 | + img_path = f'{self.img_folder}/{self.dataframe.loc[idx, "IMG_ID"]}' |
| 123 | + image = Image.open(img_path).convert('RGB') |
| 124 | + image = self.vision_processor(images=image, return_tensors='pt')['pixel_values'].reshape(3,224,224) |
| 125 | + |
| 126 | + gps_data = [] |
| 127 | + search_top1_latitude, search_top1_longitude = self.database_df.loc[self.I[idx][0], ['LAT', 'LON']].values |
| 128 | + rag_5, rag_10, rag_15, zs = [],[],[],[] |
| 129 | + for j in range(self.topn): |
| 130 | + gps_data.extend([ |
| 131 | + float(self.dataframe.loc[idx, f'5_rag_{j}_latitude']), |
| 132 | + float(self.dataframe.loc[idx, f'5_rag_{j}_longitude']), |
| 133 | + float(self.dataframe.loc[idx, f'10_rag_{j}_latitude']), |
| 134 | + float(self.dataframe.loc[idx, f'10_rag_{j}_longitude']), |
| 135 | + float(self.dataframe.loc[idx, f'15_rag_{j}_latitude']), |
| 136 | + float(self.dataframe.loc[idx, f'15_rag_{j}_longitude']), |
| 137 | + float(self.dataframe.loc[idx, f'zs_{j}_latitude']), |
| 138 | + float(self.dataframe.loc[idx, f'zs_{j}_longitude']), |
| 139 | + search_top1_latitude, |
| 140 | + search_top1_longitude |
| 141 | + ]) |
| 142 | + |
| 143 | + gps_data = np.array(gps_data).reshape(-1, 2) |
| 144 | + return image, gps_data, idx |
| 145 | + |
| 146 | +def evaluate(args, I): |
| 147 | + print('start evaluation') |
| 148 | + if args.database == 'mp16': |
| 149 | + database = args.database_df |
| 150 | + df = args.dataset_df |
| 151 | + df['NN_idx'] = I[:, 0] |
| 152 | + df['LAT_pred'] = df.apply(lambda x: database.loc[x['NN_idx'],'LAT'], axis=1) |
| 153 | + df['LON_pred'] = df.apply(lambda x: database.loc[x['NN_idx'],'LON'], axis=1) |
| 154 | + |
| 155 | + df_llm = pd.read_csv(f'./data/{args.dataset}/{args.dataset}_prediction.csv') |
| 156 | + model = torch.load('./checkpoints/g3.pth', map_location='cuda:0') |
| 157 | + topn = 5 # number of candidates |
| 158 | + |
| 159 | + dataset = GeoImageDataset(df_llm, f'./data/{args.dataset}/images', topn, vision_processor=model.vision_processor, database_df=database, I=I) |
| 160 | + data_loader = DataLoader(dataset, batch_size=256, shuffle=False, num_workers=16, pin_memory=True) |
| 161 | + |
| 162 | + for images, gps_batch, indices in tqdm(data_loader): |
| 163 | + images = images.to(args.device) |
| 164 | + image_embeds = model.vision_projection_else_2(model.vision_projection(model.vision_model(images)[1])) |
| 165 | + image_embeds = image_embeds / image_embeds.norm(p=2, dim=-1, keepdim=True) # b, 768 |
| 166 | + |
| 167 | + gps_batch = gps_batch.to(args.device) |
| 168 | + gps_input = gps_batch.clone().detach() |
| 169 | + b, c, _ = gps_input.shape |
| 170 | + gps_input = gps_input.reshape(b*c, 2) |
| 171 | + location_embeds = model.location_encoder(gps_input) |
| 172 | + location_embeds = model.location_projection_else(location_embeds.reshape(b*c, -1)) |
| 173 | + location_embeds = location_embeds / location_embeds.norm(p=2, dim=-1, keepdim=True) |
| 174 | + location_embeds = location_embeds.reshape(b, c, -1) # b, c, 768 |
| 175 | + |
| 176 | + similarity = torch.matmul(image_embeds.unsqueeze(1), location_embeds.permute(0, 2, 1)) # b, 1, c |
| 177 | + similarity = similarity.squeeze(1).cpu().detach().numpy() |
| 178 | + max_idxs = np.argmax(similarity, axis=1) |
| 179 | + |
| 180 | + # update DataFrame |
| 181 | + for i, max_idx in enumerate(max_idxs): |
| 182 | + final_idx = indices[i] |
| 183 | + final_idx = final_idx.item() |
| 184 | + final_latitude, final_longitude = gps_batch[i][max_idx] |
| 185 | + final_latitude, final_longitude = final_latitude.item(), final_longitude.item() |
| 186 | + if final_latitude < -90 or final_latitude > 90: |
| 187 | + final_latitude = 0 |
| 188 | + if final_longitude < -180 or final_longitude > 180: |
| 189 | + final_longitude = 0 |
| 190 | + df.loc[final_idx, 'LAT_pred'] = final_latitude |
| 191 | + df.loc[final_idx, 'LON_pred'] = final_longitude |
| 192 | + |
| 193 | + df['geodesic'] = df.apply(lambda x: geodesic((x['LAT'], x['LON']), (x['LAT_pred'], x['LON_pred'])).km, axis=1) |
| 194 | + print(df.head()) |
| 195 | + df.to_csv(f'./data/{args.dataset}_{args.index}_results.csv', index=False) |
| 196 | + |
| 197 | + # 1, 25, 200, 750, 2500 km level |
| 198 | + print('2500km level: ', df[df['geodesic'] < 2500].shape[0] / df.shape[0]) |
| 199 | + print('750km level: ', df[df['geodesic'] < 750].shape[0] / df.shape[0]) |
| 200 | + print('200km level: ', df[df['geodesic'] < 200].shape[0] / df.shape[0]) |
| 201 | + print('25km level: ', df[df['geodesic'] < 25].shape[0] / df.shape[0]) |
| 202 | + print('1km level: ', df[df['geodesic'] < 1].shape[0] / df.shape[0]) |
| 203 | + |
| 204 | +if __name__ == '__main__': |
| 205 | + |
| 206 | + res = faiss.StandardGpuResources() |
| 207 | + |
| 208 | + parser = argparse.ArgumentParser() |
| 209 | + parser.add_argument('--index', type=str, default='g3') |
| 210 | + parser.add_argument('--dataset', type=str, default='im2gps3k') |
| 211 | + parser.add_argument('--database', type=str, default='mp16') |
| 212 | + args = parser.parse_args() |
| 213 | + if args.dataset == 'im2gps3k': |
| 214 | + args.dataset_df = pd.read_csv('./data/im2gps3k/im2gps3k_places365.csv') |
| 215 | + elif args.dataset == 'yfcc4k': |
| 216 | + args.dataset_df = pd.read_csv('./data/yfcc4k/yfcc4k_places365.csv') |
| 217 | + |
| 218 | + if args.database == 'mp16': |
| 219 | + args.database_df = pd.read_csv('./data/MP16_Pro_filtered.csv') |
| 220 | + |
| 221 | + args.device = "cuda" if torch.cuda.is_available() else "cpu" |
| 222 | + |
| 223 | + if not os.path.exists(f'./index'): os.makedirs(f'./index') |
| 224 | + if not os.path.exists(f'./index/{args.index}.index'): |
| 225 | + build_index(args) |
| 226 | + else: |
| 227 | + # gpu_index_flat = faiss.index_cpu_to_gpu(res, 0, index) |
| 228 | + if not os.path.exists(f'./index/I_{args.index}_{args.dataset}.npy'): |
| 229 | + index = faiss.read_index(f'./index/{args.index}.index') |
| 230 | + print('read index success') |
| 231 | + D,I = search_index(args, index, 20) |
| 232 | + np.save(f'./index/D_{args.index}_{args.dataset}.npy', D) |
| 233 | + np.save(f'./index/I_{args.index}_{args.dataset}.npy', I) |
| 234 | + else: |
| 235 | + D = np.load(f'./index/D_{args.index}_{args.dataset}.npy') |
| 236 | + I = np.load(f'./index/I_{args.index}_{args.dataset}.npy') |
| 237 | + evaluate(args, I) |
| 238 | + |
0 commit comments