Translator: BXuan694

Transforms contains several commonly used image transforms that can be combined in conjunction with Compose. In addition, torchvision provides torchvision. Transforms. The functional modules. Functional provides more sophisticated transformations for building complex transformation pipelines (such as split tasks).

class torchvision.transforms.Compose(transforms)Copy the code

Used to combine a series of transformations.

Parameters:

  • transforms(the list orTransformObject) – a series of transformations that need to be combined.

Example:

�>>> transforms.Compose([>>> transforms.CenterCrop(10), >>> transforms.ToTensor(), >>>])Copy the code

PIL image transformation

class torchvision.transforms.CenterCrop(size)Copy the code

Crop the PIL picture in the center.

Parameters:

class torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)Copy the code

Randomly change the brightness, contrast, and saturation of the image.

Parameters:

  • brightness(floator Tuples of type float (min, Max)) — the magnitude of the brightness disturbance. Brightness_factor is randomly sampled from [Max (0, 1-brightness), 1 + Brightness]. It should be non-negative.
  • contrast(floator Tuples of type float (min, Max)) — Contrast disturbance amplitude. Contrast_factor was randomly sampled from [Max (0, 1-contrast), 1 + contrast]. It should be non-negative.
  • saturation(floator Tuples of type float (min, Max)) — saturation disturbance amplitude. Saturation_factor is generated by random sampling from [Max (0, 1-saturation), 1 + saturation]. It should be non-negative.
  • hue(floator Tuples of type float (min, Max)) — amplitude of hue disturbance. Hue_factor is randomly sampled from [- Hue, Hue] and its value must be 0<= Hue <= 0.5 or -0.5 <= min <= Max <= 0.5
class torchvision.transforms.FiveCrop(size)Copy the code

Cut PIL pictures from the corners and center.

Note:

This transformation returns an image tuple, which may cause the mismatch between the image and the label information given by the data set after the image is conducted in the network. See the following example for how to handle this.

Parameters:

  • size(The sequence or int) — Shapes that need to be cut out. If size is int, it will be trimmed to a square. If it is a sequence, such as (h, w), it will be clipped to a rectangle.

Example:

>>> transform = Compose([>>> > FiveCrop(size), # here produces a PIL image list >>> Lambda(Lambda crops: Torch. Stack ([ToTensor()(crop) for crop in crops])) # return 4-dimensional tensor >>>]) >>> # In test you can do this: >>> INPUT, target = Batch # input is a 5 dimensional tensor, target is a 2 dimensional tensor. >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, >>> Result_avg = result.view(BS, ncrops, -1). Mean (1) # cropsCopy the code
class torchvision.transforms.Grayscale(num_output_channels=1)Copy the code

Convert the image to grey scale.

Parameters:

  • Num_output_channels (int, 1 or 3) – The desired number of image channels.

Returns:

  • Enter a grayscale version of the image. – If num_output_channels == 1: returns the single-channel image; – If num_output_channels == 3: returns a 3-channel image, where r == g == b.

Return type:

  • PIL image.
class torchvision.transforms.Pad(padding, fill=0, padding_mode='constant')Copy the code

Each edge of PIL image is extended.

Parameters:

  • padding(int or tuple) — Width spread on each edge. If a single int is passed in, it expands on all edges. If a tuple of length 2 is passed in, specify the left, right, and bottom expansion widths. If a tuple of length 4 is passed in, the expansion widths for left, top, right, and bottom are specified in order.
  • fill(int or tuple) — pixel fill value. The default is 0. If a tuple of length 3 is specified, channels R, G, and B are filled respectively. This parameter is valid only if padding_mode is’ constant ‘.
  • padding_mode(str) — Expand type. It should be one of ‘constant’, ‘edge’, ‘reflect’ or ‘symmetric’. The default is’ constant ‘.
    • Constant: Expands with a constant, which is specified by the fill parameter.
    • Edge: Fill with the finger on the edge of the image.
    • Reflect: Fill axisymmetrically with the edge as the axis of symmetry (the edge value is not repeated). For example, filling two elements on both sides of [1, 2, 3, 4] gives �[3, 2, 1, 2, 3, 4, 3, 2].
    • Symmetric: It is filled with the inversion of the edge of an image. For example, filling two elements on both sides of [1, 2, 3, 4] yields [2, 1, 1, 2, 3, 4, 4, 3].
class torchvision.transforms.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0)Copy the code

Random affine transformation is performed on the image by keeping the distribution center of pixels unchanged.

Parameters:

  • degrees(The sequence or float or int) — Screening range of rotation Angle. If it is a sequence (min, Max), it is sampled randomly and uniformly; If it is a number, sample from (-degrees, +degrees). If no rotation is required, set it to 0.
  • translate(tuple. optional) — a tuple whose element values are the maximum absolute values of horizontal and vertical translation transformations. For example, if translate=(a, b), the horizontal displacement is sampled randomly from -img_width * a < dx < img_width * a, The vertical displacement values were sampled randomly from -img_height * b < dy < img_height * b. No translation by default.
  • scale(tuple. optional) — The inner interval of scale scaling factor, such as [a, b], the random sampling interval of scale factor scale is: A <= scale <= b. Scaling is not performed by default.
  • shear(The sequence or float or int. optional) — Filter range of distortion Angle. If it is a sequence (min, Max), it is sampled randomly and uniformly; If it is a number, sample from (-degrees, +degrees). Contorting is not performed by default.
  • resample({PIL.Image.NEAREST . PIL.Image.BILINEAR . PIL.Image.BICUBIC} . optional) for the optional resampling filter, seefilters. If this option is not available, or the Image mode is “1” or “P”, set to pil.image.nearest.
  • Fillcolor (int) – Optionally fills the color outside the transform area of the output image. (Pillow > = 5.0.0).
Class torchvision. Transforms. RandomApply (transforms, p = 0.5)Copy the code

Selected or not with the specified probability for each transform in the Transforms.

Parameters:

  • transforms(list or tuple) — the set of transformations.
  • P (float) – probability.
class torchvision.transforms.RandomChoice(transforms)Copy the code

Select a transformation at random from the list.

class torchvision.transforms.RandomCrop(size, padding=0, pad_if_needed=False)Copy the code

Crop the PIL picture at random position.

Parameters:

  • size(The sequence � or intYou want to crop out the shape of the picture. If size is int, crop by square (size, size); If size is a sequence (h, w), crop to a rectangle.
  • padding(int or The sequence . optional) — Fill the edges of the image, default 0, that is, do not fill. If a sequence of length 4 is specified, the left, top, right, and bottom fill widths are specified.
  • pad_if_needed(boolean) – If set to True, if the image is smaller than the target shape, it will be filled to avoid an exception.
  • fill(int or tuple) — pixel fill value. The default is 0. If a tuple of length 3 is specified, channels R, G, and B are filled respectively. This parameter is valid only if padding_mode is’ constant ‘.
  • padding_mode(str) — Expand type. It should be one of ‘constant’, ‘edge’, ‘reflect’ or ‘symmetric’. The default is’ constant ‘.
    • Constant: Expands with a constant, which is specified by the fill parameter.
    • Edge: Fill with the finger on the edge of the image.
    • Reflect: Fill axisymmetrically with the edge as the axis of symmetry (the edge value is not repeated). For example, filling two elements on both sides of [1, 2, 3, 4] gives �[3, 2, 1, 2, 3, 4, 3, 2].
    • Symmetric: It is filled with the inversion of the edge of an image. For example, filling two elements on both sides of [1, 2, 3, 4] yields [2, 1, 1, 2, 3, 4, 4, 3].
The class torchvision. Transforms. RandomGrayscale (p = 0.1)Copy the code

The image is randomly converted to a gray-scale image with probability P (default: 0.1).

Parameters:

  • P (float) – the probability that the image is converted to grey scale.

Returns:

  • The probability p is converted to grey order, and the probability (1-p) is not transformed. If the input image is 1 channel, the grayscale version is also 1 channel. If the input image is 3 channels, then the grayscale version is 3 channels, r == g == B.

Return type:

  • PIL image.
The class torchvision. Transforms. RandomHorizontalFlip (p = 0.5)Copy the code

Fold PIL pictures at a random level with a given probability.

Parameters:

  • P (float) – The probability of folding an image. The default is 0.5.
class torchvision.transforms.RandomOrder(transforms)Copy the code

Transform the image in a random order.

Class torchvision. Transforms. RandomResizedCrop (size, scale = (0.08, 1.0), thewire = (0.75, 1.3333333333333333), interpolation=2)Copy the code

Crop images with random shapes and aspect ratios.

Crop images with random shapes (default from 0.08 to 1.0 of the original image) and random aspect ratios (default from 3/4 to 4/3). Then adjust to the desired shape. This transformation is typically used to train the Inception network.

Parameters:

  • Size – The expected output shape for each edge.
  • Scale – The range of shapes that crop the original image.
  • Ratio – The target range cropped from the original aspect ratio.
  • Interpolation — Default: pil.image.bilinear.
class torchvision.transforms.RandomRotation(degrees, resample=False, expand=False, center=None)Copy the code

Select pictures at the specified Angle.

Parameters:

  • degrees(The sequence or float or int) — Random selection range of rotation Angle. If degrees is a sequence (min, Max), select them randomly. If it is a number, the selection range is (-degrees, +degrees).
  • resample({PIL.Image.NEAREST . PIL.Image.BILINEAR . PIL.Image.BICUBIC} . optional) for the optional resampling filter, seefilters. If this option is ignored or the Image mode is “1” or “P” set to pil.image.nearest.
  • expand(bool. optional) — Optional extension flag. If set to True, the output is expanded to a size large enough to accommodate the full graph. If set to False or not set, the output image will be the same size as the input. Note that the expand flag assumes rotation around the center and no translation.
  • center(2-tuple . optional) — Optional rotation center coordinates. Take the upper left corner as the origin. The default is image center.
class torchvision.transforms.RandomSizedCrop(*args, **kwargs)Copy the code

Note: This transform has been deprecated and can be replaced with RandomResizedCrop.

The class torchvision. Transforms. RandomVerticalFlip (p = 0.5)Copy the code

Randomly and vertically fold PIL images with a given probability.

Parameters:

  • P (float) – The probability of folding an image. The default is 0.5.
class torchvision.transforms.Resize(size, interpolation=2)Copy the code

Resize the input PIL image to the given shape.

Parameters:

  • size(The sequence or int) — Expected output shape. If size is like (h, w), the output is of that shape. If size is int the shorter sides will be adjusted to int, i.e. if height > width, then the image will be adjusted to (size * height/width, size).
  • interpolation(int. optional) — interpolation. By default thePIL.Image.BILINEAR.
class torchvision.transforms.Scale(*args, **kwargs)Copy the code

Note: This transformation has been deprecated and can be replaced with Resize.

class torchvision.transforms.TenCrop(size, vertical_flip=False)Copy the code

Cut the PIL picture in the corners and center, and add the folded version. (Fold horizontally by default)

Note:

This transformation returns an image tuple, which may cause the mismatch between the image and the label information given by the data set after the image is conducted in the network. See the following example for how to handle this.

Parameters:

  • size(The sequence or int) — the shape of the output you want to crop. The shape that needs to be cut out. If size is int, it will be trimmed to a square. If it is a sequence, such as (h, w), it will be clipped to a rectangle.
  • Vertical_flip (bool) — Whether to fold vertically.

Example:

>>> transform = Compose([>>> > TenCrop(size), # here generates a PIL image list >>> Lambda(Lambda crops: Torch. Stack ([ToTensor()(crop) for crop in crops])) returns 4-dimensional tensor >>>]) >>> > # In the test phase you can do this: >>> INPUT, target = batch # input = 5 dimensional tensor, target = 2 dimensional tensor >>> BS, Ncrops, C, H, W = input.size() >>> result = model(input.view(-1, c, h, w)) Ncrops, -1). Mean of (1) # cropsCopy the code

Torch.* Transformations on Tensor

class torchvision.transforms.LinearTransformation(transformation_matrix)Copy the code

Transform the image tensor with a pre-prepared transformation matrix.

Torch.* the Tensor will be flattened by the transformation_matrix, then dotted with the transformation matrix to adjust to the original Tensor.

Application:

  • Whiten: Process the distribution center of the data to 0 and calculate the covariance matrix of the data.
    • � Np.dot (X.T, X) can be used to process the shape of [D X D], which is decomposed by singular value and then transmitted to transformation_matrix.

Parameters:

  • Transformation_matrix (Tensor) — [D x D], D = C x H x W.
class torchvision.transforms.Normalize(mean, std)Copy the code

Standardize input images with mean and standard deviation. Given the average value (M1,… ,Mn) and standard deviation (S1,.. ,Sn), this will be standardized on torch.*Tensor at every channel, input[channel] = (input[channel] – mean[channel])/STD [channel].

Parameters:

  • mean(The sequence) — a sequence containing the average value of each channel.
  • std(The sequence) — a sequence containing the standard deviation of each channel.
__call__(tensor)Copy the code

Parameters:

  • Tensor — you need a standardized visual tensor, which has a shape of (C, H, W).

Returns:

  • Standardized picture Tensor

Return type:

  • Tensor.

Format transform

class torchvision.transforms.ToPILImage(mode=None)Copy the code

Convert tensors or NDARray to PIL images.

Torch.*Tensor or numpy matrix ndarray of shape H x W x C into PIL image, keep the upper and lower bounds of the values.

Parameters:

  • mode (PIL.Image mode) — Optional color space or pixel depth of input data. ifmodeSet toNone(Default), according to the following rules:
  1. If I put in 3 channels,modeWill be set toRGB.
  2. If I put in four channels,modeWill be set toRGBA.
  3. If � enter 1 channel,modeDetermined by the data type (i.eint.float.short).
__call__(pic)Copy the code

Parameters:

  • pic (Tensor or numpy.ndarray) — the picture to be converted into PIL image.

Returns:

  • PIL image after transformation.

Return type:

  • PIL image.
class torchvision.transforms.ToTensorCopy the code

Convert PIL Image or Numpy. Ndarray to a tensor.

Convert the PIL image or numpy. Ndarray (shape (H x W x C)) in the range [0, 255] to Torch.FloatTensor, tensor shape (C x H x W) in the range [0.0, 1.0]. The input should be PIL image and one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1), or numpy. Ndarray and type NP.uint8.

__call__(pic)Copy the code

Parameters:

  • pic (PIL image or numpy.ndarray) — to be converted to a tensor picture.

Returns:

  • The transformed image.

Return type:

  • Tensor.

General transform

class torchvision.transforms.Lambda(lambd)Copy the code

Use user-defined functions as transformations.

Parameters:

  • lambd (functionThe Lambda function or function name used for the transformation.

The Functional transformation

Functional provides more sophisticated transformations for building complex transformation pipelines. In contrast to the previous transformation, the parameters of the function transformation do not contain a random number seed generator. This means that you have to specify the values of all the parameters, but you can introduce random numbers yourself. For example, use the following functional transformation for a group of images:

import torchvision.transforms.functional as TF import random def my_segmentation_transforms(image, segmentation): if random.random() > 5: angle = random.randint(-30, 30) image = TF.rotate(image, angle) segmentation = TF.rotate(segmentation, Angle) # More conversions... return image, segmentationCopy the code
torchvision.transforms.functional.adjust_brightness(img, brightness_factor)Copy the code

� Adjust image brightness.

Parameters:

  • img(PIL image) — PIL image to adjust.
  • Brightness_factor (float) – The brightness adjustment value, which can be any non-negative integer. 0 means black image, 1 means original image, 2 means increased brightness to 2 times.

Returns:

  • Image after adjusting brightness.

Return type:

  • PIL image.
torchvision.transforms.functional.adjust_contrast(img, contrast_factor)Copy the code

Adjust image contrast.

Parameters:

  • img(PIL image� PIL image to adjust.
  • Contrast_factor (float) – The amount of contrast adjustment that can be any non-negative. 0 means gray-scale image, 1 means original image, and 2 means contrast has been increased to 2x.

Returns:

  • Adjust the contrast of the image.

Return type:

  • PIL image.
torchvision.transforms.functional.adjust_gamma(img, gamma, gain=1)Copy the code

Gamma correction of the image.

Also known as power transformation. The intensity in RGB mode is adjusted according to the following equation:

? I_{out} = 255 \times gain \times (\dfrac{I_{in}}{255})^\gamma?

See gamma correction for more details.

Parameters:

  • img(PIL image) — PIL to adjust PIL image.
  • Gamma (float) – Non-negative real number, $\gamma$in the formula. Gamma greater than 1 makes the dark region darker, and gamma lower than 1 makes the dark region brighter.
  • Gain (float) – a constant multiplier.
torchvision.transforms.functional.adjust_hue(img, hue_factor)Copy the code

Adjust the hue of the image.

To adjust, the image is first converted into HSV space, and then circulates along the hue axis (H axis). Finally switch back to the original mode of the image.

Hue_factor is the offset of H channel and must be in the range of [-0.5, 0.5].

See hue for more details.

Parameters:

  • img(PIL image) — PIL image to adjust.
  • Hue_factor (float) — H channel offset should be in the range of [-0.5, 0.5]. 0.5 and -0.5 indicate that the h-axis of HSV space moves in the positive and negative directions respectively, and 0 indicates no deviation. Thus, both -0.5 and 0.5 can represent complementary colors, and 0 represents original images.

Returns:

  • Hue adjusted image.

Return type:

  • PIL image.
torchvision.transforms.functional.adjust_saturation(img, saturation_factor)Copy the code

Adjust the color saturation of the image.

Parameters:

  • img(PIL � image) — PIL image to adjust.
  • Saturation_factor (float) – Saturation adjustment value. 0 means pure black and white image, 1 means original image, 2 means increased to 2 times original image.

Returns:

  • Adjust the saturation of the image.

Return type:

  • PIL image.
torchvision.transforms.functional.affine(img, angle, translate, scale, shear, resample=0, fillcolor=None)Copy the code

Affine transformation is performed by keeping the distribution center of image pixels unchanged.

Parameters:

  • img(PIL image) — PIL image to rotate.
  • angle({python:float or int}) — Rotation Angle should be between -180 and 180 degrees in the direction of the clock.
  • translate(list or Integer tuples) — Horizontal and vertical transformations (after rotation)
  • Scale (float) – Scale transformation.
  • Shear (float) – The Angle of distortion should be between -180 and 180 degrees in the clock direction.
  • resample(PIL.Image.NEAREST or PIL.Image.BILINEAR or PIL.Image.BICUBIC . optional) for the optional resampling filter, seefilter. If this option is not set, or the image mode is “1” or “P”, set toPIL.Image.NEAREST.
  • Fillcolor (int) – Optionally fills the color outside the transform area of the output image. (Pillow > = 5.0.0)
torchvision.transforms.functional.crop(img, i, j, h, w)Copy the code

Crop the specified PIL image.

Parameters:

  • img(PIL image) — The image to crop.
  • I – coordinates of the uppermost pixel.
  • J — coordinates of the leftmost pixel.
  • H – The height to crop out.
  • W – The width to crop out.

Returns:

  • Cropped image.

Return type:

  • PIL image.
torchvision.transforms.functional.five_crop(img, size)Copy the code

Crop the image in the corners and center.

Note:

This transformation returns an image tuple, which may cause the image to fail to match the information such as the label given by your Dataset after being transmitted through the network.

Parameters:

  • size(The sequence or int) — the desired clipping output. If size is a sequence (h, w), output rectangle; If int, the output is a square of shape (size, size).

Returns:

  • Tuples (TL, tr, BL, BR, center) representing upper left, upper right, lower left, and lower right, respectively.

Return type:

  • tuple
torchvision.transforms.functional.hflip(img)Copy the code

Folds the specified image horizontally.

Parameters:

  • img(PIL image) — the image to be folded.

Returns:

  • Horizontally folded image.

Return type:

  • PIL image.
torchvision.transforms.functional.normalize(tensor, mean, std)Copy the code

The image is normalized by means and variances.

See Normalize for more details.

Parameters:

  • Tensor — you need a standardized visual tensor, your shape should be (C, H, W).
  • mean(The sequence) — the mean value of each channel.
  • std(The sequence) — Standard deviation of each channel.

Returns:

  • Standardized image Tensor.

Return type:

  • Tensor.
torchvision.transforms.functional.pad(img, padding, fill=0, padding_mode='constant')Copy the code

Fills the PIL image with the specified fill mode and fill value.

Parameters:

  • img(PIL image) — the image to fill.
  • padding(int or tuple) — The filling width of each side. If specified as int, all edges are filled to this width. If � is specified as a tuple of length 2, it represents the left, right, top, and bottom fill widths. If a tuple of length 4 is specified, the left, top, right, and bottom fill widths are represented respectively.
  • Fill – The pixel value to fill, 0 by default. If specified as a tuple of length 3, it represents the fill value for RGB� 3 channels. This option is only useful if padding_mode is constant�.
  • padding_mode– The value can be constant, edge, Reflect, or symmetric. The default is constant.
    • Constant: Is filled with a constant value specified by fill.

    • Edge: Fills with the edge values.

    • Reflect: Fills with an edge as its axis of symmetry. (Do not repeat edge values)

      • In Reflect mode, filling both sides with two elements [1, 2, 3, 4] will yield [3, 2, 1, 2, 3, 4, 3, 2].
    • Symmetric: The symmetry axis is used for filling. (Repeat edge values)

      • In symmetric mode, filling both sides with two elements [1, 2, 3, 4] will yield [2, 1, 1, 2, 3, 4, 4, 3].

Returns:

  • The filled image.

Return type:

  • PIL image
torchvision.transforms.functional.resize(img, size, interpolation=2)Copy the code

Readjust the PIL image to the specified shape.

Parameters:

  • img(PIL image) — image to adjust shape.
  • size(The sequence or int) — Output the shape of the image. If size is specified as a sequence (h, w), output the rectangle. If size is specified as int the short side of the image will be adjusted to that number, and the long side will be adjusted to the same aspect ratio. $(size\times\frac{height}{width}, size)$if height > width
  • interpolation(int. optional) — Interpolation mode, default isPIL.Image.BILINEAR.

Returns:

  • Resize the picture.

Return type:

  • PIL image.
torchvision.transforms.functional.resized_crop(img, i, j, h, w, size, interpolation=2)Copy the code

Cut PIL and adjust to � target shape.

Note: RandomResizedCrop is called.

Parameters:

  • img(PIL image) — The image to crop.
  • I – the pixel coordinates of the uppermost side.
  • J — the leftmost pixel coordinate.
  • H – The height of the cropped image.
  • W – Width of cropped image.
  • size(The sequence or int) — image shape to output, same asscale.
  • interpolation(int. optional) — Interpolation mode, default isPIL.Image.BILINEAR.

Returns:

  • Cropped picture.

Return type:

  • PIL image.
torchvision.transforms.functional.rotate(img, angle, resample=False, expand=False, center=None)Copy the code

Rotate the image.

Parameters:

  • img(PIL image) — To rotate the PIL� image.
  • angle(float or int}) — Rotate the Angle clockwise.
  • resample(PIL.Image.NEAREST or PIL.Image.BILINEAR or PIL.Image.BICUBIC . optional) for the optional resampling filter, seefilter. If this option is not set, or the Image mode is “1” or “P”, it will be set to pil.image.nearest.
  • expand(bool. optional) — Optional extension options. If set to True, the output is large enough to include all the pixels. If set to False or not, the output should have the same shape as the input. Note that the expand option assumes the rotation center is center and does not pan.
  • center(2-tuple . optional) — Optional rotation center. The origin is in the upper left corner. The default rotation center is the image center.
torchvision.transforms.functional.ten_crop(img, size, vertical_flip=False)Copy the code

Crop the image at the corners and center and return the folded image. (Default fold horizontally)

Note:

  • The transform returns an image tuple that may cause the image to conduct after the network and yourDatasetThe information such as the label does not match.

Parameters:

  • size(The sequence or int) – The output shape after cropping. If size is int, print (size, size) square; If size is a sequence, output the rectangle.
  • vertical_flip(bool) – Use vertical folding.

Returns:

  • Tuple (TL, tr, BL, BR, Center, TL_flip, tr_flip, BL_flip, BR_flip, center_flip) – corresponding top left, top right, bottom left, bottom right, center cropped image and horizontally folded image.

Return type:

  • tuples
torchvision.transforms.functional.to_grayscale(img, num_output_channels=1)Copy the code

Output the image to a greyscale version.

Parameters:

  • img(PIL image) — the image to be converted to gray-scale image.

Returns:

  • Gray-scale version of the image. If num_output_channels == 1: returns the single-channel image; If num_output_channels == 3: returns the three-channel image, where r == g == b.

Return type:

  • PIL image.
torchvision.transforms.functional.to_pil_image(pic, mode=None)Copy the code

Transform a tensor or NDARray into a PIL image.

See ToPIlImage for more details.

Parameters:

  • pic(Tensor or numpy.ndarray) — to be converted into PIL pictures.
  • Mode (pil.image Mode) – Input data color space and pixel depth. (optional)

Returns:

  • Data to be converted into PIL images.

Return type:

  • PIL image.
torchvision.transforms.functional.to_tensor(pic)Copy the code

Convert PIL Image or Numpy. Ndarray to a tensor.

See ToTensor for more details.

Parameters:

  • pic (PIL image or numpy.ndarray) — to be converted to a tensor picture.

Returns:

  • The transformed image.

Return type:

  • Tensor.
torchvision.transforms.functional.vflip(img)Copy the code

Fold PIL image vertically.

Parameters:

  • img(PIL image) — the image to be folded.

Returns:

  • Vertically folded image.

Return type:

  • PIL image.