文档反馈

问题文档片段

问题文档片段包含公式时,显示为空格。

提交类型
issue

有点复杂...

找人问问吧。

PR

小问题,全程线上修改...

一键搞定!

请选择提交类型

问题类型
规范和低错类

- 规范和低错类:

- 错别字或拼写错误,标点符号使用错误、公式错误或显示异常。

- 链接错误、空单元格、格式错误。

- 英文中包含中文字符。

- 界面和描述不一致,但不影响操作。

- 表述不通顺,但不影响理解。

- 版本号不匹配:如软件包名称、界面版本号。

易用性

- 易用性:

- 关键步骤错误或缺失,无法指导用户完成任务。

- 缺少主要功能描述、关键词解释、必要前提条件、注意事项等。

- 描述内容存在歧义指代不明、上下文矛盾。

- 逻辑不清晰,该分类、分项、分步骤的没有给出。

正确性

- 正确性:

- 技术原理、功能、支持平台、参数类型、异常报错等描述和软件实现不一致。

- 原理图、架构图等存在错误。

- 命令、命令参数等错误。

- 代码片段错误。

- 命令无法完成对应功能。

- 界面错误,无法指导操作。

- 代码样例运行报错、运行结果不符。

风险提示

- 风险提示:

- 对重要数据或系统存在风险的操作,缺少安全提示。

内容合规

- 内容合规:

- 违反法律法规,涉及政治、领土主权等敏感词。

- 内容侵权。

请选择问题类型

问题描述

点击输入详细问题描述,以帮助我们快速定位问题。

轻量化数据处理

下载Notebook下载样例代码查看源文件

在资源条件允许的情况下,为了追求更高的性能,一般使用数据管道模式执行数据增强算子。

基于数据管道模式执行的最大特点是需要定义map算子,如下图中将ResizeCropHWC2CHW算子交由map算子调度,由其负责启动和执行给定的数据增强算子,对数据管道的数据进行映射变换。

pipelinemode1

虽然构建数据管道可以批量处理输入数据,但是数据管道的API设计要求用户从构建输入源开始,逐步定义数据管道中的各个处理算子,仅当在定义map的时候才会涉及与用户输入数据高度相关的数据增强算子。

无疑,用户只想重点关注这些与其相关度最高的代码,但其他相关度较低的代码却在整个代码场景中为用户增加了不必要的负担。

因此,MindSpore提供了一种轻量化的数据处理执行方式,称为Eager模式。

在Eager模式下,执行数据增强算子不需要依赖构建数据管道map,而是以函数式调用的方式执行数据增强算子。因此代码编写会更为简洁且能立即执行得到运行结果,推荐在小型数据增强实验、模型推理等轻量化场景中使用。

eagermode1

MindSpore目前支持在Eager模式执行各种数据增强算子,具体如下所示,更多数据增强算子参见API文档。

  • vision模块

    • 子模块transforms,基于OpenCV/Pillow实现的图像增强算子。

  • text模块

    • 子模块transforms,基于Jieba/ICU4C等库实现的文本处理算子。

  • transforms模块

    • 子模块transforms,基于C++/Python/NumPy实现的通用数据增强算子。

Eager模式

下面将简要介绍数据增强各模块算子的Eager模式使用方法。使用Eager模式,只需要将数据增强算子本身当成可执行函数即可。

数据准备

以下示例代码将图片数据下载到指定位置。

[ ]:
import os
import requests

requests.packages.urllib3.disable_warnings()

def download_dataset(dataset_url, path):
    filename = dataset_url.split("/")[-1]
    save_path = os.path.join(path, filename)
    if os.path.exists(save_path):
        return
    if not os.path.exists(path):
        os.makedirs(path)
    res = requests.get(dataset_url, stream=True, verify=False)
    with open(save_path, "wb") as f:
        for chunk in res.iter_content(chunk_size=512):
            if chunk:
                f.write(chunk)
    print("The {} file is downloaded and saved in the path {} after processing".format(os.path.basename(dataset_url), path))

download_dataset("https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/banana.jpg", ".")

vision

此示例将使用mindspore.dataset.vision模块中的算子,对给定图像进行变换。

您仅需要关注使用何种数据增强算子,而不需要关注数据管道的任何代码。

vision算子的Eager模式支持numpy.arrayPIL.Image类型的数据作为入参。

[1]:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import mindspore.dataset.vision as vision

img_ori = Image.open("banana.jpg").convert("RGB")
print("Image.type: {}, Image.shape: {}".format(type(img_ori), img_ori.size))

# Define a Resize op and execute it immediately
op1 = vision.Resize(size=(320))
img = op1(img_ori)
print("Image.type: {}, Image.shape: {}".format(type(img), img.size))

# Define a CenterCrop op and execute it immediately
op2 = vision.CenterCrop((280, 280))
img = op2(img)
print("Image.type: {}, Image.shape: {}".format(type(img), img.size))

# Define a Pad op and execute it immediately
op3 = vision.Pad(40)
img = op3(img)
print("Image.type: {}, Image.shape: {}".format(type(img), img.size))

# Show the result
plt.subplot(1, 2, 1)
plt.imshow(img_ori)
plt.title("original image")
plt.subplot(1, 2, 2)
plt.imshow(img)
plt.title("transformed image")
plt.show()
Image.type: <class 'PIL.Image.Image'>, Image.shape: (356, 200)
Image.type: <class 'PIL.Image.Image'>, Image.shape: (569, 320)
Image.type: <class 'PIL.Image.Image'>, Image.shape: (280, 280)
Image.type: <class 'PIL.Image.Image'>, Image.shape: (360, 360)
../_images/dataset_eager_7_1.svg

text

此示例将使用text模块中tranforms的算子,对给定文本进行变换。

text算子的Eager模式支持numpy.array类型数据的作为入参。

[2]:
import mindspore.dataset.text.transforms as text
import mindspore as ms

# Define a WhitespaceTokenizer op and execute it immediately
txt = "Welcome to Beijing !"
txt = text.WhitespaceTokenizer()(txt)
print("Tokenize result: {}".format(txt))

# Define a ToNumber op and execute it immediately
txt = ["123456"]
to_number = text.ToNumber(ms.int32)
txt = to_number(txt)
print("ToNumber result: {}, type: {}".format(txt, type(txt[0])))

Tokenize result: ['Welcome' 'to' 'Beijing' '!']
ToNumber result: [123456], type: <class 'numpy.int32'>

transforms

此示例将使用transforms模块中c_tranforms的的算子,对给定数据进行变换。

transforms算子的Eager模式支持numpy.array类型的数据作为入参。

[3]:
import numpy as np
import mindspore.dataset.transforms as trans

# Define a Fill op and execute it immediately
data = np.array([1, 2, 3, 4, 5])
fill = trans.Fill(0)
data = fill(data)
print("Fill result: ", data)

# Define a OneHot op and execute it immediately
label = np.array(2)
onehot = trans.OneHot(num_classes=5)
label = onehot(label)
print("OneHot result: ", label)
Fill result:  [0 0 0 0 0]
OneHot result:  [0 0 1 0 0]