Keras CIFAR-10 color image object recognition convolutional neural network

Reference books "Tensorflow + Keras deep learning practical application of artificial intelligence," Linda Gui with
Chapter IX, chap.
This is a very user-friendly introductory practice book, all code, unavoidably explained


CIFAR-10 data set is 60000 32x32 color image, divided into 10 categories, airplanes, cars, birds, cats, deer, dogs, frogs, horses, boats, trucks. 50000 training images, 10,000 test images

1. Data processing sets
1-1. Dataset download CIFAR-10

from keras.datasets import cifar10
import numpy as np
np.random.seed(10)

(x_image_train,y_label_train),\
(x_image_test,y_label_test)=cifar10.load_data()

cifar10.load_data () for downloading or reading the data set CIFAR-10, the first download will take some time

1-2. View the training data
Check the shape x_image_train
1-3. View more training data (label and image)

import matplotlib.pyplot as plt
label_dict={0:'airplane',1:'automobile',2:'bird',3:'cat',4:'deer',5:'dog',6:'frog',
													7:'horse',8:'ship',9:'truck'}
#用字典dict定义每一个数字所代表的图像类别名称
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=10)
         ax.set_xticks([]);ax.set_yticks([])
         idx+=1
     plt.show()
     
plot_images_labels_prediction(x_image_train,y_label_train,[],0)#查看训练数据前十项

prediction = [], No prediction of View
1-4 to image, label data preprocessing
image standardization, label is converted into hot code (One-Hot Encoding)
digital standardization can improve the accuracy of model accuracy of the model, the model improve convergence speed.

#image预处理 (RGB各通道除以255标准化)
x_image_train_norm=x_image_train.astype('float32')/255.0
x_image_test_norm=x_image_test.astype('float32')/255.0
x_image_train_norm[0][0][0]

See the results of the training data of a normalized image of the first point data 3 representing the RGB
Out [12 is]: Array ([0.23137255, .24313726, .24705882], DTYPE = float32)

Pretreatment of label
view label shape data before 5
Here Insert Picture Description
Here Insert Picture Description
the label into Hot-Encoding One
(0- (1000000000), l- (0100000000), ----. 6 is corresponding to (0,000,001,000))

#label 预处理
#Keras 提供了np_utils.to_categorical方法可进行One-Hot Encoding转换
from keras.utils import np_utils
y_label_train_OneHot=np_utils.to_categorical(y_label_train).reshape(50000,10)
y_label_test_OneHot=np_utils.to_categorical(y_label_test).reshape(10000,10)

Input: y_label_train_OneHot [:. 5] Here Insert Picture Description
2. Keras convolutional neural network image recognition CIFAR 10-
connected to the first partial data sets downloaded and processed
2-1
convolutional neural network can be divided into two parts,
extracting image feature: Convolution layer 1 (conv1 ), pooled layer 1 (pooling1), conv2, pooling2 , image feature extraction.
Fully connected neural networks: Neural networks planarization layer, a hidden layer, an output layer composed.

2-2 model
convolutional layer, pooled layer, fully connected network (flat, hidden output layer) stacked linear
model.add () increasing layer model

#2-1 建立模型
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.layers import Conv2D,MaxPooling2D,ZeroPadding2D

#建立线性堆叠模型,后续只要将各个神经网络加入模型即可
model=Sequential()
#建立卷积层1
model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(32,32,3),
                 activation='relu',padding='same'))
#建立池化层1,将32x32的图像缩减为16x16的图像
model.add(MaxPooling2D(pool_size=(2,2)))
#建立卷积层2
model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',padding='same'))
#加入Dropout避免过拟合
model.add(Dropout(0.25))
#建立池化层2
model.add(MaxPooling2D(pool_size=(2,2)))
#建立神经网络
    #建立平坦层
model.add(Flatten())
model.add(Dropout(0.25))
    #建立隐藏层
model.add(Dense(1024,activation='relu'))
model.add(Dropout(0.25))    
    #建立输出层
model.add(Dense(10,activation='softmax'))
  • Conv1 layer parameters
    | filters = 32, | 32 filters disposed randomly generated |
    | kernel_size = (3,3) | each filter size 3x3 |
    | = padding 'Same' | let convolved image generated by the convolution operation same size |
    | input_size = (32,32,3) | three-dimensional, the first two-dimensional image representative of the shape and size 32x32, the third dimension representing a color image, the RGB three channels |
    | activation = 'RELU' | provided the activation layer function relu |
  • Pooling 1 parameter
  • pool_size = (2,2), the image is reduced to 32x32 16x16
  • Conv1 layer parameters
    | filters = 64, | 64 filters disposed randomly generated |
    | kernel_size = (3,3) | each filter size 3x3 |
    | = padding 'Same' | let convolved image generated by the convolution operation same size |
    | activation = 'RELU' | activation function of the layer disposed RELU |
  • Dropout avoid overfitting
  • model.add (Dropout (0.25)) each training iteration give up 25% of the neurons in the neural network of
    the rest of the parameters above and similar
    input: print (model.summary ()) Model Summary View
    Here Insert Picture Description
    2-3 training
#开始训练
train_history=model.fit(x=x_image_train_norm,y=y_label_train_OneHot,validation_split=0.2,
                                epochs=20,batch_size=128,verbose=2)
  • loss: loss of function setting, in depth study using cross-entropy better training
  • optimizer: When training, use adam optimizer can make training faster convergence and improve accuracy
  • set of metrics to evaluate the way the model is accurate rate
  • model.fit training, the training process exists train_history inside, parameters model.fit need:
  • Training input data parameters
  • x_image_train_norm,y_label_train_OneHot
  • Training and validation data set ratio
  • validation_split = 0.2, 80% of the training data (50000x0.8 = 40000), 20% to verify data
  • epochs = 20, 20 network training
  • the batch_size = 128, 128 each batch of data, a total of 40,000 / 128 = 313 lots, each network training data 313 to be processed batches
  • verbose = 2, the training process is displayed

2-3 View training results
View accuracy, execution loss

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')

plt.figure()
show_train_history(train_history,'loss','val_loss')

result:
Here Insert Picture Description
Here Insert Picture Description

#查看模型正确率
scores=model.evaluate(x_image_test_norm,y_label_test_OneHot,verbose=1)

Here Insert Picture Description

#2-4 进行预测
prediction=model.predict_classes(x_image_test_norm)
#预测结果,前10项数据
prediction[:10]
#显示前10项结果
plt.figure()
plot_images_labels_prediction(x_image_test,y_label_test,prediction,0,10)

Here Insert Picture Description
2-5 shows the predicted probability of
view the probability of a test data (the probability of each classified as)

#2-5 显示预测概率
#预测概率
Predicted_Probability=model.predict(x_image_test_norm)

def show_Predicted_Probability(y_label,prediction,x_image,
                                   Predicted_Probabilty,i):
    print('label:',label_dict[y_label[i][0]],
              'predict',label_dict[prediction[i]])
    plt.figure(figsize=(2,2))
    plt.imshow(np.reshape(x_image_test[i],(32,32,3)))
    plt.show()
    for j in range(10):
        print(label_dict[j]+' '+'Probability:%1.9f'%(Predicted_Probability[i][j]))
show_Predicted_Probability(y_label_test,prediction,x_image_test,Predicted_Probability,1)  

Here Insert Picture DescriptionHere Insert Picture Description

This picture can be seen that the ship, the ship is classified as probability of 0.8812, the highest probability, the final prediction is correct, the first 0 is the classification error, you can also see the probability as above.

2-6 shows the confusion matrix
established by confusion matrix pd.crosstab, its input requirements are one-dimensional array, all with y_label_test.shape, prediction.shape, respectively, is not a one-dimensional view, which is y_label_test (10000,1), use reshape, converted into a one-dimensional array

#2-6 显示混淆矩阵
y=y_label_test.reshape(-1)  #将其转换为一维数组

import pandas as pd
print(label_dict)
pd.crosstab(y,prediction,rownames=['labels'],colnames=['predict'])

Here Insert Picture Description
Diagonal prediction is correct, it can analyze the most confusing category, and least confusing.

2-7 Load and Save the model of
each training convolution neural network will take a long time ,, can after each training is complete, save the model, or to save weight, the next execution of the training model to load weights, and then continue training

'''
#2-7 模型的加载与保存
model.save('model.h5')
#保存模型权重
model.save_weights('my_model_weights.h5')
#下次使用的时候
model = load_model('model.h5')
model.load_weights('my_model_weights.h5')
'''

Here Insert Picture Description
There will be good h5 save files in your folder to save the script file

END
create three times the convolution neural network will further improve the accuracy.

Challenges:
1. white issue, the variable name wrong tremor
2. Note the attention dimension, np_utils.to_categorical method original book can be One-Hot Encoding conversion, the shape of the converted is (50000,1, 10), followed by network training error, suggesting that the output layer to enter a two-dimensional vector, and is received (50000,1,10), so I went to reshape a bit. Charles did not understand how this np_utils.to_categorical so bad, MNIST handwritten numeral recognition before, this would be no problem.
Change:
y_label_train_OneHot = np_utils.to_categorical (y_label_train) .reshape (50000,10)

Thank you for reading ... continue to make unremitting efforts to record the learning process

Published 10 original articles · won praise 10 · views 7521

Guess you like

Origin blog.csdn.net/qq_41647438/article/details/88614989