Pytorch中图像预处理相关函数
這篇分類總結比較完整
數據處理是模型訓練之前的必備的一步,在Pytorch的TORCHVISION.TRANSFORMS.TRANSFORMS包含下面一下圖像處理的函數(transform中的函數主要是處理PIL格式圖像):
"Composes several transforms together” 組合幾種不同的變形方法
Convert a PIL Image or numpy.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.
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.
處理的數據類型是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) for n channels, this transform will normalize each channel of the input torch.*Tensor i.e. input[channel] = (input[channel] - mean[channel]) / std[channel]
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``推薦換成Resize
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 the given PIL Image on all sides with the given “pad” value
Args:
padding (int or tuple):
fill (int or tuple):
padding_mode (str):
Apply a user-defined lambda as a transform
Args:
lambd (function): Lambda/function to be used for transform.
Base class for a list of transformations with randomness
Args:
transforms (list or tuple): list of transformations
Apply randomly a list of transformations with a given probability
Args:
transforms (list or tuple): list of transformations
p (float): probability
Apply a list of transformations in a random order
Apply single transformation randomly picked from a list
Crop the given PIL Image at a random location.
Args:size (sequence or int)padding (int or sequence, optional):pad_if_needed (boolean)fillpadding_modeHorizontally flip the given PIL Image randomly with a given probability.
Args:p (float): probability of the image being flipped. Default value is 0.5Verticallly flip the given PIL Image randomly with a given probability.
Args:p (float): probability of the image being flipped. Default value is 0.5Performs Perspective transformation of the given PIL Image randomly with a given probability.
Crop the given PIL Image to random size and aspect ratio
廢棄
Crop the given PIL Image into four corners and the central crop
Five Crop后翻轉(默認是水平翻轉)
Transform a tensor image with a square transformation matrix and a mean_vector computed
offline
Randomly change the brightness, contrast and saturation of an image.
args:brightness=0, contrast=0, saturation=0, hue=0
Rotate the image by angle
args:degrees, resample=False, expand=False, center=None
Random affine transformation of the image keeping center invariant
degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0
Convert image to grayscale.
args:num_output_channels=1
Randomly convert image to grayscale with a probability of p (default 0.1)
總結
以上是生活随笔為你收集整理的Pytorch中图像预处理相关函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 调试中的一些Python错误
- 下一篇: Python函数及相关知识