-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_aro.py
70 lines (53 loc) · 2.88 KB
/
main_aro.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
import argparse
import os
import pandas as pd
from torch.utils.data import DataLoader
from model_zoo import get_model
from dataset_zoo import get_dataset
from misc import seed_all, _default_collate, save_scores
def config():
parser = argparse.ArgumentParser()
parser.add_argument("--device", default="cuda", type=str)
parser.add_argument("--batch-size", default=256, type=int)
parser.add_argument("--num_workers", default=16, type=int)
parser.add_argument("--model-name", default="openai-clip:ViT-B/32", type=str, \
choices=["openai-clip:ViT-B/32", "openai-clip:ViT-L/14", \
"NegCLIP", "laion-clip:roberta-ViT-B/32", \
"coca", "xvlm-pretrained-4m", "xvlm-pretrained-16m", \
"blip-base-14m", "blip-base-129m", "flava", \
"coca-cap", "xvlm-flickr", "xvlm-coco", \
"blip-flickr-base", "blip-coco-base"])
parser.add_argument("--dataset", default="VG_Relation", type=str, \
choices=["VG_Relation", "VG_Attribution", "COCO_Order", \
"Flickr30k_Order", "Controlled_Images_A", "Controlled_Images_B", \
"COCO_QA_one_obj", "COCO_QA_two_obj", "VG_QA_one_obj", "VG_QA_two_obj"])
parser.add_argument("--seed", default=1, type=int)
parser.add_argument("--download", action="store_true", help="Whether to download the dataset if it doesn't exist. (Default: False)")
parser.add_argument("--save-scores", action="store_true", help="Whether to save the scores for the retrieval to analyze later.")
parser.add_argument("--output-dir", default="./outputs", type=str)
return parser.parse_args()
def main(args):
seed_all(args.seed)
model, image_preprocess = get_model(args.model_name, args.device)
dataset = get_dataset(args.dataset, image_preprocess=image_preprocess, download=args.download)
# For some models we just pass the PIL images, so we'll need to handle them in the collate_fn.
collate_fn = _default_collate if image_preprocess is None else None
joint_loader = DataLoader(dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, collate_fn=collate_fn)
scores = model.get_retrieval_scores_batched(joint_loader)
result_records = dataset.evaluate_scores(scores)
for record in result_records:
record.update({"Model": args.model_name, "Dataset": args.dataset, "Seed": args.seed})
output_file = os.path.join(args.output_dir, f"{args.dataset}.csv")
df = pd.DataFrame(result_records)
print(f"Saving results to {output_file}")
if os.path.exists(output_file):
all_df = pd.read_csv(output_file, index_col=0)
all_df = pd.concat([all_df, df])
all_df.to_csv(output_file)
else:
df.to_csv(output_file)
if args.save_scores:
save_scores(scores, args)
if __name__ == "__main__":
args = config()
main(args)