keras实战-入门之CIFAR-10图像识别
CIFAR-10图像识别
也是很经典的例子,训练的话最好有GPU,不然很慢,我这里放个训练过的模型吧,虽然准确率不是很高,至少可以拿来跑跑玩,不必等CPU训练好久。
模型:
提取码:77ul
import numpy as np
from keras.utils import np_utils
#导入数据集
from keras.datasets import cifar10
import matplotlib.pyplot as plt
#设置显示中文字体
font = {'family' : 'SimHei',
# 'weight' : 'bold',
'size' : '15'}
plt.rc('font', **font) # 步骤一(设置字体的更多属性)
plt.rc('axes', unicode_minus=False) # 步骤二(解决坐标轴负数的负号显示问题)
Using TensorFlow backend.
np.random.seed(11)
(x_train_image,y_train_label),(x_test_image,y_test_label)=cifar10.load_data()
print('train data=',len(x_train_image))
print('test data=',len(x_test_image))
train data= 50000
test data= 10000
print('y_train_label',x_train_image.shape)
print('y_train_label',y_train_label.shape)
y_train_label (50000, 32, 32, 3)
y_train_label (50000, 1)
print('x_test_image:',x_test_image.shape)
print('y_test_label:',y_test_label.shape)
x_test_image: (10000, 32, 32, 3)
y_test_label: (10000, 1)
label_dict={0:'飞机',1:'汽车',2:'鸟',3:'猫',4:'鹿',5:"狗",6:'青蛙',7:'马',8:'船',9:'卡车'}
#显示图片和标签
def show_images_labels_prediction(images,labels,prediction,idx,num=10):
flig=plt.figure(figsize=(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(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()
show_images_labels_prediction(x_train_image,y_train_label,[],1)
x_train_image[0][0][0]
array([59, 62, 63], dtype=uint8)
x_train_normalize=x_train_image/255
x_test_normalize=x_test_image/255
x_train_normalize[0][0][0]
array([0.23137255, 0.24313725, 0.24705882])
y_train_oneHot=np_utils.to_categorical(y_train_label)
y_test_oneHot=np_utils.to_categorical(y_test_label)
y_train_oneHot[:5]
array([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]])
from keras import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D,Activation,ZeroPadding2D
model=Sequential()
model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',input_shape=(32,32,3),activation='relu'))
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
model.add(Dropout(0.3))
model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'))
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3138: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model.add(Dropout(0.3))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters=128,kernel_size=(3,3),padding='same',activation='relu'))
model.add(Dropout(0.3))
model.add(Conv2D(filters=128,kernel_size=(3,3),padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dropout(0.3))
model.add(Dense(units=2500,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=1500,activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(10,activation='softmax'))
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 32, 32, 32) 896
_________________________________________________________________
dropout_1 (Dropout) (None, 32, 32, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 32, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 16, 64) 18496
_________________________________________________________________
dropout_2 (Dropout) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 64) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 8, 8, 128) 73856
_________________________________________________________________
dropout_3 (Dropout) (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 2048) 0
_________________________________________________________________
dropout_4 (Dropout) (None, 2048) 0
_________________________________________________________________
dense_1 (Dense) (None, 2500) 5122500
_________________________________________________________________
dropout_5 (Dropout) (None, 2500) 0
_________________________________________________________________
dense_2 (Dense) (None, 1500) 3751500
_________________________________________________________________
dropout_6 (Dropout) (None, 1500) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 15010
=================================================================
Total params: 9,176,018
Trainable params: 9,176,018
Non-trainable params: 0
_________________________________________________________________
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
train_history=model.fit(x_train_normalize,y_train_oneHot,validation_split=0.2,epochs=20,batch_size=512,verbose=1)
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Train on 40000 samples, validate on 10000 samples
Epoch 1/20
40000/40000 [==============================] - 23s 563us/step - loss: 2.0186 - acc: 0.2366 - val_loss: 1.9901 - val_acc: 0.2832
Epoch 2/20
40000/40000 [==============================] - 16s 400us/step - loss: 1.6135 - acc: 0.3965 - val_loss: 1.7028 - val_acc: 0.3777
Epoch 3/20
40000/40000 [==============================] - 16s 394us/step - loss: 1.3934 - acc: 0.4828 - val_loss: 1.8715 - val_acc: 0.3611
Epoch 4/20
40000/40000 [==============================] - 16s 390us/step - loss: 1.2475 - acc: 0.5473 - val_loss: 1.3103 - val_acc: 0.5236
Epoch 5/20
40000/40000 [==============================] - 15s 378us/step - loss: 1.1002 - acc: 0.6040 - val_loss: 1.1018 - val_acc: 0.5939
Epoch 6/20
40000/40000 [==============================] - 15s 377us/step - loss: 1.0072 - acc: 0.6406 - val_loss: 0.9453 - val_acc: 0.6662
Epoch 7/20
40000/40000 [==============================] - 16s 389us/step - loss: 0.9290 - acc: 0.6685 - val_loss: 0.9230 - val_acc: 0.6659
Epoch 8/20
40000/40000 [==============================] - 16s 396us/step - loss: 0.8720 - acc: 0.6887 - val_loss: 0.8807 - val_acc: 0.6891
Epoch 9/20
40000/40000 [==============================] - 16s 401us/step - loss: 0.8309 - acc: 0.7034 - val_loss: 0.8583 - val_acc: 0.6976
Epoch 10/20
40000/40000 [==============================] - 16s 396us/step - loss: 0.7732 - acc: 0.7245 - val_loss: 0.8707 - val_acc: 0.6956
Epoch 11/20
40000/40000 [==============================] - 16s 409us/step - loss: 0.7110 - acc: 0.7465 - val_loss: 0.7341 - val_acc: 0.7442
Epoch 12/20
40000/40000 [==============================] - 16s 405us/step - loss: 0.6757 - acc: 0.7585 - val_loss: 0.7414 - val_acc: 0.7400
Epoch 13/20
40000/40000 [==============================] - 16s 409us/step - loss: 0.6291 - acc: 0.7765 - val_loss: 0.7097 - val_acc: 0.7546
Epoch 14/20
40000/40000 [==============================] - 16s 390us/step - loss: 0.5840 - acc: 0.7927 - val_loss: 0.7610 - val_acc: 0.7378
Epoch 15/20
40000/40000 [==============================] - 16s 405us/step - loss: 0.5554 - acc: 0.8013 - val_loss: 0.6738 - val_acc: 0.7698
Epoch 16/20
40000/40000 [==============================] - 16s 396us/step - loss: 0.5205 - acc: 0.8125 - val_loss: 0.6883 - val_acc: 0.7633
Epoch 17/20
40000/40000 [==============================] - 16s 397us/step - loss: 0.4830 - acc: 0.8296 - val_loss: 0.6906 - val_acc: 0.7689
Epoch 18/20
40000/40000 [==============================] - 15s 387us/step - loss: 0.4614 - acc: 0.8360 - val_loss: 0.6638 - val_acc: 0.7728
Epoch 19/20
40000/40000 [==============================] - 16s 393us/step - loss: 0.4265 - acc: 0.8482 - val_loss: 0.6605 - val_acc: 0.7818
Epoch 20/20
40000/40000 [==============================] - 16s 395us/step - loss: 0.4004 - acc: 0.8568 - val_loss: 0.6732 - val_acc: 0.7789
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train histiry')
plt.ylabel(train)
plt.xlabel('epoch')
plt.legend(['train,','validation'],loc='upper left')
plt.show()
import matplotlib.pyplot as plt
show_train_history(train_history,'acc','val_acc')
show_train_history(train_history,'loss','val_loss')
scores=model.evaluate(x_test_normalize,y_test_oneHot)
print('accuracy=',scores[1])
10000/10000 [==============================] - 2s 226us/step
accuracy= 0.7731
prediction=model.predict_classes(x_test_normalize)
prediction
array([3, 8, 8, ..., 5, 1, 7], dtype=int64)
#显示预测和真实标签
def show_images_labels_prediction(images,labels,prediction,idx,num=10):
flig=plt.figure(figsize=(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='labels='+str(labels[idx])
if len(prediction)>0:
title+=',predict='+str(prediction[idx])
ax.set_title(title,fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
idx+=1
plt.show()
show_images_labels_prediction(x_test_image,y_test_label,prediction,idx=3)
predicted_probability=model.predict(x_test_normalize)
predicted_probability
array([[2.9006193e-04, 1.7149459e-03, 4.2438172e-04, ..., 9.3694770e-04,
1.9664117e-03, 9.9076331e-04],
[2.9601876e-04, 4.5216369e-05, 6.3090932e-10, ..., 2.3708598e-11,
9.9965668e-01, 2.0449527e-06],
[4.3919675e-02, 1.2709180e-02, 2.2947293e-04, ..., 1.3813358e-03,
9.1092557e-01, 2.7691400e-02],
...,
[5.6735632e-07, 1.4523317e-07, 2.4530725e-03, ..., 6.9129292e-04,
1.0275818e-06, 2.9606792e-06],
[5.9888985e-02, 8.6926115e-01, 8.8816574e-03, ..., 3.6374254e-03,
6.3337351e-04, 1.8751336e-03],
[1.3187020e-10, 1.0639010e-12, 1.5599704e-07, ..., 9.9752325e-01,
2.3818768e-12, 2.2717030e-11]], dtype=float32)
#显示预测概率
def show_predicted_probability(y,prediction,x_img,predicted_probability,i):
print('label:',label_dict[y[i][0]],'predict',label_dict[prediction[i]])
plt.figure(figsize=(2,2))
plt.imshow(np.reshape(x_img[i],(32,32,3)))
plt.show()
for j in range(10):
print(label_dict[j]+' probability:%1.10f'%(predicted_probability[i][j]))
show_predicted_probability(y_test_label,prediction,x_test_image,predicted_probability,7)
label: 青蛙 predict 鹿
飞机 probability:0.0023190014
汽车 probability:0.0000304469
鸟 probability:0.1690994203
猫 probability:0.1828492135
鹿 probability:0.5292489529
狗 probability:0.0202765986
青蛙 probability:0.0775864720
马 probability:0.0176504478
船 probability:0.0007361662
卡车 probability:0.0002032647
#混淆矩阵
import pandas as pd
pd.crosstab(y_test_label.reshape(-1),prediction,rownames=['label'],colnames=['predict'])
predict | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
label | ||||||||||
0 | 835 | 10 | 41 | 19 | 17 | 5 | 1 | 11 | 38 | 23 |
1 | 18 | 888 | 2 | 12 | 0 | 5 | 6 | 5 | 16 | 48 |
2 | 65 | 1 | 681 | 52 | 82 | 51 | 31 | 26 | 7 | 4 |
3 | 20 | 5 | 52 | 603 | 51 | 161 | 38 | 49 | 10 | 11 |
4 | 19 | 2 | 50 | 58 | 737 | 34 | 19 | 75 | 2 | 4 |
5 | 9 | 5 | 36 | 176 | 32 | 669 | 13 | 52 | 5 | 3 |
6 | 11 | 6 | 38 | 71 | 37 | 29 | 792 | 8 | 5 | 3 |
7 | 16 | 3 | 19 | 44 | 43 | 35 | 0 | 835 | 1 | 4 |
8 | 75 | 23 | 12 | 15 | 9 | 5 | 4 | 6 | 836 | 15 |
9 | 27 | 63 | 7 | 17 | 3 | 3 | 3 | 9 | 13 | 855 |
df=pd.DataFrame({'label':y_test_label.reshape(-1),'predict':prediction})
print(df[(df.label==6) & (df.predict==4)])
label predict
7 6 4
309 6 4
672 6 4
762 6 4
1326 6 4
1670 6 4
1867 6 4
1890 6 4
1989 6 4
2635 6 4
3075 6 4
3711 6 4
4150 6 4
4465 6 4
4610 6 4
4943 6 4
5113 6 4
5297 6 4
5334 6 4
5443 6 4
5961 6 4
6662 6 4
6896 6 4
6908 6 4
7159 6 4
7711 6 4
7945 6 4
8291 6 4
8698 6 4
8916 6 4
8958 6 4
9002 6 4
9216 6 4
9513 6 4
9705 6 4
9842 6 4
9859 6 4
show_images_labels_prediction(x_test_image,y_test_label,prediction,idx=7,num=1)
show_images_labels_prediction(x_test_image,y_test_label,prediction,idx=672,num=1)
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵,侵删。