forked from allenai/savn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_eval.py
92 lines (75 loc) · 2.09 KB
/
main_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
from __future__ import print_function, division
import os
os.environ["OMP_NUM_THREADS"] = "1"
import torch
import torch.multiprocessing as mp
import time
import numpy as np
import random
import json
from tqdm import tqdm
from utils.net_util import ScalarMeanTracker
from runners import nonadaptivea3c_val, savn_val
def main_eval(args, create_shared_model, init_agent):
np.random.seed(args.seed)
torch.manual_seed(args.seed)
random.seed(args.seed)
if args.gpu_ids == -1:
args.gpu_ids = [-1]
else:
torch.cuda.manual_seed(args.seed)
try:
mp.set_start_method("spawn")
except RuntimeError:
pass
model_to_open = args.load_model
processes = []
res_queue = mp.Queue()
if args.model == "BaseModel" or args.model == "GCN":
args.learned_loss = False
args.num_steps = 50
target = nonadaptivea3c_val
else:
args.learned_loss = True
args.num_steps = 6
target = savn_val
rank = 0
for scene_type in args.scene_types:
p = mp.Process(
target=target,
args=(
rank,
args,
model_to_open,
create_shared_model,
init_agent,
res_queue,
250,
scene_type,
),
)
p.start()
processes.append(p)
time.sleep(0.1)
rank += 1
count = 0
end_count = 0
train_scalars = ScalarMeanTracker()
proc = len(args.scene_types)
pbar = tqdm(total=250 * proc)
try:
while end_count < proc:
train_result = res_queue.get()
pbar.update(1)
count += 1
if "END" in train_result:
end_count += 1
continue
train_scalars.add_scalars(train_result)
tracked_means = train_scalars.pop_and_reset()
finally:
for p in processes:
time.sleep(0.1)
p.join()
with open(args.results_json, "w") as fp:
json.dump(tracked_means, fp, sort_keys=True, indent=4)