CNN识别CIFAR-10–数据预处理

1.获取CIFAR10

from keras.datasets import cifar10
import matplotlib.pyplot as plt
from keras.utils import np_utils
import numpy as np
np.random.seed(10)

(x_img_train,y_label_train),(x_img_test,y_label_test) = cifar10.load_data()
2.显示训练数据的shape

print('train_data:images:',x_img_train.shape,'labels:',y_label_train.shape)
print('test_data:images:',x_img_test.shape,'labels:',y_label_test.shape)
 
train_data:images: (50000, 32, 32, 3) labels: (50000, 1)
test_data:images: (10000, 32, 32, 3) labels: (10000, 1)
3.将features(照片图像特征值)标准化
将featrues标准化可以提高模型预测的准确度,并且更快收敛

x_img_train_normalize = x_img_train.astype('float32')/255.0
x_img_test_normalize = x_img_test.astype('float32')/255.0
4.label以一位有效编码进行转换

y_label_train_OneHot = np_utils.to_categorical(y_label_train)
y_label_test_OneHot = np_utils.to_categorical(y_label_test)