<jupyter_start><jupyter_text>10.2 数据预处理<jupyter_code>from keras.datasets import cifar10
import numpy as np
np.random.seed(10)
(x_img_train, y_label_train), (x_img_test, y_label_test) = cifar10.load_data()
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)
x_img_train_normalize = x_img_train.astype('float32') / 255.0
x_img_test_normalize = x_img_test.astype('float32') / 255.0
from keras.utils import np_utils
y_label_train_OneHot = np_utils.to_categorical(y_label_train)
y_label_test_OneHot = np_utils.to_categorical(y_label_test)<jupyter_output><empty_output><jupyter_text>10.3 建立模型<jupyter_code>from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), input_shape=(32,32,3), activation='relu', padding='same'))
model.add(Dropout(rate=0.25))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'))
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dropout(rate=0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(rate=0.25))
model.add(Dense(10, activation='softmax'))
print(model.summary())<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896       
_________________________________________________________________
dropout_1 (Dropout)          (None, 32, 32, 32)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 16, 16, 64)        18496     
_________________________________________________________________
dropout_2 (Dropout)          (None, 16, 16, 64)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 64)          0         
_________________________________________________________________
flatten_1 [...]<jupyter_text>10.4 进行训练<jupyter_code>model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
train_history = model.fit(x_img_train_normalize, y_label_train_OneHot, validation_split=0.2, epochs=10, batch_size=128, verbose=1)
import matplotlib.pyplot as plt
def show_train_history(train_history, train, validation):
    plt.plot(train_history.history[train])
    plt.plot(train_history.history[validation])
    plt.title('Train History')
    plt.ylabel(train)
    plt.xlabel('Epoch')
    plt.legend(['train', 'validation'], loc='upper left')
    plt.show
show_train_history(train_history, 'acc', 'val_acc')
show_train_history(train_history, 'loss', 'val_loss')<jupyter_output><empty_output><jupyter_text>10.5 评估模型准确率<jupyter_code>scores = model.evaluate(x_img_test_normalize, y_label_test_OneHot, verbose = 0)
scores[1]<jupyter_output><empty_output><jupyter_text>10.6 进行预测<jupyter_code>prediction = model.predict_classes(x_img_test_normalize)
prediction[0:10]
label_dict = {0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 
              5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}

import matplotlib.pyplot as plt
def plot_images_labels_prediction(images, labels, prediction, idx, num=10):
    fig = plt.gcf()
    fig.set_size_inches(12,14)
    if num>25: 
        num = 25
    for i in range(0, num):
        ax = plt.subplot(5, 5, 1+i)
        ax.imshow(images[idx], cmap='binary')
        
        title=str(i) + ',' + label_dict[labels[i][0]]
        if len(prediction) > 0:
            title += '=>' + label_dict[prediction[i]]
            
        ax.set_title(title, fontsize = 12, color = 'white')
        ax.set_xticks([]); ax.set_yticks([])
        idx += 1
    plt.show()
plot_images_labels_prediction(x_img_test, y_label_test, prediction, 0, 20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14282374941561476, 'fraction_numerical': 0.042309490416082285, 'mean_word_length': 5.283406754772393, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '350398', 'license': 'Apache-2.0', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1453, 'n_words': 245, 'path': 'Chapter-10-CIFAR-10_CNN.ipynb', 'repo_name': 'Errrneist/TensorFlow_Meets_Keras', 'size': '4278'}