File size: 2,681 Bytes
b86de98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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] #METAWORLD_TASK_TO_ENV_NAME[kwargs["env_name"]]
    print(f"______Training robot for {env_name}______")

    # Generate a unique experiment 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)

    # Execute each seed in parallel.
    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)