mindspore.dataset.vision.Decode

查看源文件
class mindspore.dataset.vision.Decode(to_pil=False)[源代码]

将输入的压缩图像解码为RGB格式。当前支持的图片类型:JPEG、BMP、PNG、TIFF、GIF(需要指定 to_pil=True)、WEBP(需要指定 to_pil=True)。

支持 Ascend 硬件加速,需要通过 .device(“Ascend”) 方式开启。

参数:
  • to_pil (bool,可选) - 是否将图像解码为PIL数据类型。若为 True ,图像将被解码为PIL数据类型,否则解码为NumPy数据类型。默认值: False

异常:
  • RuntimeError - 如果输入图像不是一维序列。

  • RuntimeError - 如果输入数据不是合法的图像字节数据。

  • RuntimeError - 如果输入数据已经是解码的图像数据。

支持平台:

CPU Ascend

样例:

>>> import os
>>> import numpy as np
>>> from PIL import Image, ImageDraw
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> class MyDataset:
...     def __init__(self):
...         self.data = []
...         img = Image.new("RGB", (300, 300), (255, 255, 255))
...         draw = ImageDraw.Draw(img)
...         draw.ellipse(((0, 0), (100, 100)), fill=(255, 0, 0), outline=(255, 0, 0), width=5)
...         img.save("./1.jpg")
...         data = np.fromfile("./1.jpg", np.uint8)
...         self.data.append(data)
...
...     def __getitem__(self, index):
...         return self.data[0]
...
...     def __len__(self):
...         return 5
>>>
>>> my_dataset = MyDataset()
>>> generator_dataset = ds.GeneratorDataset(my_dataset, column_names="image")
>>> transforms_list = [vision.Decode(), vision.RandomHorizontalFlip()]
>>> generator_dataset = generator_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in generator_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(300, 300, 3) uint8
>>> os.remove("./1.jpg")
>>>
>>> # Use the transform in eager mode
>>> img = Image.new("RGB", (300, 300), (255, 255, 255))
>>> draw = ImageDraw.Draw(img)
>>> draw.polygon([(50, 50), (150, 50), (100, 150)], fill=(0, 255, 0), outline=(0, 255, 0))
>>> img.save("./2.jpg")
>>> data = np.fromfile("./2.jpg", np.uint8)
>>> output = vision.Decode()(data)
>>> print(output.shape, output.dtype)
(300, 300, 3) uint8
>>> os.remove("./2.jpg")
教程样例:
device(device_target='CPU')[源代码]

指定该变换执行的设备。

  • 当执行设备是 Ascend 时,输入数据仅支持 uint8 类型。

参数:
  • device_target (str, 可选) - 算子将在指定的设备上运行。当前支持 CPUAscend 。默认值: CPU

异常:
  • TypeError - 当 device_target 的类型不为str。

  • ValueError - 当 device_target 的取值不为 CPU / Ascend

支持平台:

CPU Ascend

样例:

>>> import os
>>> import numpy as np
>>> from PIL import Image, ImageDraw
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.vision import Inter
>>>
>>> # Use the transform in dataset pipeline mode
>>> class MyDataset:
...     def __init__(self):
...         self.data = []
...         img = Image.new("RGB", (300, 300), (255, 255, 255))
...         draw = ImageDraw.Draw(img)
...         draw.ellipse(((0, 0), (100, 100)), fill=(255, 0, 0), outline=(255, 0, 0), width=5)
...         img.save("./1.jpg")
...         data = np.fromfile("./1.jpg", np.uint8)
...         self.data.append(data)
...
...     def __getitem__(self, index):
...         return self.data[0]
...
...     def __len__(self):
...         return 5
>>>
>>> my_dataset = MyDataset()
>>> generator_dataset = ds.GeneratorDataset(my_dataset, column_names="image")
>>> decode_op = vision.Decode().device("Ascend")
>>> resize_op = vision.Resize([100, 75], Inter.BICUBIC)
>>> transforms_list = [decode_op, resize_op]
>>> generator_dataset = generator_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in generator_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 75, 3) uint8
>>> os.remove("./1.jpg")
>>>
>>> # Use the transform in eager mode
>>> img = Image.new("RGB", (300, 300), (255, 255, 255))
>>> draw = ImageDraw.Draw(img)
>>> draw.polygon([(50, 50), (150, 50), (100, 150)], fill=(0, 255, 0), outline=(0, 255, 0))
>>> img.save("./2.jpg")
>>> data = np.fromfile("./2.jpg", np.uint8)
>>> output = vision.Decode().device("Ascend")(data)
>>> print(output.shape, output.dtype)
(300, 300, 3) uint8
>>> os.remove("./2.jpg")