Shortcuts

torchvision.utils

torchvision.utils.make_grid(tensor: Union[torch.Tensor, List[torch.Tensor]], nrow: int = 8, padding: int = 2, normalize: bool = False, value_range: Optional[Tuple[int, int]] = None, scale_each: bool = False, pad_value: int = 0, **kwargs)torch.Tensor[source]

Make a grid of images.

Parameters
  • tensor (Tensor or list) – 4D mini-batch Tensor of shape (B x C x H x W) or a list of images all of the same size.

  • nrow (int, optional) – Number of images displayed in each row of the grid. The final grid size is (B / nrow, nrow). Default: 8.

  • padding (int, optional) – amount of padding. Default: 2.

  • normalize (bool, optional) – If True, shift the image to the range (0, 1), by the min and max values specified by value_range. Default: False.

  • value_range (tuple, optional) – tuple (min, max) where min and max are numbers, then these numbers are used to normalize the image. By default, min and max are computed from the tensor.

  • scale_each (bool, optional) – If True, scale each image in the batch of images separately rather than the (min, max) over all images. Default: False.

  • pad_value (float, optional) – Value for the padded pixels. Default: 0.

Returns

the tensor containing grid of images.

Return type

grid (Tensor)

Examples using make_grid:

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

Save a given Tensor into an image file.

Parameters
  • 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) – If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.

  • **kwargs – Other arguments are documented in make_grid.

torchvision.utils.draw_bounding_boxes(image: torch.Tensor, boxes: torch.Tensor, labels: Optional[List[str]] = None, colors: Optional[Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]] = None, fill: Optional[bool] = False, width: int = 1, font: Optional[str] = None, font_size: int = 10)torch.Tensor[source]

Draws bounding boxes on given image. The values of the input image should be uint8 between 0 and 255. If fill is True, Resulting Tensor should be saved as PNG image.

Parameters
  • image (Tensor) – Tensor of shape (C x H x W) and dtype uint8.

  • boxes (Tensor) – Tensor of size (N, 4) containing bounding boxes in (xmin, ymin, xmax, ymax) format. Note that the boxes are absolute coordinates with respect to the image. In other words: 0 <= xmin < xmax < W and 0 <= ymin < ymax < H.

  • labels (List[str]) – List containing the labels of bounding boxes.

  • colors (Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]) – List containing the colors or a single color for all of the bounding boxes. The colors can be represented as str or Tuple[int, int, int].

  • fill (bool) – If True fills the bounding box with specified color.

  • width (int) – Width of bounding box.

  • font (str) – A filename containing a TrueType font. If the file is not found in this filename, the loader may also search in other directories, such as the fonts/ directory on Windows or /Library/Fonts/, /System/Library/Fonts/ and ~/Library/Fonts/ on macOS.

  • font_size (int) – The requested font size in points.

Returns

Image Tensor of dtype uint8 with bounding boxes plotted.

Return type

img (Tensor[C, H, W])

Examples using draw_bounding_boxes:

torchvision.utils.draw_segmentation_masks(image: torch.Tensor, masks: torch.Tensor, alpha: float = 0.8, colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None)torch.Tensor[source]

Draws segmentation masks on given RGB image. The values of the input image should be uint8 between 0 and 255.

Parameters
  • image (Tensor) – Tensor of shape (3, H, W) and dtype uint8.

  • masks (Tensor) – Tensor of shape (num_masks, H, W) or (H, W) and dtype bool.

  • alpha (float) – Float number between 0 and 1 denoting the transparency of the masks. 0 means full transparency, 1 means no transparency.

  • colors (list or None) – List containing the colors of the masks. The colors can be represented as PIL strings e.g. “red” or “#FF00FF”, or as RGB tuples e.g. (240, 10, 157). When masks has a single entry of shape (H, W), you can pass a single color instead of a list with one element. By default, random colors are generated for each mask.

Returns

Image Tensor, with segmentation masks drawn on top.

Return type

img (Tensor[C, H, W])

Examples using draw_segmentation_masks:

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources