-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_psychlab.py
169 lines (138 loc) · 6.37 KB
/
main_psychlab.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 os
import yaml
import pickle
import argparse
import numpy as np
import torch as T
import torch.nn as nn
import torch.multiprocessing as mp
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from collections import namedtuple
from common.shared_optim import SharedAdam, SharedRMSprop
from Harlow_PsychLab.train import train, train_stacked
from Harlow_PsychLab.harlow import HarlowWrapper
from models.a3c_lstm import A3C_LSTM, A3C_StackedLSTM
from models.a3c_conv_lstm import A3C_ConvLSTM, A3C_ConvStackedLSTM
from models.resnet_lstm import ResNet_LSTM
if __name__ == "__main__":
mp.set_start_method("spawn")
os.environ['OMP_NUM_THREADS'] = '1'
parser = argparse.ArgumentParser(description='Paramaters')
parser.add_argument('-c', '--config', type=str,
default="/home/bkhmsi/Documents/Projects/lab/Meta-RL-Harlow/Harlow_PsychLab/config.yaml",
help='path of config file')
parser.add_argument('--length', type=int, default=3600,
help='Number of steps to run the agent')
parser.add_argument('--width', type=int, default=512,
help='Horizontal size of the observations')
parser.add_argument('--height', type=int, default=512,
help='Vertical size of the observations')
parser.add_argument('--fps', type=int, default=60,
help='Number of frames per second')
parser.add_argument('--runfiles_path', type=str, default=None,
help='Set the runfiles path to find DeepMind Lab data')
parser.add_argument('--level_script', type=str,
default='contributed/psychlab/harlow',
help='The environment level script to load')
parser.add_argument('--record', type=str, default=None,
help='Record the run to a demo file')
parser.add_argument('--demo', type=str, default=None,
help='Play back a recorded demo file')
parser.add_argument('--demofiles', type=str, default=None,
help='Directory for demo files')
parser.add_argument('--video', type=str, default=None,
help='Record the demo run as a video')
args = parser.parse_args()
with open(args.config, 'r', encoding="utf-8") as fin:
config = yaml.load(fin, Loader=yaml.FullLoader)
task_config = {
'fps': str(args.fps),
'width': str(args.width),
'height': str(args.height)
}
if args.record:
task_config['record'] = args.record
if args.demo:
task_config['demo'] = args.demo
if args.demofiles:
task_config['demofiles'] = args.demofiles
if args.video:
task_config['video'] = args.video
n_seeds = 1
base_seed = config["seed"]
base_run_title = config["run-title"]
for seed_idx in range(1, n_seeds + 1):
config["run-title"] = base_run_title + f"_{seed_idx}"
config["seed"] = base_seed * seed_idx
exp_path = os.path.join(config["save-path"], config["run-title"])
if not os.path.isdir(exp_path):
os.mkdir(exp_path)
out_path = os.path.join(exp_path, os.path.basename(args.config))
with open(out_path, 'w') as fout:
yaml.dump(config, fout)
############## Start Here ##############
print(f"> Running {config['run-title']} {config['mode']} using {config['optimizer']}")
if config["mode"] == "resnet":
shared_model = ResNet_LSTM(config["agent"], config["task"]["num-actions"])
elif config["mode"] == "conv-stacked":
shared_model = A3C_ConvStackedLSTM(config["agent"], config["task"]["num-actions"])
elif config["mode"] == "stacked":
shared_model = A3C_StackedLSTM(config["agent"], config["task"]["num-actions"])
elif config["mode"] == "conv-vanilla":
shared_model = A3C_ConvLSTM(config["agent"], config["task"]["num-actions"])
elif config["mode"] == "vanilla":
shared_model = A3C_LSTM(config["agent"], config["task"]["num-actions"])
else:
raise ValueError(config["mode"])
print(shared_model)
shared_model.share_memory()
shared_model.to(config['device'])
optim_class = SharedAdam if config["optimizer"] == "adam" else SharedRMSprop
optimizer = optim_class(shared_model.parameters(), lr=config["agent"]["lr"])
optimizer.share_memory()
processes = []
update_counter = 0
T.manual_seed(config["seed"])
np.random.seed(config["seed"])
T.random.manual_seed(config["seed"])
if config["copy-encoder"]:
filepath = os.path.join(
config["save-path"],
config["load-title"],
f"{config['load-title']}_{config['start-episode']}.pt"
)
print(f"> Copying Encoder from {filepath}")
pretrained_dict = T.load(filepath, map_location=T.device(config["device"]))["state_dict"]
load_dict = shared_model.state_dict()
for k, v in pretrained_dict.items():
if k in "encoder": load_dict[k] = v
shared_model.load_state_dict(load_dict)
if config["resume"]:
filepath = os.path.join(
config["save-path"],
config["load-title"],
f"{config['load-title']}_{config['start-episode']:04d}.pt"
)
print(f"> Loading Checkpoint {filepath}")
model_data = T.load(filepath, map_location=T.device(config["device"]))
update_counter = model_data["update_counter"]
pretrained_dict = model_data["state_dict"]
# load_dict = {}
# for i, (k, v) in enumerate(pretrained_dict.items()):
# load_dict[k] = v if i < 6 else eval(f"shared_model.{k}")
shared_model.load_state_dict(pretrained_dict)
train_target = train_stacked if "stacked" in config["mode"] else train
for rank in range(config["agent"]["n-workers"]):
p = mp.Process(target=train_target, args=(
config,
shared_model,
optimizer,
rank,
task_config,
update_counter,
))
p.start()
processes += [p]
for p in processes:
p.join()