MindSpore实现图像分类之数据处理部分
1、首先需要准备好本次演示使用的CIFAR-10数据集,可从MindSpore官网教程的实现图片分类页面中下载,数据集简介如下。
2、进入本次正题,处理数据集,下面是具体的操作说明和代码实现
先将数据预加载出来和预处理
加载数据集
数据加载可以通过内置数据集格式Cifar10Dataset接口完成。
cifar_ds = ds.Cifar10Dataset(data_home)
数据增强
数据增强主要是对数据进行归一化和丰富数据样本数量,调用map方法在图片上执行增强操作:
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
random_crop_op = C.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
random_horizontal_op = C.RandomHorizontalFlip()
resize_op = C.Resize((resize_height, resize_width)) # interpolation default BILINEAR
rescale_op = C.Rescale(rescale, shift)
normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
changeswap_op = C.HWC2CHW()
type_cast_op = C2.TypeCast(mstype.int32)
c_trans = []
if training:
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
cifar_ds = cifar_ds.map(operations=type_cast_op, input_columns="label")
cifar_ds = cifar_ds.map(operations=c_trans, input_columns="image")
数据混洗和批处理,可以增强模型的鲁棒性。
# apply shuffle operations
cifar_ds = cifar_ds.shuffle(buffer_size=10)
# apply batch operations
cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True)
# apply repeat operations
cifar_ds = cifar_ds.repeat(repeat_num)
3、欢迎大家参考,如有错误欢迎留言纠正哦,谢谢。