mindspore.dataset.vision.Crop

View Source On Gitee
class mindspore.dataset.vision.Crop(coordinates, size)[source]

Crop the input image at a specific location.

Supports Ascend hardware acceleration and can be enabled through the .device(“Ascend”) method.

Parameters
  • coordinates (sequence) – Coordinates of the upper left corner of the cropping image. Must be a sequence of two values, in the form of (top, left).

  • 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, it should be (height, width). The size value(s) must be larger than 0.

Raises
  • TypeError – If coordinates is not of type sequence.

  • TypeError – If size is not of type integer or sequence.

  • ValueError – If coordinates is less than 0.

  • 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

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> crop_op = vision.Crop((0, 0), 32)
>>> transforms_list = [crop_op]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(32, 32, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], dtype=np.uint8).reshape((2, 2, 3))
>>> output = vision.Crop((0, 0), 1)(data)
>>> print(output.shape, output.dtype)
(1, 1, 3) uint8
Tutorial Examples:
device(device_target='CPU')[source]

Set the device for the current operator execution.

  • When the device is Ascend, input/output shape should be limited from [4, 6] to [32768, 32768].

Parameters

device_target (str, optional) – The operator will be executed on this device. Currently supports CPU and Ascend . Default: CPU .

Raises
  • TypeError – If device_target is not of type str.

  • ValueError – If device_target is not within the valid set of [‘CPU’, ‘Ascend’].

Supported Platforms:

CPU Ascend

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> crop_op = vision.Crop((0, 0), (100, 75)).device("Ascend")
>>> transforms_list = [crop_op]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 75, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.Crop((0, 0), 64).device("Ascend")(data)
>>> print(output.shape, output.dtype)
(64, 64, 3) uint8
Tutorial Examples: