[{"data":1,"prerenderedAt":244},["ShallowReactive",2],{"content-query-KIMNjxtyAO":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":238,"_id":239,"_source":240,"_file":241,"_stem":242,"_extension":243},"/technology-blogs/zh/1917","zh",false,"","【MindSpore易点通】应用实践系列—ResNet50图像分类精讲（连载二）","选用的CIFAR-100数据集进行处理","2022-10-17","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/10/24/03598fe70b5f4b23aff3eb91b07a972c.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":235},"root",[17,25,31,36,41,46,55,60,65,76,84,89,94,99,109,114,119,124,129,169,174,179,184,188,193,201,209,217,222,230],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore易点通应用实践系列resnet50图像分类精讲连载二",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"分析完网络，接下来就是数据了。",{"type":18,"tag":26,"props":32,"children":33},{},[34],{"type":24,"value":35},"官网教程用的是CIFAR-10数据集，既然是应用实践，10个类别有点少了，就选用CIFAR-100。",{"type":18,"tag":26,"props":37,"children":38},{},[39],{"type":24,"value":40},"CIFAR-100和CIFAR-10的数据结构上基本一致。",{"type":18,"tag":26,"props":42,"children":43},{},[44],{"type":24,"value":45},"相比于CIFAR-10，CIFAR-100多了一个超类，但是实际上这个分类并没有在训练中用到。",{"type":18,"tag":26,"props":47,"children":48},{},[49],{"type":18,"tag":50,"props":51,"children":54},"img",{"alt":52,"src":53},"cke_8191.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/001/057/1f0/09dd3443210010571f0ac01982dd2c01.20221003112914.17602958904007333297531229822579:50531023084239:2400:C347B5EB35A351262B211CFE58FC3A84E4757ED8F6F8ED980CE7E3F9B0054B1D.png",[],{"type":18,"tag":26,"props":56,"children":57},{},[58],{"type":24,"value":59},"CIFAR-100 python version",{"type":18,"tag":26,"props":61,"children":62},{},[63],{"type":24,"value":64},"下载地址：",{"type":18,"tag":26,"props":66,"children":67},{},[68],{"type":18,"tag":69,"props":70,"children":74},"a",{"href":71,"rel":72},"http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz",[73],"nofollow",[75],{"type":24,"value":71},{"type":18,"tag":26,"props":77,"children":78},{},[79],{"type":18,"tag":50,"props":80,"children":83},{"alt":81,"src":82},"cke_11576.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/001/057/1f0/09dd3443210010571f0ac01982dd2c01.20221003112954.82752466383549817599202999191428:50531023084239:2400:66F3CCA67696151F4F96DA69162E678FE9A9821677C3F379A14F0BFF3A595A07.png",[],{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"meta是index对应的具体的名称。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":24,"value":93},"train是训练集",{"type":18,"tag":26,"props":95,"children":96},{},[97],{"type":24,"value":98},"test是测试集",{"type":18,"tag":100,"props":101,"children":103},"pre",{"code":102},"from mindvision.classification.dataset import Cifar100\n\n# 数据集根目录\ndata_dir = \"./dataset\"\n\ndataset_train = Cifar100(path=data_dir, split='train', batch_size=6, resize=32, download=False)\nds_train = dataset_train.run()\nstep_size = ds_train.get_dataset_size()\nprint(ds_train.get_dataset_size())\ndataset_val = Cifar100(path=data_dir, split='test', batch_size=6, resize=32, download=False)\nds_val = dataset_val.run()\nprint(ds_val.get_dataset_size())\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndata = next(ds_train.create_dict_iterator())\n\nimages = data[\"image\"].asnumpy()\nlabels = data[\"label\"].asnumpy()\nprint(f\"Image shape: {images.shape}, Label: {labels}\")\n\nplt.figure()\nfor i in range(1, 7):\n    plt.subplot(2, 3, i)\n    image_trans = np.transpose(images[i - 1], (1, 2, 0))\n    mean = np.array([0.4914, 0.4822, 0.4465])\n    std = np.array([0.2023, 0.1994, 0.2010])\n    image_trans = std * image_trans + mean\n    image_trans = np.clip(image_trans, 0, 1)\n    plt.title(f\"{dataset_train.index2label[labels[i - 1]]}\")\n    plt.imshow(image_trans)\n    plt.axis(\"off\")\nplt.show()\n",[104],{"type":18,"tag":105,"props":106,"children":107},"code",{"__ignoreMap":7},[108],{"type":24,"value":102},{"type":18,"tag":26,"props":110,"children":111},{},[112],{"type":24,"value":113},"上面是样例代码，里面直接用到了mindvision里面的Cifar100。",{"type":18,"tag":26,"props":115,"children":116},{},[117],{"type":24,"value":118},"如果设置download=True可以直接下载相关数据集。我这边因为事先下载的数据集，所以设定为False。",{"type":18,"tag":26,"props":120,"children":121},{},[122],{"type":24,"value":123},"mindvision封装了数据读取，我们可以自己看下如何读取文件。",{"type":18,"tag":26,"props":125,"children":126},{},[127],{"type":24,"value":128},"如下是Cifar10的数据：",{"type":18,"tag":130,"props":131,"children":132},"ul",{},[133,145],{"type":18,"tag":134,"props":135,"children":136},"li",{},[137,143],{"type":18,"tag":138,"props":139,"children":140},"strong",{},[141],{"type":24,"value":142},"data",{"type":24,"value":144}," -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.",{"type":18,"tag":134,"props":146,"children":147},{},[148,153,155,161,163,167],{"type":18,"tag":138,"props":149,"children":150},{},[151],{"type":24,"value":152},"labels",{"type":24,"value":154}," -- a list of 10000 numbers in the range 0-9. The number at index ",{"type":18,"tag":156,"props":157,"children":158},"em",{},[159],{"type":24,"value":160},"i",{"type":24,"value":162}," indicates the label of the _i_th image in the array ",{"type":18,"tag":138,"props":164,"children":165},{},[166],{"type":24,"value":142},{"type":24,"value":168},".",{"type":18,"tag":26,"props":170,"children":171},{},[172],{"type":24,"value":173},"The binary version of the CIFAR-100 is just like the binary version of the CIFAR-10, except that each image has two label bytes (coarse and fine) and 3072 pixel bytes, so the binary files look like this:",{"type":18,"tag":26,"props":175,"children":176},{},[177],{"type":24,"value":178},"\u003C1 x coarse label>\u003C1 x fine label>\u003C3072 x pixel>",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"...",{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":178},{"type":18,"tag":26,"props":189,"children":190},{},[191],{"type":24,"value":192},"3072 个点，分别是RGB对应的值，刚好等于3*32*32.",{"type":18,"tag":100,"props":194,"children":196},{"code":195},"import numpy as np\nimport matplotlib.pyplot as plt\ndef unpickle(file):\n    import pickle\n    with open(file, 'rb') as fo:\n        dict = pickle.load(fo, encoding='bytes')\n    return dict\na = unpickle(r'./dataset/cifar-100-python/train')\nlables = unpickle(r'./dataset/cifar-100-python/meta')\nprint(a.keys())\nprint(lables.keys())\nimg = a[b'data'][0]\nfine_label = a[b'fine_labels'][0]\ncoarse_label = a[b'coarse_labels'][0]\nprint(fine_label)\nprint(coarse_label)\nprint(lables[b'fine_label_names'][fine_label])\nprint(lables[b'coarse_label_names'][coarse_label])\n\nimgrgb = img.reshape(3, 32, 32)\nimgrgb = np.transpose(imgrgb, (1, 2, 0))\n\nplt.imshow(imgrgb)\nplt.show()\n",[197],{"type":18,"tag":105,"props":198,"children":199},{"__ignoreMap":7},[200],{"type":24,"value":195},{"type":18,"tag":26,"props":202,"children":203},{},[204],{"type":18,"tag":50,"props":205,"children":208},{"alt":206,"src":207},"cke_29938.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/001/057/1f0/09dd3443210010571f0ac01982dd2c01.20221003113317.55132637011497184407149701969887:50531023084239:2400:F65DB89214BCB64C5A2AE40D5B1A047819C3D2F00DA5F19298BAE45855D79DD1.png",[],{"type":18,"tag":26,"props":210,"children":211},{},[212],{"type":18,"tag":50,"props":213,"children":216},{"alt":214,"src":215},"cke_35085.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/001/057/1f0/09dd3443210010571f0ac01982dd2c01.20221003113338.84049296440078052602481222887673:50531023084239:2400:5EC865A17972393142101EB106CFAA6C1657BF1BEC6EF17513079C326D438C59.png",[],{"type":18,"tag":26,"props":218,"children":219},{},[220],{"type":24,"value":221},"32*32 放大了人都没法识别了，缩小了稍微好点。",{"type":18,"tag":26,"props":223,"children":224},{},[225],{"type":18,"tag":50,"props":226,"children":229},{"alt":227,"src":228},"cke_44862.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/001/057/1f0/09dd3443210010571f0ac01982dd2c01.20221003113353.45680248457800829657199878532289:50531023084239:2400:04CB33E0EEA907C6C3F518B691BDB28EB2A596BD1B791E7F284C0EF1D7BBC5C7.png",[],{"type":18,"tag":26,"props":231,"children":232},{},[233],{"type":24,"value":234},"以上就是数据集处理。",{"title":7,"searchDepth":236,"depth":236,"links":237},4,[],"markdown","content:technology-blogs:zh:1917.md","content","technology-blogs/zh/1917.md","technology-blogs/zh/1917","md",1776506117021]