CIFAR-10 官网下载的数据集,结合Tensorflow官网教程组合的代码
# 载入相应的包
import numpy as np
import tensorflow as tf
import pickle
from tensorflow.keras import datasets, layers, models
# CIFAR-10官网定义的函数
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
#读取数据及处理
batch_files = ["data_batch_1","data_batch_2","data_batch_3","data_batch_4","data_batch_5"]
data = [unpickle(f) for f in batch_files]
images = np.vstack([d[b"data"] for d in data])
n = len(images)
images = images.reshape(n,3,32,32).transpose(0,2,3,1).astype(float)/255
labels = np.hstack([d[b"labels"] for d in data])
#