showtens Documentation#
showtens is a package for displaying tensors EASILY. In one line, you can view image tensors, whether they are batched or not, grayscale or not. No need to spend several lines formatting, making into a grid, import matplotlib, etc. In just one line, your tensor shows up. In just one line, it is saved.
Installation instructions#
TODO
Function use :#
- showtens.show_image(tensor: Tensor | ndarray, columns: int | None = None, rescale: bool = False, clamp_range: tuple[float] = (0.0, 1.0), colorbar: bool = False, max_width: int | None = None, padding: int = 3, pad_value: float = 0.0) None[source]#
Shows tensor of shape (*,C,H,W) as an image using pyplot. Any extra dimensions are treated as batch dimensions, and displayed in a grid. By default, the image is simply clipped to the 0-1 range. Use rescale and clamp_range to modify this behaviour.
- Parameters:
tensor – (H,W) or (C,H,W) or (*,C,H,W) tensor or numpy array to display.
columns – number of columns to use for the grid of images (default 8 or less)
rescale – whether to rescale the images to 0-1 range. Uses min-max scaling.
clamp_range – (min,max), tuple of values to clamp the images to, before rescaling. Use None to disable clamping
colorbar – whether to add a colorbar to the image, only works for grayscale images (default False)
max_width – maximum width of the image
padding – number of pixels between images in the grid
pad_value – inter-padding value for the grid of images
- showtens.save_image(tensor: Tensor | ndarray, folder: str, name: str = 'imagetensor', columns: int | None = None, rescale: bool = False, clamp_range: tuple[float] = (0.0, 1.0), colorbar: bool = False, max_width: int | None = None, padding: int = 3, pad_value: float = 0.0, create_folder: bool = True) None[source]#
Saves tensor of shape (*,C,H,W) as a png image. Any extra dimensions are treated as batch dimensions, and displayed in a grid. By default, the image is simply clipped to the 0-1 range. Use rescale and clamp_range to modify this behaviour.
- Parameters:
tensor – (H,W) or (C,H,W) or (*,C,H,W) tensor or numpy array to display
folder – relative path of folder where to save the image
name – name of the image (do not include extension)
columns – number of columns to use for the grid of images (default 8 or less)
rescale – whether to rescale the images to 0-1 range
clamp_range – (min,max), tuple of values to clamp the images to, before rescaling. Use None to disable clamping
colorbar – whether to add a colorbar to the image, only works for grayscale images (default False)
max_width – maximum width of the image
padding – number of pixels between images in the grid
pad_value – inter-padding value for the grid of images
create_folder – whether to create the folder if it does not exist (default True)
- showtens.save_video(tensor: Tensor | ndarray, folder: str, name: str = 'videotensor', fps: int = 30, columns: int | None = None, max_width: int | None = None, padding: int = 3, pad_value: float = 0.0, create_folder: bool = True, quality: float = 8.0) None[source]#
Shows tensor as a video. Accepts both (T,H,W), (T,3,H,W) and (*,T,3,H,W) tensors. Tensor should be a float tensor with values in [0,1]. Clips the values otherwise.
Saved as H.264 (libx264) in an mp4 container.
- Parameters:
tensor – (T,H,W) or (T,3,H,W) or (*,T,3,H,W) float tensor or numpy array
folder – path to save the video
name – name of the video
fps – fps of the video (default 30)
columns – number of columns to use for the grid of videos (default 8 or less)
max_width – maximum width of the image
padding – number of pixels between images in the grid
pad_value – inter-padding value for the grid of images
create_folder – whether to create the folder if it does not exist (default True)
quality – encoding quality between 1 and 10, 10 being lossless (default 8)
- showtens.show_video(tensor: Tensor | ndarray, fps: int = 30, columns: int | None = None, max_width: int | None = None, padding: int = 3, pad_value: float = 0.0, quality: float = 8.0) None[source]#
Shows tensor as a video. Accepts both (T,H,W), (T,3,H,W) and (*,T,3,H,W) tensors. Tensor should be a float tensor with values in [0,1]. Clips the values otherwise.
In a notebook, the video is displayed inline. Otherwise, it is opened in the default web browser. Both use the same html5 player, with play/pause and seeking.
- Parameters:
tensor – (T,H,W) or (T,3,H,W) or (*,T,3,H,W) float tensor or numpy array
fps – fps of the video (default 30)
columns – number of columns to use for the grid of videos (default 8 or less)
max_width – maximum width of the video
padding – number of pixels between videos in the grid
pad_value – inter-padding value for the grid of videos
quality – encoding quality between 1 and 10, 10 being lossless (default 8)
- showtens.gridify(tensor: Tensor, max_width: int | None = None, columns: int | None = None, padding: int = 3, pad_value: float = 0.0) Tensor[source]#
Makes a grid of images/videos from a batch of images. Like torchvision’s make_grid, but more flexible. Accepts (B,*,H,W)
- Parameters:
tensor – (B,*,H,W) tensor
max_width – max width of the output grid. Resizes images to fit the width
columns – number of columns of the grid. If None, uses 8 or less
padding – padding to add to the images
pad_value – color of the padding
- Returns:
(*,H’,W’) tensor, representing the grid of images/videos