《昇思25天学习打卡营第05天|数据变换Transforms》

数据变换Transforms

  • 环境准备
# 实验环境已经预装了mindspore==2.2.14,如需更换mindspore版本,可更改下面mindspore的版本号
!pip uninstall mindspore -y
!pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore==2.2.14
import numpy as np
from PIL import Image
from download import download
from mindspore.dataset import transforms, vision, text
from mindspore.dataset import GeneratorDataset, MnistDataset

Common Transforms
Compose

  • Compose 接收一个数据增强操作序列,然后将其组合成单个数据增强操作。基于Mnist数据集呈现Transforms的应用效果
    #下载开源数据集
      url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/" \
      "notebook/datasets/MNIST_Data.zip"
      
      path = download(url, "./", kind="zip", replace=True)
      train_dataset = MnistDataset('MNIST_Data/train')
      image, label = next(train_dataset.create_tuple_iterator())
      print(image.shape)
      #output (28, 28, 1)
      	composed = transforms.Compose([
      	vision.Rescale(1.0 / 255.0, 0),
        vision.Normalize(mean=(0.1307,), std=(0.3081,)),
        vision.HWC2CHW()
        ])
        train_dataset = train_dataset.map(composed, 'image')
    	image, label = next(train_dataset.create_tuple_iterator())
    	print(image.shape)
    	#output(1, 28, 28)
    

Vision Transforms

  • Rescale
    • 调整图像像素值的大小,包括两个参数:
      • rescale: 缩放因子
      • shift: 平移因子
      • 输出像素值为 o u t p u t i = i n p u t i ∗ r e s c a l e + s h i f t output_i=input_i * rescale + shift outputi=inputirescale+shift
      random_np = np.random.randint(0, 255, (48, 48), np.uint8)
      random_image = Image.fromarray(random_np)
      rescale = vision.Rescale(1.0 / 255.0, 0)
      rescaled_image = rescale(random_image)
      
  • Normalize
    • 对输入图像的归一化,包括三个参数:
      • mean: 图像每个通道的均值。
      • std: 图像每个通道的标准差。
      • is_hwc: bool值,输入图像的格式。True为(height, width, channel), False为(channel, height, width)。
      • 图像通道根据meanstd进行调整,计算公式 o u t p u t c = i n p u t c − m e a n c s t d c , c output_c = \frac{input_c - mean_c}{std_c}, c outputc=stdcinputcmeanc,c 代表通道索引。
      normalize = vision.Normalize(mean=(0.1307,), std=(0.3081,))
      normalized_image = normalize(rescaled_image)
      
  • HWC2CHW
    HWC2CHW 变换用于转换图像格式。
    hwc_image = np.expand_dims(normalized_image, -1)
    hwc2chw = vision.HWC2CHW()
    chw_image = hwc2chw(hwc_image)
    print(hwc_image.shape, chw_image.shape)
    #Output (48, 48, 1)(1, 48, 48)
    

Text Transforms
文本数据需要有分词(Tokenize)、构建词表、Token转Index等操作。

	text = ['Welcome to Beijing']
	text_dataset = GeneratorDataset(texts, 'text')
  • PythonTokenizer 分词器
    def my_tokenizer(content):
        return content.split()
    
    test_dataset = test_dataset.map(text.PythonTokenizer(my_tokenizer))
    print(next(test_dataset.create_tuple_iterator())
    #Output [Tensor(shape=[3], dtype=String, value= ['Welcome', 'to', 'Beijing'])] 
    
  • Lookup
    Lookup为词表映射变换,用来将Token转化为index。
    • 前置条件: 需要构造词表
      	vocab = text.Vocab.from_dataset(test_dataset)
      	print(vocab.vocab())
      	#Output {'to': 2, 'Welcome': 1, 'Beijing': 0}
      	test_dataset = test_dataset.map(text.Lookup(vocab))
      	print(next(test_dataset.create_tuple_iterator()))
      	#Output [Tensor(shape=[3], dtype=Int32, value= [1, 2, 0])]
      
  • Lambda Transforms
    • 加载任意定义的Lambda函数
      test_dataset = GeneratorDataset([1, 2, 3], 'data', shuffle=False)
      test_dataset = test_dataset.map(lambda x: x * 2)
      
      def func(x):
      	return x * x + 2
      test_dataset = test_dataset.map(lambda x: func(x))
      
  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值