torchvision_sunner.transforms¶
This module define the augmentation functions. In this page, we will intorduce the parameters one by one! On the other hand, we also provide the API to deal with categorical data. You can refer the parameters and usage in here. But you just need to import the library as following:
import torchvision_sunner.transforms as sunnertransforms
In default, the whole augmentations are covered as the class interface. So you should create the operation instance first, and use it to do the augmentation next. The torchvision_sunner.transforms also defines some function that the user can call directly. You can check here for detail.
torchvision_sunner.transforms.OP [source]¶
There two kinds of operators toward augmentations. The first one is simple category. The operation is the same with arbitrary rank format or input type. The other one is complex category, we should consider for different cases toward different rank format or input type. This function provide an interface to deal with complex operation. The instance of this function cannot be create directly. But you can inherit this class, and create the new augmentation class in the future.
torchvision_sunner.transforms.ToTensor [source]¶
Change the type of tensor as torch.Tensor. If the type of input is already torch.Tensor, the function will return the parameters directly. Here is the usage:
# Use it uniquely
op = sunnertransforms.ToTensor()
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.ToTensor()
])
input = op(input)
Luckily, we revise the usage as the same as official torchvision in version 19.3.15. As the result, you should notice the different output of this function.
- For the other type input (not image type input), the function is the same.
- For the image type input, the result will become calling
ToTensor,Normalize(mean=[0, 0, 0], std=[1/127.5, 1/127.5, 1/127.5])andTranspose(sunnertransforms.BHWC2BCHW)at the same time. Here is the difference:
# Previous usage to deal with image
op = torchvision.transforms.Compose([
sunnertransforms.ToTensor(),
sunnertransforms.Normalize(mean = [0, 0, 0], std = [127.5, 127.5, 127.5]),
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW)
])
# Current usage to achieve the same effect
op = torchvision.transforms.Compose([
sunnertransforms.ToTensor(),
])
Parameters¶
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.ToFloat [source]¶
Change the type of tensor as float type, which means torch.FloatTensor. You should notice that this function should be called after you wnsure the type of tensor is torch.Tensor. Here is the usage:
# Use it uniquely
op = sunnertransforms.ToFloat()
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.ToTensor(),
sunnertransforms.ToFloat()
])
input = op(input)
Parameters¶
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.Transpose [source]¶
Transpose the rank format toward the given tensor. You should be careful to ensure the rank format while you use this function. Here is the usage:
# Use it uniquely
op = sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW)
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW)
])
input = op(input)
Parameters¶
(constructor)
- direction (int) - The constant which is defined in
torchvision_sunner.constant. We only provide two direction now:sunnertransforms.BHWC2BCHW: Transfer the rank format from [batch, height, width, channel] to [batch, channel, height, width]sunnertransforms.BCHW2BHWC: Transfer the rank format from [batch, channel, height, width] to [batch, height, width, channel]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.RandomHorizontalFlip [source]¶
Flip the tensor toward horizontal direction randomly. Here is the usage:
- Notice : You should transfer the tensor into rank format
BCHWfirst.
# Use it uniquely
op = sunnertransforms.RandomHorizontalFlip()
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
sunnertransforms.RandomHorizontalFlip()
])
input = op(input)
Parameters¶
- p (float) - The probability you want to flip, and the value should locate in [0.0, 1.0]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.RandomVerticalFlip [source]¶
Flip the tensor toward vertical direction randomly. Here is the usage:
- Notice : You should transfer the tensor into rank format
BCHWfirst.
# Use it uniquely
op = sunnertransforms.RandomVerticalFlip()
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
sunnertransforms.RandomVerticalFlip()
])
input = op(input)
Parameters¶
- p (float) - The probability you want to flip, and the value should locate in [0.0, 1.0]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.GrayStack [source]¶
Stack the gray-scale image for 3 times to become RGB image. If the input is already RGB image, this function do nothing. This function also accept the input tensor whose channel is 1. Here is the usage:
import torch
# Use it uniquely
op = sunnertransforms.GrayStack(sunnertransforms.BHW2BHWC)
input = torch.randn(32, 28, 28)
input = op(input)
print(input.size())
# >> [32, 28, 28, 3]
# The channel=1 case is Ok!
input = torch.randn(32, 28, 28, 1)
input = op(input)
print(input.size())
# >> [32, 28, 28, 3]
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.GrayStack(sunnertransforms.BHW2BHWC)
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
])
input = torch.randn(32, 28, 28)
input = op(input)
Parameters¶
(constructor)
- direction (int) - The constant which is defined in
torchvision_sunner.constant. We only provide two direction now:sunnertransforms.BHW2BHWC: Transfer the rank format from [batch, height, width] to [batch, height, width, 3]sunnertransforms.BTHW2BTHWC: Transfer the rank format from [batch, time_step, height, width] to [batch, time_step, height, width, 3]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.Resize [source]¶
Resize the tensor into corresponding size. You should call this function before ToTensor(). Here is the usage:
# Use it uniquely
op = sunnertransforms.Resize(output_size = (320, 640))
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Resize(output_size = (320, 640)),
sunnertransforms.ToTensor()
])
input = op(input)
Luckily, Torchvision_sunner adopt the usage of original torchvision in version 19.3.15. As the result, the input and output of this function should be PIL.Image.Image. On the other hand, you can use less line to achieve the same function than the previous usage!
Parameters¶
(constructor)
- output_size (tuple) - The tuple to represent the resized size. The format of tuple is
(Height, width)
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.Normalize [source]¶
This function is complex operation. Normalize the tensor for the given tensor. If you don’t assign mean and std, then we treat the range of input tensor is [0, 1]. From the version 19.3.15, we will adopt the same mean and std as VGG preprocessing setting (mean is [0.485, 0.456, 0.406], and std is [0.229, 0.224, 0.225]). Here is the usage:
- Notice : You should transfer the tensor into rank format
BCHWfirst. - Notice : You should call
ToFloat()first.
# Use it uniquely
op = sunnertransforms.Normalize(mean = [0.5, 0.5, 0.5], std = [0.5, 0.5, 0.5])
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
sunnertransforms.ToFloat(),
sunnertransforms.Normalize(mean = [0.5, 0.5, 0.5], std = [0.5, 0.5, 0.5]),
])
input = op(input)
Parameters¶
(constructor)
- mean (list) - The mean of pixel in RGB order. You should make sure that the length of list should be the same as the channel number of given tensor. The default is
[0.485, 0.456, 0.406] - std (list) - The std of pixel in RGB order. You should make sure that the length of list should be the same as the channel number of given tensor. The default is
[0.229, 0.224, 0.225]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.UnNormalize [source]¶
This function is complex operation. Un-normalize the tensor for the given tensor. If you don’t assign mean and std, then we treat the range of input tensor is [0, 1]. From the version 19.3.15, we will adopt the same mean and std as VGG preprocessing setting (mean is [0.485, 0.456, 0.406], and std is [0.229, 0.224, 0.225]). Here is the usage:
- Notice : You should transfer the tensor into rank format
BCHWfirst.
# Use it uniquely
op = sunnertransforms.UnNormalize(mean = [0.5, 0.5, 0.5], std = [0.5, 0.5, 0.5])
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
sunnertransforms.UnNormalize(mean = [0.5, 0.5, 0.5], std = [0.5, 0.5, 0.5]),
])
input = op(input)
Parameters¶
(constructor)
- mean (list) - The mean of pixel in RGB order. You should make sure that the length of list should be the same as the channel number of given tensor. The default is
[0.485, 0.456, 0.406] - std (list) - The std of pixel in RGB order. You should make sure that the length of list should be the same as the channel number of given tensor. The default is
[0.229, 0.224, 0.225]
(inference)
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.
torchvision_sunner.transforms.ToGray [source]¶
This function is complex operation. Transfer the tensor into gray-scale. Here is the usage:
- Notice : You should transfer the tensor into rank format
BCHWfirst.
# Use it uniquely
op = sunnertransforms.ToGray()
input = op(input)
# Use it with other augmentation
op = torchvision.transforms.Compose([
sunnertransforms.Transpose(sunnertransforms.BHWC2BCHW),
sunnertransforms.ToGray()
])
input = op(input)
Parameters¶
- tensor (np.ndarray or torch.Tensor) - The tensor you want to deal with.