读取cifar10数据集的代码读取数据集的代码
ves import cPickle as pickle
import os
import platform
import numpy as np
import pickle
打靶归来串词filename =r'D:\PythonProjects\cifar-10-batches-py\data_batch_1' # cifar10⼆进制⽂件路径
class = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'hor', 'ship', 'truck')
def load_pickle(f):
version = platform.python_version_tuple()
if version[0] == '2':
return pickle.load(f)
elif version[0] == '3':
return pickle.load(f, encoding='latin1')
rai ValueError("invalid python version: {}".format(version))
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
datadict = load_pickle(f)
X = datadict['data']dash
Y = datadict['labels']
X = X.reshape(10000, 3072)
Y = np.array(Y)
return X, Y
def load_CIFAR10(ROOT):
""" load all of cifar """
xs = []
ys = []
for b in range(1, 6):
f = os.path.join(ROOT, 'data_batch_%d' % (b,))
X, Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
del X, Y
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
return Xtr, Ytr, Xte, Yte
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
# Load the raw CIFAR-10 data
cifar10_dir = r'D:\PythonProjects\cifar-10-batches-py'
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
# Subsample the data
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
软件工程大学排名x_train = X_train.astype('float32')
x_test = X_test.astype('float32')
x_train /= 255
x_test /= 255
return x_train, y_train, X_val, y_val, x_test, y_test
# 调⽤上⾯的函数来获取数据
x_train, y_train, x_val, y_val, x_test, y_test = get_CIFAR10_data()
print('Train data shape: ', x_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', x_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', x_test.shape)
print('Test labels shape: ', y_test.shape)
运⾏结果:
显⽰图⽚的代码:
电视连续剧红高粱ves import cPickle as pickle
import numpy as np
import pickle
from matplotlib import pyplot as plt
import cv2
# cifar10数据集10分类数据集,32X32⼤⼩的RGB3通道图⽚,50000张⽤于训练,10000张⽤于测试def unpickle(file): # CIFAR-10官⽅给出的使⽤⽅法
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='iso-8859-1')
return dict
# 加载训练集
file = r'D:\PythonProjects\cifar-10-batches-py\data_batch_' # ⽂件的路径,只加载了10000张图⽚x_train = np.empty(shape=[0, 3072])
y_train = []
for ii in range(5):
file1 = file + str(ii + 1)
dict_train_batch1 = unpickle(file1) # 将data_batch⽂件读⼊到数据结构(字典)中
奶粉品牌
data_train_batch1 = dict_('data') # 字典中取data
labels1 = dict_('labels') # 字典中取labels
x_train = np.append(x_train, data_train_batch1, axis=0)
繁华造句
y_train = np.append(y_train, labels1)
# 加载测试集
file = r'D:\PythonProjects\cifar-10-batches-py\test_batch'
dict_test = unpickle(file)
x_test = ("data")
虾仁意面y_test = ("labels")
# 改x_test[ ]中的数字就会显⽰出不同的图⽚
image_m = np.reshape(x_test[16], (3, 32, 32))
r = image_m[0, :, :]
g = image_m[1, :, :]男孩女孩名字
b = image_m[2, :, :]
img23 = ([r, g, b])
plt.figure()
plt.imshow(img23)
plt.show()
运⾏结果: