jittor.transform¶
这里是Jittor的 数据变换 模块的API文档,您可以通过from jittor import transform
来获取该模块。
- class jittor.transform.CenterCrop(size)[源代码]¶
Class for cropping image centrally.
Args:
[in] size(int or tuple): Size want to crop.
Example:
transform = transform.CenterCrop(224) img_ = transform(img)
- class jittor.transform.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)[源代码]¶
Randomly change the brightness, contrast, saturation and hue of an image. Args:
[in] brightness (float or tuple of float (min, max)): How much to jitter brightness. brightness_factor is chosen uniformly from [max(0, 1 - brightness), 1 + brightness] or the given [min, max]. Should be non negative numbers. [in] contrast (float or tuple of float (min, max)): How much to jitter contrast. contrast_factor is chosen uniformly from [max(0, 1 - contrast), 1 + contrast] or the given [min, max]. Should be non negative numbers. [in] saturation (float or tuple of float (min, max)): How much to jitter saturation. saturation_factor is chosen uniformly from [max(0, 1 - saturation), 1 + saturation] or the given [min, max]. Should be non negative numbers. [in] hue (float or tuple of float (min, max)): How much to jitter hue. hue_factor is chosen uniformly from [-hue, hue] or the given [min, max]. Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.
- class jittor.transform.Compose(transforms)[源代码]¶
Base class for combining various transformations.
Args:
[in] transforms(list): a list of transform.
Example:
transform = transform.Compose([ transform.Resize(opt.img_size), transform.Gray(), transform.ImageNormalize(mean=[0.5], std=[0.5]), ]) img_ = transform(img)
- class jittor.transform.Crop(top, left, height, width)[源代码]¶
Crop and the PIL Image to given size.
Args:
top(int): top pixel indexes
left(int): left pixel indexes
height(int): image height
width(int): image width
- class jittor.transform.FiveCrop(size)[源代码]¶
Crop the given PIL Image into four corners and the central crop
注解
This transform returns a tuple of images and there may be a mismatch in the number of inputs and targets your Dataset returns. See below for an example of how to deal with this.
- 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 of size (size, size) is made.
- size (sequence or int): Desired output size of the crop. If size is an
- Example:
>>> transform = Compose([ >>> FiveCrop(size), # this is a list of PIL Images >>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor >>> ]) >>> #In your test loop you can do the following: >>> input, target = batch # input is a 5d tensor, target is 2d >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops >>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
- class jittor.transform.Gray(num_output_channels=1)[源代码]¶
Convert image to grayscale.
Example:
transform = transform.Gray() img_ = transform(img)
- class jittor.transform.ImageNormalize(mean, std)[源代码]¶
Class for normalizing the input image.
Args:
[in] mean(list): the mean value of Normalization. [in] std(list): the std value of Normalization.
Example:
transform = transform.ImageNormalize(mean=[0.5], std=[0.5]) img_ = transform(img)
- class jittor.transform.Lambda(lambd)[源代码]¶
Apply a user-defined lambda as a transform. Args:
lambd (function): Lambda/function to be used for transform.
- class jittor.transform.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0)[源代码]¶
Random affine transformation of the image keeping center invariant
- Args:
- degrees (sequence or float or int): Range of degrees to select from.
If degrees is a number instead of sequence like (min, max), the range of degrees will be (-degrees, +degrees). Set to 0 to deactivate rotations.
- translate (tuple, optional): tuple of maximum absolute fraction for horizontal
and vertical translations. For example translate=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a and vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b. Will not translate by default.
- scale (tuple, optional): scaling factor interval, e.g (a, b), then scale is
randomly sampled from the range a <= scale <= b. Will keep original scale by default.
- shear (sequence or float or int, optional): Range of degrees to select from.
If shear is a number, a shear parallel to the x axis in the range (-shear, +shear) will be apllied. Else if shear is a tuple or list of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied. Else if shear is a tuple or list of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied. Will not apply shear by default
- resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
An optional resampling filter. See filters for more information. If omitted, or if the image has mode “1” or “P”, it is set to PIL.Image.NEAREST.
- fillcolor (tuple or int): Optional fill color (Tuple for RGB Image And int for grayscale) for the area
outside the transform in the output image.(Pillow>=5.0.0)
- class jittor.transform.RandomApply(transforms, p=0.5)[源代码]¶
Apply randomly a list of transformations with a given probability Args:
[in] transforms (list or tuple): list of transformations [in] p (float): probability
- class jittor.transform.RandomChoice(transforms)[源代码]¶
Apply single transformation randomly picked from a list. Args:
[in] transforms (list or tuple): list of transformations [in] p (float): probability
- class jittor.transform.RandomCrop(size)[源代码]¶
Class for randomly cropping the input image.
Args:
[in] size(tuple or int): the size want to crop.
Example:
transform = transform.RandomCrop(128) img_ = transform(img)
- class jittor.transform.RandomCropAndResize(size, scale: tuple = (0.08, 1.0), ratio: tuple = (0.75, 1.3333333333333333), interpolation=2)[源代码]¶
Random crop and resize the given PIL Image to given size.
Args:
[in] size(int or tuple): [height, width] of the output image. [in] scale(tuple): range of scale ratio of the area. [in] ratio(tuple): range of aspect ratio. [in] interpolation: type of resize. default: PIL.Image.BILINEAR.
Example:
transform = transform.RandomCropAndResize(224) img_ = transform(img)
- class jittor.transform.RandomGray(p=0.1)[源代码]¶
Randomly convert image to grayscale. Args:
[in] p (float): probability that image should be converted to grayscale, default: 0.1
- Returns::
- [out] PIL Image: Grayscale version of the image with probability p and unchanged
with probability (1-p). - If input image is 1 channel: grayscale version is 1 channel - If input image is 3 channel: grayscale version is 3 channel with r == g == b
- Example::
transform = transform.Gray() img_ = transform(img)
- class jittor.transform.RandomHorizontalFlip(p=0.5)[源代码]¶
Random flip the image horizontally.
Args:
[in] p(float): The probability of image flip, default: 0.5.
Example:
transform = transform.RandomHorizontalFlip(0.6) img_ = transform(img)
- class jittor.transform.RandomOrder(transforms)[源代码]¶
Apply a list of transformations in a random order. Args:
[in] transforms (list or tuple): list of transformations [in] p (float): probability
- class jittor.transform.RandomPerspective(distortion_scale=0.5, p=0.5, interpolation=3)[源代码]¶
Performs Perspective transformation of the given PIL Image randomly with a given probability.
- Args:
interpolation : Default- Image.BICUBIC
p (float): probability of the image being perspectively transformed. Default value is 0.5
distortion_scale(float): it controls the degree of distortion and ranges from 0 to 1. Default value is 0.5.
- static get_params(width, height, distortion_scale)[源代码]¶
Get parameters for
perspective
for a random perspective transform.- Args:
width : width of the image. height : height of the image.
- Returns:
List containing [top-left, top-right, bottom-right, bottom-left] of the original image, List containing [top-left, top-right, bottom-right, bottom-left] of the transformed image.
- class jittor.transform.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2)[源代码]¶
Crop the given PIL Image to random size and aspect ratio.
A crop of random size (default: of 0.08 to 1.0) of the original size and a random aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop is finally resized to given size. This is popularly used to train the Inception networks.
- Args:
size: expected output size of each edge scale: range of size of the origin size cropped ratio: range of aspect ratio of the origin aspect ratio cropped interpolation: Default: PIL.Image.BILINEAR
- static get_params(img, scale, ratio)[源代码]¶
Get parameters for
crop
for a random sized crop.- Args:
img (PIL Image): Image to be cropped. scale (tuple): range of size of the origin size cropped ratio (tuple): range of aspect ratio of the origin aspect ratio cropped
- Returns:
- tuple: params (i, j, h, w) to be passed to
crop
for a random sized crop.
- tuple: params (i, j, h, w) to be passed to
- class jittor.transform.RandomRotation(degrees, resample=False, expand=False, center=None, fill=None)[源代码]¶
Rotate the image by angle.
- Args:
- degrees (sequence or float or int): Range of degrees to select from.
If degrees is a number instead of sequence like (min, max), the range of degrees will be (-degrees, +degrees).
- resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
An optional resampling filter. See filters for more information. If omitted, or if the image has mode “1” or “P”, it is set to PIL.Image.NEAREST.
- expand (bool, optional): Optional expansion flag.
If true, expands the output to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.
- center (2-tuple, optional): Optional center of rotation.
Origin is the upper left corner. Default is the center of the image.
- fill (n-tuple or int or float): Pixel fill value for area outside the rotated
image. If int or float, the value is used for all bands respectively. Defaults to 0 for all bands. This option is only available for
pillow>=5.2.0
.
- jittor.transform.RandomSizedCrop¶
alias of
jittor.transform.RandomResizedCrop
- class jittor.transform.RandomVerticalFlip(p=0.5)[源代码]¶
Random flip the image vertically. Args:
[in] p(float): The probability of image flip, default: 0.5.
- Example::
transform = transform.RandomVerticalFlip(0.6) img_ = transform(img)
- class jittor.transform.Resize(size, mode=2)[源代码]¶
Class for resizing image.
Args:
[in] size(int or tuple): Size want to resize. [h, w] [in] mode(int): type of resize.
Example:
transform = transform.Resize(224) img_ = transform(img)
- class jittor.transform.TenCrop(size, vertical_flip=False)[源代码]¶
Crop the given PIL Image into four corners and the central crop plus the flipped version of these (horizontal flipping is used by default)
注解
This transform returns a tuple of images and there may be a mismatch in the number of inputs and targets your Dataset returns. See below for an example of how to deal with this.
- 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.
vertical_flip (bool): Use vertical flipping instead of horizontal
- Example:
>>> transform = Compose([ >>> TenCrop(size), # this is a list of PIL Images >>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor >>> ]) >>> #In your test loop you can do the following: >>> input, target = batch # input is a 5d tensor, target is 2d >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops >>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
- class jittor.transform.ToPILImage(mode=None)[源代码]¶
Convert a tensor or an ndarray to PIL Image. Args:
pic (Tensor or numpy.ndarray): Image(HWC format) to be converted to PIL Image. mode (PIL.Image mode): color space and pixel depth of input data (optional).
- Returns:
PIL Image: Image converted to PIL Image.
- jittor.transform.adjust_brightness(img, brightness_factor)[源代码]¶
Function for adjusting brightness of an RGB image. Args:
[in] img (PIL Image.Image): Image to be adjusted. [in] brightness_factor (float): How much to adjust the brightness. Can be any non negative number. 0 gives a black image, 1 gives the original image while 2 increases the brightness by a factor of 2.
- Returns::
[out] PIL Image.Image: Brightness adjusted image.
Example:
img = Image.open(...) img_ = transform.adjust_brightness(img, 0.5)
- jittor.transform.adjust_contrast(img, contrast_factor)[源代码]¶
Function for adjusting contrast of an image. Args:
[in] img (PIL Image.Image): Image to be adjusted. [in] contrast_factor (float): How much to adjust the contrast. Can be any non negative number. 0 gives a solid gray image, 1 gives the original image while 2 increases the contrast by a factor of 2.
- Returns::
[out] PIL Image.Image: Contrast adjusted image.
Example:
img = Image.open(...) img_ = transform.adjust_contrast(img, 0.5)
- jittor.transform.adjust_gamma(img, gamma, gain=1)[源代码]¶
Function for performing gamma correction on an image. Also known as Power Law Transform. Intensities in RGB mode are adjusted based on the following equation: .. math:
I_{ ext{out}} = 255 imes ext{gain} imes \left(
rac{I_{ ext{in}}}{255} ight)^{gamma}
See `Gamma Correction`_ for more details. .. _Gamma Correction: https://en.wikipedia.org/wiki/Gamma_correction Args:
[in] img (PIL Image.Image): Image to be adjusted. [in] gamma (float): Non negative real number, same as :math:`\gamma` in the equation. gamma larger than 1 make the shadows darker, while gamma smaller than 1 make dark regions lighter. [in] gain (float): The constant multiplier.
- Returns::
[out] PIL Image.Image: Gamma adjusted image.
- jittor.transform.adjust_hue(img, hue_factor)[源代码]¶
Function for adjusting hue of an image. The image hue is adjusted by converting the image to HSV and cyclically shifting the intensities in the hue channel (H). The image is then converted back to original image mode. hue_factor is the amount of shift in H channel and must be in the interval [-0.5, 0.5]. See `Hue`_ for more details. .. _Hue: https://en.wikipedia.org/wiki/Hue Args:
[in] img (PIL Image.Image): Image to be adjusted. [in] hue_factor (float): How much to shift the hue channel. Should be in [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in HSV space in positive and negative direction respectively. 0 means no shift. Therefore, both -0.5 and 0.5 will give an image with complementary colors while 0 gives the original image.
- Returns::
[out] PIL Image.Image: Saturation adjusted image.
Example:
img = Image.open(...) img_ = transform.adjust_hue(img, 0.1)
- jittor.transform.adjust_saturation(img, saturation_factor)[源代码]¶
Function for adjusting saturation of an image. Args:
[in] img (PIL Image.Image): Image to be adjusted. [in] saturation_factor (float): How much to adjust the saturation. 0 will give a black and white image, 1 will give the original image while 2 will enhance the saturation by a factor of 2.
- Returns::
[out] PIL Image.Image: Saturation adjusted image.
Example:
img = Image.open(...) img_ = transform.adjust_saturation(img, 0.5)
- jittor.transform.center_crop(img, output_size)[源代码]¶
Function for cropping the given image at the center. Args:
[in] img(PIL Image.Image): Input image. [in] output_size (sequence or int): (height, width) of the crop box. If int or sequence with single int, it is used for both directions.
- Returns::
PIL Image.Image: Cropped image.
- jittor.transform.crop(img, top, left, height, width)[源代码]¶
Function for cropping image.
Args:
[in] img(Image.Image): Input image. [in] top(int): the top boundary of the cropping box. [in] left(int): the left boundary of the cropping box. [in] height(int): height of the cropping box. [in] width(int): width of the cropping box.
Example:
img = Image.open(...) img_ = transform.crop(img, 10, 10, 100, 100)
- jittor.transform.crop_and_resize(img, top, left, height, width, size, interpolation=2)[源代码]¶
Function for cropping and resizing image.
Args:
[in] img(Image.Image): Input image. [in] top(int): the top boundary of the cropping box. [in] left(int): the left boundary of the cropping box. [in] height(int): height of the cropping box. [in] width(int): width of the cropping box. [in] size: resize size. [h, w] [in] interpolation(int): type of resize. default: PIL.Image.BILINEAR
Example:
img = Image.open(...) img_ = transform.resize(img, 10,10,200,200,100)
- jittor.transform.gray(img, num_output_channels)[源代码]¶
Function for converting PIL image of any mode (RGB, HSV, LAB, etc) to grayscale version of image. Args:
[in] img(PIL Image.Image): Input image. [in] num_output_channels (int): number of channels of the output image. Value can be 1 or 3. Default, 1.
- Returns::
- [out] PIL Image: Grayscale version of the image.
if num_output_channels = 1 : returned image is single channel if num_output_channels = 3 : returned image is 3 channel with r = g = b
- jittor.transform.hflip(img)[源代码]¶
Function for horizontally flipping the given image. Args:
[in] img(PIL Image.Image): Input image.
Example:
img = Image.open(...) img_ = transform.hflip(img)
- jittor.transform.image_normalize(img, mean, std)[源代码]¶
Function for normalizing image. Args:: [in] image(PIL Image.Image or np.ndarray): input image.
If type of input image is np.ndarray, it should be in shape (C, H, W).
[in] mean(list): the mean value of Normalization. [in] std(list): the std value of Normalization. Example:
img = Image.open(...) img_ = transform.image_normalize(img, mean=[0.5], std=[0.5])
- jittor.transform.pil_to_tensor(pic)¶
Function for turning Image.Image to np.array with CHW format.
Args:
[in] img(Image.Image): Input image.
Example:
img = Image.open(...) img_ = transform.to_tensor(img)
- jittor.transform.resize(img, size, interpolation=2)[源代码]¶
Function for resizing image.
Args:
[in] img(Image.Image): Input image. [in] size: resize size. [h, w] [in] interpolation(int): type of resize. default: PIL.Image.BILINEAR
Example:
img = Image.open(...) img_ = transform.resize(img, (100, 100))
- jittor.transform.to_pil_image(pic, mode=None)[源代码]¶
Convert a tensor or an ndarray to PIL Image. Args:
pic (Tensor or numpy.ndarray): Image(HWC format) to be converted to PIL Image. mode (PIL.Image mode): color space and pixel depth of input data (optional).
- Returns:
PIL Image: Image converted to PIL Image.