-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_weight.py
213 lines (174 loc) · 6.33 KB
/
save_weight.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
import os
from typing import Optional, Type
import pickle
import ray
from ray.rllib.models import ModelCatalog
from ray.rllib.policy.policy import Policy
from ray.rllib.utils.framework import try_import_tf, try_import_torch
from ray.rllib.utils.test_utils import check_learning_achieved
from ray.tune.logger import pretty_print
from ray.tune.registry import get_trainable_cls
from ray.rllib.algorithms.ppo import PPO
from ray.rllib.algorithms.ppo import PPOConfig
from ray.rllib.algorithms.ppo import PPO as PPOAlgorithm
from ray.tune.logger import pretty_print
from ray.rllib.policy.policy import Policy
from env.symbolic_env import SymbolicEnv
from env.symbolic_env_wo_pun import SymbolicEnvWoPun
from model.helper_v3 import HelperModel
from model.custom_callback import CustomCallbacks
from env.type import sample_type
from env.task import sample_task
from model.end2end import End2End
from model.helper_without_super import HelperModelWithoutSupervise
from model.helper_flow import HelperModelFlow
from model.helper_v4 import HelperModelV4
from model.helper_new import HelperModelNew
from model.helper_dep import HelperModelDep
import shutil
import json
from constants import AgentType
import wandb
# from tqdm import tqdm
tf1, tf, tfv = try_import_tf()
torch, nn = try_import_torch()
parser = argparse.ArgumentParser()
parser.add_argument(
"--run", type=str, default="PPO", help="The RLlib-registered algorithm to use."
)
parser.add_argument(
"--model", type=str, default="simple", help="The model to use in A2C, for testing only."
)
parser.add_argument(
"--agent_num", type=int, default=2, help="The number of agents."
)
parser.add_argument(
"--framework",
choices=["tf", "tf2", "torch"],
default="torch",
help="The DL framework specifier.",
)
parser.add_argument(
"--as-test",
action="store_true",
help="Whether this script should be run as a test: --stop-reward must "
"be achieved within --stop-timesteps AND --stop-iters.",
)
parser.add_argument(
"--stop-iters", type=int, default=50, help="Number of iterations to train."
)
parser.add_argument(
"--stop-timesteps", type=int, default=100000, help="Number of timesteps to train."
)
parser.add_argument(
"--stop-reward", type=float, default=0.1, help="Reward at which we stop training."
)
parser.add_argument(
"--no-tune",
action="store_true",
help="Run without Tune using a manual train loop instead. In this case,"
"use PPO without grid search and no TensorBoard.",
)
parser.add_argument(
"--local-mode",
action="store_true",
help="Init Ray in local mode for easier debugging.",
)
controller_kwargs = {
}
if __name__ == "__main__":
os.environ['LD_LIBRARY_PATH'] = "/usr/local/cuda/lib64"
print("Start the program", torch.cuda.is_available(), )
save_path_dict = {}
args = parser.parse_args()
config = {
'agents_num': 2,
# 'agents_type': {0: AgentType.AGENT_WITH_FULL_CAPABILITIES, 1: AgentType.AGENT_WITH_TOGGLE_ISSUES},
'agents_type': {0: [2, 2, 1, 1, 1, 1], 1: sample_type()},
'main_agent_id': 1,
'mode': 'train',
'controller_kwargs': {
"agentCount": 2,
"scene": 'FloorPlan2',
"local_executable_path": "/home/zhihao/Downloads/thor-Linux64-local/thor-Linux64-local",
"renderDepthImage": False,
"renderInstanceSegmentation": False,
"visibilityDistance": 30,
"quality": "Very Low",
},
'task': sample_task(),
}
ray.init()
position_list = []
algo = None
if args.run == 'PPO':
ModelCatalog.register_custom_model(
"helper", HelperModel
)
ModelCatalog.register_custom_model(
"helperv4", HelperModelV4
)
ModelCatalog.register_custom_model(
"e2e", End2End
)
ModelCatalog.register_custom_model(
"helperwithoutsuper", HelperModelWithoutSupervise
)
ModelCatalog.register_custom_model(
"helperflow", HelperModelFlow
)
ModelCatalog.register_custom_model(
"helpernew", HelperModelNew
)
ModelCatalog.register_custom_model(
"helperdep", HelperModelDep
)
# 恢复训练算法
algorithm = PPOAlgorithm.from_checkpoint("/home/zhihao/Downloads/ours_wo_pun/839")
config_PPO = PPOConfig()
config_PPO["preprocessor_pref"] = None
# 打印默认的学习率
print("默认学习率:", algorithm.config["lr"])
# 获取原始算法的模型权重
original_weights = algorithm.get_policy().get_weights()
with open('model_weights.pkl', 'wb') as f:
pickle.dump(original_weights, f)
exit()
algo = (
config_PPO
.rollouts(num_rollout_workers=6)
# .rl_module( _enable_rl_module_api=False)
.resources(num_gpus=1)
.experimental(_enable_new_api_stack=False)
# .framework("torch")
.training(model={
"use_lstm": True,
"lstm_cell_size": 512,
"custom_model": "helperdep",
"vf_share_layers": True,},
train_batch_size=2000)
.environment(SymbolicEnvWoPun, env_config=config)
.callbacks(CustomCallbacks)
.build()
)
# algo = Algorithm.from_checkpoint('/home/zhihao/Downloads/ours/1834')
# # my_restored_policy = Policy.from_checkpoint('/home/zhihao/ray_results/helper_failed/checkpoint_000955')
print("algo build finished!!!")
for i in range(2000):
# inner_progress_bar = tqdm(total=4000, desc=f"Iteration {i+1}")
print("Training Start")
result = algo.train()
print(pretty_print(result))
log_dict = {}
for k, v in result.items():
if v is not None:
log_dict[str(k)] = v
# wandb.log(log_dict)
if i % 5 == 4:
checkpoint_dir = algo.save()
print(f"Checkpoint saved in directory {checkpoint_dir.checkpoint.path}")
save_path_dict[i] = checkpoint_dir.checkpoint.path
with open("save_path.json", "w") as json_file:
json.dump(save_path_dict, json_file)
shutil.copytree(checkpoint_dir.checkpoint.path, f"/home/zhihao/Downloads/ours_wo_pun/{i}/")