|
| 1 | +"""This script demonstrates how to train Diffusion Policy on a real-world dataset.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +from lerobot.configs.types import FeatureType |
| 8 | +from lerobot.datasets.lerobot_dataset import LeRobotDataset, LeRobotDatasetMetadata |
| 9 | +from lerobot.datasets.utils import dataset_to_policy_features |
| 10 | +from lerobot.policies.diffusion.configuration_diffusion import DiffusionConfig |
| 11 | +from lerobot.policies.diffusion.modeling_diffusion import DiffusionPolicy |
| 12 | +from lerobot.policies.factory import make_pre_post_processors |
| 13 | + |
| 14 | + |
| 15 | +def make_delta_timestamps(delta_indices: list[int] | None, fps: int) -> list[float]: |
| 16 | + if delta_indices is None: |
| 17 | + return [0] |
| 18 | + |
| 19 | + return [i / fps for i in delta_indices] |
| 20 | + |
| 21 | + |
| 22 | +output_directory = Path("outputs/robot_learning_tutorial/diffusion") |
| 23 | +output_directory.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | +# Select your device |
| 26 | +device = torch.device("mps") # or "cuda" or "cpu" |
| 27 | + |
| 28 | +dataset_id = "lerobot/svla_so101_pickplace" |
| 29 | + |
| 30 | +# This specifies the inputs the model will be expecting and the outputs it will produce |
| 31 | +dataset_metadata = LeRobotDatasetMetadata(dataset_id) |
| 32 | +features = dataset_to_policy_features(dataset_metadata.features) |
| 33 | + |
| 34 | +output_features = {key: ft for key, ft in features.items() if ft.type is FeatureType.ACTION} |
| 35 | +input_features = {key: ft for key, ft in features.items() if key not in output_features} |
| 36 | + |
| 37 | +cfg = DiffusionConfig(input_features=input_features, output_features=output_features) |
| 38 | +policy = DiffusionPolicy(cfg) |
| 39 | +preprocessor, postprocessor = make_pre_post_processors(cfg, dataset_stats=dataset_metadata.stats) |
| 40 | + |
| 41 | +policy.train() |
| 42 | +policy.to(device) |
| 43 | + |
| 44 | +# To perform action chunking, ACT expects a given number of actions as targets |
| 45 | +delta_timestamps = { |
| 46 | + "observation.state": make_delta_timestamps(cfg.observation_delta_indices, dataset_metadata.fps), |
| 47 | + "action": make_delta_timestamps(cfg.action_delta_indices, dataset_metadata.fps), |
| 48 | +} |
| 49 | + |
| 50 | +# add image features if they are present |
| 51 | +delta_timestamps |= { |
| 52 | + k: make_delta_timestamps(cfg.observation_delta_indices, dataset_metadata.fps) for k in cfg.image_features |
| 53 | +} |
| 54 | + |
| 55 | +# Instantiate the dataset |
| 56 | +dataset = LeRobotDataset(dataset_id, delta_timestamps=delta_timestamps) |
| 57 | + |
| 58 | +# Create the optimizer and dataloader for offline training |
| 59 | +optimizer = cfg.get_optimizer_preset().build(policy.parameters()) |
| 60 | +batch_size = 32 |
| 61 | +dataloader = torch.utils.data.DataLoader( |
| 62 | + dataset, |
| 63 | + batch_size=batch_size, |
| 64 | + shuffle=True, |
| 65 | + pin_memory=device.type != "cpu", |
| 66 | + drop_last=True, |
| 67 | +) |
| 68 | + |
| 69 | +# Number of training steps and logging frequency |
| 70 | +training_steps = 1 |
| 71 | +log_freq = 1 |
| 72 | + |
| 73 | +# Run training loop |
| 74 | +step = 0 |
| 75 | +done = False |
| 76 | +while not done: |
| 77 | + for batch in dataloader: |
| 78 | + batch = preprocessor(batch) |
| 79 | + loss, _ = policy.forward(batch) |
| 80 | + loss.backward() |
| 81 | + optimizer.step() |
| 82 | + optimizer.zero_grad() |
| 83 | + |
| 84 | + if step % log_freq == 0: |
| 85 | + print(f"step: {step} loss: {loss.item():.3f}") |
| 86 | + step += 1 |
| 87 | + if step >= training_steps: |
| 88 | + done = True |
| 89 | + break |
| 90 | + |
| 91 | +# Save the policy checkpoint, alongside the pre/post processors |
| 92 | +policy.save_pretrained(output_directory) |
| 93 | +preprocessor.save_pretrained(output_directory) |
| 94 | +postprocessor.save_pretrained(output_directory) |
| 95 | + |
| 96 | +# Save all assets to the Hub |
| 97 | +policy.push_to_hub("fracapuano/robot_learning_tutorial_diffusion") |
| 98 | +preprocessor.push_to_hub("fracapuano/robot_learning_tutorial_diffusion") |
| 99 | +postprocessor.push_to_hub("fracapuano/robot_learning_tutorial_diffusion") |
0 commit comments