| """ |
| Test script for CQL algorithms. Each test trains a variant of CQL |
| for a handful of gradient steps and tries one rollout with |
| the model. Excludes stdout output by default (pass --verbose |
| to see stdout output). |
| """ |
| import argparse |
| from collections import OrderedDict |
|
|
| import robomimic |
| from robomimic.config import Config |
| import robomimic.utils.test_utils as TestUtils |
| from robomimic.utils.log_utils import silence_stdout |
| from robomimic.utils.torch_utils import dummy_context_mgr |
|
|
|
|
| def get_algo_base_config(): |
| """ |
| Base config for testing CQL algorithms. |
| """ |
|
|
| |
| config = TestUtils.get_base_config(algo_name="cql") |
|
|
| |
| |
| config.observation.modalities.obs.low_dim = ["robot0_eef_pos", "robot0_eef_quat", "robot0_gripper_qpos", "object"] |
| config.observation.modalities.obs.rgb = [] |
|
|
| |
| config.algo.actor.bc_start_steps = 40 |
| config.algo.critic.target_q_gap = 5.0 |
| config.algo.actor.target_entropy = "default" |
|
|
| |
| config.train.batch_size = 100 |
|
|
| return config |
|
|
|
|
| def convert_config_for_images(config): |
| """ |
| Modify config to use image observations. |
| """ |
|
|
| |
| config.train.hdf5_cache_mode = "low_dim" |
| config.train.num_data_workers = 0 |
| config.train.batch_size = 16 |
|
|
| |
| config.observation.modalities.obs.low_dim = ["robot0_eef_pos", "robot0_eef_quat", "robot0_gripper_qpos"] |
| config.observation.modalities.obs.rgb = ["agentview_image"] |
|
|
| |
| config.observation.encoder.rgb.core_class = "VisualCore" |
| config.observation.encoder.rgb.core_kwargs.feature_dimension = 64 |
| config.observation.encoder.rgb.core_kwargs.backbone_class = 'ResNet18Conv' |
| config.observation.encoder.rgb.core_kwargs.backbone_kwargs.pretrained = False |
| config.observation.encoder.rgb.core_kwargs.backbone_kwargs.input_coord_conv = False |
| config.observation.encoder.rgb.core_kwargs.pool_class = "SpatialSoftmax" |
| config.observation.encoder.rgb.core_kwargs.pool_kwargs.num_kp = 32 |
| config.observation.encoder.rgb.core_kwargs.pool_kwargs.learnable_temperature = False |
| config.observation.encoder.rgb.core_kwargs.pool_kwargs.temperature = 1.0 |
| config.observation.encoder.rgb.core_kwargs.pool_kwargs.noise_std = 0.0 |
|
|
| |
| config.observation.encoder.rgb.obs_randomizer_class = None |
|
|
| return config |
|
|
|
|
| def make_image_modifier(config_modifier): |
| """ |
| turn a config modifier into its image version. Note that |
| this explicit function definition is needed for proper |
| scoping of @config_modifier |
| """ |
| return lambda x: config_modifier(convert_config_for_images(x)) |
|
|
|
|
| |
| MODIFIERS = OrderedDict() |
| def register_mod(test_name): |
| def decorator(config_modifier): |
| MODIFIERS[test_name] = config_modifier |
| return decorator |
|
|
|
|
| @register_mod("cql-fixed-entropy") |
| def cql_entropy_modifier(config): |
| config.algo.actor.target_entropy = None |
| return config |
|
|
|
|
| @register_mod("cql-fixed-q-gap") |
| def cql_q_gap_modifier(config): |
| config.algo.critic.target_q_gap = None |
| config.algo.critic.cql_weight = 1.0 |
| return config |
|
|
|
|
| @register_mod("cql-fixed-gaussian") |
| def cql_gaussian_modifier(config): |
| config.algo.actor.net.gaussian.fixed_std = True |
| return config |
|
|
|
|
| |
| image_modifiers = OrderedDict() |
| for test_name in MODIFIERS: |
| lst = test_name.split("-") |
| name = "-".join(lst[:1] + ["rgb"] + lst[1:]) |
| image_modifiers[name] = make_image_modifier(MODIFIERS[test_name]) |
| MODIFIERS.update(image_modifiers) |
|
|
|
|
| |
| @register_mod("cql-image-crop") |
| def cql_image_crop_modifier(config): |
| config = convert_config_for_images(config) |
|
|
| |
| config.observation.encoder.rgb.obs_randomizer_class = "CropRandomizer" |
|
|
| |
| config.observation.encoder.rgb.obs_randomizer_kwargs.crop_height = 76 |
| config.observation.encoder.rgb.obs_randomizer_kwargs.crop_width = 76 |
| config.observation.encoder.rgb.obs_randomizer_kwargs.num_crops = 1 |
| config.observation.encoder.rgb.obs_randomizer_kwargs.pos_enc = False |
| return config |
|
|
|
|
| def test_cql(silence=True): |
| for test_name in MODIFIERS: |
| context = silence_stdout() if silence else dummy_context_mgr() |
| with context: |
| base_config = get_algo_base_config() |
| res_str = TestUtils.test_run(base_config=base_config, config_modifier=MODIFIERS[test_name]) |
| print("{}: {}".format(test_name, res_str)) |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| "--verbose", |
| action='store_true', |
| help="don't suppress stdout during tests", |
| ) |
| args = parser.parse_args() |
|
|
| test_cql(silence=(not args.verbose)) |
|
|