-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.py
150 lines (128 loc) · 5.99 KB
/
eval.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
import argparse
import json
import logging
import os
import subprocess
import tqdm
import wget
from collections import defaultdict
from datetime import datetime
from pathlib import Path
import numpy as np
import torch
import random
def extracting_log_info(log_files, experiment, logging):
metrics_t2v = defaultdict(list)
metrics_v2t = defaultdict(list)
for file_name in log_files:
output_string = f"{experiment}:\n"
with open(Path("./logs_eval") / file_name, 'r') as f:
content_lines = f.read().splitlines()
content_lines = content_lines[-14:]
for line in content_lines:
if 't2v' in line:
metric_entry = line.split('test_t2v_metrics_')[1].split(':')[0]
metrics_t2v[metric_entry].append(float(line.split('test_t2v_metrics_')[1].split(':')[1]))
elif 'v2t' in line:
metric_entry = line.split('test_v2t_metrics_')[1].split(':')[0]
metrics_v2t[metric_entry].append(float(line.split('test_v2t_metrics_')[1].split(':')[1]))
keys = list(metrics_t2v.keys())
for key in keys:
output_string += f"{key}_t2v: {np.mean(metrics_t2v[key]):.1f}, {np.std(metrics_t2v[key], ddof=1):.1f}\n"
for key in keys:
output_string += f"{key}_v2t: {np.mean(metrics_v2t[key]):.1f}, {np.std(metrics_v2t[key], ddof=1):.1f}\n"
logging.info(output_string)
with open(Path("logs_eval") / f"{experiment}_summary.txt", 'w') as f:
f.write(output_string)
def run_exp(experiments, logging):
for experiment in experiments:
logging.info(f"Now running {experiment}")
run_one_exp(experiment, experiments, logging)
def download_configs(experiment, trained_model_path, group_id, seed, timestamp):
new_folder = trained_model_path.parent
url_config = f"http://www.robots.ox.ac.uk/~vgg/research/collaborative-experts/data/models/{experiment}/{group_id}/{seed}/{timestamp}/config.json"
config_path = Path(new_folder) / 'config.json'
wget.download(url_config, out=str(config_path))
with open(config_path, 'r') as f:
config_content = json.load(f)
config_content['seed'] = int(seed[-1])
with open(config_path, 'w') as f:
json.dump(config_content, f)
def download_models(experiment, logging, trained_model_path,
group_id, seed, timestamp):
new_folder = trained_model_path.parent
if os.path.exists(trained_model_path) is False:
logging.info(f"Downloading model for {seed} since it does not exist on the local machine")
url = f"http://www.robots.ox.ac.uk/~vgg/research/collaborative-experts/data/models/{experiment}/{group_id}/{seed}/{timestamp}/trained_model.pth"
# import pdb; pdb.set_trace()
new_folder.mkdir(exist_ok=True, parents=True)
wget.download(url, out=str(trained_model_path))
else:
logging.info(f"Model already downloaded for {experiment} seed {seed}")
if os.path.exists(new_folder / 'config.json') is False:
download_configs(experiment, trained_model_path, group_id, seed, timestamp)
else:
logging.info(f"Config already downloaded for {experiment} seed {seed}")
def run_one_exp(experiment, experiments, logging):
if 'mmt' in experiment:
if 'clotho' in experiment:
ds = 'Clotho'
elif 'audiocaps' in experiment:
ds = 'AudioCaps'
elif 'sounddescs' in experiment:
ds = 'SoundDescs'
cmd = f"python mmt/train.py --only_eval --config mmt/configs/%s_mmt.json --experiment %s"%(ds,experiment)
subprocess.call(cmd, shell=True)
else:
group_id = experiments[experiment][0]
with open('exp_to_seed_time.json', 'r') as f:
json_dict = json.load(f)
log_files = []
for (group_id, seed, timestamp) in json_dict[experiment]:
group_id_path = Path("data/saved/models") / experiment / group_id
logging.info("Running evaluation on existent seeds")
(Path("logs_eval")).mkdir(exist_ok=True, parents=True)
trained_model_path = group_id_path / seed / timestamp / 'trained_model.pth'
download_models(experiment, logging, trained_model_path,
group_id, seed, timestamp)
config_path = group_id_path / seed / timestamp / 'config.json'
cmd = f"python test.py --config {config_path} --resume {trained_model_path} --device 0 --eval_from_training_config 2>&1 | tee logs_eval/log_{group_id}_{seed}.txt"
# if using windows, comment the line above and uncomment the line below
# cmd = f"python test.py --config {config_path} --resume {trained_model_path} --device 0 --eval_from_training_config 2>&1 | wtee logs_eval/log_{group_id}_{seed}.txt"
log_files.append(f"log_{group_id}_{seed}.txt")
logging.info(cmd)
subprocess.call(cmd, shell=True)
logging.info("Now averaging results")
extracting_log_info(log_files, experiment, logging)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--experiments_path", default="misc/experiments-audiocaps.json")
parser.add_argument("--experiment", type=str, default=None)
parser.add_argument(
"--data_dir",
type=Path,
default="data",
)
parser.add_argument(
"--dataset",
type=str,
default="data",
)
parser.add_argument(
"--refresh",
action="store_true",
)
args = parser.parse_args()
os.makedirs('logs', exist_ok=True)
logging.basicConfig(filename=f"logs/{datetime.now().strftime(r'%m%d_%H%M%S')}.log",
level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler())
logging.info(args)
with open(args.experiments_path, "r") as f:
experiments = json.load(f)
if args.experiment is None:
run_exp(experiments, logging)
else:
run_one_exp(args.experiment, experiments, logging)
if __name__ == "__main__":
main()