CIFAR-10/CIFAR-100数据集本地保存/图片格式转换 色彩准确
1 官网下载
CIFAR 10
http://www.cs.toronto.edu/~kriz/cifar.html
得到5个训练集batch的文件(5*10000张)和一个batch的测试集文件
CIFAR 100
得到训练集文件、测试集文件、包含标签的meta文件
2 解析文件
def unpickle(file):
fo = open(file, 'rb')
dict = pickle.load(fo, encoding='latin1')
fo.close()
return dict
3 CIFAR10
分为5个训练集batch 和 测试集 两步进行
def cifar10_to_images():
tar_dir = 'your/path/to/cifar-10-batches-py/'
train_root_dir = 'your/path/to/cifar-10-batches-py/train/'
test_root_dir = 'your/path/to/cifar-10-batches-py/test/'
if not os.path.exists(train_root_dir):
os.makedirs(train_root_dir)
if not os.path.exists(test_root_dir):
os.makedirs(test_root_dir)
label_names = ["airplane", "automobile", "bird", "cat", "dear", "dog", "frog", "horse", "ship", "truck"]
for j in range(1, 6):
dataName = tar_dir + "data_batch_" + str(j)
Xtr = unpickle(dataName)
print(dataName + " is loading...")
for i in range(0, 10000):
img = np.reshape(Xtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
train_class_dir = train_root_dir +label_names[Xtr['labels'][i]]+'/'
os.makedirs(train_class_dir,exist_ok=True)
picName = train_class_dir + str(Xtr['labels'][i]) + '_' + label_names[Xtr['labels'][i]] + '_' + str(
i + (j - 1) * 10000) + '.jpg' # label+class+index
# cv2.imwrite(picName, img)
img.save(picName)
print(dataName + " loaded.")
print("test_batch is loading...")
# 生成测试集图片
testXtr = unpickle(tar_dir + "test_batch")
for i in range(0, 10000):
img = np.reshape(testXtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
test_class_dir = test_root_dir + label_names[testXtr['labels'][i]]+ '/'
os.makedirs(test_class_dir,exist_ok=True)
picName = test_class_dir + str(testXtr['labels'][i]) + '_' + label_names[testXtr['labels'][i]] + '_' + str(
i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print("test_batch loaded.")
4 CIFAR 100
解析标签、转换训练集、转换测试集
def cifar100_to_images():
tar_dir = 'your/path/to/cifar-100-python/'
train_root_dir = 'your/path/to/cifar-100-python/trainset/'
test_root_dir = 'your/path/to/cifar-100-python/testset/'
if not os.path.exists(train_root_dir):
os.makedirs(train_root_dir)
if not os.path.exists(test_root_dir):
os.makedirs(test_root_dir)
meta_Name = tar_dir + "meta"
Meta_dic = unpickle(meta_Name)
coarse_label_names = Meta_dic['coarse_label_names']
fine_label_names = Meta_dic['fine_label_names']
print(fine_label_names)
dataName = tar_dir + "train"
Xtr = unpickle(dataName)
print(dataName + " is loading...")
for i in range(0, Xtr['data'].shape[0]):
img = np.reshape(Xtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
###img_name:fine_label+coarse_label+fine_class+coarse_class+index
train_class_dir = train_root_dir + fine_label_names[Xtr['fine_labels'][i]] + '/'
os.makedirs(train_class_dir,exist_ok=True)
picName = train_class_dir + str(Xtr['fine_labels'][i]) + '_' + str(Xtr['coarse_labels'][i]) + '_&' + \
fine_label_names[Xtr['fine_labels'][i]] + '&_' + coarse_label_names[
Xtr['coarse_labels'][i]] + '_' + str(i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print(dataName + " loaded.")
print("test_batch is loading...")
# 生成测试集图片
testXtr = unpickle(tar_dir + "test")
for i in range(0, testXtr['data'].shape[0]):
img = np.reshape(testXtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
test_class_dir = test_root_dir + fine_label_names[testXtr['fine_labels'][i]] + '/'
os.makedirs(test_class_dir,exist_ok=True)
picName = test_class_dir + str(testXtr['fine_labels'][i]) + '_' + str(testXtr['coarse_labels'][i]) + '_&' + \
fine_label_names[testXtr['fine_labels'][i]] + '&_' + coarse_label_names[
testXtr['coarse_labels'][i]] + '_' + str(i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print("test_batch loaded.")
5 总体代码(改个路径直接用!不要看着长! 其实很简单)
import cv2
import numpy as np
import pickle
import os
from PIL import Image
# 解压缩,返回解压后的字典
def unpickle(file):
fo = open(file, 'rb')
dict = pickle.load(fo, encoding='latin1')
fo.close()
return dict
def cifar10_to_images():
tar_dir = 'your/path/to/cifar-10-batches-py/'
train_root_dir = 'your/path/to/cifar-10-batches-py/trainset/'
test_root_dir = 'your/path/to/cifar-10-batches-py/testset/'
if not os.path.exists(train_root_dir):
os.makedirs(train_root_dir)
if not os.path.exists(test_root_dir):
os.makedirs(test_root_dir)
label_names = ["airplane", "automobile", "bird", "cat", "dear", "dog", "frog", "horse", "ship", "truck"]
for j in range(1, 6):
dataName = tar_dir + "data_batch_" + str(j)
Xtr = unpickle(dataName)
print(dataName + " is loading...")
for i in range(0, 10000):
img = np.reshape(Xtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
train_class_dir = train_root_dir +label_names[Xtr['labels'][i]]+'/'
os.makedirs(train_class_dir,exist_ok=True)
picName = train_class_dir + str(Xtr['labels'][i]) + '_' + label_names[Xtr['labels'][i]] + '_' + str(
i + (j - 1) * 10000) + '.jpg' # label+class+index
# cv2.imwrite(picName, img)
img.save(picName)
print(dataName + " loaded.")
print("test_batch is loading...")
# 生成测试集图片
testXtr = unpickle(tar_dir + "test_batch")
for i in range(0, 10000):
img = np.reshape(testXtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
test_class_dir = test_root_dir + label_names[testXtr['labels'][i]]+ '/'
os.makedirs(test_class_dir,exist_ok=True)
picName = test_class_dir + str(testXtr['labels'][i]) + '_' + label_names[testXtr['labels'][i]] + '_' + str(
i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print("test_batch loaded.")
def cifar100_to_images():
tar_dir = 'your/path/to/cifar-100-python/'
train_root_dir = 'your/path/to/cifar-100-python/trainset/'
test_root_dir = 'your/path/to/cifar-100-python/testset/'
if not os.path.exists(train_root_dir):
os.makedirs(train_root_dir)
if not os.path.exists(test_root_dir):
os.makedirs(test_root_dir)
meta_Name = tar_dir + "meta"
Meta_dic = unpickle(meta_Name)
coarse_label_names = Meta_dic['coarse_label_names']
fine_label_names = Meta_dic['fine_label_names']
print(fine_label_names)
dataName = tar_dir + "train"
Xtr = unpickle(dataName)
print(dataName + " is loading...")
for i in range(0, Xtr['data'].shape[0]):
img = np.reshape(Xtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
###img_name:fine_label+coarse_label+fine_class+coarse_class+index
train_class_dir = train_root_dir + fine_label_names[Xtr['fine_labels'][i]] + '/'
os.makedirs(train_class_dir,exist_ok=True)
picName = train_class_dir + str(Xtr['fine_labels'][i]) + '_' + str(Xtr['coarse_labels'][i]) + '_&' + \
fine_label_names[Xtr['fine_labels'][i]] + '&_' + coarse_label_names[
Xtr['coarse_labels'][i]] + '_' + str(i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print(dataName + " loaded.")
print("test_batch is loading...")
# 生成测试集图片
testXtr = unpickle(tar_dir + "test")
for i in range(0, testXtr['data'].shape[0]):
img = np.reshape(testXtr['data'][i], (3, 32, 32))
i0 = Image.fromarray(img[0])
i1 = Image.fromarray(img[1])
i2 = Image.fromarray(img[2])
img = Image.merge('RGB', (i0, i1, i2))
test_class_dir = test_root_dir + fine_label_names[testXtr['fine_labels'][i]] + '/'
os.makedirs(test_class_dir,exist_ok=True)
picName = test_class_dir + str(testXtr['fine_labels'][i]) + '_' + str(testXtr['coarse_labels'][i]) + '_&' + \
fine_label_names[testXtr['fine_labels'][i]] + '&_' + coarse_label_names[
testXtr['coarse_labels'][i]] + '_' + str(i) + '.jpg'
# cv2.imwrite(picName, img)
img.save(picName)
print("test_batch loaded.")
cifar10_to_images()
拿别的方法保存我也试过,但是CIFAR100的图片要么黑白要么是蓝色。则会中方法可以准确保存图片的色彩。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0821c32d99a14687812b0b578d2c9b8e.png