Spaces:
Running
Running
Upload 2 files
Browse files- .gitattributes +1 -0
- pusht_policy.mp4 +3 -0
- run_pusht.py +102 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
pusht_policy.mp4 filter=lfs diff=lfs merge=lfs -text
|
pusht_policy.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:de267c80a2133d2af9783649b663f0f2c29fa75e33b5fd0025eeb4473432e6d1
|
| 3 |
+
size 195654
|
run_pusht.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gymnasium as gym
|
| 3 |
+
import gym_pusht
|
| 4 |
+
import torch
|
| 5 |
+
import imageio
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import safetensors.torch
|
| 8 |
+
from lerobot.policies.diffusion.modeling_diffusion import DiffusionPolicy
|
| 9 |
+
from lerobot.configs.policies import PreTrainedConfig
|
| 10 |
+
from lerobot.policies.factory import make_pre_post_processors
|
| 11 |
+
from lerobot.envs.utils import preprocess_observation
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
# 1. Download checkpoint and load config
|
| 15 |
+
print("Downloading config from lerobot/diffusion_pusht...")
|
| 16 |
+
cfg = PreTrainedConfig.from_pretrained('lerobot/diffusion_pusht')
|
| 17 |
+
|
| 18 |
+
# We override the observation.image feature shape to (3, 384, 384) to match the environment defaults,
|
| 19 |
+
# which instantiates the model's pos_grid as [144, 2] instead of [9, 2] (checkpoint size).
|
| 20 |
+
cfg.input_features['observation.image'].shape = (3, 384, 384)
|
| 21 |
+
|
| 22 |
+
# Build the DiffusionPolicy
|
| 23 |
+
print("Building DiffusionPolicy...")
|
| 24 |
+
policy = DiffusionPolicy(cfg)
|
| 25 |
+
print("Initial pos_grid shape in model:", policy.diffusion.rgb_encoder.pool.pos_grid.shape)
|
| 26 |
+
|
| 27 |
+
# Load weights with strict=False
|
| 28 |
+
print("Downloading and loading safetensors model weights...")
|
| 29 |
+
model_file = hf_hub_download(repo_id='lerobot/diffusion_pusht', filename='model.safetensors')
|
| 30 |
+
state_dict = safetensors.torch.load_file(model_file)
|
| 31 |
+
policy.load_state_dict(state_dict, strict=False)
|
| 32 |
+
|
| 33 |
+
# 2. Patch the pos_grid shape mismatch so inference works
|
| 34 |
+
print("Patching the pos_grid shape mismatch...")
|
| 35 |
+
checkpoint_pos_grid = state_dict['diffusion.rgb_encoder.pool.pos_grid']
|
| 36 |
+
policy.diffusion.rgb_encoder.pool.register_buffer('pos_grid', checkpoint_pos_grid)
|
| 37 |
+
print("Patched pos_grid shape in model:", policy.diffusion.rgb_encoder.pool.pos_grid.shape)
|
| 38 |
+
|
| 39 |
+
# Move policy to correct device and set to eval mode
|
| 40 |
+
policy.to(cfg.device)
|
| 41 |
+
policy.eval()
|
| 42 |
+
|
| 43 |
+
# 3. Create preprocessor / postprocessor with the extracted dataset stats
|
| 44 |
+
print("Creating preprocessor and postprocessor...")
|
| 45 |
+
dataset_stats = {
|
| 46 |
+
'observation.image': {
|
| 47 |
+
'mean': state_dict['normalize_inputs.buffer_observation_image.mean'],
|
| 48 |
+
'std': state_dict['normalize_inputs.buffer_observation_image.std'],
|
| 49 |
+
},
|
| 50 |
+
'observation.state': {
|
| 51 |
+
'max': state_dict['normalize_inputs.buffer_observation_state.max'],
|
| 52 |
+
'min': state_dict['normalize_inputs.buffer_observation_state.min'],
|
| 53 |
+
},
|
| 54 |
+
'action': {
|
| 55 |
+
'max': state_dict['normalize_targets.buffer_action.max'],
|
| 56 |
+
'min': state_dict['normalize_targets.buffer_action.min'],
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
preprocessor, postprocessor = make_pre_post_processors(cfg, dataset_stats=dataset_stats)
|
| 60 |
+
|
| 61 |
+
# 4. Instantiate the gym environment
|
| 62 |
+
print("Creating PushT environment...")
|
| 63 |
+
env = gym.make('gym_pusht/PushT-v0', render_mode='rgb_array', obs_type='pixels_agent_pos')
|
| 64 |
+
|
| 65 |
+
# Reset env and cache initial frame
|
| 66 |
+
policy.reset()
|
| 67 |
+
obs, info = env.reset()
|
| 68 |
+
frames = [env.render()]
|
| 69 |
+
|
| 70 |
+
# Run rollout for 300 steps
|
| 71 |
+
print("Running 300 steps rollout...")
|
| 72 |
+
for step in range(300):
|
| 73 |
+
# Format observations to LeRobot format
|
| 74 |
+
obs_t = preprocess_observation(obs)
|
| 75 |
+
obs_t = preprocessor(obs_t)
|
| 76 |
+
|
| 77 |
+
# Select action
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
action = policy.select_action(obs_t)
|
| 80 |
+
action = postprocessor(action)
|
| 81 |
+
|
| 82 |
+
# Extract numpy action and apply to env (drop batch dimension)
|
| 83 |
+
action_numpy = action.to("cpu").numpy()[0]
|
| 84 |
+
obs, reward, terminated, truncated, info = env.step(action_numpy)
|
| 85 |
+
|
| 86 |
+
# Render frame
|
| 87 |
+
frame = env.render()
|
| 88 |
+
frames.append(frame)
|
| 89 |
+
|
| 90 |
+
if terminated or truncated:
|
| 91 |
+
obs, info = env.reset()
|
| 92 |
+
|
| 93 |
+
# Close env
|
| 94 |
+
env.close()
|
| 95 |
+
|
| 96 |
+
# 5. Save the frames as pusht_policy.mp4
|
| 97 |
+
print("Saving video to pusht_policy.mp4...")
|
| 98 |
+
imageio.mimsave("pusht_policy.mp4", frames, fps=10)
|
| 99 |
+
print("Done! Video saved successfully.")
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
main()
|