(
self,
generation_outputs: torch.Tensor,
unnorm_key: Optional[str] = None,
)
| 214 | return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) |
| 215 | |
| 216 | def decode_actions( |
| 217 | self, |
| 218 | generation_outputs: torch.Tensor, |
| 219 | unnorm_key: Optional[str] = None, |
| 220 | ) -> Dict[str, torch.Tensor]: |
| 221 | action_token_num = 3 # translation + rotation + gripper |
| 222 | predicted_action_token_ids = generation_outputs[0, : action_token_num * self.action_chunk_size].detach().cpu().long().numpy() |
| 223 | assert self.tokenizer.eos_token != predicted_action_token_ids[-1], "[error] actions contain EOS token, please check you truncation settings!" |
| 224 | |
| 225 | if predicted_action_token_ids.shape[0] < action_token_num * self.action_chunk_size: # pad with zeros |
| 226 | logger.warning(f"Padding zero action!") |
| 227 | predicted_action_token_ids = np.concatenate( |
| 228 | [ |
| 229 | predicted_action_token_ids, |
| 230 | np.zeros(action_token_num * self.action_chunk_size - predicted_action_token_ids.shape[0], dtype=np.longlong), |
| 231 | ] |
| 232 | ) |
| 233 | predicted_action_token_ids = predicted_action_token_ids.reshape(-1, action_token_num) |
| 234 | normalized_action_chunks = self.action_tokenizer.decode_token_ids_to_actions(predicted_action_token_ids) |
| 235 | |
| 236 | if unnorm_key is None: |
| 237 | logger.warning(f"unnorm_key {unnorm_key} is not in statistics, use next one") |
| 238 | unnorm_key = next(self.statistics.keys()) |
| 239 | action_norm_stats = self.statistics[unnorm_key]["action"] |
| 240 | |
| 241 | action_dim = len(action_norm_stats["q01"]) |
| 242 | mask = np.array(action_norm_stats.get("mask", np.ones(action_dim)), dtype=bool) |
| 243 | action_high, action_low = np.array(action_norm_stats["q99"]), np.array(action_norm_stats["q01"]) |
| 244 | |
| 245 | actions = [] |
| 246 | for normalized_actions in normalized_action_chunks: |
| 247 | action = np.where( |
| 248 | mask, |
| 249 | 0.5 * (normalized_actions + 1) * (action_high - action_low) + action_low, |
| 250 | normalized_actions, |
| 251 | ) |
| 252 | actions.append(action) |
| 253 | actions = np.stack(actions) |
| 254 | return {"actions": actions, "action_ids": predicted_action_token_ids} |
no test coverage detected