images: np.ndarray of shape (N, H, W, 3) in [0, 255] or torch.Tensor of shape (N, 3, H, W) in range [0, 1]
(images: Union[np.ndarray, torch.Tensor], save_path: str)
| 23 | |
| 24 | |
| 25 | def save_image_grid_auto(images: Union[np.ndarray, torch.Tensor], save_path: str): |
| 26 | """ |
| 27 | images: np.ndarray of shape (N, H, W, 3) in [0, 255] or torch.Tensor of shape (N, 3, H, W) in range [0, 1] |
| 28 | """ |
| 29 | if isinstance(images, torch.Tensor): |
| 30 | assert images.ndim == 4 and (images.shape[1] == 3 or images.shape[-1] == 3), f"images must be a 4D torch tensor with shape (N, 3, H, W) or (N, H, W, 3)" |
| 31 | if images.shape[1] == 3: |
| 32 | images = images.permute(0, 2, 3, 1) |
| 33 | images = (images.detach().cpu().numpy() * 255).astype(np.uint8) |
| 34 | elif isinstance(images, np.ndarray): |
| 35 | assert images.ndim == 4 and images.shape[3] == 3, f"images must be a 4D numpy array with shape (N, H, W, 3)" |
| 36 | else: |
| 37 | raise ValueError(f"images must be a numpy array or a torch tensor, but got {type(images)}") |
| 38 | |
| 39 | rows = math.floor(math.sqrt(len(images))) |
| 40 | cols = math.ceil(len(images) / rows) |
| 41 | save_image_grid(images, (rows, cols), save_path) |
no test coverage detected