-
Notifications
You must be signed in to change notification settings - Fork 7
/
train.py
208 lines (182 loc) · 6.76 KB
/
train.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
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
from pathlib import Path
from typing import Optional, Tuple
import tyro
from dataclasses import dataclass, asdict
import wandb
import time
import random
import numpy as np
from tqdm import tqdm
import sac
import specs
import replay
from robopianist import suite
import dm_env_wrappers as wrappers
import robopianist.wrappers as robopianist_wrappers
@dataclass(frozen=True)
class Args:
root_dir: str = "/tmp/robopianist"
seed: int = 42
max_steps: int = 1_000_000
warmstart_steps: int = 5_000
log_interval: int = 1_000
eval_interval: int = 10_000
eval_episodes: int = 1
batch_size: int = 256
discount: float = 0.99
tqdm_bar: bool = False
replay_capacity: int = 1_000_000
project: str = "robopianist"
entity: str = ""
name: str = ""
tags: str = ""
notes: str = ""
mode: str = "disabled"
environment_name: str = "RoboPianist-debug-TwinkleTwinkleRousseau-v0"
n_steps_lookahead: int = 10
trim_silence: bool = False
gravity_compensation: bool = False
reduced_action_space: bool = False
control_timestep: float = 0.05
stretch_factor: float = 1.0
shift_factor: int = 0
wrong_press_termination: bool = False
disable_fingering_reward: bool = False
disable_forearm_reward: bool = False
disable_colorization: bool = False
disable_hand_collisions: bool = False
primitive_fingertip_collisions: bool = False
frame_stack: int = 1
clip: bool = True
record_dir: Optional[Path] = None
record_every: int = 1
record_resolution: Tuple[int, int] = (480, 640)
camera_id: Optional[str | int] = "piano/back"
action_reward_observation: bool = False
agent_config: sac.SACConfig = sac.SACConfig()
def prefix_dict(prefix: str, d: dict) -> dict:
return {f"{prefix}/{k}": v for k, v in d.items()}
def get_env(args: Args, record_dir: Optional[Path] = None):
env = suite.load(
environment_name=args.environment_name,
seed=args.seed,
stretch=args.stretch_factor,
shift=args.shift_factor,
task_kwargs=dict(
n_steps_lookahead=args.n_steps_lookahead,
trim_silence=args.trim_silence,
gravity_compensation=args.gravity_compensation,
reduced_action_space=args.reduced_action_space,
control_timestep=args.control_timestep,
wrong_press_termination=args.wrong_press_termination,
disable_fingering_reward=args.disable_fingering_reward,
disable_forearm_reward=args.disable_forearm_reward,
disable_colorization=args.disable_colorization,
disable_hand_collisions=args.disable_hand_collisions,
primitive_fingertip_collisions=args.primitive_fingertip_collisions,
change_color_on_activation=True,
),
)
if record_dir is not None:
env = robopianist_wrappers.PianoSoundVideoWrapper(
environment=env,
record_dir=record_dir,
record_every=args.record_every,
camera_id=args.camera_id,
height=args.record_resolution[0],
width=args.record_resolution[1],
)
env = wrappers.EpisodeStatisticsWrapper(
environment=env, deque_size=args.record_every
)
env = robopianist_wrappers.MidiEvaluationWrapper(
environment=env, deque_size=args.record_every
)
else:
env = wrappers.EpisodeStatisticsWrapper(environment=env, deque_size=1)
if args.action_reward_observation:
env = wrappers.ObservationActionRewardWrapper(env)
env = wrappers.ConcatObservationWrapper(env)
if args.frame_stack > 1:
env = wrappers.FrameStackingWrapper(
env, num_frames=args.frame_stack, flatten=True
)
env = wrappers.CanonicalSpecWrapper(env, clip=args.clip)
env = wrappers.SinglePrecisionWrapper(env)
env = wrappers.DmControlWrapper(env)
return env
def main(args: Args) -> None:
if args.name:
run_name = args.name
else:
run_name = f"SAC-{args.environment_name}-{args.seed}-{time.time()}"
# Create experiment directory.
experiment_dir = Path(args.root_dir) / run_name
experiment_dir.mkdir(parents=True)
# Seed RNGs.
random.seed(args.seed)
np.random.seed(args.seed)
wandb.init(
project=args.project,
entity=args.entity or None,
tags=(args.tags.split(",") if args.tags else []),
notes=args.notes or None,
config=asdict(args),
mode=args.mode,
name=run_name,
)
env = get_env(args)
eval_env = get_env(args, record_dir=experiment_dir / "eval")
spec = specs.EnvironmentSpec.make(env)
agent = sac.SAC.initialize(
spec=spec,
config=args.agent_config,
seed=args.seed,
discount=args.discount,
)
replay_buffer = replay.Buffer(
state_dim=spec.observation_dim,
action_dim=spec.action_dim,
max_size=args.replay_capacity,
batch_size=args.batch_size,
)
timestep = env.reset()
replay_buffer.insert(timestep, None)
start_time = time.time()
for i in tqdm(range(1, args.max_steps + 1), disable=not args.tqdm_bar):
# Act.
if i < args.warmstart_steps:
action = spec.sample_action(random_state=env.random_state)
else:
agent, action = agent.sample_actions(timestep.observation)
# Observe.
timestep = env.step(action)
replay_buffer.insert(timestep, action)
# Reset episode.
if timestep.last():
wandb.log(prefix_dict("train", env.get_statistics()), step=i)
timestep = env.reset()
replay_buffer.insert(timestep, None)
# Train.
if i >= args.warmstart_steps:
if replay_buffer.is_ready():
transitions = replay_buffer.sample()
agent, metrics = agent.update(transitions)
if i % args.log_interval == 0:
wandb.log(prefix_dict("train", metrics), step=i)
# Eval.
if i % args.eval_interval == 0:
for _ in range(args.eval_episodes):
timestep = eval_env.reset()
while not timestep.last():
timestep = eval_env.step(agent.eval_actions(timestep.observation))
log_dict = prefix_dict("eval", eval_env.get_statistics())
music_dict = prefix_dict("eval", eval_env.get_musical_metrics())
wandb.log(log_dict | music_dict, step=i)
video = wandb.Video(str(eval_env.latest_filename), fps=4, format="mp4")
wandb.log({"video": video, "global_step": i})
eval_env.latest_filename.unlink()
if i % args.log_interval == 0:
wandb.log({"train/fps": int(i / (time.time() - start_time))}, step=i)
if __name__ == "__main__":
main(tyro.cli(Args, description=__doc__))