|
| 1 | +import os |
| 2 | +import copy |
| 3 | + |
| 4 | +from spirl.utils.general_utils import AttrDict |
| 5 | +from spirl.rl.components.agent import FixedIntervalHierarchicalAgent |
| 6 | +from spirl.rl.components.critic import SplitObsMLPCritic |
| 7 | +from spirl.rl.components.sampler import ACMultiImageAugmentedHierarchicalSampler |
| 8 | +from spirl.rl.components.replay_buffer import UniformReplayBuffer |
| 9 | +from spirl.rl.policies.prior_policies import ACLearnedPriorAugmentedPIPolicy |
| 10 | +from spirl.rl.envs.block_stacking import HighStack11StackEnvV0, SparseHighStack11StackEnvV0 |
| 11 | +from spirl.rl.agents.ac_agent import SACAgent |
| 12 | +from spirl.rl.agents.prior_sac_agent import ActionPriorSACAgent |
| 13 | +from spirl.rl.policies.cl_model_policies import ACClModelPolicy |
| 14 | +from spirl.models.closed_loop_spirl_mdl import ImageClSPiRLMdl |
| 15 | +from spirl.configs.default_data_configs.block_stacking import data_spec |
| 16 | + |
| 17 | + |
| 18 | +current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 19 | + |
| 20 | +notes = 'used to test the RL implementation' |
| 21 | + |
| 22 | +configuration = { |
| 23 | + 'seed': 42, |
| 24 | + 'agent': FixedIntervalHierarchicalAgent, |
| 25 | + 'environment': SparseHighStack11StackEnvV0, |
| 26 | + 'sampler': ACMultiImageAugmentedHierarchicalSampler, |
| 27 | + 'data_dir': '.', |
| 28 | + 'num_epochs': 100, |
| 29 | + 'max_rollout_len': 1000, |
| 30 | + 'n_steps_per_epoch': 1e5, |
| 31 | + 'n_warmup_steps': 5e3, |
| 32 | +} |
| 33 | +configuration = AttrDict(configuration) |
| 34 | + |
| 35 | + |
| 36 | +# Replay Buffer |
| 37 | +replay_params = AttrDict( |
| 38 | + capacity=1e5, |
| 39 | + dump_replay=False, |
| 40 | +) |
| 41 | + |
| 42 | +# Observation Normalization |
| 43 | +obs_norm_params = AttrDict( |
| 44 | +) |
| 45 | + |
| 46 | +sampler_config = AttrDict( |
| 47 | + n_frames=2, |
| 48 | +) |
| 49 | + |
| 50 | +base_agent_params = AttrDict( |
| 51 | + batch_size=256, |
| 52 | + replay=UniformReplayBuffer, |
| 53 | + replay_params=replay_params, |
| 54 | + clip_q_target=False, |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +###### Low-Level ###### |
| 59 | +# LL Policy Model |
| 60 | +ll_model_params = AttrDict( |
| 61 | + state_dim=data_spec.state_dim, |
| 62 | + action_dim=data_spec.n_actions, |
| 63 | + n_rollout_steps=10, |
| 64 | + kl_div_weight=1e-2, |
| 65 | + prior_input_res=data_spec.res, |
| 66 | + n_input_frames=2, |
| 67 | + cond_decode=True, |
| 68 | +) |
| 69 | + |
| 70 | +# LL Policy |
| 71 | +ll_policy_params = AttrDict( |
| 72 | + policy_model=ImageClSPiRLMdl, |
| 73 | + policy_model_params=ll_model_params, |
| 74 | + policy_model_checkpoint=os.path.join(os.environ["EXP_DIR"], "skill_learning/block_stacking/hierarchical_cl"), |
| 75 | + initial_log_sigma=-50., |
| 76 | +) |
| 77 | +ll_policy_params.update(ll_model_params) |
| 78 | + |
| 79 | +# LL Critic |
| 80 | +ll_critic_params = AttrDict( |
| 81 | + action_dim=data_spec.n_actions, |
| 82 | + input_dim=data_spec.state_dim, |
| 83 | + output_dim=1, |
| 84 | + action_input=True, |
| 85 | + unused_obs_size=10, # ignore HL policy z output in observation for LL critic |
| 86 | +) |
| 87 | + |
| 88 | +# LL Agent |
| 89 | +ll_agent_config = copy.deepcopy(base_agent_params) |
| 90 | +ll_agent_config.update(AttrDict( |
| 91 | + policy=ACClModelPolicy, |
| 92 | + policy_params=ll_policy_params, |
| 93 | + critic=SplitObsMLPCritic, |
| 94 | + critic_params=ll_critic_params, |
| 95 | +)) |
| 96 | + |
| 97 | + |
| 98 | +###### High-Level ######## |
| 99 | +# HL Policy |
| 100 | +hl_policy_params = AttrDict( |
| 101 | + action_dim=10, # z-dimension of the skill VAE |
| 102 | + input_dim=data_spec.state_dim, |
| 103 | + max_action_range=2., # prior is Gaussian with unit variance |
| 104 | + prior_model=ll_policy_params.policy_model, |
| 105 | + prior_model_params=ll_policy_params.policy_model_params, |
| 106 | + prior_model_checkpoint=ll_policy_params.policy_model_checkpoint, |
| 107 | +) |
| 108 | + |
| 109 | +# HL Critic |
| 110 | +hl_critic_params = AttrDict( |
| 111 | + action_dim=hl_policy_params.action_dim, |
| 112 | + input_dim=hl_policy_params.input_dim, |
| 113 | + output_dim=1, |
| 114 | + n_layers=2, # number of policy network layers |
| 115 | + nz_mid=256, |
| 116 | + action_input=True, |
| 117 | + unused_obs_size=ll_model_params.prior_input_res **2 * 3 * ll_model_params.n_input_frames, |
| 118 | +) |
| 119 | + |
| 120 | +# HL Agent |
| 121 | +hl_agent_config = copy.deepcopy(base_agent_params) |
| 122 | +hl_agent_config.update(AttrDict( |
| 123 | + policy=ACLearnedPriorAugmentedPIPolicy, |
| 124 | + policy_params=hl_policy_params, |
| 125 | + critic=SplitObsMLPCritic, |
| 126 | + critic_params=hl_critic_params, |
| 127 | + td_schedule_params=AttrDict(p=5.), |
| 128 | +)) |
| 129 | + |
| 130 | + |
| 131 | +##### Joint Agent ####### |
| 132 | +agent_config = AttrDict( |
| 133 | + hl_agent=ActionPriorSACAgent, |
| 134 | + hl_agent_params=hl_agent_config, |
| 135 | + ll_agent=SACAgent, |
| 136 | + ll_agent_params=ll_agent_config, |
| 137 | + hl_interval=ll_model_params.n_rollout_steps, |
| 138 | + log_videos=True, |
| 139 | + update_hl=True, |
| 140 | + update_ll=False, |
| 141 | +) |
| 142 | + |
| 143 | +# Dataset - Random data |
| 144 | +data_config = AttrDict() |
| 145 | +data_config.dataset_spec = data_spec |
| 146 | + |
| 147 | +# Environment |
| 148 | +env_config = AttrDict( |
| 149 | + name="block_stacking", |
| 150 | + reward_norm=1., |
| 151 | + screen_width=data_spec.res, |
| 152 | + screen_height=data_spec.res, |
| 153 | + env_config=AttrDict(camera_name='agentview', |
| 154 | + screen_width=data_spec.res, |
| 155 | + screen_height=data_spec.res,) |
| 156 | +) |
| 157 | + |
0 commit comments