Spaces:
Runtime error
Runtime error
File size: 20,949 Bytes
f638d9c |
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# %%
import json
import sys
import pickle
sys.path.append("../")
import collections
from models.fused_model import Model
import os
import tqdm
import time
import json
import random
from PIL import ImageFile
from PIL import Image, ImageDraw
import clip
import torch
import numpy as np
import torchvision.transforms as T
import torchvision.transforms.functional as F
from pathlib import Path
import pandas as pd
ImageFile.LOAD_TRUNCATED_IMAGES = True
# %%
from types import SimpleNamespace
# get config
import os
from omegaconf import OmegaConf
from hydra.core.global_hydra import GlobalHydra
from hydra import initialize, initialize_config_module, initialize_config_dir, compose
os.environ['ROOT'] = os.path.dirname(os.path.realpath(__file__))
os.environ['DATA_ROOT'] = os.path.join(os.environ['ROOT'], 'data')
# initialize hydra config
GlobalHydra.instance().clear()
initialize(config_path="./config")
config = compose(config_name='with_decoder.yaml',
overrides=["clip_model=ViT-L/14@336px",
"rationale_type=0", "val_rationale_type=0"])
class SquarePad:
def __call__(self, image):
max_wh = max(image.size)
p_left, p_top = [(max_wh - s) // 2 for s in image.size]
p_right, p_bottom = [max_wh - (s + pad) for s, pad in zip(image.size, [p_left, p_top])]
padding = (p_left, p_top, p_right, p_bottom)
return F.pad(image, padding, 0, 'constant')
class VarDatasetForAuxEncoders:
def __init__(self, config, file_path, split="train", mode="combined", do_swap=False, tensorize=True, do_crop=True):
self.config = config
self.mode = mode
self.split = split
self.do_swap = do_swap
self.rationale_type = config.rationale_type if split == "train" else config.val_rationale_type
self.root_path = Path(config.root)
self.anno_path = file_path #self.root_path / f'annotations/13_05/anno_{split}_{mode}.json'
if split == "test" and mode == "combined" and config.overfit:
self.anno_path = self.root_path / f'annotations/13_05/anno_{split}_{mode}_overfit.json'
self.data = json.load(open(self.anno_path))
self.idx2name = list(self.data.keys())
if 'bounding_box' in self.data[list(self.data.keys())[0]]['details'][-1]:
self.one_ent_keys = [k for k, v in self.data.items() if len(v['details'][-1]["bounding_box"]) == 1]
self.two_ent_keys = [k for k, v in self.data.items() if len(v['details'][-1]["bounding_box"]) == 2]
self.three_ent_keys = [k for k, v in self.data.items() if len(v['details'][-1]["bounding_box"]) == 3]
self.all_ent_keys = self.one_ent_keys + self.two_ent_keys + self.three_ent_keys
self.keys = {1: self.one_ent_keys, 2: self.two_ent_keys, 3: self.three_ent_keys}
if self.config.widescreen_processing in [0, 1]:
self.resize_crop = self.get_transform(config.img_size, split == "train", padding=False)
else:
self.resize_crop = self.get_transform(config.img_size, split == "train", padding=True)
self.tensorize = tensorize
self.jitter_transform = T.ColorJitter(brightness=.5, hue=.3, saturation=.3) if split == "train" else lambda x: x
self.final_transform = T.Compose([
lambda image: image.convert("RGB"),
T.ToTensor() if tensorize else lambda x: x,
T.Normalize(
(0.48145466, 0.4578275, 0.40821073),
(0.26862954, 0.26130258, 0.27577711),
) if tensorize else lambda x: x
])
def get_transform(self, n_px, training, padding=False):
resize = T.Resize((n_px + 16, n_px + 16), interpolation=Image.BICUBIC)
# for traning split
if training and not padding: # train
return T.Compose([resize, T.RandomCrop(n_px)])
if training and padding: # train_pad
return T.Compose([SquarePad(), resize, T.RandomCrop(n_px)])
# for test and val split
if not training and not padding: # test
return T.Compose([resize, T.CenterCrop(n_px)])
if not training and padding: # test_pad
return T.Compose([SquarePad(), resize, T.CenterCrop(n_px)])
def key2img_path(self, key):
file_paths = [
self.root_path / f"var_images/{key}.jpg",
self.root_path / f"var_images/{key}.png",
self.root_path / f"images/{key}.jpg",
self.root_path / f"img/train/{key.split('_')[0]}/{key}.png",
self.root_path / f"img/val/{key.split('_')[0]}/{key}.png",
self.root_path / f"img/test/{key.split('_')[0]}/{key}.png",
self.root_path / f"img/{key}.png",
self.root_path / f"img/{key}.jpg",
self.root_path / f"images/{key}.png",
self.root_path / f"images/{key}.jpg",
]
for file_path in file_paths:
if file_path.exists():
return file_path
def key2img(self, key):
file_path = self.key2img_path(key)
return Image.open(file_path)
def hide_region(self, image, bboxes):
image = image.convert('RGBA')
if self.config.hide_true_bbox == 1: # hide mode
draw = ImageDraw.Draw(image, 'RGBA')
if self.config.hide_true_bbox in [2, 5, 7, 8, 9]: #highlight mode
overlay = Image.new('RGBA', image.size, '#00000000')
draw = ImageDraw.Draw(overlay, 'RGBA')
if self.config.hide_true_bbox == 3 or self.config.hide_true_bbox == 6: #blackout mode or position only mode
overlay = Image.new('RGBA', image.size, '#7B7575ff')
draw = ImageDraw.Draw(overlay, 'RGBA')
color_fill_list = ['#ff05cd3c', '#00F1E83c', '#F2D4003c'] # Green, Blue, Yellow?
for idx, bbox in enumerate(bboxes):
if bbox == None:
continue
color_fill = color_fill_list[idx]
x, y = bbox['left'], bbox['top']
if self.config.hide_true_bbox == 1: # hide mode
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill='#7B7575')
elif self.config.hide_true_bbox in [2, 5, 7, 8, 9]: # highlight mode
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill=color_fill, outline='#05ff37ff',
width=3) # Fill with Pink 60% ##00F1E8
elif self.config.hide_true_bbox == 3: # blackout mode
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill='#00000000')
elif self.config.hide_true_bbox == 6: # position only mode
draw.rectangle([(x, y), (x + bbox['width'], y + bbox['height'])], fill=color_fill)
if self.config.hide_true_bbox in [2, 3, 5, 6, 7, 8, 9]:
image = Image.alpha_composite(image, overlay)
return image
def get_entity_codes(self):
entity_codes = [0, 1, 2]
if self.do_swap:
random.shuffle(entity_codes)
return entity_codes
def swap_entities(self, bboxes, text, entity_codes):
# text
for entity_idx, entity_code in enumerate(entity_codes):
text = text.replace(f"Entity #{entity_idx + 1}", f"Entity #{entity_code + 1}")
# bboxes: [1, 0, 2] -> [b[1], b[0], b[2]]
new_boxes = [bboxes[entity_code] for entity_code in entity_codes]
return new_boxes, text
def get_text_from_meta(self, meta):
n_boxes = len(meta['bounding_box']) # key ['1', '2', '3']
# for rationale
text = 'Rationale: ' + str(meta['rationale'])
if self.rationale_type == 1 or self.rationale_type == 2:
for box_idx in range(n_boxes):
ent_name = f'Entity #{box_idx + 1}'
ent_desc = f'{ent_name}, {meta[ent_name]}'
# todo: replace randomly
text = text.replace(ent_name, ent_desc, 1)
return text
def get_itm_text(self, ori_file_key):
file_key = ori_file_key
if random.random() < 0.5:
n_boxes = len(self.data[file_key]['details'][-1]['bounding_box'])
file_key = random.choice(self.keys[n_boxes])
if self.config.get('no_hard_negative_itm', False):
file_key = random.choice(self.all_ent_keys)
itm_label = 1 if file_key == ori_file_key else 0
meta = self.data[file_key]['details'][-1]
itm_text = self.get_text_from_meta(meta)
return itm_text, itm_label
def get_bboxes_and_text(self, file_key, meta):
text = self.get_text_from_meta(meta)
bboxes = [meta['bounding_box'].get(str(box_idx + 1), None) for box_idx in range(3)]
entity_codes = self.get_entity_codes()
bboxes, text = self.swap_entities(bboxes, text, entity_codes)
itm_text, itm_label = self.get_itm_text(file_key)
_, itm_text = self.swap_entities([None, None, None], itm_text, entity_codes)
return {'bboxes': bboxes, 'text': text, 'itm_text': itm_text, 'itm_label': itm_label}
def get_image(self, file_key, bboxes):
image = self.key2img(file_key)
image = self.jitter_transform(image)
image = self.hide_region(image, bboxes)
image = self.final_transform(self.resize_crop(image))
return image
def __getitem__(self, idx):
file_key = self.idx2name[idx]
# Select the last version of label of the sample
meta = self.data[file_key]['details'][-1]
# read bboxes and rationale
outputs = self.get_bboxes_and_text(file_key, meta)
text = clip.tokenize(outputs['text'], truncate=True).squeeze()
itm_text = clip.tokenize(outputs['itm_text'], truncate=True).squeeze()
itm_label = torch.tensor(outputs['itm_label'])
image = self.get_image(file_key, outputs['bboxes'])
return {'image': image, 'caption': text, 'raw_text': text, 'file_key': file_key, 'itm_text': itm_text, 'itm_label': itm_label}
def __len__(self):
if self.config.overfit and not (self.split == 'test' and self.mode == 'combined'):
return 16
return len(self.data)
# %%
class VarDatasetImageOnly(VarDatasetForAuxEncoders):
def __init__(self, args, file_path, split="val", mode="combined", do_swap= False):
super().__init__(args, file_path, split=split, mode=mode, do_swap=do_swap)
def __getitem__(self, idx):
file_key = self.idx2name[idx]
meta = self.data[file_key]['details'][-1]
bboxes = [meta['bounding_box'].get(str(box_idx + 1), None) for box_idx in range(3)]
entity_codes = self.get_entity_codes()
bboxes = [bboxes[entity_code] for entity_code in entity_codes]
image = self.get_image(file_key, bboxes)
return {'image': image, 'file_key': file_key}
# %%
class VarDatasetTextOnly(VarDatasetForAuxEncoders):
def __init__(self, args, file_path, split="val", mode="combined", do_swap= False):
super().__init__(args, file_path, split=split, mode=mode, do_swap=do_swap)
def __getitem__(self, idx):
file_key = self.idx2name[idx]
meta = self.data[file_key]['details'][-1]
# text = self.get_text_from_meta(meta)
if 'Entity #3' in meta['hazard']:
n_boxes = 3
elif 'Entity #2' in meta['hazard']:
n_boxes = 2
else:
n_boxes = 1
# for rationale
text = 'Rationale: ' + str(meta['hazard'])
if self.rationale_type == 1 or self.rationale_type == 2:
for box_idx in range(n_boxes):
ent_name = f'Entity #{box_idx + 1}'
ent_desc = f'{ent_name}, {meta[ent_name]}'
# todo: replace randomly
text = text.replace(ent_name, ent_desc, 1)
entity_codes = self.get_entity_codes()
for entity_idx, entity_code in enumerate(entity_codes):
text = text.replace(f"Entity #{entity_idx + 1}", f"Entity #{entity_code + 1}")
text = clip.tokenize(text, truncate=True).squeeze()
return {'caption': text,'file_key': file_key}
# %%
import os
import sys
sys.path.append('..')
import json
import fire
import tqdm
import clip
import torch
import sklearn
import numpy as np
from omegaconf import OmegaConf
from models.fused_model import Model
from torch.utils.data import DataLoader
# from datasets import VarDatasetForAuxEncoders
from scipy.stats import rankdata
from sklearn.metrics import ndcg_score
from sklearn.metrics import pairwise_distances
# def get_data_loader(config, split="test", mode="combined", do_swap=False):
# dataset = VarDatasetForAuxEncoders(config, split=split, mode=mode, do_swap=do_swap)
# return DataLoader(dataset, batch_size=4, shuffle=False)
def get_image_data_loader(config, file_path, split="test", mode="combined", do_swap=False):
dataset = VarDatasetImageOnly(config, file_path, split=split, mode=mode, do_swap=do_swap)
return DataLoader(dataset, batch_size=4, shuffle=False)
def get_text_data_loader(config, file_path, split="test", mode="combined", do_swap=False):
dataset = VarDatasetTextOnly(config, file_path, split=split, mode=mode, do_swap=do_swap)
return DataLoader(dataset, batch_size=4, shuffle=False)
# def get_data_loader(config, split="test", mode="combined", do_swap=False):
# dataset = VarDatasetForAuxEncoders(config, split=split, mode=mode, do_swap=do_swap)
# return DataLoader(dataset, batch_size=4, shuffle=False)
def compute_rand_rank(split='test', mode='spec', img_token_dict={}, txt_token_dict={}): # the dicts contain all 2000 test samples
data = json.load(open( os.path.join(os.environ['ROOT'], f"data/annotations/13_05/anno_random_{split}_{mode}_ids.json")))
i2t_ranks = []
t2i_ranks = []
i2t_rank_dict = {}
t2i_rank_dict = {}
for file_key in data.keys():
img_emb = (img_token_dict[file_key]).unsqueeze(0)
txt_emb = (txt_token_dict[file_key]).unsqueeze(0)
txt_embs = torch.stack([txt_token_dict[k] for k in data[file_key]])
img_embs = torch.stack([img_token_dict[k] for k in data[file_key]])
assert txt_embs.shape[0] == img_embs.shape[0] == 1000
i2t_rank = rankdata(pairwise_distances(img_emb, txt_embs, metric='cosine', n_jobs=8), axis=1)[0]
t2i_rank = rankdata(pairwise_distances(txt_emb, img_embs, metric='cosine', n_jobs=8), axis=1)[0]
i2t_ranks.append(i2t_rank[-1])
t2i_ranks.append(t2i_rank[-1])
i2t_rank_dict[file_key] = i2t_rank
t2i_rank_dict[file_key] = t2i_rank
assert len(i2t_ranks) == len(t2i_ranks) == 1000
print(f"Random split, mode={mode} i2t rank: ", sum(i2t_ranks) / len(i2t_ranks))
print(f"Random split, mode={mode} t2i rank: ", sum(t2i_ranks) / len(t2i_ranks))
# for k in i2t_rank_dict.keys():
# print(k, i2t_rank_dict[k])
# print('------------------')
# break
return i2t_rank_dict # for computing the NDCG scores
def read_relevance_scores(anno_path="anno_random_test_obvi_ids.json", gpt_path="chatgpt_similarity_score_test_direct_combined.json"):
gpt_scores = json.load(open(gpt_path))
data = json.load(open(anno_path))
# add_missing_relevance_scores
for k in tqdm.tqdm(data, total=len(data)):
cand_keys = data[k]
for cand_key in cand_keys:
if cand_key not in gpt_scores[k]:
gpt_scores[k][cand_key] = 0.0
if cand_key == k:
gpt_scores[k][cand_key] = 1.0
return gpt_scores
# %%
def compute_ndcg(ranks, scores, k=3):
"""
ranks = [5, 1, 4, 2, 3]
scores = [0.1, 0.5, 0.3, 0.95, 1.0]
"""
rank_score_tuple = list(zip(ranks, scores))
top_k = sorted(rank_score_tuple, key=lambda x: x[1], reverse=True)[:k]
dcg = sum([score / np.log2(rank + 1) for rank, score in top_k])
ideal_dcg = sum([score / np.log2(idx + 2) for idx, (_, score) in enumerate(top_k)])
ndcg = dcg / ideal_dcg
return ndcg
def compute_ndcg_score_per_mode(pred_rank_dict, gpt_rel_scores, mode='spec', split='test', k=200):
data = json.load(open(os.path.join(os.environ['ROOT'],f"data/annotations/13_05/anno_random_{split}_{mode}_ids.json")))
ndcg_scores = []
for key in tqdm.tqdm(pred_rank_dict.keys(), total=len(pred_rank_dict.keys())):
gpt_scores_for_key = [gpt_rel_scores[key][cand_key] for cand_key in data[key]]
pred_rank_for_key = pred_rank_dict[key]
ndcg_score = compute_ndcg(pred_rank_for_key, gpt_scores_for_key, k=k)
ndcg_scores.append(ndcg_score)
avg_ndcg_score = sum(ndcg_scores) / len(ndcg_scores)
print(f"Random split, mode={mode} ndcg score: ", avg_ndcg_score)
return avg_ndcg_score
# %%
def main():
# %%
## Load Model
config_path= os.path.join(os.environ['ROOT'],"results/config.yaml")
model_path= os.path.join(os.environ['ROOT'],"results/model_epoch3.pth")
# %%
print("Loading config from:", config_path)
config = OmegaConf.load(config_path)
#print(OmegaConf.to_yaml(config))
# %%
# load checkpoint
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
print("Loaded model from:", model_path)
clip_model, _ = clip.load(config.clip_model, jit=False)
model = Model(clip_model, config)
model.load_state_dict(checkpoint['model_state_dict'])
model = model.to(config.device)
model = model.eval()
model = model.float()
logit_scale = model.clip_model.logit_scale.exp()
image_path = os.path.join(os.environ['ROOT'], "data/eval_test_image.json")
text_path = os.path.join(os.environ['ROOT'], "data/eval_test_text.json")
data_loader_image = get_image_data_loader(config, image_path, split='test', mode='combined' )
data_loader_text = get_text_data_loader(config, text_path, split='test', mode='combined' )
# %%
key_text_dict = {}
text_tensor_embedding = None
with torch.no_grad():
for i, d in tqdm.tqdm(enumerate(data_loader_text), total=len(data_loader_text)):
# print("d", d['file_key'])
# with torch.amp.autocast(device_type=config.device, dtype=torch.float16):
text_tensor_out, text_cls_out = model.var_txt_forward(d['caption'].to(config.device))
#print("text_tensor_out", text_tensor_out[0].shape)
if text_tensor_embedding == None:
text_tensor_embedding = text_cls_out
else:
text_tensor_embedding = torch.cat((text_tensor_embedding, text_cls_out), 0)
for j,key in enumerate(d['file_key']):
key_text_dict[key] = int(i*len(d['file_key']) +j)
# %%
key_image_dict = {}
image_tensor_embedding = None
with torch.no_grad():
for i, d in tqdm.tqdm(enumerate(data_loader_image), total=len(data_loader_image)):
image_tensor_out, img_cls_out = model.var_img_forward(d['image'].to(config.device))
if image_tensor_embedding == None:
image_tensor_embedding = img_cls_out
else:
image_tensor_embedding = torch.cat((image_tensor_embedding, img_cls_out), 0)
for j,key in enumerate(d['file_key']):
key_image_dict[key] = int(i*len(d['file_key']) +j)
idx2img = {idx: k for idx, k in enumerate(key_image_dict)}
idx2text = {idx: k for idx, k in enumerate(key_text_dict)}
# %%
image_tensor_embedding = image_tensor_embedding.to('cpu')
text_tensor_embedding = text_tensor_embedding.to('cpu')
# %%
similarity_matrix = pairwise_distances(image_tensor_embedding, text_tensor_embedding, metric='cosine', n_jobs=8)
# %%
results_pair_dict = {}
## put into matrix
for i in range (2000):
for j in range (2000):
results_pair_dict[str(idx2img[i])+':'+str(idx2text[j])] = float(similarity_matrix[i][j])
# %%
results_pair_dict1 = {}
results_pair_dict2 = {}
len_ = int(len(results_pair_dict)/2)
for j, key in enumerate(results_pair_dict):
if j <= len_:
results_pair_dict1[key] = results_pair_dict[key]
else:
results_pair_dict2[key] = results_pair_dict[key]
# %%
# with open(os.path.join(os.environ['ROOT'],'results_pair_dict1.json'), 'w', encoding='utf-8') as f:
# json.dump(results_pair_dict1, f, ensure_ascii=False, indent=4)
# with open(os.path.join(os.environ['ROOT'],'results_pair_dict2.json'), 'w', encoding='utf-8') as f:
# json.dump(results_pair_dict2, f, ensure_ascii=False, indent=4)
df = pd.DataFrame(results_pair_dict1.items(), columns=['key_pair','score'])
df.to_csv(os.path.join(os.environ['ROOT'],'results_pair_dict1.csv'))
df = pd.DataFrame(results_pair_dict2.items(), columns=['key_pair','score'])
df.to_csv(os.path.join(os.environ['ROOT'],'results_pair_dict2.csv'))
# %%
if __name__ == "__main__":
main()
# %%
|