tinyms.vision

This module is to support vision augmentations. transforms is a high performance image augmentation module which is developed with C++ OpenCV.

class tinyms.vision.ImageViewer(image, label=None)[source]

ImageViewer is a class defined for visualizing the input image.

Parameters:
  • image (Union[PIL.Image, numpy.ndarray]) – image input.

  • label (str, optional) – specifies the label of this image. Default: None.

Raises:

TypeError – When image input is not numpy.ndarray or PIL.Image.

draw(pred_res, labels)[source]

Draw the predicting boxes on the picture and show the visualized picture.

Parameters:
  • pred_res (dict) – The predcition result from tinyms.serving.predict method.

  • labels (list) – The labels should be input manually with a list of string. This argument is required for distinguishing the color from different classes.

Note

This function is only valid when being called in interactive environment, such like Jupyter notebook.

Examples

>>> form PIL import Image
>>>
>>> img = Image.open('example.jpg')
>>> img_viewer = ImageViewer(img)
>>> labels = ['1', '2', '3']
>>> img_viewer.draw(pred_res, labels)
show()[source]

Directly show the visualized picture.

Note

This function is only valid when being called in interactive environment, such like Jupyter notebook.

Examples

>>> form PIL import Image
>>>
>>> img = Image.open('example.jpg')
>>> img_viewer = ImageViewer(img, label='cat')
>>> img_viewer.show()
class tinyms.vision.Inter[source]

Interpolation Modes.

Possible enumeration values are: Inter.NEAREST, Inter.ANTIALIAS, Inter.LINEAR, Inter.BILINEAR, Inter.CUBIC, Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC.

  • Inter.NEAREST: means interpolation method is nearest-neighbor interpolation.

  • Inter.ANTIALIAS: means the interpolation method is antialias interpolation.

  • Inter.LINEAR: means interpolation method is bilinear interpolation, here is the same as Inter.BILINEAR.

  • Inter.BILINEAR: means interpolation method is bilinear interpolation.

  • Inter.CUBIC: means the interpolation method is bicubic interpolation, here is the same as Inter.BICUBIC.

  • Inter.BICUBIC: means the interpolation method is bicubic interpolation.

  • Inter.AREA: means interpolation method is pixel area interpolation.

  • Inter.PILCUBIC: means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

class tinyms.vision.Border[source]

Padding Mode, Border Type.

Possible enumeration values are: Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC.

  • Border.CONSTANT: means it fills the border with constant values.

  • Border.EDGE: means it pads with the last value on the edge.

  • Border.REFLECT: means it reflects the values on the edge omitting the last value of edge. For example, padding [1,2,3,4] with 2 elements on both sides will result in [3,2,1,2,3,4,3,2].

  • Border.SYMMETRIC: means it reflects the values on the edge repeating the last value of edge. For example, padding [1,2,3,4] with 2 elements on both sides will result in [2,1,1,2,3,4,4,3].

Note

This class derived from class str to support json serializable.

class tinyms.vision.ImageBatchFormat[source]

Data Format of images after batch operation.

Possible enumeration values are: ImageBatchFormat.NHWC, ImageBatchFormat.NCHW.

  • ImageBatchFormat.NHWC: in orders like, batch N, height H, width W, channels C to store the data.

  • ImageBatchFormat.NCHW: in orders like, batch N, channels C, height H, width W to store the data.

tinyms.vision.ssd_bboxes_encode(boxes)[source]

Labels anchors with ground truth inputs.

Parameters:

boxes (numpy.ndarray) – Ground truth with shape [N, 5], for each row, it stores [ymin, xmin, ymax, xmax, cls].

Returns:

numpy.ndarray, location ground truth with shape [num_anchors, 4]. numpy.ndarray, class ground truth with shape [num_anchors, 1]. numpy.ndarray, number of positives in an image.

tinyms.vision.ssd_bboxes_filter(boxes, box_scores, image_shape)[source]

Filter predict boxes with minimum score and nms threshold.

Parameters:
  • boxes (numpy.ndarray) – Ground truth with shape [N, 4], for each row, it stores [ymin, xmin, ymax, xmax].

  • box_scores (numpy.ndarray) – Class scores with shape [N, 21].

  • image_shape (tuple) – Shape of original image with the format [h, w].

Returns:

list[list[float]], ground truth with shape [N, 4], for each row, it stores [ymin, xmin, ymax, xmax]. list[list[float]], class scores with shape [N, 21]. list[list[int]], class label with shape [N, 21].

tinyms.vision.coco_eval(pred_data, anno_file)[source]

Calculate mAP of predicted bboxes.

class tinyms.vision.MnistTransform(configs=None)[source]

Mnist dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in Mnist-style.

Outputs:

numpy.ndarray, transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import MnistTransform
>>>
>>> mnist_transform = MnistTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = mnist_transform(img)
apply_ds(mnist_ds, repeat_size=1, batch_size=32, num_parallel_workers=None)[source]

Apply preprocess operation on MnistDataset instance.

Parameters:
  • mnist_ds (data.MnistDataset) – MnistDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

Returns:

data.MnistDataset, the preprocessed MnistDataset instance.

Examples

>>> from tinyms.vision import MnistTransform
>>>
>>> mnist_transform = MnistTransform()
>>> mnist_ds = mnist_transform.apply_ds(mnist_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

Parameters:
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

Returns:

str, the postprocess result.

class tinyms.vision.Cifar10Transform(configs=None)[source]

Cifar10 dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in Cifar10-style.

Outputs:

numpy.ndarray, Transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import Cifar10Transform
>>>
>>> cifar10_transform = Cifar10Transform()
>>> img = Image.open('object_detection.jpg')
>>> img = cifar10_transform(img)

“””

apply_ds(cifar10_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[source]

Apply preprocess operation on Cifar10Dataset instance.

Parameters:
  • cifar10_ds (data.Cifar10Dataset) – Cifar10Dataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

Returns:

data.Cifar10Dataset, the preprocessed Cifar10Dataset instance.

Examples

>>> from tinyms.vision import Cifar10Transform
>>>
>>> cifar10_transform = Cifar10Transform()
>>> cifar10_ds = cifar10_transform.apply_ds(cifar10_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

Parameters:
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

Returns:

str, the postprocess result.

class tinyms.vision.ImageFolderTransform(configs=None)[source]

ImageFolder dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in ImageFolder-style.

Outputs:

numpy.ndarray, transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import ImageFolderTransform
>>>
>>> imagefolder_transform = ImageFolderTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = imagefolder_transform(img)
apply_ds(imagefolder_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[source]

Apply preprocess operation on ImageFolderDataset instance.

Parameters:
  • cifar10_ds (data.ImageFolderDataset) – ImageFolderDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

Returns:

data.ImageFolderDataset, the preprocessed ImageFolderDataset instance.

Examples

>>> from tinyms.vision import ImageFolderTransform
>>>
>>> imagefolder_transform = ImageFolderTransform()
>>> imagefolder_ds = imagefolder_transform.apply_ds(imagefolder_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

Parameters:
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

Returns:

str, the postprocess result.

class tinyms.vision.VOCTransform(configs=None)[source]

VOC dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in VOC-style.

Outputs:

numpy.ndarray, transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import VOCTransform
>>>
>>> voc_transform = VOCTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = voc_transform(img)
apply_ds(voc_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[source]

Apply preprocess operation on VOCDataset instance.

Parameters:
  • voc_ds (data.VOCDataset) – VOCDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

Returns:

data.VOCDataset, the preprocessed VOCDataset instance.

Examples

>>> from tinyms.vision import VOCTransform
>>>
>>> VOC_transform = VOCTransform()
>>> voc_ds = voc_transform.apply_ds(voc_ds)
postprocess(input, image_shape, strategy='TOP1_CLASS')[source]

Apply postprocess operation for prediction result.

Parameters:
  • input (numpy.ndarray) – Prediction result.

  • image_shape (tuple) – Image shape.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

Returns:

dict, the postprocess result.

class tinyms.vision.ShanshuiTransform(configs=None)[source]

Shanshui dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in VOC-style.

Outputs:

numpy.ndarray, transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import ShanshuiTransform
>>>
>>> shanshui_transform = ShanshuiTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = shanshui_transform(img)
apply_ds(voc_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)

Apply preprocess operation on VOCDataset instance.

Parameters:
  • voc_ds (data.VOCDataset) – VOCDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

Returns:

data.VOCDataset, the preprocessed VOCDataset instance.

Examples

>>> from tinyms.vision import VOCTransform
>>>
>>> VOC_transform = VOCTransform()
>>> voc_ds = voc_transform.apply_ds(voc_ds)
postprocess(input, image_shape, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

Parameters:
  • input (numpy.ndarray) – Prediction result.

  • image_shape (tuple) – Image shape.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

Returns:

dict, the postprocess result.

class tinyms.vision.CycleGanDatasetTransform(configs=None)[source]

CycleGan dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in city_scape.

Outputs:

numpy.ndarray, transformed image.

Examples

>>> from PIL import Image
>>> from tinyms.vision import CycleGanDatasetTransform
>>>
>>> cyclegan_transform = CycleGanDatasetTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = cyclegan_transform(img)
apply_ds(gan_generator_ds, repeat_size=1, batch_size=1, num_parallel_workers=1, shuffle=True, phase='train')[source]

Apply preprocess operation on GeneratorDataset instance.

Parameters:
  • gan_generator_ds (data.GeneratorDataset) – GeneratorDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: 1.

  • shuffle (bool) – Specifies if applying shuffle operation. Default: True.

  • phase (str) – Specifies the current phase. Default: train.

Returns:

data.GeneratorDataset, the preprocessed GeneratorDataset instance.

Examples

>>> from tinyms.vision import CycleGanDatasetTransform
>>>
>>> cyclegan_transform = CycleGanDatasetTransform()
>>> gan_generator_ds = cyclegan_transform.apply_ds(gan_generator_ds)
Raises:

TypeError – If gan_generator_ds is not instance of GeneratorDataset.

class tinyms.vision.AutoContrast(**kwargs)[source]

Apply automatic contrast on input image. This operation calculates histogram of image, reassign cutoff percent of the lightest pixels from histogram to 255, and reassign cutoff percent of the darkest pixels from histogram to 0.

Parameters:
  • cutoff (float, optional) – Percent of lightest and darkest pixels to cut off from the histogram of input image. The value must be in the range [0.0, 50.0). Default: 0.0.

  • ignore (Union[int, sequence], optional) – The background pixel values to ignore, The ignore values must be in range [0, 255]. Default: None.

Raises:
  • TypeError – If cutoff is not of type float.

  • TypeError – If ignore is not of type int or sequence.

  • ValueError – If cutoff is not in range [0, 50.0).

  • ValueError – If ignore is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.AutoContrast(cutoff=10.0, ignore=[10, 20])]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.BoundingBoxAugment(**kwargs)[source]

Apply a given image processing operation on a random selection of bounding box regions of a given image.

Parameters:
  • transform (TensorOperation) – C++ transformation operation to be applied on random selection of bounding box regions of a given image.

  • ratio (float, optional) – Ratio of bounding boxes to apply augmentation on. Range: [0.0, 1.0]. Default: 0.3.

Raises:
  • TypeError – If transform is not an image processing operation in mindspore.dataset.vision.c_transforms .

  • TypeError – If ratio is not of type float.

  • ValueError – If ratio is not in range [0.0, 1.0].

  • RuntimeError – If given bounding box is invalid.

Supported Platforms:

CPU

Examples

>>> # set bounding box operation with ratio of 1 to apply rotation on all bounding boxes
>>> bbox_aug_op = c_vision.BoundingBoxAugment(c_vision.RandomRotation(90), 1)
>>> # map to apply ops
>>> image_folder_dataset = image_folder_dataset.map(operations=[bbox_aug_op],
...                                                 input_columns=["image", "bbox"],
...                                                 output_columns=["image", "bbox"])
class tinyms.vision.CenterCrop(**kwargs)[source]

Crop the input image at the center to the given size. If input image size is smaller than output size, input image will be padded with 0 before cropping.

Parameters:

size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, an image of size (height, width) will be cropped. The size value(s) must be larger than 0.

Raises:
  • TypeError – If size is not of type int or sequence.

  • ValueError – If size is less than or equal to 0.

  • RuntimeError – If given tensor shape is not <H, W> or <…, H, W, C>.

Supported Platforms:

CPU

Examples

>>> # crop image to a square
>>> transforms_list1 = [c_vision.Decode(), c_vision.CenterCrop(50)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # crop image to portrait style
>>> transforms_list2 = [c_vision.Decode(), c_vision.CenterCrop((60, 40))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.CutMixBatch(**kwargs)[source]

Apply CutMix transformation on input batch of images and labels. Note that you need to make labels into one-hot format and batched before calling this operation.

Parameters:
  • image_batch_format (ImageBatchFormat) – The method of padding. Can be any of [ImageBatchFormat.NHWC, ImageBatchFormat.NCHW].

  • alpha (float, optional) – Hyperparameter of beta distribution, must be larger than 0. Default: 1.0.

  • prob (float, optional) – The probability by which CutMix is applied to each image, range: [0, 1]. Default: 1.0.

Raises:
  • TypeError – If image_batch_format is not of type mindspore.dataset.vision.ImageBatchFormat .

  • TypeError – If alpha is not of type float.

  • TypeError – If prob is not of type float.

  • ValueError – If alpha is less than or equal 0.

  • ValueError – If prob is not in range [0, 1].

  • RuntimeError – If given tensor shape is not <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import ImageBatchFormat
>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> image_folder_dataset= image_folder_dataset.map(operations=onehot_op,
...                                                input_columns=["label"])
>>> cutmix_batch_op = c_vision.CutMixBatch(ImageBatchFormat.NHWC, 1.0, 0.5)
>>> image_folder_dataset = image_folder_dataset.batch(5)
>>> image_folder_dataset = image_folder_dataset.map(operations=cutmix_batch_op,
...                                                 input_columns=["image", "label"])
class tinyms.vision.CutOut(**kwargs)[source]

Randomly cut (mask) out a given number of square patches from the input image array.

Parameters:
  • length (int) – The side length of each square patch, must be larger than 0.

  • num_patches (int, optional) – Number of patches to be cut out of an image, must be larger than 0. Default: 1.

Raises:
  • TypeError – If length is not of type int.

  • TypeError – If num_patches is not of type int.

  • ValueError – If length is less than or equal 0.

  • ValueError – If num_patches is less than or equal 0.

  • RuntimeError – If given tensor shape is not <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.CutOut(80, num_patches=10)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Decode(**kwargs)[source]

Decode the input image.

Parameters:

rgb (bool, optional) – Mode of decoding input image. Default: True. If True means format of decoded image is RGB else BGR (deprecated).

Raises:
  • RuntimeError – If rgb is False, since this option is deprecated.

  • RuntimeError – If given tensor is not a 1D sequence.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Equalize(**kwargs)[source]

Apply histogram equalization on input image.

Raises:

RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.Equalize()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Grayscale(**kwargs)[source]

Convert the input PIL Image to grayscale.

Parameters:

num_output_channels (int) – The number of channels desired for the output image, must be 1 or 3. If 3 is provided, the returned image will have 3 identical RGB channels. Default: 1.

Raises:
  • TypeError – If num_output_channels is not of type int.

  • ValueError – If num_output_channels is not 1 or 3.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.transforms.py_transforms import Compose
>>>
>>> transforms_list = Compose([py_vision.Decode(),
...                            py_vision.Grayscale(3),
...                            py_vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")
classmethod from_json(json_string)

Base from_json for Python tensor operations class

to_json()

Base to_json for Python tensor operations class

class tinyms.vision.HWC2CHW(**kwargs)[source]

Transpose the input image from shape (H, W, C) to (C, H, W). If the input image is of shape <H, W>, it will remain unchanged.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Raises:

RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

Ascend GPU CPU

Examples

>>> transforms_list = [c_vision.Decode(),
...                    c_vision.RandomHorizontalFlip(0.75),
...                    c_vision.RandomCrop(512),
...                    c_vision.HWC2CHW()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Invert(**kwargs)[source]

Apply invert on input image in RGB mode. This operation will reassign every pixel to (255 - pixel).

Raises:

RuntimeError – If given tensor shape is not <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.Invert()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.MixUpBatch(**kwargs)[source]

Apply MixUp transformation on input batch of images and labels. Each image is multiplied by a random weight (lambda) and then added to a randomly selected image from the batch multiplied by (1 - lambda). The same formula is also applied to the one-hot labels.

The lambda is generated based on the specified alpha value. Two coefficients x1, x2 are randomly generated in the range [alpha, 1], and lambda = (x1 / (x1 + x2)).

Note that you need to make labels into one-hot format and batched before calling this operation.

Parameters:

alpha (float, optional) – Hyperparameter of beta distribution. The value must be positive. Default: 1.0.

Raises:
  • TypeError – If alpha is not of type float.

  • ValueError – If alpha is not positive.

  • RuntimeError – If given tensor shape is not <N, H, W, C> or <N, C, H, W>.

Supported Platforms:

CPU

Examples

>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> image_folder_dataset= image_folder_dataset.map(operations=onehot_op,
...                                                input_columns=["label"])
>>> mixup_batch_op = c_vision.MixUpBatch(alpha=0.9)
>>> image_folder_dataset = image_folder_dataset.batch(5)
>>> image_folder_dataset = image_folder_dataset.map(operations=mixup_batch_op,
...                                                 input_columns=["image", "label"])
class tinyms.vision.Normalize(**kwargs)[source]

Normalize the input image with respect to mean and standard deviation. This operation will normalize the input image with: output[channel] = (input[channel] - mean[channel]) / std[channel], where channel >= 1.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:
  • mean (sequence) – List or tuple of mean values for each channel, with respect to channel order. The mean values must be in range [0.0, 255.0].

  • std (sequence) – List or tuple of standard deviations for each channel, with respect to channel order. The standard deviation values must be in range (0.0, 255.0].

Raises:
  • TypeError – If mean is not of type sequence.

  • TypeError – If std is not of type sequence.

  • ValueError – If mean is not in range [0.0, 255.0].

  • ValueError – If std is not in range (0.0, 255.0].

  • RuntimeError – If given tensor shape is not <H, W> or <…,H, W, C>.

Supported Platforms:

Ascend GPU CPU

Examples

>>> decode_op = c_vision.Decode()
>>> normalize_op = c_vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0])
>>> transforms_list = [decode_op, normalize_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Pad(**kwargs)[source]

Pad the image.

Parameters:
  • padding (Union[int, Sequence[tuple]]) – The number of pixels to pad each border of the image. If a single number is provided, it pads all borders with this value. If a tuple or lists of 2 values are provided, it pads the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, it pads the left, top, right and bottom respectively. The pad values must be non-negative.

  • fill_value (Union[int, tuple[int]], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255]. Default: 0.

  • padding_mode (Border, optional) –

    The method of padding. Default: Border.CONSTANT. Can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

Note

The behavior when padding is a sequence of length 2 will change from padding left/top with the first value and right/bottom with the second, to padding left/right with the first one and top/bottom with the second in the future. Or you can pass in a 4-element sequence to specify left, top, right and bottom respectively.

Raises:
  • TypeError – If padding is not of type int or Sequence[int].

  • TypeError – If fill_value is not of type int or tuple[int].

  • TypeError – If padding_mode is not of type mindspore.dataset.vision.Border .

  • ValueError – If padding is negative.

  • ValueError – If fill_value is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.Pad([100, 100, 100, 100])]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
tinyms.vision.PILRandomHorizontalFlip

alias of mindspore.dataset.vision.py_transforms.RandomHorizontalFlip

class tinyms.vision.RandomAffine(**kwargs)[source]

Apply Random affine transformation to the input image.

Parameters:
  • degrees (Union[int, float, sequence]) – Range of the rotation degrees. If degrees is a number, the range will be (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • translate (sequence, optional) – Sequence (tx_min, tx_max, ty_min, ty_max) of minimum/maximum translation in x(horizontal) and y(vertical) directions, range [-1.0, 1.0]. Default: None. The horizontal and vertical shift is selected randomly from the range: (tx_min*width, tx_max*width) and (ty_min*height, ty_max*height), respectively. If a tuple or list of size 2, then a translate parallel to the X axis in the range of (translate[0], translate[1]) is applied. If a tuple or list of size 4, then a translate parallel to the X axis in the range of (translate[0], translate[1]) and a translate parallel to the Y axis in the range of (translate[2], translate[3]) are applied. If None, no translation is applied.

  • scale (sequence, optional) – Scaling factor interval, which must be non negative. Default: None, original scale is used.

  • shear (Union[int, float, sequence], optional) – Range of shear factor, which must be positive. Default: None. If a number, then a shear parallel to the X axis in the range of (-shear, +shear) is applied. If a tuple or list of size 2, then a shear parallel to the X axis in the range of (shear[0], shear[1]) is applied. If a tuple or list of size 4, then a shear parallel to X axis in the range of (shear[0], shear[1]) and a shear parallel to Y axis in the range of (shear[2], shear[3]) is applied. If None, no shear is applied.

  • resample (Inter, optional) –

    An optional resampling filter. Default: Inter.NEAREST. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.AREA].

    • Inter.BILINEAR, means resample method is bilinear interpolation.

    • Inter.NEAREST, means resample method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means resample method is bicubic interpolation.

    • Inter.AREA, means resample method is pixel area interpolation.

  • fill_value (Union[int, tuple[int]], optional) – Optional fill_value to fill the area outside the transform in the output image. There must be three elements in tuple and the value of single element is [0, 255]. Default: 0, filling is performed.

Raises:
  • TypeError – If degrees is not of type int, float or sequence.

  • TypeError – If translate is not of type sequence.

  • TypeError – If scale is not of type sequence.

  • TypeError – If shear is not of type int, float or sequence.

  • TypeError – If resample is not of type mindspore.dataset.vision.Inter .

  • TypeError – If fill_value is not of type int or tuple[int].

  • ValueError – If degrees is negative.

  • ValueError – If translate is not in range [-1.0, 1.0].

  • ValueError – If scale is negative.

  • ValueError – If shear is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> random_affine_op = c_vision.RandomAffine(degrees=15,
...                                          translate=(-0.1, 0.1, 0, 0),
...                                          scale=(0.9, 1.1),
...                                          resample=Inter.NEAREST)
>>> transforms_list = [decode_op, random_affine_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomColor(**kwargs)[source]

Adjust the color of the input image by a fixed or random degree. This operation works only with 3-channel RGB images.

Parameters:

degrees (Sequence[float], optional) – Range of random color adjustment degrees, which must be non-negative. It should be in (min, max) format. If min=max, then it is a single fixed magnitude operation. Default: (0.1, 1.9).

Raises:
Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomColor((0.5, 2.0))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomColorAdjust(**kwargs)[source]

Randomly adjust the brightness, contrast, saturation, and hue of the input image.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:
  • brightness (Union[float, Sequence[float]], optional) – Brightness adjustment factor. Default: (1, 1). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness), 1+brightness]. If it is a sequence, it should be [min, max] for the range.

  • contrast (Union[float, Sequence[float]], optional) – Contrast adjustment factor. Default: (1, 1). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast), 1+contrast]. If it is a sequence, it should be [min, max] for the range.

  • saturation (Union[float, Sequence[float]], optional) – Saturation adjustment factor. Default: (1, 1). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation), 1+saturation]. If it is a sequence, it should be [min, max] for the range.

  • hue (Union[float, Sequence[float]], optional) – Hue adjustment factor. Default: (0, 0). If it is a float, the range will be [-hue, hue]. Value should be 0 <= hue <= 0.5. If it is a sequence, it should be [min, max] where -0.5 <= min <= max <= 0.5.

Raises:
  • TypeError – If brightness is not of type float or Sequence[float].

  • TypeError – If contrast is not of type float or Sequence[float].

  • TypeError – If saturation is not of type float or Sequence[float].

  • TypeError – If hue is not of type float or Sequence[float].

  • ValueError – If brightness is negative.

  • ValueError – If contrast is negative.

  • ValueError – If saturation is negative.

  • ValueError – If hue is not in range [-0.5, 0.5].

  • RuntimeError – If given tensor shape is not <H, W, C>.

Supported Platforms:

Ascend GPU CPU

Examples

>>> decode_op = c_vision.Decode()
>>> transform_op = c_vision.RandomColorAdjust(brightness=(0.5, 1),
...                                           contrast=(0.4, 1),
...                                           saturation=(0.3, 1))
>>> transforms_list = [decode_op, transform_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCrop(**kwargs)[source]

Crop the input image at a random location. If input image size is smaller than output size, input image will be padded before cropping.

Note

If the input image is more than one, then make sure that the image size is the same.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the cropped image. The size value(s) must be positive. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • padding (Union[int, Sequence[int]], optional) – The number of pixels to pad each border of the image. The padding value(s) must be non-negative. Default: None. If padding is not None, pad image first with padding values. If a single number is provided, pad all borders with this value. If a tuple or lists of 2 values are provided, pad the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size. Default: False.

  • fill_value (Union[int, tuple[int]], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255]. Default: 0.

  • padding_mode (Border, optional) –

    The method of padding. Default: Border.CONSTANT. It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

Note

The behavior when padding is a sequence of length 2 will change from padding left/top with the first value and right/bottom with the second, to padding left/right with the first one and top/bottom with the second in the future. Or you can pass in a 4-element sequence to specify left, top, right and bottom respectively.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If padding is not of type int or Sequence[int].

  • TypeError – If pad_if_needed is not of type boolean.

  • TypeError – If fill_value is not of type int or tuple[int].

  • TypeError – If padding_mode is not of type mindspore.dataset.vision.Border .

  • ValueError – If size is not positive.

  • ValueError – If padding is negative.

  • ValueError – If fill_value is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <…, H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Border
>>> decode_op = c_vision.Decode()
>>> random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=Border.EDGE)
>>> transforms_list = [decode_op, random_crop_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCropDecodeResize(**kwargs)[source]

A combination of Crop , Decode and Resize . It will get better performance for JPEG images. This operation will crop the input image at a random location, decode the cropped image in RGB mode, and resize the decoded image.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • scale (Union[list, tuple], optional) – Range [min, max) of respective size of the original size to be cropped, which must be non-negative. Default: (0.08, 1.0).

  • ratio (Union[list, tuple], optional) – Range [min, max) of aspect ratio to be cropped, which must be non-negative. Default: (3. / 4., 4. / 3.).

  • interpolation (Inter, optional) –

    Image interpolation mode for resize operation. Default: Inter.BILINEAR. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

    • Inter.AREA, means interpolation method is pixel area interpolation.

    • Inter.PILCUBIC, means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area. Default: 10. If exceeded, fall back to use center_crop instead. The max_attempts value must be positive.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If scale is not of type tuple or list.

  • TypeError – If ratio is not of type tuple or list.

  • TypeError – If interpolation is not of type mindspore.dataset.vision.Inter .

  • TypeError – If max_attempts is not of type int.

  • ValueError – If size is not positive.

  • ValueError – If scale is negative.

  • ValueError – If ratio is negative.

  • ValueError – If max_attempts is not positive.

  • RuntimeError – If given tensor is not a 1D sequence.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> resize_crop_decode_op = c_vision.RandomCropDecodeResize(size=(50, 75),
...                                                         scale=(0.25, 0.5),
...                                                         interpolation=Inter.NEAREST,
...                                                         max_attempts=5)
>>> transforms_list = [resize_crop_decode_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCropWithBBox(**kwargs)[source]

Crop the input image at a random location and adjust bounding boxes accordingly.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the cropped image. The size value(s) must be positive. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • padding (Union[int, Sequence[int]], optional) – The number of pixels to pad the image The padding value(s) must be non-negative. Default: None. If padding is not None, first pad image with padding values. If a single number is provided, pad all borders with this value. If a tuple or lists of 2 values are provided, pad the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size. Default: False.

  • fill_value (Union[int, tuple[int]], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255]. Default: 0.

  • padding_mode (Border, optional) –

    The method of padding. Default: Border.CONSTANT. It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

Note

The behavior when padding is a sequence of length 2 will change from padding left/top with the first value and right/bottom with the second, to padding left/right with the first one and top/bottom with the second in the future. Or you can pass in a 4-element sequence to specify left, top, right and bottom respectively.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If padding is not of type int or Sequence[int].

  • TypeError – If pad_if_needed is not of type boolean.

  • TypeError – If fill_value is not of type int or tuple[int].

  • TypeError – If padding_mode is not of type mindspore.dataset.vision.Border .

  • ValueError – If size is not positive.

  • ValueError – If padding is negative.

  • ValueError – If fill_value is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> decode_op = c_vision.Decode()
>>> random_crop_with_bbox_op = c_vision.RandomCropWithBBox([512, 512], [200, 200, 200, 200])
>>> transforms_list = [decode_op, random_crop_with_bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomHorizontalFlip(**kwargs)[source]

Randomly flip the input image horizontally with a given probability.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:

prob (float, optional) – Probability of the image being flipped, which must be in range of [0, 1]. Default: 0.5.

Raises:
  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0, 1].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

Ascend GPU CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip(0.75)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomHorizontalFlipWithBBox(**kwargs)[source]

Flip the input image horizontally randomly with a given probability and adjust bounding boxes accordingly.

Parameters:

prob (float, optional) – Probability of the image being flipped, which must be in range of [0, 1]. Default: 0.5.

Raises:
  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0, 1].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlipWithBBox(0.70)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomPosterize(**kwargs)[source]

Reduce the number of bits for each color channel to posterize the input image randomly with a given probability.

Parameters:

bits (Union[int, Sequence[int]], optional) – Range of random posterize to compress image. Bits values must be in range of [1,8], and include at least one integer value in the given range. It must be in (min, max) or integer format. If min=max, then it is a single fixed magnitude operation. Default: (8, 8).

Raises:
  • TypeError – If bits is not of type int or sequence of int.

  • ValueError – If bits is not in range [1, 8].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomPosterize((6, 8))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResize(**kwargs)[source]

Resize the input image using a randomly selected interpolation mode.

Parameters:

size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, a square of size (size, size) will be cropped with this value. If size is a sequence of length 2, an image of size (height, width) will be cropped.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> # randomly resize image, keeping aspect ratio
>>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResize(50)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # randomly resize image to landscape style
>>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResize((40, 60))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.RandomResizedCrop(**kwargs)[source]

This operation will crop the input image randomly, and resize the cropped image using a selected interpolation mode.

Note

If the input image is more than one, then make sure that the image size is the same.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, a square of size (size, size) will be cropped with this value. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • scale (Union[list, tuple], optional) – Range [min, max) of respective size of the original size to be cropped, which must be non-negative. Default: (0.08, 1.0).

  • ratio (Union[list, tuple], optional) – Range [min, max) of aspect ratio to be cropped, which must be non-negative. Default: (3. / 4., 4. / 3.).

  • interpolation (Inter, optional) –

    Method of interpolation. Default: Inter.BILINEAR. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

    • Inter.AREA, means interpolation method is pixel area interpolation.

    • Inter.PILCUBIC, means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area. Default: 10. If exceeded, fall back to use center_crop instead.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If scale is not of type tuple or list.

  • TypeError – If ratio is not of type tuple or list.

  • TypeError – If interpolation is not of type mindspore.dataset.vision.Inter .

  • TypeError – If max_attempts is not of type int.

  • ValueError – If size is not positive.

  • ValueError – If scale is negative.

  • ValueError – If ratio is negative.

  • ValueError – If max_attempts is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <…, H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> resize_crop_op = c_vision.RandomResizedCrop(size=(50, 75), scale=(0.25, 0.5),
...                                             interpolation=Inter.BILINEAR)
>>> transforms_list = [decode_op, resize_crop_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResizedCropWithBBox(**kwargs)[source]

Crop the input image to a random size and aspect ratio and adjust bounding boxes accordingly.

Parameters:
  • size (Union[int, Sequence[int]]) – The size of the output image. The size value(s) must be positive. If size is an integer, a square of size (size, size) will be cropped with this value. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • scale (Union[list, tuple], optional) – Range (min, max) of respective size of the original size to be cropped, which must be non-negative. Default: (0.08, 1.0).

  • ratio (Union[list, tuple], optional) – Range (min, max) of aspect ratio to be cropped, which must be non-negative. Default: (3. / 4., 4. / 3.).

  • interpolation (Inter mode, optional) –

    Method of interpolation. Default: Inter.BILINEAR. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC] .

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop area. Default: 10. If exceeded, fall back to use center crop instead.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If scale is not of type tuple or list.

  • TypeError – If ratio is not of type tuple or list.

  • TypeError – If interpolation is not of type mindspore.dataset.vision.Inter .

  • TypeError – If max_attempts is not of type int.

  • ValueError – If size is not positive.

  • ValueError – If scale is negative.

  • ValueError – If ratio is negative.

  • ValueError – If max_attempts is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> bbox_op = c_vision.RandomResizedCropWithBBox(size=50, interpolation=Inter.NEAREST)
>>> transforms_list = [decode_op, bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResizeWithBBox(**kwargs)[source]

Tensor operation to resize the input image using a randomly selected interpolation mode and adjust bounding boxes accordingly.

Parameters:

size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, a square of size (size, size) will be cropped with this value. If size is a sequence of length 2, an image of size (height, width) will be cropped.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> # randomly resize image with bounding boxes, keeping aspect ratio
>>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResizeWithBBox(60)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # randomly resize image with bounding boxes to portrait style
>>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResizeWithBBox((80, 60))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.RandomRotation(**kwargs)[source]

Rotate the input image randomly within a specified range of degrees.

Parameters:
  • degrees (Union[int, float, sequence]) – Range of random rotation degrees. If degrees is a number, the range will be converted to (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • resample (Inter, optional) –

    An optional resampling filter. Default: Inter.NEAREST. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.AREA].

    • Inter.BILINEAR, means resample method is bilinear interpolation.

    • Inter.NEAREST, means resample method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means resample method is bicubic interpolation.

    • Inter.AREA: means the interpolation method is pixel area interpolation.

  • expand (bool, optional) – Optional expansion flag. Default: False. If set to True, expand the output image to make it large enough to hold the entire rotated image. If set to False or omitted, make the output image the same size as the input. Note that the expand flag assumes rotation around the center and no translation.

  • center (tuple, optional) – Optional center of rotation (a 2-tuple). Default: None. Origin is the top left corner. None sets to the center of the image.

  • fill_value (Union[int, tuple[int]], optional) – Optional fill color for the area outside the rotated image. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255]. Default: 0.

Raises:
  • TypeError – If degrees is not of type int, float or sequence.

  • TypeError – If resample is not of type mindspore.dataset.vision.Inter .

  • TypeError – If expand is not of type boolean.

  • TypeError – If center is not of type tuple.

  • TypeError – If fill_value is not of type int or tuple[int].

  • ValueError – If fill_value is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> transforms_list = [c_vision.Decode(),
...                    c_vision.RandomRotation(degrees=5.0,
...                    resample=Inter.NEAREST,
...                    expand=True)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomSelectSubpolicy(**kwargs)[source]

Choose a random sub-policy from a policy list to be applied on the input image.

Parameters:

policy (list[list[tuple[TensorOperation, float]]]) – List of sub-policies to choose from. A sub-policy is a list of tuple[operation, prob], where operation is a data processing operation and prob is the probability that this operation will be applied, and the prob values must be in range [0, 1]. Once a sub-policy is selected, each operation within the sub-policy with be applied in sequence according to its probability.

Raises:

TypeError – If policy contains invalid data processing operations.

Supported Platforms:

CPU

Examples

>>> policy = [[(c_vision.RandomRotation((45, 45)), 0.5),
...            (c_vision.RandomVerticalFlip(), 1),
...            (c_vision.RandomColorAdjust(), 0.8)],
...           [(c_vision.RandomRotation((90, 90)), 1),
...            (c_vision.RandomColorAdjust(), 0.2)]]
>>> image_folder_dataset = image_folder_dataset.map(operations=c_vision.RandomSelectSubpolicy(policy),
...                                                 input_columns=["image"])
class tinyms.vision.RandomSharpness(**kwargs)[source]

Adjust the sharpness of the input image by a fixed or random degree. Degree of 0.0 gives a blurred image, degree of 1.0 gives the original image, and degree of 2.0 gives a sharpened image.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:

degrees (Union[list, tuple], optional) – Range of random sharpness adjustment degrees, which must be non-negative. It should be in (min, max) format. If min=max, then it is a single fixed magnitude operation. Default: (0.1, 1.9).

Raises:
  • TypeError – If degrees is not of type list or tuple.

  • ValueError – If degrees is negative.

  • ValueError – If degrees is in (max, min) format instead of (min, max).

Supported Platforms:

Ascend GPU CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomSharpness(degrees=(0.2, 1.9))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomSolarize(**kwargs)[source]

Randomly selects a subrange within the specified threshold range and sets the pixel value within the subrange to (255 - pixel).

Parameters:

threshold (tuple, optional) – Range of random solarize threshold. Default: (0, 255). Threshold values should always be in (min, max) format, where min and max are integers in the range [0, 255], and min <= max. If min=max, then invert all pixel values above min(max).

Raises:
  • TypeError – If threshold is not of type tuple.

  • ValueError – If threshold is not in range of [0, 255].

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomSolarize(threshold=(10,100))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomVerticalFlip(**kwargs)[source]

Randomly flip the input image vertically with a given probability.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:

prob (float, optional) – Probability of the image being flipped. Default: 0.5.

Raises:
  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0, 1].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

Ascend GPU CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlip(0.25)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomVerticalFlipWithBBox(**kwargs)[source]

Flip the input image vertically, randomly with a given probability and adjust bounding boxes accordingly.

Parameters:

prob (float, optional) – Probability of the image being flipped. Default: 0.5.

Raises:
  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0, 1].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlipWithBBox(0.20)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Rescale(**kwargs)[source]

Rescale the input image with the given rescale and shift. This operation will rescale the input image with: output = image * rescale + shift.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:
  • rescale (float) – Rescale factor.

  • shift (float) – Shift factor.

Raises:
  • TypeError – If rescale is not of type float.

  • TypeError – If shift is not of type float.

Supported Platforms:

Ascend GPU CPU

Examples

>>> transforms_list = [c_vision.Decode(), c_vision.Rescale(1.0 / 255.0, -1.0)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Resize(**kwargs)[source]

Resize the input image to the given size with a given interpolation mode.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, the smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

  • interpolation (Inter, optional) –

    Image interpolation mode. Default: Inter.LINEAR. It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC].

    • Inter.LINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

    • Inter.AREA, means interpolation method is pixel area interpolation.

    • Inter.PILCUBIC, means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If interpolation is not of type mindspore.dataset.vision.Inter .

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> resize_op = c_vision.Resize([100, 75], Inter.BICUBIC)
>>> transforms_list = [decode_op, resize_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.ResizeWithBBox(**kwargs)[source]

Resize the input image to the given size and adjust bounding boxes accordingly.

Parameters:
  • size (Union[int, Sequence[int]]) – The output size of the resized image. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

  • interpolation (Inter, optional) –

    Image interpolation mode. Default: Inter.LINEAR. It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.LINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • TypeError – If interpolation is not of type mindspore.dataset.vision.Inter .

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> bbox_op = c_vision.ResizeWithBBox(50, Inter.NEAREST)
>>> transforms_list = [decode_op, bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.SoftDvppDecodeRandomCropResizeJpeg(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), max_attempts=10)[source]

A combination of Crop , Decode and Resize using the simulation algorithm of Ascend series chip DVPP module.

The usage scenario is consistent with SoftDvppDecodeResizeJpeg. The input image size should be in range [32*32, 8192*8192]. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16]. Only images with an even resolution can be output. The output of odd resolution is not supported.

Note

SoftDvppDecodeRandomCropResizeJpeg is not supported as of 1.8 version. Please use RandomCropDecodeResize instead.

Parameters:
  • size (Union[int, Sequence[int]]) – The size of the output image. The size value(s) must be positive. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, an image of size (height, width) will be cropped.

  • scale (Union[list, tuple], optional) – Range [min, max) of respective size of the original size to be cropped, which must be non-negative. Default: (0.08, 1.0).

  • ratio (Union[list, tuple], optional) – Range [min, max) of aspect ratio to be cropped, which must be non-negative. Default: (3. / 4., 4. / 3.).

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area. Default: 10. If exceeded, fall back to use center_crop instead. The max_attempts value must be positive.

Raises:
Supported Platforms:

CPU

class tinyms.vision.SoftDvppDecodeResizeJpeg(size)[source]

Decode and resize JPEG image using the simulation algorithm of Ascend series chip DVPP module.

It is recommended to use this algorithm in the following scenarios: When training, the DVPP of the Ascend chip is not used, and the DVPP of the Ascend chip is used during inference, and the accuracy of inference is lower than the accuracy of training; and the input image size should be in range [32*32, 8192*8192]. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16]. Only images with an even resolution can be output. The output of odd resolution is not supported.

Note

SoftDvppDecodeResizeJpeg is not supported as of 1.8 version. Please use Decode and Resize instead.

Parameters:

size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, an image of size (height, width) will be cropped.

Raises:
  • TypeError – If size is not of type int or Sequence[int].

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor is not a 1D sequence.

Supported Platforms:

CPU

class tinyms.vision.UniformAugment(**kwargs)[source]

Perform randomly selected augmentation on input image.

Parameters:
  • transforms (TensorOperation) – C++ transformation operation to be applied on random selection of bounding box regions of a given image (Python operations are not accepted).

  • num_ops (int, optional) – Number of operations to be selected and applied, which must be positive. Default: 2.

Raises:
  • TypeError – If transform is not an image processing operation in mindspore.dataset.vision.c_transforms .

  • TypeError – If num_ops is not of type int.

  • ValueError – If num_ops is not positive.

Supported Platforms:

CPU

Examples

>>> import mindspore.dataset.vision.py_transforms as py_vision
>>> transforms_list = [c_vision.RandomHorizontalFlip(),
...                    c_vision.RandomVerticalFlip(),
...                    c_vision.RandomColorAdjust(),
...                    c_vision.RandomRotation(degrees=45)]
>>> uni_aug_op = c_vision.UniformAugment(transforms=transforms_list, num_ops=2)
>>> transforms_all = [c_vision.Decode(), c_vision.Resize(size=[224, 224]),
...                   uni_aug_op]
>>> image_folder_dataset_1 = image_folder_dataset.map(operations=transforms_all,
...                                                   input_columns="image",
...                                                   num_parallel_workers=1)
class tinyms.vision.Compose(**kwargs)[source]

Compose a list of transforms into a single transform.

Parameters:

transforms (list) – List of transformations to be applied.

Raises:
  • TypeError – If transforms is not of type list.

  • ValueError – If transforms is empty.

  • TypeError – If elements of transforms are neither Python callable objects nor data processing operations in c_transforms.

Supported Platforms:

CPU

Examples

>>> compose = c_transforms.Compose([c_vision.Decode(), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=compose)
class tinyms.vision.Concatenate(**kwargs)[source]

Tensor operation that concatenates all columns into a single tensor.

Parameters:
  • axis (int, optional) – Concatenate the tensors along given axis. Default: 0.

  • prepend (numpy.array, optional) – NumPy array to be prepended to the already concatenated tensors. Default: None.

  • append (numpy.array, optional) – NumPy array to be appended to the already concatenated tensors. Default: None.

Raises:
  • TypeError – If axis is not of type int.

  • TypeError – If prepend is not of type numpy.ndarray.

  • TypeError – If append is not of type numpy.ndarray.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> # concatenate string
>>> prepend_tensor = np.array(["dw", "df"], dtype='S')
>>> append_tensor = np.array(["dwsdf", "df"], dtype='S')
>>> concatenate_op = c_transforms.Concatenate(0, prepend_tensor, append_tensor)
>>> data = [["This","is","a","string"]]
>>> dataset = ds.NumpySlicesDataset(data)
>>> dataset = dataset.map(operations=concatenate_op)
class tinyms.vision.Duplicate(**kwargs)[source]

Duplicate the input tensor to output, only support transform one column each time.

Raises:

RuntimeError – If given tensor has two columns.

Supported Platforms:

CPU

Examples

>>> # Data before
>>> # |  x      |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1,2,3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["x"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Duplicate(),
...                                                 input_columns=["x"],
...                                                 output_columns=["x", "y"])
>>> # Data after
>>> # |  x      |  y      |
>>> # +---------+---------+
>>> # | [1,2,3] | [1,2,3] |
>>> # +---------+---------+
class tinyms.vision.Fill(**kwargs)[source]

Tensor operation to fill all elements in the tensor with the specified value. The output tensor will have the same shape and type as the input tensor.

Parameters:

fill_value (Union[str, bytes, int, float, bool]) – scalar value to fill the tensor with.

Raises:

TypeError – If fill_value is not of type str, float, bool, int or bytes.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> # generate a 1D integer numpy array from 0 to 4
>>> def generator_1d():
...     for i in range(5):
...         yield (np.array([i]),)
>>> generator_dataset = ds.GeneratorDataset(generator_1d, column_names="col1")
>>> # [[0], [1], [2], [3], [4]]
>>> fill_op = c_transforms.Fill(3)
>>> generator_dataset = generator_dataset.map(operations=fill_op)
>>> # [[3], [3], [3], [3], [3]]
class tinyms.vision.Mask(**kwargs)[source]

Mask content of the input tensor with the given predicate. Any element of the tensor that matches the predicate will be evaluated to True, otherwise False.

Parameters:
  • operator (Relational) – relational operators, it can be any of [Relational.EQ, Relational.NE, Relational.LT, Relational.GT, Relational.LE, Relational.GE], take Relational.EQ as example, EQ refers to equal.

  • constant (Union[str, int, float, bool]) – Constant to be compared to.

  • dtype (mindspore.dtype, optional) – Type of the generated mask. Default: mstype.bool_.

Raises:
  • TypeErroroperator is not of type Relational.

  • TypeErrorconstant is not of type string int, float or bool.

  • TypeErrordtype is not of type mindspore.dtype.

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.transforms.c_transforms import Relational
>>> # Data before
>>> # |  col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Mask(Relational.EQ, 2))
>>> # Data after
>>> # |       col         |
>>> # +--------------------+
>>> # | [False,True,False] |
>>> # +--------------------+
class tinyms.vision.OneHot(**kwargs)[source]

Tensor operation to apply one hot encoding.

Parameters:

num_classes (int) – Number of classes of objects in dataset. It should be larger than the largest label number in the dataset.

Raises:
Supported Platforms:

CPU

Examples

>>> # Assume that dataset has 10 classes, thus the label ranges from 0 to 9
>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> mnist_dataset = mnist_dataset.map(operations=onehot_op, input_columns=["label"])
class tinyms.vision.PadEnd(**kwargs)[source]

Pad input tensor according to pad_shape, input tensor needs to have same rank.

Parameters:
  • pad_shape (list(int)) – List of integers representing the shape needed. Dimensions that set to None will not be padded (i.e., original dim will be used). Shorter dimensions will truncate the values.

  • pad_value (Union[str, bytes, int, float, bool], optional) – Value used to pad. Default to 0 or empty string in case of tensors of strings.

Raises:
  • TypeError – If pad_shape is not of type list.

  • TypeError – If pad_value is not of type str, float, bool, int or bytes.

  • TypeError – If elements of pad_shape is not of type int.

  • ValueError – If elements of pad_shape is not of positive.

Supported Platforms:

CPU

Examples

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.PadEnd(pad_shape=[4],
...                                                                                pad_value=10))
>>> # Data after
>>> # |    col     |
>>> # +------------+
>>> # | [1,2,3,10] |
>>> # +------------|
class tinyms.vision.RandomApply(**kwargs)[source]

Randomly perform a series of transforms with a given probability.

Parameters:
  • transforms (list) – List of transformations to be applied.

  • prob (float, optional) – The probability to apply the transformation list. Default: 0.5.

Raises:
  • TypeError – If transforms is not of type list.

  • ValueError – If transforms is empty.

  • TypeError – If elements of transforms are neither Python callable objects nor data processing operations in c_transforms.

  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0.0, 1.0].

Supported Platforms:

CPU

Examples

>>> rand_apply = c_transforms.RandomApply([c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_apply)
class tinyms.vision.RandomChoice(**kwargs)[source]

Randomly select one transform from a list of transforms to perform operation.

Parameters:

transforms (list) – List of transformations to be chosen from to apply.

Raises:
  • TypeError – If transforms is not of type list.

  • ValueError – If transforms is empty.

  • TypeError – If elements of transforms are neither Python callable objects nor data processing operations in c_transforms.

Supported Platforms:

CPU

Examples

>>> rand_choice = c_transforms.RandomChoice([c_vision.CenterCrop(50), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_choice)
class tinyms.vision.Slice(**kwargs)[source]

Slice operation to extract a tensor out using the given n slices.

The functionality of Slice is similar to NumPy’s indexing feature (Currently only rank-1 tensors are supported).

Parameters:

slices (Union[int, list[int], slice, None, Ellipsis]) –

Maximum n number of arguments to slice a tensor of rank n . One object in slices can be one of:

  1. int: Slice this index only along the first dimension. Negative index is supported.

  2. list(int): Slice these indices along the first dimension. Negative indices are supported.

  3. slice: Slice the generated indices from the slice object along the first dimension. Similar to start:stop:step.

  4. None: Slice the whole dimension. Similar to [:] in Python indexing.

  5. Ellipsis: Slice the whole dimension, same result with None .

Raises:

TypeError – If slices is not of type int, list[int], slice , None or Ellipsis .

Supported Platforms:

CPU

Examples

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> # slice indices 1 and 2 only
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Slice(slice(1,3)))
>>> # Data after
>>> # |   col   |
>>> # +---------+
>>> # |  [2,3]  |
>>> # +---------|
class tinyms.vision.TypeCast(**kwargs)[source]

Tensor operation to cast to a given MindSpore data type.

Note

This operation supports running on Ascend or GPU platforms by Offload.

Parameters:

data_type (mindspore.dtype) – mindspore.dtype to be cast to.

Raises:

TypeError – If data_type is not of type bool, int, float or string.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import dtype as mstype
>>>
>>> # Generate 1d int numpy array from 0 - 63
>>> def generator_1d():
...     for i in range(64):
...         yield (np.array([i]),)
>>>
>>> dataset = ds.GeneratorDataset(generator_1d, column_names='col')
>>> type_cast_op = c_transforms.TypeCast(mstype.int32)
>>> dataset = dataset.map(operations=type_cast_op)
class tinyms.vision.Unique(**kwargs)[source]

Perform the unique operation on the input tensor, only support transform one column each time.

Return 3 tensor: unique output tensor, index tensor, count tensor.

  • Output tensor contains all the unique elements of the input tensor in the same order that they occur in the input tensor.

  • Index tensor that contains the index of each element of the input tensor in the unique output tensor.

  • Count tensor that contains the count of each element of the output tensor in the input tensor.

Note

Call batch op before calling this function.

Raises:

RuntimeError – If given Tensor has two columns.

Supported Platforms:

CPU

Examples

>>> # Data before
>>> # |  x                 |
>>> # +--------------------+
>>> # | [[0,1,2], [1,2,3]] |
>>> # +--------------------+
>>> data = [[[0,1,2], [1,2,3]]]
>>> dataset = ds.NumpySlicesDataset(data, ["x"])
>>> dataset = dataset.map(operations=c_transforms.Unique(),
...                       input_columns=["x"],
...                       output_columns=["x", "y", "z"])
>>> # Data after
>>> # |  x      |  y              |z        |
>>> # +---------+-----------------+---------+
>>> # | [0,1,2,3] | [0,1,2,1,2,3] | [1,2,2,1]
>>> # +---------+-----------------+---------+