-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep_1_training_victims_pedestrian.py
466 lines (401 loc) · 14.6 KB
/
step_1_training_victims_pedestrian.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/bin/env python
import gym
import macad_gym # noqa F401
import argparse
import os
from pprint import pprint
import cv2
import ray
import ray.tune as tune
from gym.spaces import Box, Discrete
from macad_agents.rllib.env_wrappers import wrap_deepmind
from macad_agents.rllib.models import register_mnih15_net
from ray.rllib.agents.ppo.ppo_tf_policy import PPOTFPolicy #0.8.5
from ray.rllib.models.catalog import ModelCatalog
from ray.rllib.models.preprocessors import Preprocessor
from ray.tune import register_env
import time
import tensorflow as tf
from tensorboardX import SummaryWriter
# from tensorflow.compat.v1 import ConfigProto
# from tensorflow.compat.v1 import InteractiveSession
# config = tf.ConfigProto()
# config.gpu_options.allow_growth = True
# session = InteractiveSession(config=config)
# config = tf.ConfigProto()
# config.gpu_options.per_process_gpu_memory_fraction = 0.7
# tf.keras.backend.set_session(tf.Session(config=config));
parser = argparse.ArgumentParser()
parser.add_argument(
"--env",
default="PongNoFrameskip-v4",
help="Name Gym env. Used only in debug mode. Default=PongNoFrameskip-v4")
parser.add_argument(
"--disable-comet",
action="store_true",
help="Disables comet logging. Used for local smoke tests")
parser.add_argument(
"--num-workers",
default=1, #2 #fix
type=int,
help="Num workers (CPU cores) to use")
parser.add_argument(
"--num-gpus", default=1, type=int, help="Number of gpus to use. Default=2")
parser.add_argument(
"--sample-bs-per-worker", #one iteration
default=1024,
type=int,
help="Number of samples in a batch per worker. Default=50")
parser.add_argument(
"--train-bs",
default=128,
type=int,
help="Train batch size. Use as per available GPU mem. Default=500")
parser.add_argument(
"--envs-per-worker",
default=1,
type=int,
help="Number of env instances per worker. Default=10")
parser.add_argument(
"--notes",
default=None,
help="Custom experiment description to be added to comet logs")
parser.add_argument(
"--model-arch",
default="mnih15",
help="Model architecture to use. Default=mnih15")
parser.add_argument(
"--num-steps",
default=4000000,
type=int,
help="Number of steps to train. Default=20M")
parser.add_argument(
"--num-iters",
default=300,
type=int,
help="Number of training iterations. Default=20")
parser.add_argument(
"--log-graph",
action="store_true",
help="Write TF graph on Tensorboard for debugging",default=True)
parser.add_argument(
"--num-framestack",
type=int,
default=4,
help="Number of obs frames to stack")
parser.add_argument(
"--debug", action="store_true", help="Run in debug-friendly mode", default=False)
parser.add_argument(
"--redis-address",
default=None,
help="Address of ray head node. Be sure to start ray with"
"ray start --redis-address <...> --num-gpus<.> before running this script")
parser.add_argument(
"--use-lstm", action="store_true", help="Append a LSTM cell to the model",default=True)
args = parser.parse_args()
model_name = args.model_arch
if model_name == "mnih15":
register_mnih15_net() # Registers mnih15
else:
print("Unsupported model arch. Using default")
register_mnih15_net()
model_name = "mnih15"
# Used only in debug mode
env_name = "HeteNcomIndePOIntrxMATLS1B2C1PTWN3-v0"
env = gym.make(env_name)
# print (env.spec.max_episode_steps,"-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
# env.spec.max_episode_steps=1024
# print (env.spec.max_episode_steps,"-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+")
env_actor_configs = env.configs
num_framestack = args.num_framestack
# env_config["env"]["render"] = False
def env_creator(env_config):
import macad_gym
env = gym.make("HeteNcomIndePOIntrxMATLS1B2C1PTWN3-v0")
# Apply wrappers to: convert to Grayscale, resize to 84 x 84,
# stack frames & some more op
env = wrap_deepmind(env, dim=84, num_framestack=num_framestack)
return env
register_env(env_name, lambda config: env_creator(config))
# Placeholder to enable use of a custom pre-processor
class ImagePreproc(Preprocessor):
def _init_shape(self, obs_space, options):
self.shape = (84, 84, 3) # Adjust third dim if stacking frames
return self.shape
def transform(self, observation):
observation = cv2.resize(observation, (self.shape[0], self.shape[1]))
return observation
def transform(self, observation):
observation = cv2.resize(observation, (self.shape[0], self.shape[1]))
return observation
ModelCatalog.register_custom_preprocessor("sq_im_84", ImagePreproc)
if args.redis_address is not None:
# num_gpus (& num_cpus) must not be provided when connecting to an
# existing cluster
ray.init(redis_address=args.redis_address,object_store_memory=10**10,log_to_driver=False)
else:
ray.init(num_gpus=args.num_gpus,object_store_memory=10**10,log_to_driver=False)
config = {
# Model and preprocessor options.
"model": {
"custom_model": model_name,
"custom_options": {
# Custom notes for the experiment
"notes": {
"args": vars(args)
},
},
# NOTE:Wrappers are applied by RLlib if custom_preproc is NOT specified
"custom_preprocessor": "sq_im_84",
"dim": 84,
"free_log_std": False, # if args.discrete_actions else True,
"grayscale": True,
# conv_filters to be used with the custom CNN model.
# "conv_filters": [[16, [4, 4], 2], [32, [3, 3], 2], [16, [3, 3], 2]]
},
# preproc_pref is ignored if custom_preproc is specified
# "preprocessor_pref": "deepmind",
# env_config to be passed to env_creator
"env_config": env_actor_configs
}
def default_policy():
env_actor_configs["env"]["render"] = False
config = {
# Model and preprocessor options.
"model": {
"custom_model": model_name,
"custom_options": {
# Custom notes for the experiment
"notes": {
"args": vars(args)
},
},
# NOTE:Wrappers are applied by RLlib if custom_preproc is NOT specified
"custom_preprocessor": "sq_im_84",
"dim": 84,
"free_log_std": False, # if args.discrete_actions else True,
"grayscale": True,
# conv_filters to be used with the custom CNN model.
# "conv_filters": [[16, [4, 4], 2], [32, [3, 3], 2], [16, [3, 3], 2]]
},
# Should use a critic as a baseline (otherwise don't use value baseline;
# required for using GAE).
"use_critic": True,
# If true, use the Generalized Advantage Estimator (GAE)
# with a value function, see https://arxiv.org/pdf/1506.02438.pdf.
"use_gae": True,
# The GAE(lambda) parameter.
"lambda": 1.0,
# Initial coefficient for KL divergence.
"kl_coeff": 0.3,
# Size of batches collected from each worker.
"rollout_fragment_length": 128,
# Number of timesteps collected for each SGD round. This defines the size
# of each SGD epoch.
# "train_batch_size": 4000,
# Total SGD batch size across all devices for SGD. This defines the
# minibatch size within each epoch.
"sgd_minibatch_size": 18,
# Whether to shuffle sequences in the batch when training (recommended).
"shuffle_sequences": True,
# Number of SGD iterations in each outer loop (i.e., number of epochs to
# execute per train batch).
"num_sgd_iter": 4,
# Stepsize of SGD.
"lr": 5e-5,
# Learning rate schedule.
# "lr_schedule": None,
# Share layers for value function. If you set this to True, it's important
# to tune vf_loss_coeff.
"vf_share_layers": False,
# Coefficient of the value function loss. IMPORTANT: you must tune this if
# you set vf_share_layers: True.
"vf_loss_coeff": 1.0,
# Coefficient of the entropy regularizer.
"entropy_coeff": 0.1,
# Decay schedule for the entropy regularizer.
"entropy_coeff_schedule": None,
# PPO clip parameter.
"clip_param": 0.3,
# Clip param for the value function. Note that this is sensitive to the
# scale of the rewards. If your expected V is large, increase this.
"vf_clip_param": 10.0,
# If specified, clip the global norm of gradients by this amount.
"grad_clip": None,
# Target value for KL divergence.
"kl_target": 0.03,
# Whether to rollout "complete_episodes" or "truncate_episodes".
"batch_mode": "complete_episodes",
# Which observation filter to apply to the observation.
"observation_filter": "NoFilter",
# Uses the sync samples optimizer instead of the multi-gpu one. This is
# usually slower, but you might want to try it if you run into issues with
# the default optimizer.
"simple_optimizer": False,
# Use PyTorch as framework?
"use_pytorch": False,
# Discount factor of the MDP.
"gamma": 0.99,
# Number of steps after which the episode is forced to terminate. Defaults
# to `env.spec.max_episode_steps` (if present) for Gym envs.
"horizon": 512,
# Calculate rewards but don't reset the environment when the horizon is
# hit. This allows value estimation and RNN state to span across logical
# episodes denoted by horizon. This only has an effect if horizon != inf.
"soft_horizon": True,
# Don't set 'done' at the end of the episode. Note that you still need to
# set this if soft_horizon=True, unless your env is actually running
# forever without returning done=True.
"no_done_at_end": True,
"monitor": True,
# System params.
# Should be divisible by num_envs_per_worker
"sample_batch_size":
args.sample_bs_per_worker,
"train_batch_size":
args.train_bs,
# "rollout_fragment_length": 128,
"num_workers":
args.num_workers,
# Number of environments to evaluate vectorwise per worker.
"num_envs_per_worker":
args.envs_per_worker,
"num_cpus_per_worker":
1,
"num_gpus_per_worker":
1,
# "eager_tracing": True,
# # Learning params.
# "grad_clip":
# 40.0,
# "clip_rewards":
# True,
# either "adam" or "rmsprop"
"opt_type":
"adam",
# "lr":
# 0.003,
"lr_schedule": [
[0, 0.0006],
[20000000, 0.000000000001], # Anneal linearly to 0 from start 2 end
],
# rmsprop considered
"decay":
0.5,
"momentum":
0.0,
"epsilon":
0.1,
# # balancing the three losses
# "vf_loss_coeff":
# 0.5, # Baseline loss scaling
# "entropy_coeff":
# -0.01,
# preproc_pref is ignored if custom_preproc is specified
# "preprocessor_pref": "deepmind",
# "gamma": 0.99,
"use_lstm": args.use_lstm,
# env_config to be passed to env_creator
"env":{
"render": True
},
# "in_evaluation": True,
# "evaluation_num_episodes": 1,
"env_config": env_actor_configs
}
# pprint (config)
return (PPOTFPolicy, Box(0.0, 255.0, shape=(84, 84, 3)), Discrete(9),config)
# pprint (args.checkpoint_path)
# pprint(os.path.isfile(args.checkpoint_path))
if args.debug:
# For checkpoint loading and retraining (not used in this script)
experiment_spec = tune.Experiment(
"multi-carla/" + args.model_arch,
"PPO",
# restore=args.checkpoint_path,
# timesteps_total is init with None (not 0) which causes issue
# stop={"timesteps_total": args.num_steps},
stop={"timesteps_since_restore": args.num_steps},
config=config,
# checkpoint_freq=1000, #1000
# checkpoint_at_end=True,
resources_per_trial={
"cpu": 1,
"gpu": 1
})
experiment_spec = tune.run_experiments({
"MA-Inde-PPO-SSUI3CCARLA": {
"run": "PPO",
"env": env_name,
"stop": {
"training_iteration": args.num_iters,
"timesteps_total": args.num_steps,
"episodes_total": 1024,
},
# "restore":args.checkpoint_path,
"config": {
"log_level": "DEBUG",
# "num_sgd_iter": 10, # Enables Experience Replay
"multiagent": {
"policies": {
id: default_policy()
for id in env_actor_configs["actors"].keys()
},
"policy_mapping_fn":
tune.function(lambda agent_id: agent_id),
"policies_to_train": ["car2"],
},
"env_config": env_actor_configs,
"num_workers": args.num_workers,
"num_envs_per_worker": args.envs_per_worker,
"sample_batch_size": args.sample_bs_per_worker,
"train_batch_size": args.train_bs,
"horizon": 512,
},
"checkpoint_freq": 10,
"checkpoint_at_end": True,
}
})
else:
experiment_spec = tune.Experiment(
"multi-carla/" + args.model_arch,
"PPO",
stop={"timesteps_since_restore": args.num_steps},
config=config,
resources_per_trial={
"cpu": 1,
"gpu": 1
})
experiment_spec = tune.run_experiments({
"MA-Inde-PPO-SSUI3CCARLA": {
"run": "PPO",
"env": env_name,
"stop": {
"training_iteration": args.num_iters,
"timesteps_total": args.num_steps,
"episodes_total": 1024,
},
"config": {
"log_level": "DEBUG",
# "num_sgd_iter": 10, # Enables Experience Replay
"multiagent": {
"policies": {
id: default_policy()
for id in env_actor_configs["actors"].keys()
},
"policy_mapping_fn":
tune.function(lambda agent_id: agent_id),
"policies_to_train": ["car2"], #
},
"env_config": env_actor_configs,
"num_workers": args.num_workers,
"num_envs_per_worker": args.envs_per_worker,
"sample_batch_size": args.sample_bs_per_worker,
"train_batch_size": args.train_bs,
#"horizon": 512, #yet to be fixed
},
"checkpoint_freq": 10,
"checkpoint_at_end": True,
}
})
ray.shutdown()