| """ |
| WARNING: This script is only for instructive purposes, and is missing several useful |
| components used during training such as logging and rollout evaluation. |
| |
| Example script for demonstrating how the SequenceDataset class and a training loop |
| can interact. This is meant to help others who would like to use our provided |
| datasets and dataset class in other applications. |
| """ |
| import numpy as np |
|
|
| import torch |
| from torch.utils.data import DataLoader |
|
|
| import robomimic |
| import robomimic.utils.obs_utils as ObsUtils |
| import robomimic.utils.torch_utils as TorchUtils |
| import robomimic.utils.test_utils as TestUtils |
| import robomimic.utils.file_utils as FileUtils |
| from robomimic.utils.dataset import SequenceDataset |
|
|
| from robomimic.config import config_factory |
| from robomimic.algo import algo_factory |
|
|
|
|
| def get_data_loader(dataset_path): |
| """ |
| Get a data loader to sample batches of data. |
| |
| Args: |
| dataset_path (str): path to the dataset hdf5 |
| """ |
| dataset = SequenceDataset( |
| hdf5_path=dataset_path, |
| obs_keys=( |
| "robot0_eef_pos", |
| "robot0_eef_quat", |
| "robot0_gripper_qpos", |
| "object", |
| ), |
| action_keys=("actions",), |
| dataset_keys=( |
| "actions", |
| "rewards", |
| "dones", |
| ), |
| action_config={"actions": {}}, |
| load_next_obs=True, |
| frame_stack=1, |
| seq_length=10, |
| pad_frame_stack=True, |
| pad_seq_length=True, |
| get_pad_mask=False, |
| goal_mode=None, |
| hdf5_cache_mode="all", |
| hdf5_use_swmr=True, |
| hdf5_normalize_obs=False, |
| filter_by_attribute=None, |
| ) |
| print("\n============= Created Dataset =============") |
| print(dataset) |
| print("") |
|
|
| data_loader = DataLoader( |
| dataset=dataset, |
| sampler=None, |
| batch_size=100, |
| shuffle=True, |
| num_workers=0, |
| drop_last=True |
| ) |
| return data_loader |
|
|
|
|
| def get_example_model(dataset_path, device): |
| """ |
| Use a default config to construct a BC model. |
| """ |
|
|
| |
| config = config_factory(algo_name="bc") |
|
|
| |
| config.train.data = [{"path": dataset_path}] |
|
|
| |
| ObsUtils.initialize_obs_utils_with_config(config) |
|
|
| |
| shape_meta = FileUtils.get_shape_metadata_from_dataset( |
| dataset_config=config.train.data[0], |
| action_keys=config.train.action_keys, |
| all_obs_keys=sorted(( |
| "robot0_eef_pos", |
| "robot0_eef_quat", |
| "robot0_gripper_qpos", |
| "object", |
| )), |
| ) |
|
|
| |
| model = algo_factory( |
| algo_name=config.algo_name, |
| config=config, |
| obs_key_shapes=shape_meta["all_shapes"], |
| ac_dim=shape_meta["ac_dim"], |
| device=device, |
| ) |
| return model |
|
|
|
|
| def print_batch_info(batch): |
| print("\n============= Batch Info =============") |
| for k in batch: |
| if k in ["obs", "next_obs"]: |
| print("key {}".format(k)) |
| for obs_key in batch[k]: |
| print(" obs key {} with shape {}".format(obs_key, batch[k][obs_key].shape)) |
| else: |
| print("key {} with shape {}".format(k, batch[k].shape)) |
| print("") |
|
|
|
|
| def run_train_loop(model, data_loader): |
| """ |
| Note: this is a stripped down version of @TrainUtils.run_epoch and the train loop |
| in the train function in train.py. Logging and evaluation rollouts were removed. |
| |
| Args: |
| model (Algo instance): instance of Algo class to use for training |
| data_loader (torch.utils.data.DataLoader instance): torch DataLoader for |
| sampling batches |
| """ |
| num_epochs = 50 |
| gradient_steps_per_epoch = 100 |
| has_printed_batch_info = False |
|
|
| |
| model.set_train() |
|
|
| for epoch in range(1, num_epochs + 1): |
|
|
| |
| data_loader_iter = iter(data_loader) |
|
|
| |
| losses = [] |
|
|
| for _ in range(gradient_steps_per_epoch): |
|
|
| |
| try: |
| batch = next(data_loader_iter) |
| except StopIteration: |
| |
| data_loader_iter = iter(data_loader) |
| batch = next(data_loader_iter) |
|
|
| if not has_printed_batch_info: |
| has_printed_batch_info = True |
| print_batch_info(batch) |
|
|
| |
| input_batch = model.process_batch_for_training(batch) |
| input_batch = model.postprocess_batch_for_training(input_batch, obs_normalization_stats=None) |
|
|
| |
| info = model.train_on_batch(batch=input_batch, epoch=epoch, validate=False) |
|
|
| |
| step_log = model.log_info(info) |
| losses.append(step_log["Loss"]) |
|
|
| |
| model.on_epoch_end(epoch) |
|
|
| print("Train Epoch {}: Loss {}".format(epoch, np.mean(losses))) |
|
|
|
|
| if __name__ == "__main__": |
|
|
| |
| dataset_path = TestUtils.example_dataset_path() |
|
|
| |
| device = TorchUtils.get_torch_device(try_to_use_cuda=True) |
|
|
| |
| model = get_example_model(dataset_path=dataset_path, device=device) |
|
|
| |
| data_loader = get_data_loader(dataset_path=dataset_path) |
|
|
| |
| run_train_loop(model=model, data_loader=data_loader) |
|
|