-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
169 lines (139 loc) · 5.18 KB
/
main.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
import argparse
from utils.sys_utils import *
from utils.vsum_tools import *
from tools.funcs import *
import importlib
import time
def eval_split(hps, splits_filename, data_dir="test"):
print("\n")
model = engine.sumnet(hps)
model.initialize()
model.load_datasets()
model.load_split_file(splits_filename)
val_fscores = []
for split_id in range(len(model.splits)):
model.select_split(split_id)
weights_filename = model.lookup_weights_file(data_dir)
print("Loading model:", weights_filename)
model.load_model(weights_filename)
val_fscore, video_scores = model.eval(model.test_keys)
val_fscores.append(val_fscore)
val_fscore_avg = np.mean(val_fscores)
if hps.verbose:
video_scores = [["No.", "Video", "F-score"]] + video_scores
print_table(video_scores, cell_width=[4, 45, 5])
print("Avg F-score: ", val_fscore)
print("")
print("Total AVG F-score: ", val_fscore_avg)
return val_fscore_avg
def train(hps):
os.makedirs(hps.output_dir, exist_ok=True)
os.makedirs(os.path.join(hps.output_dir, "models"), exist_ok=True)
# Create a file to collect results from all splits
f = open(hps.output_dir + "/results.txt", "wt")
for split_filename in hps.splits:
dataset_name, dataset_type, splits = parse_splits_filename(split_filename)
# For no augmentation use only a dataset corresponding to the split file
datasets = None
if dataset_type == "":
datasets = hps.get_dataset_by_name(dataset_name)
if datasets is None:
datasets = hps.datasets
f_avg = 0
n_folds = len(splits)
for split_id in range(n_folds):
model = engine.sumnet(hps)
model.initialize()
model.load_datasets(datasets=datasets)
model.load_split_file(splits_file=split_filename)
model.select_split(split_id=split_id)
fscore, fscore_epoch = model.train(output_dir=hps.output_dir)
f_avg += fscore
# Log F-score for this split_id
f.write(
split_filename
+ ", "
+ str(split_id)
+ ", "
+ str(fscore)
+ ", "
+ str(fscore_epoch)
+ "\n"
)
f.flush()
# Save model with the highest F score
_, log_file = os.path.split(split_filename)
log_dir, _ = os.path.splitext(log_file)
log_dir += "_" + str(split_id)
log_file = os.path.join(hps.output_dir, "models", log_dir) + ".tar.pth"
os.makedirs(
os.path.join(
hps.output_dir,
"models",
),
exist_ok=True,
)
os.system(
"mv "
+ hps.output_dir
+ "/models_temp/"
+ log_dir
+ "/"
+ str(fscore_epoch)
+ "_*.pth.tar "
+ log_file
)
os.system("rm -rf " + hps.output_dir + "/models_temp")
print(
"Split: {0:} Best F-score: {1:0.5f} Model: {2:}".format(
split_filename, fscore, log_file
)
)
# Write average F-score for all splits to the results.txt file
f_avg /= n_folds
f.write(split_filename + ", " + str("avg") + ", " + str(f_avg) + "\n")
f.flush()
f.close()
if __name__ == "__main__":
print_pkg_versions()
parser = argparse.ArgumentParser("PyTorch implementation of VJMHT")
parser.add_argument(
"-d",
"--datasets",
type=str,
help="Path to a comma separated list of h5 datasets",
)
parser.add_argument(
"-s", "--splits", type=str, help="Comma separated list of split files."
)
parser.add_argument("-e", "--eval", action="store_true", help="eval")
parser.add_argument(
"-v", "--verbose", action="store_true", help="Prints out more messages"
)
parser.add_argument("-c", "--config", type=str, default="cfg", help="config file")
args = parser.parse_args()
# MAIN
# ======================
args.config = args.config.replace("/", ".").replace(".py", "")
config = importlib.import_module(args.config)
EXP = args.config.split(".")[-1].upper()
args.output_dir = os.path.join("results", EXP)
os.makedirs(args.output_dir, exist_ok=True)
hps = config.HParameters()
engine = importlib.import_module("tools." + hps.engine)
hps.load_from_args(args.__dict__)
print("Parameters:")
print("----------------------------------------------------------------------")
print(hps)
if not hps.eval:
train(hps)
results = [["No", "Split", "Mean F-score"]]
start = time.time()
for i, split_filename in enumerate(hps.splits):
f_score = eval_split(hps, split_filename, data_dir=hps.output_dir)
results.append([i + 1, split_filename, str(round(f_score * 100.0, 3)) + "%"])
end = time.time()
print(end - start)
print("\nFinal Results:")
print_table(results)
sys.exit(0)