-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_domain_randomization.py
157 lines (118 loc) · 5.66 KB
/
visual_domain_randomization.py
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
import torch
import numpy as np
from torchrl.collectors.collectors import SyncDataCollector
from torchrl.envs.utils import RandomPolicy
from torchrl.record.loggers.csv import CSVLogger
from torchrl.record import VideoRecorder
from torchrl.envs import RoboHiveEnv
from torchrl.envs.transforms import TransformedEnv
from torchrl.envs import EnvBase
class VisualDomainRandomizedEnv(EnvBase):
def __init__(self, env, cameras: list = ["top_cam"]):
super().__init__(
device=env.device, batch_size=env.batch_size, allow_done_after_reset=False
)
self._base_env = env
self.cameras = cameras
self.observation_spec = env.observation_spec.clone()
self.action_spec = env.action_spec.clone()
def _step(self, tensordict):
# self.randomize_visual()
tensordict = self._base_env._step(tensordict)
# obs = self.get_sim_observation()
# tensordict.set("observation", obs)
return tensordict
def apply_visual_domain_randomization(self) -> torch.Tensor:
# Randomize geometry color
self._base_env.sim.model.geom_rgba[:] = np.random.rand(
self._base_env.sim.model.ngeom, 4
)
# Randomize geometry size
# self._base_env.sim.model.geom_size[:] = np.random.uniform(low=0.8, high=1.2, size=self._base_env.sim.model.geom_size.shape) * self._base_env.sim.model.geom_size
# Randomize geometry positions
# self._base_env.sim.model.geom_pos[:] += np.random.normal(0, 0.01, self._base_env.sim.model.geom_pos.shape)
# Randomize geometry orientation
# self._base_env.sim.model.geom_quat[:] = np.random.randn(self._base_env.sim.model.ngeom, 4)
# self._base_env.sim.model.geom_quat[:] = self._base_env.sim.model.geom_quat[:] / np.linalg.norm(self._base_env.sim.model.geom_quat[:], axis=-1, keepdims=True)
# Randomize material textures
# self._base_env.sim.model.mat_texid[:] = np.random.randint(low=0, high=self._base_env.sim.model.ntex, size=self._base_env.sim.model.mat_texid.shape)
# Randomize material colors
# self._base_env.sim.model.mat_rgba[:] = np.random.rand(self._base_env.sim.model.nmat, 4)
# Randomize material shininess
# self._base_env.sim.model.mat_shininess[:] = np.random.uniform(0, 1, size=self._base_env.sim.model.mat_shininess.shape)
# self._base_env.sim.model.mat_specular[:] = np.random.uniform(0, 1, size=self._base_env.sim.model.mat_specular.shape)
# self._base_env.sim.model.mat_emission[:] = np.random.uniform(0, 1, size=self._base_env.sim.model.mat_emission.shape)
# Randomize light diffuse
# self._base_env.sim.model.light_diffuse[:] = np.random.rand(3)
# Randomize ambiant lighting
# self._base_env.sim.model.light_ambient[:] = np.random.rand(self._base_env.sim.model.nlight, 3)
# Randomize light position
# self._base_env.sim.model.light_pos[:] += np.random.normal(0, 0.1, size=self._base_env.sim.model.light_pos.shape)
# Randomize light direction
# self._base_env.sim.model.light_dir[:] += np.random.normal(0, 0.1, size=self._base_env.sim.model.light_dir.shape)
# Randomize light specular
# self._base_env.sim.model.light_specular[:] = np.random.rand(self._base_env.sim.model.nlight, 3)
# Randomize cam FOV
# self._base_env.sim.model.cam_fovy[:] = np.random.uniform(30, 90, size=self._base_env.sim.model.cam_fovy.shape)
# Randomize cam pos
# self._base_env.sim.model.cam_pos[:] += np.random.normal(0, 0.1, size=self._base_env.sim.model.cam_pos.shape)
_, _, _, _, info = self._base_env.unwrapped.forward(update_exteroception=True)
return self.get_cameras_pixels(info)
def get_cameras_pixels(self, info: dict) -> torch.Tensor:
cameras_pixels = []
for camera_key in info["visual_dict"].keys():
for camera_name in self.cameras:
if (
camera_key.startswith("rgb:")
and camera_name in camera_key
and camera_key.endswith("2d")
):
cameras_pixels.append(info["visual_dict"][camera_key])
return torch.from_numpy(np.array(cameras_pixels))
def _reset(self, tensordict):
tensordict = self._base_env._reset(tensordict)
pixels = self.apply_visual_domain_randomization()
tensordict.set("pixels", pixels)
return tensordict
def _set_seed(self, seed):
self._base_env._set_seed(seed)
if __name__ == "__main__":
print(f"Envs : {RoboHiveEnv.available_envs}")
video_logger = CSVLogger(
exp_name="visual_domain_randomization",
log_dir="videos",
video_format="mp4",
video_fps=30,
)
recorder = VideoRecorder(logger=video_logger, tag="iteration", skip=2)
env_name = "FetchReachRandom-v0"
cameras = ["left_cam", "right_cam", "top_cam", "head_camera_rgb"]
env = TransformedEnv(
VisualDomainRandomizedEnv(
RoboHiveEnv(
env_name=env_name,
from_pixels=True,
pixels_only=True,
from_depths=False,
frame_skip=None,
cameras=cameras,
),
cameras=cameras,
)
)
env.append_transform(recorder)
policy = RandomPolicy(action_spec=env.action_spec)
device = torch.device("cpu")
collector = SyncDataCollector(
create_env_fn=env,
policy=policy,
total_frames=150,
max_frames_per_traj=50,
frames_per_batch=150,
device=device,
storing_device=device,
)
for _ in collector:
pass
env.transform.dump()
env.close()