機器學習與計算機視覺(第一個卷積神經網絡)

【 聲明:版權所有,歡迎轉載,請勿用於商業用途。  聯繫信箱:feixiaoxing @163.com】

 

    之前學習了keras和mnist,知道了如何用keras編寫簡單的感知器。感知器的優點是比較簡單,但是缺點也很明顯。訓練出來的識別正確率不是很高,所以自己就想試試卷積網絡。網上的卷積網絡算法和代碼也比較多,正好可以學習一下。

 

1、keras支持多種卷積核

    目前keras中支持多種卷積核,有Conv1D、Conv2D、Conv3D等等。

 

2、cnn是圖像分類的標配

    對於特徵提取、圖像分類的場景來說,cnn基本上是標配。

 

3、歸一化

    圖像輸入給卷積核之前一般先歸一化一下,即x_train = x_train / 255

 

4、池化層

    卷積層一般和池化層配合使用。一個卷積神經網絡可能只有一組卷積層、池化層,也可能有很多組卷積層、池化層。

 

5、模型大小

    一般而言,卷積神經網絡比感知器的模型稍大一點。

 

6、示例代碼

#!/usr/bin/python
# -*- coding: utf-8 -*- 
#
# 首次使用卷積神經網絡來進行處理 20191215
# 
# 參考鏈接:https://blog.csdn.net/weixin_41055137/article/details/81071226
# 理論上卷積神經網絡可以訓練幾千到上萬次
#

import numpy

#from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as k

#高版本的keras
#k.set_image_dim_ordering('th')

#低版本的keras
k.image_data_format() == 'channels_first'

seed = 7
numpy.random.seed(seed)

# load data
#(x_train, y_train), (x_test, y_test) = mnist.load_data()

#
# 原本數據從利用keras.datasets獲取的
# 但是keras是從亞馬遜下載,這裏直接從第三方下載好,然後用numpy加載即可
#

x_train = numpy.load("./mnist/x_train.npy")
y_train = numpy.load("./mnist/y_train.npy")
x_test = numpy.load("./mnist/x_test.npy")
y_test = numpy.load("./mnist/y_test.npy")

# reshape to be [samples][pixels][width][height]

#x_train = x_train.reshape(x_train.shape[0], 1, 28, 28).astype('float32')
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32')
#x_test = x_test.reshape(x_test.shape[0], 1, 28, 28).astype('float32')
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32')

x_train = x_train / 255
x_test = x_test / 255

# one hot encode outputs

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

num_classes = y_test.shape[1]

def baseline_model():

    # create model

    model = Sequential()
    #model.add(Conv2D(32, (5, 5), input_shape=(1, 28, 28), activation='relu'))
	
	#width-height=channel
    model.add(Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))

    # Compile model

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
	
# build the model
model = baseline_model()

# Fit the model
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=200, verbose=2)

# Final evaluation of the model
scores = model.evaluate(x_test, y_test, verbose=0)

model.save('keras_cnn.h5')
del model
print("Baseline Error: %.2f%%" % (100-scores[1]*100))

 

7、輸出結果

python3 keras_cnn.py
Using TensorFlow backend.
2019-12-16 21:19:30.357686: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
 - 27s - loss: 0.2235 - accuracy: 0.9364 - val_loss: 0.0746 - val_accuracy: 0.9772
Epoch 2/10
 - 28s - loss: 0.0711 - accuracy: 0.9781 - val_loss: 0.0468 - val_accuracy: 0.9845
Epoch 3/10
 - 27s - loss: 0.0505 - accuracy: 0.9844 - val_loss: 0.0425 - val_accuracy: 0.9859
Epoch 4/10
 - 28s - loss: 0.0400 - accuracy: 0.9875 - val_loss: 0.0384 - val_accuracy: 0.9874
Epoch 5/10
 - 29s - loss: 0.0319 - accuracy: 0.9901 - val_loss: 0.0343 - val_accuracy: 0.9888
Epoch 6/10
 - 29s - loss: 0.0260 - accuracy: 0.9919 - val_loss: 0.0328 - val_accuracy: 0.9904
Epoch 7/10
 - 28s - loss: 0.0221 - accuracy: 0.9929 - val_loss: 0.0333 - val_accuracy: 0.9891
Epoch 8/10
 - 28s - loss: 0.0189 - accuracy: 0.9941 - val_loss: 0.0336 - val_accuracy: 0.9888
Epoch 9/10
 - 28s - loss: 0.0163 - accuracy: 0.9947 - val_loss: 0.0306 - val_accuracy: 0.9897
Epoch 10/10
 - 26s - loss: 0.0129 - accuracy: 0.9960 - val_loss: 0.0301 - val_accuracy: 0.9910
Baseline Error: 0.90%

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章