ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cifar10数据集解压缩,按名字分文件夹

2022-04-07 21:31:53  阅读:79  来源: 互联网

标签:loc img cifar10 os 解压缩 label 文件夹 path data


描述

数据集来自http://www.cs.toronto.edu/~kriz/cifar.html python版,下载后解压缩:

然后在该目录下执行python,运行后效果:

代码

import pickle
import numpy as np
import os
import cv2

def unpickle(file):
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict

loc_1 = './train_cifar10'
loc_2 = './test_cifar10'
if not os.path.exists(loc_1):
    os.mkdir(loc_1)
if not os.path.exists(loc_2):
    os.mkdir(loc_2)

def unzip():
    meta = unpickle('batches.meta')
    label_names = meta[b'label_names']
    for i in label_names:
        dir1 = loc_1 + '/' + i.decode()
        dir2 = loc_2 + '/' + i.decode()
        if not os.path.exists(dir1):
            os.mkdir(dir1)
        if not os.path.exists(dir2):
            os.mkdir(dir2)

    for i in range(1,6):
        data_name = 'data_batch_' + str(i)
        data = unpickle(data_name)
        for j in range (10000):
            img = np.reshape(data[b'data'][j], (3, 32, 32))
            img = np.transpose(img, (1, 2, 0))
            label = label_names[data[b'labels'][j]].decode()
            img_name = label + '_' + str(i*10000 + j) + '.jpg'
            img_save_path = loc_1 + '/' + label + '/' + img_name
            cv2.imwrite(img_save_path, img)
        print(data_name + ' finished')

    test_data = unpickle('test_batch')
    for i in range (10000):
            img = np.reshape(test_data[b'data'][i], (3, 32, 32))
            img = np.transpose(img, (1, 2, 0))
            label = label_names[test_data[b'labels'][i]].decode()
            img_name = label + '_' + str(i) + '.jpg'
            img_save_path = loc_2 + '/' + label + '/' + img_name
            cv2.imwrite(img_save_path, img)
    print('test_batch finished')

if __name__ == '__main__':
    unzip()

PS: imwrite保存失败居然不提示

标签:loc,img,cifar10,os,解压缩,label,文件夹,path,data
来源: https://www.cnblogs.com/odd4o4/p/16114347.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有