Converts a PyTorch tensor to a PIL image. Automatically moves the channel dimension (if it has size 3) to the last axis before converting. Args: tensor (torch.Tensor): Input tensor. Expected shape can be [C, H, W], [H, W, C], or [H, W]. Returns: PIL.Image: The
(tensor)
| 103 | return total_params |
| 104 | |
| 105 | def tensor_to_pil(tensor): |
| 106 | """ |
| 107 | Converts a PyTorch tensor to a PIL image. Automatically moves the channel dimension |
| 108 | (if it has size 3) to the last axis before converting. |
| 109 | |
| 110 | Args: |
| 111 | tensor (torch.Tensor): Input tensor. Expected shape can be [C, H, W], [H, W, C], or [H, W]. |
| 112 | |
| 113 | Returns: |
| 114 | PIL.Image: The converted PIL image. |
| 115 | """ |
| 116 | if torch.is_tensor(tensor): |
| 117 | array = tensor.detach().cpu().numpy() |
| 118 | else: |
| 119 | array = tensor |
| 120 | |
| 121 | return array_to_pil(array) |
| 122 | |
| 123 | |
| 124 | def array_to_pil(array): |
nothing calls this directly
no test coverage detected