2020-05-12 08:23:10 +02:00
|
|
|
# Explains/tests Issues:
|
|
|
|
# https://github.com/ray-project/ray/issues/6928
|
|
|
|
# https://github.com/ray-project/ray/issues/6732
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from gym.spaces import Discrete, Box
|
|
|
|
import numpy as np
|
2020-10-02 23:07:44 +02:00
|
|
|
import os
|
2020-05-12 08:23:10 +02:00
|
|
|
|
2020-10-02 23:07:44 +02:00
|
|
|
from ray import tune
|
2020-05-12 08:23:10 +02:00
|
|
|
from ray.rllib.examples.env.random_env import RandomEnv
|
|
|
|
from ray.rllib.examples.models.mobilenet_v2_with_lstm_models import \
|
|
|
|
MobileV2PlusRNNModel, TorchMobileV2PlusRNNModel
|
|
|
|
from ray.rllib.models import ModelCatalog
|
2020-06-16 08:52:20 +02:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2020-05-12 08:23:10 +02:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
cnn_shape = (4, 4, 3)
|
|
|
|
# The torch version of MobileNetV2 does channels first.
|
|
|
|
cnn_shape_torch = (3, 224, 224)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-05-18 13:18:12 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--framework",
|
|
|
|
choices=["tf", "tf2", "tfe", "torch"],
|
|
|
|
default="tf",
|
|
|
|
help="The DL framework specifier.")
|
2020-10-02 23:07:44 +02:00
|
|
|
parser.add_argument("--stop-iters", type=int, default=200)
|
|
|
|
parser.add_argument("--stop-reward", type=float, default=0.0)
|
|
|
|
parser.add_argument("--stop-timesteps", type=int, default=100000)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Register our custom model.
|
|
|
|
ModelCatalog.register_custom_model(
|
|
|
|
"my_model", TorchMobileV2PlusRNNModel
|
2021-05-18 13:18:12 +02:00
|
|
|
if args.framework == "torch" else MobileV2PlusRNNModel)
|
2020-05-12 08:23:10 +02:00
|
|
|
|
2020-10-02 23:07:44 +02:00
|
|
|
stop = {
|
|
|
|
"training_iteration": args.stop_iters,
|
|
|
|
"timesteps_total": args.stop_timesteps,
|
|
|
|
"episode_reward_mean": args.stop_reward,
|
|
|
|
}
|
|
|
|
|
2020-05-12 08:23:10 +02:00
|
|
|
# Configure our Trainer.
|
|
|
|
config = {
|
2020-10-02 23:07:44 +02:00
|
|
|
"env": RandomEnv,
|
2021-05-18 13:18:12 +02:00
|
|
|
"framework": args.framework,
|
2020-05-12 08:23:10 +02:00
|
|
|
"model": {
|
|
|
|
"custom_model": "my_model",
|
|
|
|
# Extra config passed to the custom model's c'tor as kwargs.
|
2020-05-27 10:19:47 +02:00
|
|
|
"custom_model_config": {
|
2021-05-18 13:18:12 +02:00
|
|
|
# By default, torch CNNs use "channels-first",
|
|
|
|
# tf "channels-last".
|
|
|
|
"cnn_shape": cnn_shape_torch
|
|
|
|
if args.framework == "torch" else cnn_shape,
|
2020-05-12 08:23:10 +02:00
|
|
|
},
|
|
|
|
"max_seq_len": 20,
|
2021-01-19 09:51:35 +01:00
|
|
|
"vf_share_layers": True,
|
2020-05-12 08:23:10 +02:00
|
|
|
},
|
2020-10-02 23:07:44 +02:00
|
|
|
# Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
|
|
|
|
"num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
|
2020-05-12 08:23:10 +02:00
|
|
|
"num_workers": 0, # no parallelism
|
|
|
|
"env_config": {
|
|
|
|
"action_space": Discrete(2),
|
|
|
|
# Test a simple Image observation space.
|
|
|
|
"observation_space": Box(
|
|
|
|
0.0,
|
|
|
|
1.0,
|
2021-05-18 13:18:12 +02:00
|
|
|
shape=cnn_shape_torch
|
|
|
|
if args.framework == "torch" else cnn_shape,
|
2020-05-12 08:23:10 +02:00
|
|
|
dtype=np.float32)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-02 23:07:44 +02:00
|
|
|
tune.run("PPO", config=config, stop=stop, verbose=1)
|