Convert predictions to the unbatched NumPy format used by vis code.
(predictions, images=None)
| 305 | |
| 306 | |
| 307 | def prepare_for_visualization(predictions, images=None): |
| 308 | """Convert predictions to the unbatched NumPy format used by vis code.""" |
| 309 | vis_predictions = {} |
| 310 | for k, v in predictions.items(): |
| 311 | if isinstance(v, torch.Tensor): |
| 312 | v = _squeeze_single_batch(k, v.detach().cpu()) |
| 313 | vis_predictions[k] = v.numpy() |
| 314 | elif isinstance(v, np.ndarray): |
| 315 | vis_predictions[k] = _squeeze_single_batch(k, v) |
| 316 | else: |
| 317 | vis_predictions[k] = v |
| 318 | |
| 319 | if images is None: |
| 320 | images = predictions.get("images") |
| 321 | |
| 322 | if isinstance(images, torch.Tensor): |
| 323 | images = images.detach().cpu() |
| 324 | if isinstance(images, np.ndarray): |
| 325 | images = _squeeze_single_batch("images", images) |
| 326 | elif isinstance(images, torch.Tensor): |
| 327 | images = _squeeze_single_batch("images", images).numpy() |
| 328 | |
| 329 | if isinstance(images, torch.Tensor): |
| 330 | images = images.numpy() |
| 331 | |
| 332 | if images is not None: |
| 333 | vis_predictions["images"] = images |
| 334 | |
| 335 | return vis_predictions |
| 336 | |
| 337 | |
| 338 | # ============================================================================= |
no test coverage detected