neural_compressor.data.transforms.transform
Neural Compressor built-in Transforms on multiple framework backends.
Classes
INC supports built-in preprocessing, postprocessing and general methods on different framework backends. |
|
Tensorflow Transforms subclass. |
|
Mxnet Transforms subclass. |
|
Pytorch Transforms subclass. |
|
Onnxrt_qlinearops Transforms subclass. |
|
Onnxrt_integerops Transforms subclass. |
|
Transforms collection class. |
|
The base class for transform. |
|
Tensorflow wrapper function class. |
|
Tensorflow transform class, the subclass of BaseTransform. |
|
Pytorch and MXNet wrapper function class. |
|
Pytorch and Mxnet transform class, the subclass of BaseTransform. |
|
Composes several transforms together. |
|
Crops an image to a specified bounding box. |
|
Crops an image to a specified bounding box. |
|
Crops an image to a specified bounding box. |
|
Crops an image to a specified bounding box. |
|
Resize image with aspect ratio and pad it to max shape(optional). |
|
Resize image with aspect ratio and pad it to max shape(optional). |
|
Transpose image according to perm. |
|
Transpose image according to perm. |
|
Transpose image according to perm. |
|
Transpose image according to perm. |
|
Vertically flip the given image randomly. |
|
Vertically flip the given image randomly. |
|
Horizontally flip the given image randomly. |
|
Horizontally flip the given image randomly. |
|
Convert PIL Image or NDArray to numpy array. |
|
Convert image to given dtype. |
|
Convert image to given dtype. |
|
Convert image to given dtype. |
|
Crops the given image at the center to the given size. |
|
Crops the given image at the center to the given size with padding. |
|
Resize the input image to the given size. |
|
Resize the input image to the given size. |
|
Crop the image at a random location to the given size. |
|
Crop the given image to random size and aspect ratio. |
|
Crop the given image to random size and aspect ratio. |
|
Crop the given image to random size and aspect ratio. |
|
Normalize a image with mean and standard deviation. |
|
Scale the values of image to [0,1]. |
|
Scale the values of image to [0,1]. |
|
Scale the values of image to [0,1]. |
|
Align image channel, now just support [H,W]->[H,W,dim], [H,W,4]->[H,W,3] and [H,W,3]->[H,W]. |
|
Align image channel, now just support [H,W,4]->[H,W,3] and [H,W,3]->[H,W]. |
|
Convert np.array to NDArray. |
|
Resize the input image to the given size. |
|
Resize the input image to the given size. |
|
Crop the input image with given location and resize it. |
|
Crop the input image with given location and resize it. |
|
Crop the input image with given location and resize it. |
|
Crop the input image with given location and resize it. |
|
Crops the given image at the center to the given size. |
|
Normalize a image with mean and standard deviation. |
|
Normalize a image with mean and standard deviation. |
|
Normalize a image with mean and standard deviation. |
|
Crop the image at a random location to the given size. |
|
Crop the given image to random size and aspect ratio. |
|
A single training/test example for simple sequence classification. |
|
A single set of features of data. |
|
Postprocess the predictions, collect data. |
|
Postprocess the predictions of bert on SQuAD. |
|
Postprocess the predictions of model zoo, collect data. |
|
Postprocess the predictions of bert on SQuADV1.1. |
|
Parse features in Example proto. |
Functions
|
Class decorator used to register all transform subclasses. |
|
Get torchvision interpolation map. |
|
Project the tokenized prediction back to the original text. |
|
Read a SQuAD json file into a list of SquadExample. |
|
Load a data file into a list of `InputBatch`s. |
Module Contents
- class neural_compressor.data.transforms.transform.Transforms(process, concat_general=True)[source]
INC supports built-in preprocessing, postprocessing and general methods on different framework backends.
Transforms base class provides the abstract methods. Users can also register their own Transforms classes by inheriting this base class.
- class neural_compressor.data.transforms.transform.TensorflowTransforms(process, concat_general=True)[source]
Tensorflow Transforms subclass.
- class neural_compressor.data.transforms.transform.MXNetTransforms(process, concat_general=True)[source]
Mxnet Transforms subclass.
- class neural_compressor.data.transforms.transform.PyTorchTransforms(process, concat_general=True)[source]
Pytorch Transforms subclass.
- class neural_compressor.data.transforms.transform.ONNXRTQLTransforms(process, concat_general=True)[source]
Onnxrt_qlinearops Transforms subclass.
- class neural_compressor.data.transforms.transform.ONNXRTITTransforms(process, concat_general=True)[source]
Onnxrt_integerops Transforms subclass.
- class neural_compressor.data.transforms.transform.TRANSFORMS(framework, process)[source]
Transforms collection class.
Provide register method to register new Transforms and provide __getitem__ method to get Transforms according to Transforms type.
- neural_compressor.data.transforms.transform.transform_registry(transform_type, process, framework)[source]
Class decorator used to register all transform subclasses.
- Parameters:
transform_type (str) – Transform registration name
process (str) – support 3 process including ‘preprocess’, ‘postprocess’, ‘general’
framework (str) – support 4 framework including ‘tensorflow’, ‘pytorch’, ‘mxnet’, ‘onnxrt’
cls (class) – The class of register.
- Returns:
The class of register.
- Return type:
cls
- class neural_compressor.data.transforms.transform.BaseTransform[source]
The base class for transform.
- class neural_compressor.data.transforms.transform.TensorflowWrapFunction(transform_func)[source]
Tensorflow wrapper function class.
- class neural_compressor.data.transforms.transform.TensorflowTransform(transform_func, **kwargs)[source]
Tensorflow transform class, the subclass of BaseTransform.
- class neural_compressor.data.transforms.transform.PytorchMxnetWrapFunction(transform_func)[source]
Pytorch and MXNet wrapper function class.
- class neural_compressor.data.transforms.transform.PytorchMxnetTransform(transform_func)[source]
Pytorch and Mxnet transform class, the subclass of BaseTransform.
- neural_compressor.data.transforms.transform.get_torchvision_map(interpolation)[source]
Get torchvision interpolation map.
- class neural_compressor.data.transforms.transform.ComposeTransform(transform_list)[source]
Composes several transforms together.
- Parameters:
transform_list (list of Transform objects) – list of transforms to compose
- Returns:
tuple of processed image and label
- Return type:
sample (tuple)
- class neural_compressor.data.transforms.transform.CropToBoundingBox(offset_height, offset_width, target_height, target_width)[source]
Crops an image to a specified bounding box.
- Parameters:
offset_height (int) – Vertical coordinate of the top-left corner of the result in the input
offset_width (int) – Horizontal coordinate of the top-left corner of the result in the input
target_height (int) – Height of the result
target_width (int) – Width of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.MXNetCropToBoundingBox(offset_height, offset_width, target_height, target_width)[source]
Crops an image to a specified bounding box.
- Parameters:
offset_height (int) – Vertical coordinate of the top-left corner of the result in the input
offset_width (int) – Horizontal coordinate of the top-left corner of the result in the input
target_height (int) – Height of the result
target_width (int) – Width of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ONNXRTCropToBoundingBox(offset_height, offset_width, target_height, target_width)[source]
Crops an image to a specified bounding box.
- Parameters:
offset_height (int) – Vertical coordinate of the top-left corner of the result in the input
offset_width (int) – Horizontal coordinate of the top-left corner of the result in the input
target_height (int) – Height of the result
target_width (int) – Width of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.TensorflowCropToBoundingBox(offset_height, offset_width, target_height, target_width)[source]
Crops an image to a specified bounding box.
- Parameters:
offset_height (int) – Vertical coordinate of the top-left corner of the result in the input
offset_width (int) – Horizontal coordinate of the top-left corner of the result in the input
target_height (int) – Height of the result
target_width (int) – Width of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ResizeWithRatio(min_dim=800, max_dim=1365, padding=False, constant_value=0)[source]
Resize image with aspect ratio and pad it to max shape(optional).
If the image is padded, the label will be processed at the same time. The input image should be np.array.
- Parameters:
min_dim (int, default=800) – Resizes the image such that its smaller dimension == min_dim
max_dim (int, default=1365) – Ensures that the image longest side doesn’t exceed this value
padding (bool, default=False) – If true, pads image with zeros so its size is max_dim x max_dim
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.TensorflowResizeWithRatio(min_dim=800, max_dim=1365, padding=False, constant_value=0)[source]
Resize image with aspect ratio and pad it to max shape(optional).
If the image is padded, the label will be processed at the same time. The input image should be np.array or tf.Tensor.
- Parameters:
min_dim (int, default=800) – Resizes the image such that its smaller dimension == min_dim
max_dim (int, default=1365) – Ensures that the image longest side doesn’t exceed this value
padding (bool, default=False) – If true, pads image with zeros so its size is max_dim x max_dim
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.Transpose(perm)[source]
Transpose image according to perm.
- Parameters:
perm (list) – A permutation of the dimensions of input image
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.TensorflowTranspose(perm)[source]
Transpose image according to perm.
- Parameters:
perm (list) – A permutation of the dimensions of input image
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.MXNetTranspose(perm)[source]
Transpose image according to perm.
- Parameters:
perm (list) – A permutation of the dimensions of input image
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.PyTorchTranspose(perm)[source]
Transpose image according to perm.
- Parameters:
perm (list) – A permutation of the dimensions of input image
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomVerticalFlip[source]
Vertically flip the given image randomly.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.TensorflowRandomVerticalFlip[source]
Vertically flip the given image randomly.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomHorizontalFlip[source]
Horizontally flip the given image randomly.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.TensorflowRandomHorizontalFlip[source]
Horizontally flip the given image randomly.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ToArray[source]
Convert PIL Image or NDArray to numpy array.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CastTFTransform(dtype='float32')[source]
Convert image to given dtype.
- Parameters:
dtype (str, default='float32') – A dtype to convert image to
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CastONNXTransform(dtype='float32')[source]
Convert image to given dtype.
- Parameters:
dtype (str, default='float32') – A dtype to convert image to
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CastPyTorchTransform(dtype='float32')[source]
Convert image to given dtype.
- Parameters:
dtype (str, default='float32') – A dtype to convert image to
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CenterCropTFTransform(size)[source]
Crops the given image at the center to the given size.
- Parameters:
size (list or int) – Size of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.PaddedCenterCropTransform(size, crop_padding=0)[source]
Crops the given image at the center to the given size with padding.
- Parameters:
size (list or int) – Size of the result
crop_padding (int) – crop padding number
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ResizeTFTransform(size, interpolation='bilinear')[source]
Resize the input image to the given size.
- Parameters:
size (list or int) – Size of the result
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ResizePytorchTransform(size, interpolation='bilinear')[source]
Resize the input image to the given size.
- Parameters:
size (list or int) – Size of the result
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomCropTFTransform(size)[source]
Crop the image at a random location to the given size.
- Parameters:
size (list or tuple or int) – Size of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomResizedCropPytorchTransform(size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation='bilinear')[source]
Crop the given image to random size and aspect ratio.
- Parameters:
size (list or int) – Size of the result
scale (tuple or list, default=(0.08, 1.0)) – range of size of the origin size cropped
ratio (tuple or list, default=(3. / 4., 4. / 3.)) – range of aspect ratio of the origin aspect ratio cropped
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomResizedCropMXNetTransform(size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation='bilinear')[source]
Crop the given image to random size and aspect ratio.
- Parameters:
size (list or int) – Size of the result
scale (tuple or list, default=(0.08, 1.0)) – range of size of the origin size cropped
ratio (tuple or list, default=(3. / 4., 4. / 3.)) – range of aspect ratio of the origin aspect ratio cropped
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomResizedCropTFTransform(size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation='bilinear')[source]
Crop the given image to random size and aspect ratio.
- Parameters:
size (list or int) – Size of the result
scale (tuple or list, default=(0.08, 1.0)) – range of size of the origin size cropped
ratio (tuple or list, default=(3. / 4., 4. / 3.)) – range of aspect ratio of the origin aspect ratio cropped
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.NormalizeTFTransform(mean=[0.0], std=[1.0], rescale=None)[source]
Normalize a image with mean and standard deviation.
- Parameters:
mean (list, default=[0.0]) – means for each channel, if len(mean)=1, mean will be broadcasted to each channel, otherwise its length should be same with the length of image shape
std (list, default=[1.0]) – stds for each channel, if len(std)=1, std will be broadcasted to each channel, otherwise its length should be same with the length of image shape
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RescaleKerasPretrainTransform(rescale=None)[source]
Scale the values of image to [0,1].
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RescaleTFTransform[source]
Scale the values of image to [0,1].
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RescaleTransform[source]
Scale the values of image to [0,1].
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.AlignImageChannelTransform(dim=3)[source]
Align image channel, now just support [H,W]->[H,W,dim], [H,W,4]->[H,W,3] and [H,W,3]->[H,W].
Input image must be np.ndarray.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.PyTorchAlignImageChannel(dim=3)[source]
Align image channel, now just support [H,W,4]->[H,W,3] and [H,W,3]->[H,W].
Input image must be PIL Image.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ToNDArrayTransform[source]
Convert np.array to NDArray.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ResizeMXNetTransform(size, interpolation='bilinear')[source]
Resize the input image to the given size.
- Parameters:
size (list or int) – Size of the result
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.ResizeTransform(size, interpolation='bilinear')[source]
Resize the input image to the given size.
- Parameters:
size (list or int) – Size of the result
interpolation (str, default='bilinear') – Desired interpolation type,
'bilinear' (support)
'nearest'
'bicubic'.
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CropResizeTFTransform(x, y, width, height, size, interpolation='bilinear')[source]
Crop the input image with given location and resize it.
- Parameters:
x (int) – Left boundary of the cropping area
y (int) – Top boundary of the cropping area
width (int) – Width of the cropping area
height (int) – Height of the cropping area
size (list or int) – resize to new size after cropping
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.PyTorchCropResizeTransform(x, y, width, height, size, interpolation='bilinear')[source]
Crop the input image with given location and resize it.
- Parameters:
x (int) – Left boundary of the cropping area
y (int) – Top boundary of the cropping area
width (int) – Width of the cropping area
height (int) – Height of the cropping area
size (list or int) – resize to new size after cropping
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.MXNetCropResizeTransform(x, y, width, height, size, interpolation='bilinear')[source]
Crop the input image with given location and resize it.
- Parameters:
x (int) – Left boundary of the cropping area
y (int) – Top boundary of the cropping area
width (int) – Width of the cropping area
height (int) – Height of the cropping area
size (list or int) – resize to new size after cropping
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CropResizeTransform(x, y, width, height, size, interpolation='bilinear')[source]
Crop the input image with given location and resize it.
- Parameters:
x (int) – Left boundary of the cropping area
y (int) – Top boundary of the cropping area
width (int) – Width of the cropping area
height (int) – Height of the cropping area
size (list or int) – resize to new size after cropping
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’, ‘bicubic’
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.CenterCropTransform(size)[source]
Crops the given image at the center to the given size.
- Parameters:
size (list or int) – Size of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.MXNetNormalizeTransform(mean=[0.0], std=[1.0])[source]
Normalize a image with mean and standard deviation.
- Parameters:
mean (list, default=[0.0]) – means for each channel, if len(mean)=1, mean will be broadcasted to each channel, otherwise its length should be same with the length of image shape
std (list, default=[1.0]) – stds for each channel, if len(std)=1, std will be broadcasted to each channel, otherwise its length should be same with the length of image shape
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.PyTorchNormalizeTransform(mean=[0.0], std=[1.0])[source]
Normalize a image with mean and standard deviation.
- Parameters:
mean (list, default=[0.0]) – means for each channel, if len(mean)=1, mean will be broadcasted to each channel, otherwise its length should be same with the length of image shape
std (list, default=[1.0]) – stds for each channel, if len(std)=1, std will be broadcasted to each channel, otherwise its length should be same with the length of image shape
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.NormalizeTransform(mean=[0.0], std=[1.0])[source]
Normalize a image with mean and standard deviation.
- Parameters:
mean (list, default=[0.0]) – means for each channel, if len(mean)=1, mean will be broadcasted to each channel, otherwise its length should be same with the length of image shape
std (list, default=[1.0]) – stds for each channel, if len(std)=1, std will be broadcasted to each channel, otherwise its length should be same with the length of image shape
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomCropTransform(size)[source]
Crop the image at a random location to the given size.
- Parameters:
size (list or tuple or int) – Size of the result
- Returns:
tuple of processed image and label
- class neural_compressor.data.transforms.transform.RandomResizedCropTransform(size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation='bilinear')[source]
Crop the given image to random size and aspect ratio.
- Parameters:
size (list or int) – Size of the result
scale (tuple or list, default=(0.08, 1.0)) – range of size of the origin size cropped
ratio (tuple or list, default=(3. / 4., 4. / 3.)) – range of aspect ratio of the origin aspect ratio cropped
interpolation (str, default='bilinear') – Desired interpolation type, support ‘bilinear’, ‘nearest’
- Returns:
tuple of processed image and label
- neural_compressor.data.transforms.transform.get_final_text(pred_text, orig_text, do_lower_case)[source]
Project the tokenized prediction back to the original text.
- class neural_compressor.data.transforms.transform.SquadExample(qas_id, question_text, doc_tokens, orig_answer_text=None, start_position=None, end_position=None, is_impossible=False)[source]
A single training/test example for simple sequence classification.
For examples without an answer, the start and end position are -1.
- class neural_compressor.data.transforms.transform.InputFeatures(unique_id, example_index, doc_span_index, tokens, token_to_orig_map, token_is_max_context, input_ids, input_mask, segment_ids, start_position=None, end_position=None, is_impossible=None)[source]
A single set of features of data.
- neural_compressor.data.transforms.transform.read_squad_examples(input_file)[source]
Read a SQuAD json file into a list of SquadExample.
- neural_compressor.data.transforms.transform.convert_examples_to_features(examples, tokenizer, max_seq_length, doc_stride, max_query_length, output_fn)[source]
Load a data file into a list of `InputBatch`s.
- class neural_compressor.data.transforms.transform.CollectTransform(length=10833)[source]
Postprocess the predictions, collect data.
- class neural_compressor.data.transforms.transform.TFSquadV1PostTransform(label_file, vocab_file, n_best_size=20, max_seq_length=384, max_query_length=64, max_answer_length=30, do_lower_case=True, doc_stride=128)[source]
Postprocess the predictions of bert on SQuAD.
- Parameters:
label_file (str) – path of label file
vocab_file (str) – path of vocabulary file
n_best_size (int, default=20) – The total number of n-best predictions to generate in nbest_predictions.json
max_seq_length (int, default=384) – The maximum total input sequence length after WordPiece tokenization. Sequences longer than this will be truncated, shorter than this will be padded
max_query_length (int, default=64) – The maximum number of tokens for the question. Questions longer than this will be truncated to this length
max_answer_length (int, default=30) – The maximum length of an answer that can be generated. This is needed because the start and end predictions are not conditioned on one another
do_lower_case (bool, default=True) – Whether to lower case the input text. Should be True for uncased models and False for cased models
doc_stride (int, default=128) – When splitting up a long document into chunks, how much stride to take between chunks
- Returns:
tuple of processed prediction and label
- class neural_compressor.data.transforms.transform.TFModelZooCollectTransform(length=10833)[source]
Postprocess the predictions of model zoo, collect data.
- class neural_compressor.data.transforms.transform.TFSquadV1ModelZooPostTransform(label_file, vocab_file, n_best_size=20, max_seq_length=384, max_query_length=64, max_answer_length=30, do_lower_case=True, doc_stride=128)[source]
Postprocess the predictions of bert on SQuADV1.1.
See class TFSquadV1PostTransform for more details