将CIFAR10数据集拆分为train、val以及test

import cv2
import  numpy as np
from pathlib import Path
from tqdm import tqdm

import pickle

#先读标签
with open('cifar-10-batches-py/batches.meta', 'rb') as fo:
	dict_meta = pickle.load(fo, encoding='bytes')
label_names = dict_meta[b'label_names']

#训练集和验证集
single_train_number = 9000
train_paths = ['cifar-10-batches-py/data_batch_1','cifar-10-batches-py/data_batch_2','cifar-10-batches-py/data_batch_3',
				'cifar-10-batches-py/data_batch_4','cifar-10-batches-py/data_batch_5']
				
for i in tqdm(range(len(train_paths))):
	with open(train_paths[i], 'rb') as fo:
		dict = pickle.load(fo, encoding='bytes')
		
	for j in tqdm(range(single_train_number-1)):
		path = Path("./cifar-10-batches-py/train/")
		tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
		path = path/tag
		if path.exists()==False :
			path.mkdir(parents=True)
		image_path = "./cifar-10-batches-py/train/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
		a = dict[b'data'][j]
		b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
		c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
		d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
		img = np.concatenate((b,c,d),axis = 2)
		cv2.imwrite(image_path, img)
		
	for j in tqdm(range(len(dict[b'data'])-single_train_number)):
		path = Path("./cifar-10-batches-py/val/")
		j = j + single_train_number
		tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
		path = path/tag
		if path.exists()==False :
			path.mkdir(parents=True)
		image_path = "./cifar-10-batches-py/val/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
		a = dict[b'data'][j]
		b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
		c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
		d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
		img = np.concatenate((b,c,d),axis = 2)
		cv2.imwrite(image_path, img)

#测试集
test_path = 'cifar-10-batches-py/test_batch'

with open(test_path, 'rb') as fo:
	dict = pickle.load(fo, encoding='bytes')
		
for j in tqdm(range(len(dict[b'labels']))):
	path = Path("./cifar-10-batches-py/test/")
	tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
	path = path/tag
	if path.exists()==False :
		path.mkdir(parents=True)
	image_path = "./cifar-10-batches-py/test/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
	a = dict[b'data'][j]
	b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
	c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
	d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
	img = np.concatenate((b,c,d),axis = 2)
	cv2.imwrite(image_path, img)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值