MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / save_image

Function save_image

func_2d/utils.py:155–176  ·  view source on GitHub ↗

Save a given Tensor into an image file. Args: tensor (Tensor or list): Image to be saved. If given a mini-batch tensor, saves the tensor as a grid of images by calling ``make_grid``. fp (string or file object): A filename or a file object format(Optional)

(
    tensor: Union[torch.Tensor, List[torch.Tensor]],
    fp: Union[Text, pathlib.Path, BinaryIO],
    format: Optional[str] = None,
    **kwargs
)

Source from the content-addressed store, hash-verified

153
154@torch.no_grad()
155def save_image(
156 tensor: Union[torch.Tensor, List[torch.Tensor]],
157 fp: Union[Text, pathlib.Path, BinaryIO],
158 format: Optional[str] = None,
159 **kwargs
160) -> None:
161 """
162 Save a given Tensor into an image file.
163 Args:
164 tensor (Tensor or list): Image to be saved. If given a mini-batch tensor,
165 saves the tensor as a grid of images by calling ``make_grid``.
166 fp (string or file object): A filename or a file object
167 format(Optional): If omitted, the format to use is determined from the filename extension.
168 If a file object was used instead of a filename, this parameter should always be used.
169 **kwargs: Other arguments are documented in ``make_grid``.
170 """
171
172 grid = make_grid(tensor, **kwargs)
173 # Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
174 ndarr = grid.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()
175 im = Image.fromarray(ndarr)
176 im.save(fp, format=format)
177
178
179def create_logger(log_dir, phase='train'):

Callers

nothing calls this directly

Calls 1

make_gridFunction · 0.85

Tested by

no test coverage detected