File size: 9,948 Bytes
8652b14 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | """
MineDojo Episode Collection Script
This script generates trajectory data from MineDojo environment using a simple agent
that randomly explores the environment. It supports parallel data collection and
saves video and action/pose data.
"""
import argparse
import math
import multiprocessing as mp
import os
import os.path as osp
import random
from typing import Dict, Optional, Tuple
import cv2
import minedojo
import numpy as np
from tqdm import tqdm
# Action mappings for the agent
# Format: [forward/back, ?, ?, pitch, yaw, ?, ?, ?]
ACTIONS: Dict[str, np.ndarray] = {
'forward': np.array([1, 0, 0, 12, 12, 0, 0, 0]),
'back': np.array([2, 0, 0, 12, 12, 0, 0, 0]),
'left': np.array([0, 0, 0, 12, 11, 0, 0, 0]),
'right': np.array([0, 0, 0, 12, 13, 0, 0, 0]),
'up': np.array([0, 0, 0, 11, 12, 0, 0, 0]),
'down': np.array([0, 0, 0, 13, 12, 0, 0, 0]),
'noop': np.array([0, 0, 0, 12, 12, 0, 0, 0])
}
def sample_action(prob_forward: float) -> str:
"""
Sample an action based on forward probability.
Args:
prob_forward: Probability of moving forward or backward
Returns:
Action name string
"""
prob_turn = (1 - prob_forward) / 2
action = np.random.choice(
['forward', 'back', 'left', 'right', 'up', 'down'],
p=[prob_forward / 2 - 0.1, prob_forward / 2 - 0.1, prob_turn, prob_turn, 0.1, 0.1]
)
return action
class SimpleAgent:
"""
Simple agent that explores the environment with random actions.
Attributes:
action_repeat: Number of times to repeat the same action
prob_forward: Probability of moving forward/backward
max_consec_fwd: Maximum consecutive forward actions (currently unused)
"""
def __init__(self, prob_forward: float, action_repeat: int, max_consec_fwd: int):
"""
Initialize the SimpleAgent.
Args:
prob_forward: Probability of moving forward or backward
action_repeat: How many steps to repeat each sampled action
max_consec_fwd: Maximum consecutive forward movements
"""
self.action_repeat = action_repeat
self.prob_forward = prob_forward
self.max_consec_fwd = max_consec_fwd
self.reset()
def reset(self) -> None:
"""Reset the agent's internal state."""
self.n_fwd = 0
self.counter = 0
self.action = None
def sample(self, pos: np.ndarray) -> np.ndarray:
"""
Sample an action given the current position.
Args:
pos: Current position array containing [x, y, z, pitch, yaw]
Returns:
Action array
"""
prob_forward = self.prob_forward
# Sample new action if needed (or if previous was up/down)
if (self.action is None or
self.counter % self.action_repeat == 0 or
self.action in ['up', 'down']):
self.action = sample_action(prob_forward)
self.counter += 1
return ACTIONS[self.action]
def collect_episode(env, agent: SimpleAgent, traj_length: int) -> Optional[Tuple[np.ndarray, np.ndarray, np.ndarray]]:
"""
Collect a single episode of trajectory data.
Args:
env: MineDojo environment
agent: Agent to collect data with
traj_length: Length of trajectory to collect
Returns:
Tuple of (rgb observations, actions, poses) or None if collection fails
"""
agent.reset()
# Retry environment reset until successful
success = False
max_retries = 10
retries = 0
while not success and retries < max_retries:
try:
obs = env.reset()
success = True
except Exception as e:
retries += 1
if retries >= max_retries:
print(f"Failed to reset environment after {max_retries} retries")
return None
observations = [obs['rgb']]
actions = [env.action_space.no_op()]
pose = [np.concatenate([
obs['location_stats']['pos'],
obs['location_stats']['pitch'],
obs['location_stats']['yaw']
])]
for ei in range(traj_length):
curr_actions = agent.sample(pose[ei])
obs, reward, done, info = env.step(curr_actions)
actions.append(curr_actions)
observations.append(obs['rgb'])
pose.append(np.concatenate([
obs['location_stats']['pos'],
obs['location_stats']['pitch'],
obs['location_stats']['yaw']
]))
rgb = np.stack(observations, axis=0)
actions = np.array(actions, dtype=np.int32)
pose = np.array(pose)
return rgb, actions, pose
def worker(worker_id: int, args: argparse.Namespace) -> None:
"""
Worker process for parallel data collection.
Args:
worker_id: Unique ID for this worker process
args: Command-line arguments
"""
# Create worker-specific output directory
worker_output_dir = osp.join(args.output_dir, f'{worker_id}')
os.makedirs(worker_output_dir, exist_ok=True)
# Set worker-specific random seeds for reproducibility
# Use a large offset between workers to ensure independent random streams
worker_seed = args.base_seed + worker_id * 10000
np.random.seed(worker_seed)
random.seed(worker_seed)
agent = SimpleAgent(args.prob_forward, args.action_repeat, args.max_consec_fwd)
# Calculate number of episodes for this worker
num_episodes = args.num_episodes // args.n_parallel
if worker_id < (args.num_episodes % args.n_parallel):
num_episodes += 1
pbar = tqdm(total=num_episodes, position=worker_id, desc=f"Worker {worker_id}")
episode_count = 0
while episode_count < num_episodes:
# Create environment with unique seeds for each worker and episode
# Ensure world_seed and seed are different for each worker and episode
episode_seed_base = worker_seed + episode_count * 100
world_seed = episode_seed_base
env_seed = episode_seed_base + 1
env = minedojo.make(
task_id="open-ended",
image_size=(360, 640),
world_seed=world_seed,
seed=env_seed,
generate_world_type='specified_biome',
specified_biome=args.env_type,
initial_weather='rain'
)
# Collect episode data
out = collect_episode(env, agent, args.traj_length)
if out is None:
env.close()
continue
rgb, actions, poses = out
# Save video
video_fname = osp.join(worker_output_dir, f'{episode_count:06d}.mp4')
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(video_fname, fourcc, 10.0, (rgb.shape[3], rgb.shape[2]))
for t in range(rgb.shape[0]):
frame = rgb[t].transpose(1, 2, 0)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
writer.write(frame)
writer.release()
# Save actions and poses
action_fname = osp.join(worker_output_dir, f'{episode_count:06d}.npz')
np.savez_compressed(action_fname, actions=actions, poses=poses)
episode_count += 1
env.close()
pbar.update(1)
pbar.close()
def main(args: argparse.Namespace) -> None:
"""
Main function to orchestrate parallel data collection.
Args:
args: Command-line arguments
"""
os.makedirs(args.output_dir, exist_ok=True)
# Create and start worker processes
procs = [mp.Process(target=worker, args=(i, args)) for i in range(args.n_parallel)]
for p in procs:
p.start()
for p in procs:
p.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Generate MineDojo trajectory data with parallel collection'
)
parser.add_argument(
'-o', '--output_dir',
type=str,
default='test',
help='Output directory for generated data'
)
parser.add_argument(
'--env_type',
type=str,
default='test',
help='Biome type for environment generation'
)
parser.add_argument(
'-z', '--n_parallel',
type=int,
default=1,
help='Number of parallel workers (default: 1)'
)
parser.add_argument(
'-a', '--action_repeat',
type=int,
default=5,
help='Number of times to repeat each action (default: 5)'
)
parser.add_argument(
'-p', '--prob_forward',
type=float,
default=0.7,
help='Probability of forward/backward actions (default: 0.7)'
)
parser.add_argument(
'-m', '--max_consec_fwd',
type=int,
default=50,
help='Maximum consecutive forward movements (default: 50)'
)
parser.add_argument(
'-t', '--traj_length',
type=int,
default=1500,
help='Length of each trajectory (default: 1500)'
)
parser.add_argument(
'-n', '--num_episodes',
type=int,
default=100000,
help='Total number of episodes to generate (default: 100000)'
)
parser.add_argument(
'-r', '--resolution',
type=int,
default=128,
help='Resolution (currently unused, default: 128)'
)
parser.add_argument(
'-rh', '--resolution_h',
type=int,
default=360,
help='Height resolution (currently unused, default: 360)'
)
parser.add_argument(
'-rw', '--resolution_w',
type=int,
default=640,
help='Width resolution (currently unused, default: 640)'
)
parser.add_argument(
'--base_seed',
type=int,
default=42,
help='Base RNG seed; worker i uses base_seed+i (default: 42)'
)
args = parser.parse_args()
main(args)
|