-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_seed_fn.py
164 lines (140 loc) · 6.16 KB
/
run_seed_fn.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
import os
import pickle
import gc
import logging
from typing import List
import hydra
import numpy as np
import torch
from omegaconf import DictConfig
from rlbench import CameraConfig, ObservationConfig
from yarr.replay_buffer.wrappers.pytorch_replay_buffer import PyTorchReplayBuffer
from yarr.runners.offline_train_runner import OfflineTrainRunner
from yarr.utils.stat_accumulator import SimpleAccumulator
from helpers.custom_rlbench_env import CustomRLBenchEnv, CustomMultiTaskRLBenchEnv
import torch.distributed as dist
from termcolor import cprint
import lightning as L
from tqdm import tqdm
def run_seed(
rank,
cfg: DictConfig,
obs_config: ObservationConfig,
cams,
multi_task,
seed,
world_size,
fabric: L.Fabric = None,
) -> None:
if fabric is not None:
rank = fabric.global_rank
else:
dist.init_process_group("gloo",
rank=rank,
world_size=world_size)
task = cfg.rlbench.tasks[0]
tasks = cfg.rlbench.tasks
replay_path = os.path.join(cfg.replay.path, 'seed%d' % seed)
if cfg.method.name == 'GNFACTOR_BC':
from agents import gnfactor_bc
replay_buffer = gnfactor_bc.launch_utils.create_replay(
cfg.replay.batch_size, cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams, cfg.method.voxel_sizes,
cfg.rlbench.camera_resolution,
cfg=cfg)
gnfactor_bc.launch_utils.fill_multi_task_replay(
cfg, obs_config, 0,
replay_buffer, tasks, cfg.rlbench.demos,
cfg.method.demo_augmentation, cfg.method.demo_augmentation_every_n,
cams, cfg.rlbench.scene_bounds,
cfg.method.voxel_sizes, cfg.method.bounds_offset,
cfg.method.rotation_resolution, cfg.method.crop_augmentation,
keypoint_method=cfg.method.keypoint_method
)
agent = gnfactor_bc.launch_utils.create_agent(cfg)
elif cfg.method.name == 'ManiGaussian_BC':
from agents import manigaussian_bc
replay_buffer = manigaussian_bc.launch_utils.create_replay(
cfg.replay.batch_size, cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams, cfg.method.voxel_sizes,
cfg.rlbench.camera_resolution,
cfg=cfg)
if cfg.replay.use_disk and (os.path.exists(replay_path) and len(os.listdir(replay_path)) > 1): # default: True
logging.info(f"Found replay files in {replay_path}. Loading...")
replay_files = [os.path.join(replay_path, f) for f in os.listdir(replay_path) if f.endswith('.replay')]
for replay_file in tqdm(replay_files, desc="Processing replay files"): # NOTE: Experimental, please check your replay buffer carefully.
with open(replay_file, 'rb') as f:
try:
replay_data = pickle.load(f)
replay_buffer._add(replay_data)
except pickle.UnpicklingError as e:
logging.error(f"Error unpickling file {replay_file}: {e}")
else:
manigaussian_bc.launch_utils.fill_multi_task_replay(
cfg, obs_config, 0,
replay_buffer, tasks, cfg.rlbench.demos,
cfg.method.demo_augmentation, cfg.method.demo_augmentation_every_n,
cams, cfg.rlbench.scene_bounds,
cfg.method.voxel_sizes, cfg.method.bounds_offset,
cfg.method.rotation_resolution, cfg.method.crop_augmentation,
keypoint_method=cfg.method.keypoint_method,
fabric=fabric,
)
agent = manigaussian_bc.launch_utils.create_agent(cfg)
elif cfg.method.name == 'PERACT_BC':
from agents import peract_bc
replay_buffer = peract_bc.launch_utils.create_replay(
cfg.replay.batch_size, cfg.replay.timesteps,
cfg.replay.prioritisation,
cfg.replay.task_uniform,
replay_path if cfg.replay.use_disk else None,
cams, cfg.method.voxel_sizes,
cfg.rlbench.camera_resolution)
peract_bc.launch_utils.fill_multi_task_replay(
cfg, obs_config, rank,
replay_buffer, tasks, cfg.rlbench.demos,
cfg.method.demo_augmentation, cfg.method.demo_augmentation_every_n,
cams, cfg.rlbench.scene_bounds,
cfg.method.voxel_sizes, cfg.method.bounds_offset,
cfg.method.rotation_resolution, cfg.method.crop_augmentation,
keypoint_method=cfg.method.keypoint_method)
agent = peract_bc.launch_utils.create_agent(cfg)
else:
raise ValueError('Method %s does not exists.' % cfg.method.name)
wrapped_replay = PyTorchReplayBuffer(replay_buffer, num_workers=cfg.framework.num_workers)
stat_accum = SimpleAccumulator(eval_video_fps=30)
cwd = os.getcwd()
weightsdir = os.path.join(cwd, 'seed%d' % seed, 'weights') # load from the last checkpoint
logdir = os.path.join(cwd, 'seed%d' % seed)
cprint(f'Project path: {weightsdir}', 'cyan')
train_runner = OfflineTrainRunner(
agent=agent,
wrapped_replay_buffer=wrapped_replay,
train_device=rank,
stat_accumulator=stat_accum,
iterations=cfg.framework.training_iterations,
logdir=logdir,
logging_level=cfg.framework.logging_level,
log_freq=cfg.framework.log_freq,
weightsdir=weightsdir,
num_weights_to_keep=cfg.framework.num_weights_to_keep,
save_freq=cfg.framework.save_freq,
tensorboard_logging=cfg.framework.tensorboard_logging,
csv_logging=cfg.framework.csv_logging,
load_existing_weights=cfg.framework.load_existing_weights,
rank=rank,
world_size=world_size,
cfg=cfg,
fabric=fabric)
cprint('Starting training!!', 'green')
train_runner.start()
del train_runner
del agent
gc.collect()
torch.cuda.empty_cache()