MindSpore实现图像分类之数据处理部分
1、首先需要准备好本次演示使用的CIFAR-10数据集,可从MindSpore官网教程的实现图片分类页面中下载,数据集简介如下。
2、进入本次正题,处理数据集,下面是具体的操作说明和代码实现先将数据预加载出来和预处理加载数据集数据加载可以通过内置数据集格式潮贾篡绐Cifar10Dataset接口完成。cifar_ds = ds.Cifar10Dataset(data_home)数据增强数据增强主要是对数据进行归一化和丰富数据样本数量,调用map方法在图片上执行增强操作:resize_height = 224resize_width = 224rescale = 1.0 / 255.0shift = 0.0# define map operationsrandom_crop_op = C.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANTrandom_horizontal_op = C.RandomHorizontalFlip()resize_op = C.Resize((resize_height, resize_width)) # interpolation default BILINEARrescale_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 imagescifar_ds = cifar_ds.map(operations=type_cast_op, input_columns="label")cifar_ds = cifar_ds.map(operations=c_trans, input_columns="image")数据混洗和批处理,可以增强模型的鲁棒性。# apply shuffle operationscifar_ds = cifar_ds.shuffle(buffer_size=10)# apply batch operationscifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True)# apply repeat operationscifar_ds = cifar_ds.repeat(repeat_num)
3、欢迎大家参考,如有错误欢迎留言纠正哦,谢谢。