加载cifar10脚本

#加载cifar10脚本
import pickle
def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict


label_name = [
    "airplane",
    "automobile",
    "bird",
    "cat",
    "deer",
    "dog",
    "frog",
    "horse",
    "ship",
    "truck",
]

# 通过glob文件
import glob
import numpy as np
import cv2
import os
# glob 匹配当前文件夹下所有文件
train_list = glob.glob("data_batch_*")


for l in train_list:

    l_dict = unpickle(l)



for im_idx, im_data in enumerate(l_dict[b'data']):

    im_label = l_dict[b'labels'][im_idx]
    im_name = l_dict[b'filenames'][im_idx]
    #print(im_label,im_name,im_data)
    im_label_name = label_name[im_label]
    # 存储到训练的文件夹下
    # 要先将图片转化为维度[3,32,32]
    im_data = np.reshape(im_data, [3,32,32])
    # 将第一个维度变为最后一个维度
    im_data = np.transpose(im_data,(1,2,0))

    # cv2.imshow("im_data", im_data)
    # cv2.waitKey(0)
    save_path = "train"
    # 用os库进行文件的创建
    if not os.path.exists("{}/{}".format(save_path,im_label_name)):
        os.mkdir("{}/{}".format(save_path,im_label_name))

    cv2.imwrite("{}/{}/{}".format(save_path,im_label_name, im_name.decode("utf-8")),im_data)

你可能感兴趣的:(动手深度学习,python,计算机视觉,人工智能)