这篇分类总结比较完整
数据处理是模型训练之前的必备的一步,在Pytorch的TORCHVISION.TRANSFORMS.TRANSFORMS包含下面一下图像处理的函数(transform
中的函数主要是处理PIL
格式图像):
- Compose
Args:
transforms (list of ``Transform`` objects): list of transforms to compose.
"Composes several transforms together” 组合几种不同的变形方法
- ToTensor
Convert a
PIL Image
ornumpy.ndarray
to tensor. Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]。将PIL图像或者numpy.ndarry类型数据转成tensor.
- ToPILImage
Convert a tensor or an ndarray to PIL Image.
Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range.
- Normalize 有参数(mean,std)
处理的数据类型是tensor类型
Normalize a tensor image with mean and standard deviation.
Args:
mean (sequence): Sequence of means for each channel.
std (sequence): Sequence of standard deviations for each channel.
Given mean:
(M1,...,Mn)
and std:(S1,..,Sn)
forn
channels, this transform will normalize each channel of the inputtorch.*Tensor
i.e.input[channel] = (input[channel] - mean[channel]) / std[channel]
- Resize
Resize the input PIL Image to the given size,默认采用PIL.Image.BILINEAR插值法。
Args:
size (sequence or int): Desired output size. If size is a sequence like
(h, w), output size will be matched to this. If size is an int,
smaller edge of the image will be matched to this number.
i.e, if height > width, then image will be rescaled to
(size * height / width, size)
interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR``
- Scale
推荐换成Resize
- CenterCrop 参数(目标尺寸)
Crops the given PIL Image at the center
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made
- Pad
Pad the given PIL Image on all sides with the given “pad” value
Args:
padding (int or tuple):
fill (int or tuple):
padding_mode (str):
- Lambda
Apply a user-defined lambda as a transform
Args:
lambd (function): Lambda/function to be used for transform.
- RandomTransforms
Base class for a list of transformations with randomness
Args:
transforms (list or tuple): list of transformations
- RandomApply
Apply randomly a list of transformations with a given probability
Args:
transforms (list or tuple): list of transformations
p (float): probability
- RandomOrder
Apply a list of transformations in a random order
- RandomChoice
Apply single transformation randomly picked from a list
- RandomCrop
Crop the given PIL Image at a random location.
Args:
size (sequence or int)
padding (int or sequence, optional):
pad_if_needed (boolean)
fill
padding_mode
- RandomHorizontalFlip
Horizontally flip the given PIL Image randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
- RandomVerticalFlip
Verticallly flip the given PIL Image randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
- RandomPerspective
Performs Perspective transformation of the given PIL Image randomly with a given probability.
- RandomResizedCrop
Crop the given PIL Image to random size and aspect ratio
- RandomSizedCrop
废弃
- FiveCrop
Crop the given PIL Image into four corners and the central crop
- TenCrop
Five Crop后翻转(默认是水平翻转)
- LinearTransformation
Transform a tensor image with a square transformation matrix and a mean_vector computed
offline
- ColorJitter
Randomly change the brightness, contrast and saturation of an image.
args:brightness=0, contrast=0, saturation=0, hue=0
- RandomRotation
Rotate the image by angle
args:degrees, resample=False, expand=False, center=None
- RandomAffine
Random affine transformation of the image keeping center invariant
degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0
- Grayscale
Convert image to grayscale.
args:num_output_channels=1
- RandomGrayscale
Randomly convert image to grayscale with a probability of p (default 0.1)