patrickbdevaney commited on
Commit
620dc33
·
verified ·
1 Parent(s): 8cd4be8

upload of hacakthon dataset, related python code, xml files

Browse files
finetune.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import torch
4
+ from stable_baselines3 import PPO
5
+ from stable_baselines3.common.vec_env import DummyVecEnv
6
+ import time
7
+
8
+ # Verify GPU availability
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+ print(f"Using device: {device}")
11
+
12
+ # Set working directory (assumed to be the dataset directory with stacking_env.py)
13
+ current_dir = os.getcwd()
14
+ model_save_path = os.path.join(current_dir, "stacking_model_20250615_1618")
15
+ os.makedirs(model_save_path, exist_ok=True)
16
+
17
+ # Load .npz files directly from the directory
18
+ npz_files = [f for f in os.listdir(current_dir) if f.endswith(".npz")]
19
+ data = {}
20
+ for npz_file in npz_files:
21
+ data[npz_file] = np.load(os.path.join(current_dir, npz_file))
22
+ print(f"Loaded {npz_file}")
23
+
24
+ # Prepare training data from .npz files
25
+ def prepare_training_data(data_dict):
26
+ observations = []
27
+ actions = []
28
+ rewards = []
29
+ dones = []
30
+ for npz_file in data_dict:
31
+ observations.append(data_dict[npz_file]["observations"])
32
+ actions.append(data_dict[npz_file]["actions"])
33
+ rewards.append(data_dict[npz_file]["rewards"])
34
+ dones.append(data_dict[npz_file]["dones"])
35
+ return (np.concatenate(observations, axis=0),
36
+ np.concatenate(actions, axis=0),
37
+ np.concatenate(rewards, axis=0),
38
+ np.concatenate(dones, axis=0))
39
+
40
+ obs, acts, rews, dons = prepare_training_data(data)
41
+ print("Training data shapes:", obs.shape, acts.shape, rews.shape, dons.shape)
42
+
43
+ # Define environment from stacking_env.py in the same directory
44
+ import stacking_env
45
+ def make_env():
46
+ return stacking_env.StackingEnv(render_mode=None) # Disable rendering for training
47
+
48
+ env = DummyVecEnv([make_env])
49
+
50
+ # Initialize PPO policy with proper net_arch format
51
+ policy_kwargs = {
52
+ "net_arch": {
53
+ "pi": [64, 64], # Policy network architecture
54
+ "vf": [64, 64] # Value network architecture
55
+ }
56
+ }
57
+ model = PPO(
58
+ "MlpPolicy",
59
+ env,
60
+ policy_kwargs=policy_kwargs,
61
+ verbose=1,
62
+ learning_rate=3e-4,
63
+ n_steps=2048,
64
+ batch_size=64,
65
+ device=device # Use detected device (cuda or cpu)
66
+ )
67
+
68
+ # RL training loop with simulated HIL interventions
69
+ total_timesteps = 50000 # Adjust based on time; reduce to 10000 if needed
70
+ for _ in range(int(total_timesteps / 2048)):
71
+ model.learn(total_timesteps=2048, reset_num_timesteps=False)
72
+ if np.random.random() < 0.1: # 10% chance of simulated intervention
73
+ print(f"Simulated human intervention at step {model.num_timesteps}")
74
+
75
+ # Save the trained model
76
+ model_path = os.path.join(model_save_path, "ppo_hil_serl_stacking_offline")
77
+ model.save(model_path)
78
+ print(f"Model saved to {model_path}")
79
+
80
+ # Cleanup
81
+ print("Training complete. Upload the dataset folder and a separate demonstration (e.g., using screenshots) to the Hugging Face LeRobot Worldwide Hackathon community.")
panda.xml ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <mujoco model="panda">
2
+ <compiler angle="radian" meshdir="assets" autolimits="true"/>
3
+
4
+ <option integrator="implicitfast"/>
5
+
6
+ <default>
7
+ <default class="panda">
8
+ <material specular="0.5" shininess="0.25"/>
9
+ <joint armature="0.1" damping="1" axis="0 0 1" range="-2.8973 2.8973"/>
10
+ <general dyntype="none" biastype="affine" ctrlrange="-2.8973 2.8973" forcerange="-87 87"/>
11
+ <default class="finger">
12
+ <joint axis="0 1 0" type="slide" range="0 0.04"/>
13
+ </default>
14
+
15
+ <default class="visual">
16
+ <geom type="mesh" contype="0" conaffinity="0" group="2"/>
17
+ </default>
18
+ <default class="collision">
19
+ <geom type="mesh" group="3"/>
20
+ <default class="fingertip_pad_collision_1">
21
+ <geom type="box" size="0.0085 0.004 0.0085" pos="0 0.0055 0.0445"/>
22
+ </default>
23
+ <default class="fingertip_pad_collision_2">
24
+ <geom type="box" size="0.003 0.002 0.003" pos="0.0055 0.002 0.05"/>
25
+ </default>
26
+ <default class="fingertip_pad_collision_3">
27
+ <geom type="box" size="0.003 0.002 0.003" pos="-0.0055 0.002 0.05"/>
28
+ </default>
29
+ <default class="fingertip_pad_collision_4">
30
+ <geom type="box" size="0.003 0.002 0.0035" pos="0.0055 0.002 0.0395"/>
31
+ </default>
32
+ <default class="fingertip_pad_collision_5">
33
+ <geom type="box" size="0.003 0.002 0.0035" pos="-0.0055 0.002 0.0395"/>
34
+ </default>
35
+ </default>
36
+ </default>
37
+ </default>
38
+
39
+ <asset>
40
+ <material class="panda" name="white" rgba="1 1 1 1"/>
41
+ <material class="panda" name="off_white" rgba="0.901961 0.921569 0.929412 1"/>
42
+ <material class="panda" name="black" rgba="0.25 0.25 0.25 1"/>
43
+ <material class="panda" name="green" rgba="0 1 0 1"/>
44
+ <material class="panda" name="light_blue" rgba="0.039216 0.541176 0.780392 1"/>
45
+
46
+ <!-- Collision meshes -->
47
+ <mesh name="link0_c" file="link0.stl"/>
48
+ <mesh name="link1_c" file="link1.stl"/>
49
+ <mesh name="link2_c" file="link2.stl"/>
50
+ <mesh name="link3_c" file="link3.stl"/>
51
+ <mesh name="link4_c" file="link4.stl"/>
52
+ <mesh name="link5_c0" file="link5_collision_0.obj"/>
53
+ <mesh name="link5_c1" file="link5_collision_1.obj"/>
54
+ <mesh name="link5_c2" file="link5_collision_2.obj"/>
55
+ <mesh name="link6_c" file="link6.stl"/>
56
+ <mesh name="link7_c" file="link7.stl"/>
57
+ <mesh name="hand_c" file="hand.stl"/>
58
+
59
+ <!-- Visual meshes -->
60
+ <mesh file="link0_0.obj"/>
61
+ <mesh file="link0_1.obj"/>
62
+ <mesh file="link0_2.obj"/>
63
+ <mesh file="link0_3.obj"/>
64
+ <mesh file="link0_4.obj"/>
65
+ <mesh file="link0_5.obj"/>
66
+ <mesh file="link0_7.obj"/>
67
+ <mesh file="link0_8.obj"/>
68
+ <mesh file="link0_9.obj"/>
69
+ <mesh file="link0_10.obj"/>
70
+ <mesh file="link0_11.obj"/>
71
+ <mesh file="link1.obj"/>
72
+ <mesh file="link2.obj"/>
73
+ <mesh file="link3_0.obj"/>
74
+ <mesh file="link3_1.obj"/>
75
+ <mesh file="link3_2.obj"/>
76
+ <mesh file="link3_3.obj"/>
77
+ <mesh file="link4_0.obj"/>
78
+ <mesh file="link4_1.obj"/>
79
+ <mesh file="link4_2.obj"/>
80
+ <mesh file="link4_3.obj"/>
81
+ <mesh file="link5_0.obj"/>
82
+ <mesh file="link5_1.obj"/>
83
+ <mesh file="link5_2.obj"/>
84
+ <mesh file="link6_0.obj"/>
85
+ <mesh file="link6_1.obj"/>
86
+ <mesh file="link6_2.obj"/>
87
+ <mesh file="link6_3.obj"/>
88
+ <mesh file="link6_4.obj"/>
89
+ <mesh file="link6_5.obj"/>
90
+ <mesh file="link6_6.obj"/>
91
+ <mesh file="link6_7.obj"/>
92
+ <mesh file="link6_8.obj"/>
93
+ <mesh file="link6_9.obj"/>
94
+ <mesh file="link6_10.obj"/>
95
+ <mesh file="link6_11.obj"/>
96
+ <mesh file="link6_12.obj"/>
97
+ <mesh file="link6_13.obj"/>
98
+ <mesh file="link6_14.obj"/>
99
+ <mesh file="link6_15.obj"/>
100
+ <mesh file="link6_16.obj"/>
101
+ <mesh file="link7_0.obj"/>
102
+ <mesh file="link7_1.obj"/>
103
+ <mesh file="link7_2.obj"/>
104
+ <mesh file="link7_3.obj"/>
105
+ <mesh file="link7_4.obj"/>
106
+ <mesh file="link7_5.obj"/>
107
+ <mesh file="link7_6.obj"/>
108
+ <mesh file="link7_7.obj"/>
109
+ <mesh file="hand_0.obj"/>
110
+ <mesh file="hand_1.obj"/>
111
+ <mesh file="hand_2.obj"/>
112
+ <mesh file="hand_3.obj"/>
113
+ <mesh file="hand_4.obj"/>
114
+ <mesh file="finger_0.obj"/>
115
+ <mesh file="finger_1.obj"/>
116
+ </asset>
117
+
118
+ <worldbody>
119
+ <light name="top" pos="0 0 2" mode="trackcom"/>
120
+ <body name="link0" childclass="panda">
121
+ <inertial mass="0.629769" pos="-0.041018 -0.00014 0.049974"
122
+ fullinertia="0.00315 0.00388 0.004285 8.2904e-7 0.00015 8.2299e-6"/>
123
+ <geom mesh="link0_0" material="off_white" class="visual"/>
124
+ <geom mesh="link0_1" material="black" class="visual"/>
125
+ <geom mesh="link0_2" material="off_white" class="visual"/>
126
+ <geom mesh="link0_3" material="black" class="visual"/>
127
+ <geom mesh="link0_4" material="off_white" class="visual"/>
128
+ <geom mesh="link0_5" material="black" class="visual"/>
129
+ <geom mesh="link0_7" material="white" class="visual"/>
130
+ <geom mesh="link0_8" material="white" class="visual"/>
131
+ <geom mesh="link0_9" material="black" class="visual"/>
132
+ <geom mesh="link0_10" material="off_white" class="visual"/>
133
+ <geom mesh="link0_11" material="white" class="visual"/>
134
+ <geom mesh="link0_c" class="collision"/>
135
+ <body name="link1" pos="0 0 0.333">
136
+ <inertial mass="4.970684" pos="0.003875 0.002081 -0.04762"
137
+ fullinertia="0.70337 0.70661 0.0091170 -0.00013900 0.0067720 0.019169"/>
138
+ <joint name="joint1"/>
139
+ <geom material="white" mesh="link1" class="visual"/>
140
+ <geom mesh="link1_c" class="collision"/>
141
+ <body name="link2" quat="1 -1 0 0">
142
+ <inertial mass="0.646926" pos="-0.003141 -0.02872 0.003495"
143
+ fullinertia="0.0079620 2.8110e-2 2.5995e-2 -3.925e-3 1.0254e-2 7.04e-4"/>
144
+ <joint name="joint2" range="-2.8973 2.8973"/>
145
+ <geom material="white" mesh="link2" class="visual"/>
146
+ <geom mesh="link2_c" class="collision"/>
147
+ <body name="link3" pos="0 -0.316 0" quat="1 1 0 0">
148
+ <joint name="joint3"/>
149
+ <inertial mass="3.228604" pos="2.7518e-2 3.9252e-2 -6.6502e-2"
150
+ fullinertia="3.7242e-2 3.6155e-2 1.083e-2 -4.761e-3 -1.1396e-2 -1.2805e-2"/>
151
+ <geom mesh="link3_0" material="white" class="visual"/>
152
+ <geom mesh="link3_1" material="white" class="visual"/>
153
+ <geom mesh="link3_2" material="white" class="visual"/>
154
+ <geom mesh="link3_3" material="black" class="visual"/>
155
+ <geom mesh="link3_c" class="collision"/>
156
+ <body name="link4" pos="0.0825 0 0" quat="1 1 0 0">
157
+ <inertial mass="3.587895" pos="-5.317e-2 1.04419e-1 2.7454e-2"
158
+ fullinertia="2.5853e-2 1.9552e-2 2.8323e-2 7.796e-3 -1.332e-3 8.641e-3"/>
159
+ <joint name="joint4" range="-2.8973 2.8973"/>
160
+ <geom mesh="link4_0" material="white" class="visual"/>
161
+ <geom mesh="link4_1" material="white" class="visual"/>
162
+ <geom mesh="link4_2" material="black" class="visual"/>
163
+ <geom mesh="link4_3" material="white" class="visual"/>
164
+ <geom mesh="link4_c" class="collision"/>
165
+ <body name="link5" pos="-0.0825 0.384 0" quat="1 -1 0 0">
166
+ <inertial mass="1.225946" pos="-1.1953e-2 4.1065e-2 -3.8437e-2"
167
+ fullinertia="3.5549e-2 2.9474e-2 8.627e-3 -2.117e-3 -4.037e-3 2.29e-4"/>
168
+ <joint name="joint5"/>
169
+ <geom mesh="link5_0" material="black" class="visual"/>
170
+ <geom mesh="link5_1" material="white" class="visual"/>
171
+ <geom mesh="link5_2" material="white" class="visual"/>
172
+ <geom mesh="link5_c0" class="collision"/>
173
+ <geom mesh="link5_c1" class="collision"/>
174
+ <geom mesh="link5_c2" class="collision"/>
175
+ <body name="link6" quat="1 1 0 0">
176
+ <inertial mass="1.666555" pos="6.0149e-2 -1.4117e-2 -1.0517e-2"
177
+ fullinertia="1.964e-3 4.354e-3 5.433e-3 1.09e-4 -1.158e-3 3.41e-4"/>
178
+ <joint name="joint6" range="-2.8973 2.8973"/>
179
+ <geom mesh="link6_0" material="off_white" class="visual"/>
180
+ <geom mesh="link6_1" material="white" class="visual"/>
181
+ <geom mesh="link6_2" material="black" class="visual"/>
182
+ <geom mesh="link6_3" material="white" class="visual"/>
183
+ <geom mesh="link6_4" material="white" class="visual"/>
184
+ <geom mesh="link6_5" material="white" class="visual"/>
185
+ <geom mesh="link6_6" material="white" class="visual"/>
186
+ <geom mesh="link6_7" material="light_blue" class="visual"/>
187
+ <geom mesh="link6_8" material="light_blue" class="visual"/>
188
+ <geom mesh="link6_9" material="black" class="visual"/>
189
+ <geom mesh="link6_10" material="black" class="visual"/>
190
+ <geom mesh="link6_11" material="white" class="visual"/>
191
+ <geom mesh="link6_12" material="green" class="visual"/>
192
+ <geom mesh="link6_13" material="white" class="visual"/>
193
+ <geom mesh="link6_14" material="black" class="visual"/>
194
+ <geom mesh="link6_15" material="black" class="visual"/>
195
+ <geom mesh="link6_16" material="white" class="visual"/>
196
+ <geom mesh="link6_c" class="collision"/>
197
+ <body name="link7" pos="0.088 0 0" quat="1 1 0 0">
198
+ <inertial mass="7.35522e-01" pos="1.0517e-2 -4.252e-3 6.1597e-2"
199
+ fullinertia="1.2516e-2 1.0027e-2 4.815e-3 -4.28e-4 -1.196e-3 -7.41e-4"/>
200
+ <joint name="joint7" range="-2.8973 2.8973"/>
201
+ <geom mesh="link7_0" material="white" class="visual"/>
202
+ <geom mesh="link7_1" material="black" class="visual"/>
203
+ <geom mesh="link7_2" material="black" class="visual"/>
204
+ <geom mesh="link7_3" material="black" class="visual"/>
205
+ <geom mesh="link7_4" material="black" class="visual"/>
206
+ <geom mesh="link7_5" material="black" class="visual"/>
207
+ <geom mesh="link7_6" material="black" class="visual"/>
208
+ <geom mesh="link7_7" material="white" class="visual"/>
209
+ <geom mesh="link7_c" class="collision"/>
210
+ <body name="hand" pos="0 0 0.107" quat="0.9238795 0 0 -0.3826834">
211
+ <inertial mass="0.73" pos="-0.01 0 0.03" diaginertia="0.001 0.0025 0.0017"/>
212
+ <geom mesh="hand_0" material="off_white" class="visual"/>
213
+ <geom mesh="hand_1" material="black" class="visual"/>
214
+ <geom mesh="hand_2" material="black" class="visual"/>
215
+ <geom mesh="hand_3" material="white" class="visual"/>
216
+ <geom mesh="hand_4" material="off_white" class="visual"/>
217
+ <geom mesh="hand_c" class="collision"/>
218
+ <body name="left_finger" pos="0 0 0.0584">
219
+ <inertial mass="0.015" pos="0 0 0" diaginertia="2.375e-6 2.375e-6 7.5e-7"/>
220
+ <joint name="finger_joint1" class="finger"/>
221
+ <geom mesh="finger_0" material="off_white" class="visual"/>
222
+ <geom mesh="finger_1" material="black" class="visual"/>
223
+ <geom mesh="finger_0" class="collision"/>
224
+ <geom class="fingertip_pad_collision_1"/>
225
+ <geom class="fingertip_pad_collision_2"/>
226
+ <geom class="fingertip_pad_collision_3"/>
227
+ <geom class="fingertip_pad_collision_4"/>
228
+ <geom class="fingertip_pad_collision_5"/>
229
+ </body>
230
+ <body name="right_finger" pos="0 0 0.0584" quat="0 0 0 1">
231
+ <inertial mass="0.015" pos="0 0 0" diaginertia="2.375e-6 2.375e-6 7.5e-7"/>
232
+ <joint name="finger_joint2" class="finger"/>
233
+ <geom mesh="finger_0" material="off_white" class="visual"/>
234
+ <geom mesh="finger_1" material="black" class="visual"/>
235
+ <geom mesh="finger_0" class="collision"/>
236
+ <geom class="fingertip_pad_collision_1"/>
237
+ <geom class="fingertip_pad_collision_2"/>
238
+ <geom class="fingertip_pad_collision_3"/>
239
+ <geom class="fingertip_pad_collision_4"/>
240
+ <geom class="fingertip_pad_collision_5"/>
241
+ </body>
242
+ </body>
243
+ </body>
244
+ </body>
245
+ </body>
246
+ </body>
247
+ </body>
248
+ </body>
249
+ </body>
250
+ </body>
251
+ </worldbody>
252
+
253
+ <tendon>
254
+ <fixed name="split">
255
+ <joint joint="finger_joint1" coef="0.5"/>
256
+ <joint joint="finger_joint2" coef="0.5"/>
257
+ </fixed>
258
+ </tendon>
259
+
260
+ <equality>
261
+ <joint joint1="finger_joint1" joint2="finger_joint2" solimp="0.95 0.99 0.001" solref="0.005 1"/>
262
+ </equality>
263
+
264
+ <actuator>
265
+ <general class="panda" name="actuator1" joint="joint1" gainprm="4500" biasprm="0 -4500 -450" ctrlrange="-2.8973 2.8973"/>
266
+ <general class="panda" name="actuator2" joint="joint2" gainprm="4500" biasprm="0 -4500 -450" ctrlrange="-1.57 1.57"/>
267
+ <general class="panda" name="actuator3" joint="joint3" gainprm="3500" biasprm="0 -3500 -350" ctrlrange="-2.8973 2.8973"/>
268
+ <general class="panda" name="actuator4" joint="joint4" gainprm="3500" biasprm="0 -3500 -350" ctrlrange="-2.8973 2.8973"/>
269
+ <general class="panda" name="actuator5" joint="joint5" gainprm="2000" biasprm="0 -2000 -200" forcerange="-12 12" ctrlrange="-2.8973 2.8973"/>
270
+ <general class="panda" name="actuator6" joint="joint6" gainprm="2000" biasprm="0 -2000 -200" forcerange="-12 12" ctrlrange="-2.8973 2.8973"/>
271
+ <general class="panda" name="actuator7" joint="joint7" gainprm="2000" biasprm="0 -2000 -200" forcerange="-12 12" ctrlrange="-2.8973 2.8973"/>
272
+ <!-- Remap original ctrlrange (0, 0.04) to (0, 255): 0.04 * 100 / 255 = 0.01568627451 -->
273
+ <general class="panda" name="actuator8" tendon="split" forcerange="-100 100" ctrlrange="0 255"
274
+ gainprm="0.01568627451 0 0" biasprm="0 -100 -10"/>
275
+ </actuator>
276
+
277
+ <keyframe>
278
+ <key name="home" qpos="0 0 0.1 1 0 0 0 0 0 0 0 0 0 0 -1.57079 0 1.57079 -0.7853 0.04 0.04 0 0 0 0 0 0 0 0 0 0" ctrl="0 0 0 -1.57 0 1.57 -0.7853 255"/>
279
+ </keyframe>
280
+ </mujoco>
stacking.xml ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <mujoco model="franka_stacking">
2
+ <!-- Include the prebuilt Panda model from MuJoCo Menagerie -->
3
+ <include file="panda.xml"/>
4
+
5
+ <!-- Compiler and option settings -->
6
+ <compiler angle="degree" coordinate="local" inertiafromgeom="true"/>
7
+ <option timestep="0.002" gravity="0 0 -9.81" integrator="implicitfast"/>
8
+
9
+ <!-- Visual settings -->
10
+ <visual>
11
+ <map znear="0.01" zfar="50" fogstart="10" fogend="50"/>
12
+ <rgba fog="0.8 0.8 0.8 1"/>
13
+ </visual>
14
+
15
+ <!-- Default joint settings (optional, for new joints if added) -->
16
+ <default>
17
+ <joint/>
18
+ </default>
19
+
20
+ <!-- Extend the worldbody with blocks and target zone -->
21
+ <worldbody>
22
+ <!-- Reference the included Panda arm, positioned for stacking -->
23
+ <body name="panda" pos="0 0 0.1"/>
24
+
25
+ <!-- Floor -->
26
+ <geom name="floor" type="plane" size="2 2 0.1" material="floor_mat" condim="4"/>
27
+
28
+ <!-- Blocks (moved slightly forward and adjusted z for clearance) -->
29
+ <body name="block0" pos="0.3 0.1 0.15"> <!-- Moved to x=0.3, z=0.15 to avoid base -->
30
+ <joint name="block0_free" type="free"/>
31
+ <geom name="block0_geom" type="box" size="0.05 0.05 0.05" material="block0_mat" rgba="1 0 0 1"/>
32
+ </body>
33
+ <body name="block1" pos="0.2 0.1 0.15"> <!-- Moved to x=0.2, z=0.15 -->
34
+ <joint name="block1_free" type="free"/>
35
+ <geom name="block1_geom" type="box" size="0.05 0.05 0.05" material="block1_mat" rgba="0 1 0 1"/>
36
+ </body>
37
+ <body name="block2" pos="0.1 0.1 0.15"> <!-- Moved to x=0.1, z=0.15 -->
38
+ <joint name="block2_free" type="free"/>
39
+ <geom name="block2_geom" type="box" size="0.05 0.05 0.05" material="block2_mat" rgba="0 0 1 1"/>
40
+ </body>
41
+
42
+ <!-- Target Zone -->
43
+ <body name="target_zone" pos="0 0 0.01">
44
+ <geom name="target_zone_geom" type="plane" size="0.2 0.2 0.01" material="target_mat" rgba="0 1 1 0.5"/>
45
+ </body>
46
+ </worldbody>
47
+
48
+ <!-- Materials for visualization -->
49
+ <asset>
50
+ <material name="floor_mat" rgba="0.8 0.8 0.8 1"/>
51
+ <material name="block0_mat" rgba="1 0 0 1"/> <!-- Red -->
52
+ <material name="block1_mat" rgba="0 1 0 1"/> <!-- Green -->
53
+ <material name="block2_mat" rgba="0 0 1 1"/> <!-- Blue -->
54
+ <material name="target_mat" rgba="0 1 1 0.5"/> <!-- Cyan, semi-transparent -->
55
+ </asset>
56
+ </mujoco>
stacking_demo_20250615_1528_episode_0.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0736fc4ee1e0d8714765867e0ea11b15f3b43f5c67c5793e733465f9ece74349
3
+ size 2257
stacking_demo_20250615_1528_episode_1.npz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:27f5491b7f52719153c15753f6f931c4d81e4760e442bd5b6d93c4d3019ac2c5
3
+ size 2257
stacking_demo_20250615_1532_episode_0_step_0.jpg.png ADDED

Git LFS Details

  • SHA256: b07580619e5e22da134e3691cea2ac2ed4d9a240b91d5d37fe8518fae700f1ec
  • Pointer size: 131 Bytes
  • Size of remote file: 179 kB
stacking_demo_20250615_1532_episode_0_step_4.jpg.png ADDED

Git LFS Details

  • SHA256: 92fc10ed76f47aee53e81ed2aee788ac335611945c5c74fef5a6dc70b52c3150
  • Pointer size: 131 Bytes
  • Size of remote file: 135 kB
stacking_env.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gymnasium as gym
2
+ import mujoco
3
+ import numpy as np
4
+ from gymnasium.spaces import Box
5
+ from pathlib import Path
6
+ import mujoco.viewer
7
+
8
+ class StackingEnv(gym.Env):
9
+ def __init__(self, render_mode="human"):
10
+ super().__init__()
11
+ self.render_mode = render_mode
12
+
13
+ # Load stacking.xml
14
+ xml_path = str(Path(__file__).parent.parent / "assets/stacking.xml")
15
+ print(f"Loading XML from: {xml_path}")
16
+ try:
17
+ self.model = mujoco.MjModel.from_xml_path(xml_path)
18
+ self.data = mujoco.MjData(self.model)
19
+
20
+ # Debug: Print model dimensions
21
+ self.nq = self.model.nq # Total degrees of freedom
22
+ self.nv = self.model.nv # Velocity dimensions
23
+ self.nu = self.model.nu # Control dimensions
24
+ print(f"Model dimensions - nq: {self.nq}, nv: {self.nv}, nu: {self.nu}")
25
+
26
+ # Set a valid initial qpos to avoid invalid defaults
27
+ self.data.qpos = np.zeros(self.nq)
28
+ if self.nq >= 3: # Base position
29
+ self.data.qpos[0:3] = [0, 0, 0.1] # x, y, z
30
+ if self.nq >= 7: # Orientation
31
+ self.data.qpos[3:7] = [1, 0, 0, 0] # quaternion
32
+ mujoco.mj_forward(self.model, self.data)
33
+
34
+ except ValueError as e:
35
+ print(f"Error loading model: {e}")
36
+ print("Check stacking.xml for valid qpos or keyframe definitions.")
37
+ raise
38
+
39
+ self.viewer = None
40
+ if render_mode == "human":
41
+ try:
42
+ self.viewer = mujoco.viewer.launch_passive(self.model, self.data)
43
+ print("Viewer initialized successfully")
44
+ except Exception as e:
45
+ print(f"Viewer initialization failed: {e}")
46
+
47
+ # Define observation and action spaces to match dataset
48
+ self.observation_space = Box(low=-np.inf, high=np.inf, shape=(21,), dtype=np.float32) # Fixed to (21,) per dataset
49
+ self.action_space = Box(low=-1.0, high=1.0, shape=(9,), dtype=np.float32) # Fixed to (9,) per dataset
50
+
51
+ # Get body IDs and verify they exist
52
+ self.block_ids = []
53
+ for i in range(3):
54
+ block_id = mujoco.mj_name2id(self.model, mujoco.mjtObj.mjOBJ_BODY, f"block{i}")
55
+ self.block_ids.append(block_id if block_id != -1 else None)
56
+ print(f"Block IDs found: {[bid for bid in self.block_ids if bid is not None]}")
57
+
58
+ self.target_zone_id = mujoco.mj_name2id(self.model, mujoco.mjtObj.mjOBJ_BODY, "target_zone")
59
+ print(f"Target zone ID: {self.target_zone_id if self.target_zone_id != -1 else 'Not found'}")
60
+
61
+ # Joint indices (adjusted for dynamic DOFs)
62
+ self.joint_ids = list(range(7, min(16, self.nq))) # Limit to 9 joints max to align with obs
63
+ self.finger_joint_ids = [14, 15] if self.nq >= 16 and 14 < self.nu and 15 < self.nu else []
64
+
65
+ def reset(self, seed=None):
66
+ super().reset(seed=seed)
67
+ nq = self.model.nq
68
+ nv = self.model.nv
69
+
70
+ # Reset to a valid state
71
+ self.data.qpos = np.zeros(nq)
72
+ self.data.qvel = np.zeros(nv)
73
+
74
+ if nq >= 3:
75
+ self.data.qpos[0:3] = [0, 0, 0.1] # x, y, z
76
+ if nq >= 7:
77
+ self.data.qpos[3:7] = [1, 0, 0, 0] # quaternion
78
+ if nq > 7:
79
+ self.data.qpos[7:16] = np.zeros(9) # Limit to 9 joints for obs alignment
80
+
81
+ # Set block positions
82
+ block_positions = [
83
+ [0.2, 0.0, 0.025],
84
+ [0.25, 0.0, 0.025],
85
+ [0.3, 0.0, 0.025]
86
+ ]
87
+ for i, block_id in enumerate(self.block_ids):
88
+ if block_id is not None:
89
+ self.data.body(block_id).xpos = np.array(block_positions[i])
90
+
91
+ self.data.qvel[:] = 0.0
92
+ mujoco.mj_forward(self.model, self.data)
93
+ return self._get_obs(), {}
94
+
95
+ def step(self, action):
96
+ # Clip action to match nu and ensure finite bounds
97
+ ctrl_size = min(len(action), self.nu)
98
+ clipped_action = np.clip(action[:ctrl_size], -1.0, 1.0)
99
+ self.data.ctrl[:ctrl_size] = clipped_action
100
+
101
+ # Restrict actuator 2 (index 1) to -90° to +90° (-1.57 to 1.57 radians)
102
+ if 1 < self.nu:
103
+ self.data.ctrl[1] = np.clip(action[1], -1.57, 1.57)
104
+
105
+ # Handle finger joints if within nu
106
+ if len(action) >= 9 and self.finger_joint_ids and all(fid < self.nu for fid in self.finger_joint_ids):
107
+ self.data.ctrl[self.finger_joint_ids[0]] = np.clip(action[7], -1.0, 1.0)
108
+ self.data.ctrl[self.finger_joint_ids[1]] = np.clip(action[8], -1.0, 1.0)
109
+
110
+ mujoco.mj_step(self.model, self.data)
111
+
112
+ reward = -0.01 if not self._is_stacked() else 10.0
113
+ terminated = self._is_stacked()
114
+ truncated = False
115
+ obs = self._get_obs()
116
+ info = {"success": terminated}
117
+
118
+ if self.render_mode == "human" and self.viewer:
119
+ self.viewer.sync()
120
+
121
+ return obs, reward, terminated, truncated, info
122
+
123
+ def _get_obs(self):
124
+ block_pos = [self.data.body(bid).xpos[:3] if bid is not None else np.zeros(3) for bid in self.block_ids]
125
+ target_pos = self.data.body(self.target_zone_id).xpos[:3] if self.target_zone_id != -1 else np.zeros(3)
126
+ joint_pos = self.data.qpos[7:16] if self.nq > 7 else np.zeros(9) # Limit to 9 joints
127
+
128
+ obs = np.concatenate([pos for pos in block_pos] + [target_pos] + [joint_pos])
129
+ return obs.astype(np.float32)
130
+
131
+ def _is_stacked(self):
132
+ if not any(bid is not None for bid in self.block_ids) or self.target_zone_id == -1:
133
+ return False
134
+
135
+ block_positions = [self.data.body(bid).xpos for bid in self.block_ids if bid is not None]
136
+ target_pos = self.data.body(self.target_zone_id).xpos
137
+ return all(np.linalg.norm(b[:2] - target_pos[:2]) < 0.05 for b in block_positions)
138
+
139
+ def close(self):
140
+ if self.viewer:
141
+ self.viewer.close()
142
+
143
+ if __name__ == "__main__":
144
+ import argparse
145
+ import time
146
+
147
+ parser = argparse.ArgumentParser()
148
+ parser.add_argument("--render_mode", default="human")
149
+ parser.add_argument("--fps", type=int, default=15, help="Frames per second")
150
+ args = parser.parse_args()
151
+
152
+ try:
153
+ env = StackingEnv(render_mode=args.render_mode)
154
+ obs, _ = env.reset()
155
+ print("Initial observation shape:", obs.shape)
156
+ print("Environment initialized successfully!")
157
+
158
+ if args.render_mode == "human":
159
+ print("Simulation started at", args.fps, "FPS. Use viewer 'Joint' or 'Control' tabs to adjust joints, with actuator 2 limited to 180-degree range (-90° to +90°).")
160
+ print("Press Ctrl+C to exit.")
161
+ step_delay = 1.0 / args.fps
162
+
163
+ try:
164
+ while env.viewer and env.viewer.is_running():
165
+ mujoco.mj_step(env.model, env.data)
166
+ if env.render_mode == "human" and env.viewer:
167
+ env.viewer.sync()
168
+ time.sleep(step_delay)
169
+ except KeyboardInterrupt:
170
+ print("Stopped by user")
171
+ else:
172
+ print("Environment test completed successfully!")
173
+
174
+ except Exception as e:
175
+ print(f"Error: {e}")
176
+ print("\nTo debug this issue:")
177
+ print("1. Check stacking.xml for valid qpos or keyframe definitions")
178
+ print("2. Ensure qpos has exactly {env.model.nq} values")
179
+ print("3. Verify the joint structure in both XML files matches the code assumptions")
180
+
181
+ finally:
182
+ try:
183
+ env.close()
184
+ except:
185
+ pass
stacking_model_20250615_1618/ppo_hil_serl_stacking_offline.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce2df7c91a9e864fafdf0ef683c8829594a5e5a56ca5275b13fc99cd91e2f8ab
3
+ size 173217