File size: 12,563 Bytes
987ed1b | 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 | import wandb
import numpy as np
import torch
import collections
import pathlib
import tqdm
import dill
import math
import wandb.sdk.data_types.video as wv
from diffusion_policy.env.particle.spread_env import ParticleSpreadEnv
from diffusion_policy.gym_util.async_vector_env import AsyncVectorEnv
from diffusion_policy.gym_util.sync_vector_env import SyncVectorEnv
from diffusion_policy.gym_util.multistep_wrapper import MultiStepWrapper
from diffusion_policy.gym_util.video_recording_wrapper import VideoRecordingWrapper, VideoRecorder
from gym.wrappers import FlattenObservation
from diffusion_policy.policy.base_lowdim_policy import BaseLowdimPolicy
from diffusion_policy.common.pytorch_util import dict_apply
from diffusion_policy.env_runner.base_lowdim_runner import BaseLowdimRunner
import pdb
from diffusion_policy.sampler.single import consensus_sampler, coherence_sampler
from diffusion_policy.sampler.multi import contrastive_sampler
class ParticleLowdimRunner(BaseLowdimRunner):
def __init__(self,
output_dir,
n_train=10,
n_train_vis=3,
train_start_seed=0,
n_test=22,
n_test_vis=6,
test_start_seed=10000,
max_steps=200,
n_obs_steps=8,
n_action_steps=8,
fps=5,
crf=22,
past_action=False,
abs_action=False,
obs_eef_target=True,
tqdm_interval_sec=5.0,
n_envs=None
):
super().__init__(output_dir)
if n_envs is None:
n_envs = n_train + n_test
steps_per_render = max(10 // fps, 1)
def env_fn():
return MultiStepWrapper(
VideoRecordingWrapper(
FlattenObservation(
ParticleSpreadEnv()
),
video_recoder=VideoRecorder.create_h264(
fps=fps,
codec='h264',
input_pix_fmt='rgb24',
crf=crf,
thread_type='FRAME',
thread_count=1
),
file_path=None,
steps_per_render=steps_per_render
),
n_obs_steps=n_obs_steps,
n_action_steps=n_action_steps,
max_episode_steps=max_steps
)
env_fns = [env_fn] * n_envs
env_seeds = list()
env_prefixs = list()
env_init_fn_dills = list()
# train
for i in range(n_train):
seed = train_start_seed + i
enable_render = i < n_train_vis
def init_fn(env, seed=seed, enable_render=enable_render):
# setup rendering
# video_wrapper
assert isinstance(env.env, VideoRecordingWrapper)
env.env.video_recoder.stop()
env.env.file_path = None
if enable_render:
filename = pathlib.Path(output_dir).joinpath(
'media', f"seed{seed}.mp4")
filename.parent.mkdir(parents=False, exist_ok=True)
filename = str(filename)
env.env.file_path = filename
# set seed
assert isinstance(env, MultiStepWrapper)
env.seed(seed)
env_seeds.append(seed)
env_prefixs.append('train/')
env_init_fn_dills.append(dill.dumps(init_fn))
# test
for i in range(n_test):
seed = test_start_seed + i
enable_render = i < n_test_vis
def init_fn(env, seed=seed, enable_render=enable_render):
# setup rendering
# video_wrapper
assert isinstance(env.env, VideoRecordingWrapper)
env.env.video_recoder.stop()
env.env.file_path = None
if enable_render:
filename = pathlib.Path(output_dir).joinpath(
'media', f"seed{seed}.mp4")
filename.parent.mkdir(parents=False, exist_ok=True)
filename = str(filename)
env.env.file_path = filename
# set seed
assert isinstance(env, MultiStepWrapper)
env.seed(seed)
env_seeds.append(seed)
env_prefixs.append('test/')
env_init_fn_dills.append(dill.dumps(init_fn))
env = AsyncVectorEnv(env_fns)
# env = SyncVectorEnv(env_fns)
self.env = env
self.env_fns = env_fns
self.env_seeds = env_seeds
self.env_prefixs = env_prefixs
self.env_init_fn_dills = env_init_fn_dills
self.fps = fps
self.crf = crf
self.n_obs_steps = n_obs_steps
self.n_action_steps = n_action_steps
self.past_action = past_action
self.max_steps = max_steps
self.tqdm_interval_sec = tqdm_interval_sec
self.obs_eef_target = obs_eef_target
self.sampler = None
self.n_samples = 0
self.topk = 0
self.weak = None
def set_sampler(self, sampler, nsample=1, topk=1, noise=0.0, decay=1.0):
self.sampler = sampler
self.n_samples = nsample
self.topk = topk
self.noise = noise
self.decay = decay
print(f'Set sampler: {sampler} {topk}/{nsample}')
def set_reference(self, weak):
self.weak = weak
def run(self, policy: BaseLowdimPolicy):
device = policy.device
dtype = policy.dtype
env = self.env
# plan for rollout
n_envs = len(self.env_fns)
n_inits = len(self.env_init_fn_dills)
n_chunks = math.ceil(n_inits / n_envs)
# allocate data
all_video_paths = [None] * n_inits
all_rewards = [None] * n_inits
last_info = [None] * n_inits
for chunk_idx in range(n_chunks):
start = chunk_idx * n_envs
end = min(n_inits, start + n_envs)
this_global_slice = slice(start, end)
this_n_active_envs = end - start
this_local_slice = slice(0,this_n_active_envs)
this_init_fns = self.env_init_fn_dills[this_global_slice]
n_diff = n_envs - len(this_init_fns)
if n_diff > 0:
this_init_fns.extend([self.env_init_fn_dills[0]]*n_diff)
assert len(this_init_fns) == n_envs
# init envs
env.call_each('run_dill_function',
args_list=[(x,) for x in this_init_fns])
# start rollout
obs = env.reset()
past_action = None
policy.reset()
pbar = tqdm.tqdm(total=self.max_steps, desc=f"Eval ParticleLowdimRunner {chunk_idx+1}/{n_chunks}",
leave=False, mininterval=self.tqdm_interval_sec)
done = False
while not done:
# create obs dict
if not self.obs_eef_target:
obs[...,8:10] = 0
np_obs_dict = {
'obs': obs.astype(np.float32)
}
if self.past_action and (past_action is not None):
# TODO: not tested
np_obs_dict['past_action'] = past_action[
:,-(self.n_obs_steps-1):].astype(np.float32)
# device transfer
obs_dict = dict_apply(np_obs_dict,
lambda x: torch.from_numpy(x).to(
device=device))
# run policy
with torch.no_grad():
if self.sampler == 'random':
action_dict = policy.predict_action(obs_dict)
elif self.sampler == 'consensus':
action_dict = consensus_sampler(policy, obs_dict, self.n_samples, self.topk)
elif self.sampler == 'contrast':
action_dict = contrastive_sampler(policy, self.weak, obs_dict, self.n_samples)
elif self.sampler == 'coherence':
if 'action_prior' not in locals():
action_prior = None
action_dict = coherence_sampler(policy, action_prior, obs_dict, self.n_samples)
action_prior = action_dict['action_pred'][:,self.n_action_steps:]
else:
action_dict = policy.predict_action(obs_dict)
# device_transfer
np_action_dict = dict_apply(action_dict,
lambda x: x.detach().to('cpu').numpy())
action = np_action_dict['action']
# step env
obs, reward, done, info = env.step(action)
done = np.all(done)
past_action = action
# update pbar
pbar.update(action.shape[1])
pbar.close()
# collect data for this round
all_video_paths[this_global_slice] = env.render()[this_local_slice]
all_rewards[this_global_slice] = env.call('get_attr', 'reward')[this_local_slice]
last_info[this_global_slice] = [dict((k,v[-1]) for k, v in x.items()) for x in info][this_local_slice]
# log
total_rewards = collections.defaultdict(list)
total_p1 = collections.defaultdict(list)
total_p2 = collections.defaultdict(list)
total_steps_till_success = collections.defaultdict(list)
prefix_event_counts = collections.defaultdict(lambda :collections.defaultdict(lambda : 0))
prefix_counts = collections.defaultdict(lambda : 0)
log_data = dict()
# results reported in the paper are generated using the commented out line below
# which will only report and average metrics from first n_envs initial condition and seeds
# fortunately this won't invalidate our conclusion since
# 1. This bug only affects the variance of metrics, not their mean
# 2. All baseline methods are evaluated using the same code
# to completely reproduce reported numbers, uncomment this line:
# for i in range(len(self.env_fns)):
# and comment out this line
for i in range(n_inits):
seed = self.env_seeds[i]
prefix = self.env_prefixs[i]
this_rewards = all_rewards[i]
total_reward = np.unique(this_rewards).sum() # (0, 0.49, 0.51)
p1 = total_reward > 0.4
p2 = total_reward > 0.9
steps_till_success = len(this_rewards) if total_reward > 0.9 else self.max_steps
total_rewards[prefix].append(total_reward)
total_p1[prefix].append(p1)
total_p2[prefix].append(p2)
total_steps_till_success[prefix].append(steps_till_success)
log_data[prefix+f'sim_max_reward_{seed}'] = total_reward
# # aggregate event counts
# prefix_counts[prefix] += 1
# for key, value in last_info[i].items():
# delta_count = 1 if value > 0 else 0
# prefix_event_counts[prefix][key] += delta_count
# visualize sim
video_path = all_video_paths[i]
if video_path is not None:
sim_video = wandb.Video(video_path)
log_data[prefix+f'sim_video_{seed}'] = sim_video
# log aggregate metrics
for prefix, value in total_rewards.items():
name = prefix+'mean_score'
value = np.mean(value)
log_data[name] = value
for prefix, value in total_p1.items():
name = prefix+'p1'
value = np.mean(value)
log_data[name] = value
for prefix, value in total_p2.items():
name = prefix+'p2'
value = np.mean(value)
log_data[name] = value
for prefix, value in total_steps_till_success.items():
name = prefix+'mean_steps_till_success'
value = np.mean(value)
log_data[name] = value
# summarize probabilities
for prefix, events in prefix_event_counts.items():
prefix_count = prefix_counts[prefix]
for event, count in events.items():
prob = count / prefix_count
key = prefix + event
log_data[key] = prob
return log_data
|