| """MetaWorld: train policy with learned rewards.""" |
|
|
| import os |
| import subprocess |
|
|
| from absl import app |
| from absl import flags |
| from absl import logging |
| from configs.constants import METAWORLDTASKS |
| from configs.constants import METAWORLD_TASK_TO_ENV_NAME |
| from torchkit.experiment import string_from_kwargs |
| from torchkit.experiment import unique_id |
| import yaml |
|
|
| FLAGS = flags.FLAGS |
| CONFIG_PATH = "configs/metaworld/rl/env_reward.py" |
|
|
| flags.DEFINE_enum("env_name", None, METAWORLDTASKS, "MetaWorld env to train on.") |
| flags.DEFINE_string("pretrained_path", None, "Path to pretraining experiment.") |
| flags.DEFINE_string("initial_policy_checkpoint_path", None, "Path to initial policy.") |
| flags.DEFINE_list("seeds", [0, 1], "List specifying the range of seeds to run.") |
| flags.DEFINE_string("device", "cuda:0", "The compute device.") |
| flags.DEFINE_integer( |
| "switch_to_generative_subgoals_step", |
| -1, |
| "The training step to switch to generated subgoals.", |
| ) |
|
|
| def main(_): |
| with open(os.path.join(FLAGS.pretrained_path, "metadata.yaml"), "r") as fp: |
| kwargs = yaml.load(fp, Loader=yaml.FullLoader) |
| |
| if kwargs["algo"] == "goal_classifier": |
| reward_type = "goal_classifier" |
| else: |
| reward_type = "distance_to_goal" |
|
|
| env_name = METAWORLD_TASK_TO_ENV_NAME[FLAGS.env_name] |
| print(f"______Training robot for {env_name}______") |
|
|
| |
| experiment_name = string_from_kwargs( |
| env_name=env_name, |
| reward="learned", |
| reward_type=reward_type, |
| algo=kwargs["algo"], |
| uid=unique_id(), |
| ) |
| logging.info("Experiment name: %s", experiment_name) |
|
|
| |
| procs = [] |
| for seed in range(*list(map(int, FLAGS.seeds))): |
| proc = subprocess.Popen([ |
| "python", |
| "train_policy.py", |
| "--experiment_name", |
| experiment_name, |
| "--env_name", |
| f"{env_name}", |
| "--config", |
| f"{CONFIG_PATH}:{FLAGS.env_name}", |
| "--config.reward_wrapper.pretrained_path", |
| f"{FLAGS.pretrained_path}", |
| "--config.reward_wrapper.type", |
| f"{reward_type}", |
| "--seed", |
| f"{seed}", |
| "--device", |
| f"{FLAGS.device}", |
| "--initial_policy_checkpoint_path", |
| f"{FLAGS.initial_policy_checkpoint_path}", |
| "--switch_to_generative_subgoals_step", |
| f"{FLAGS.switch_to_generative_subgoals_step}", |
| ]) |
| procs.append(proc) |
|
|
| for p in procs: |
| p.wait() |
|
|
| if __name__ == "__main__": |
| app.run(main) |
|
|
|
|