1. 程式人生 > >keras學習筆記(二)

keras學習筆記(二)

學習過tensorflow後,繼續學習了keras,想用keras自己去寫一個卷積神經網路,用於cifar10的資料分類問題。

例項:CIFAR10分類

下面程式碼主要介紹了keras中三種構建網路結構的方式,僅供參考,可能還有更多的寫網路結構的方式,但目前我作為初學者,瞭解的沒有那麼廣泛。

'''
注:第二種構建網路方法,我沒有寫完,so識別精度很低,但是可以跑通,如果有興趣的話可以自己去補全,
我沒有加任何的技巧,所以另外兩種方法的精度在0.75左右。如果想達到一個較高的識別率,請參考
我修改別人完全寫好的用在mnist資料上的程式碼,我修改了資料集和網路引數,精度達到0.99。
另外,這三種構建網路的辦法我均用了註釋號註釋掉了,自己想去實現的話,選擇其中一種,將註釋去掉即可
'''
import keras
import keras.layers
import numpy as np
from keras.utils import np_utils
from keras.layers import Dense,Activation,Convolution2D,MaxPooling2D,Flatten,Dropout
from keras.optimizers import RMSprop
#資料預處理
nb_classes=10
(X_train, Y_train), (X_test,Y_test) = keras.datasets.cifar10.load_data()
X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255
#將Y_train的shape由一列,變為一行
Y_train =Y_train.reshape(Y_train.shape[0])/10#一種非常好的標籤處理方式,如果不對標籤進行處理,我得到的精度只有0.75,加上這兩句之後,提升到了0.99
Y_test = Y_test.reshape(Y_test.shape[0])/10
Y_train=np_utils.to_categorical(Y_train,nb_classes)#一種編碼方式,one-hot編碼,比如第二個類別就是第二個數是1,其他都是0
Y_test =np_utils.to_categorical(Y_test ,nb_classes)

#第一種建立神經網路模型的方法,由輸入到輸出。
#keras.Input開始定義輸入,每一層後面都加一個小括號,把上一層的輸出放進去,表示這一層的輸入,對於其他的引數如果不理解,請參考之前的部落格
x = keras.Input  (shape=(32, 32, 3))
#在層內參數部分設定的啟用函式activation=,首字母一定小寫,否則報錯。
y = Convolution2D(filters  = 64, kernel_size=3, strides=1, padding='same', activation='relu')(x)
y = Convolution2D(filters  = 64, kernel_size=3, strides=1, padding='same', activation='relu')(y)
y = MaxPooling2D (pool_size= 2,  strides = 2,   padding='same')(y)
y = Convolution2D(filters  = 128,kernel_size=3, strides=1, padding='same', activation='relu')(y)
y = Convolution2D(filters  = 128,kernel_size=3, strides=1, padding='same', activation='relu')(y)
y = MaxPooling2D (pool_size=2,   strides = 2,   padding='same')(y)
y = Convolution2D(filters  = 256,kernel_size=3, strides=1, padding='same', activation='relu')(y)
y = Convolution2D(filters  = 256,kernel_size=3, strides=1, padding='same', activation='relu')(y)
y = MaxPooling2D (pool_size=2,   strides = 2,   padding='same')(y)
y = Flatten()(y)
y = Dropout(0.5)(y) #新增隨機失活層,這個機制可以降低過擬合
y = Dense(units=nb_classes, activation='softmax')(y)
model = keras.models.Model(inputs=x, outputs=y, name='model')
model.summary()

'''
#第二種建立神經網路模型的方法(建立一個模型,然後不斷新增隱藏層)
model=keras.Sequential()
#這裡的啟用函式的首字母一定不要大寫,大寫了就一直報錯。找錯找了半天
model.add(keras.layers.Conv2D(filters=64, kernel_size=3, strides=1, padding='same',activation='relu'))
model.add(keras.layers.Conv2D(filters=64, kernel_size=3, strides=1, padding='same',activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(keras.layers.Conv2D(filters=128,kernel_size=3,strides=1,padding='same',activation='relu'))
model.add(keras.layers.Conv2D(filters=128,kernel_size=3,strides=1,padding='same',activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(keras.layers.Conv2D(filters=256,kernel_size=3,strides=1,padding='same',activation='relu'))
model.add(keras.layers.Conv2D(filters=128,kernel_size=3,strides=1,padding='same',activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='same'))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dropout(0.5))
model.add(Dense(10,activation='softmax'))
'''
'''
#第三種建立神經網路的方法(直接在Sequential中新增隱藏層)
model=keras.Sequential([
    keras.layers.Conv2D(filters=64, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),     #這種形式設定啟用函式Activation首字母要大寫
    keras.layers.Conv2D(filters=64, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),
    MaxPooling2D(pool_size=2, strides=2, padding='same'),
    keras.layers.Conv2D(filters=128, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),
    keras.layers.Conv2D(filters=128, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),
    MaxPooling2D(pool_size=2, strides=2, padding='same'),
    Convolution2D(filters=256, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),
    Convolution2D(filters=256, kernel_size=3, strides=1, padding='same', kernel_initializer='he_normal'),
    Activation('relu'),
    MaxPooling2D(pool_size=2, strides=2, padding='same'),
    Flatten(),
    Dropout(0.5),
    Dense(10),#注意這裡的10千萬不要手殘打引號,這個錯誤我也找了半天,表示最後全連線層的輸入維度為10
    Activation('softmax')#經過softmax處理後,將圖片分類
])
'''
#建立模型包括網路,優化器,損失函式
optimizer=keras.optimizers.RMSprop(lr=0.001,rho=0.9,epsilon=1e-8,decay=0.0)
model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])#metrics可以儲存一些訓練過程中的我想要的資料
#迭代訓練
model.fit(X_train,Y_train,nb_epoch=10,batch_size=100)
loss,accuracy=model.evaluate(X_test,Y_test)
print('test loss',loss)
print('accuracy',accuracy)

我自己寫的這幾個網路結構,訓練過程如下圖所示:

下面這一段是我修改別人完全寫好的用在mnist資料上的程式碼,我修改了資料集和網路引數,精度達到0.99。

import keras.layers
import numpy as np
from keras.utils import np_utils
from keras import Sequential
from keras.layers import Dense,Activation,Convolution2D,MaxPooling2D,Flatten,Dropout
from keras.optimizers import RMSprop
from keras.models import Model

nb_classes = 10
(X_train, Y_train), (X_test, Y_test) = keras.datasets.cifar10.load_data()
Y_train =Y_train.reshape(Y_train.shape[0])/10
Y_test = Y_test.reshape(Y_test.shape[0])/10
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)

x = keras.Input(shape=(32, 32, 3))
y = x
y = Convolution2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = Convolution2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = MaxPooling2D(pool_size=2, strides=2, padding='same')(y)
y = Convolution2D(filters=128, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = Convolution2D(filters=128, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = MaxPooling2D(pool_size=2, strides=2, padding='same')(y)
y = Convolution2D(filters=256, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = Convolution2D(filters=256, kernel_size=3, strides=1, padding='same', activation='relu', kernel_initializer='he_normal')(y)
y = MaxPooling2D(pool_size=2, strides=2, padding='same')(y)
y = Flatten()(y)
y = Dropout(0.5)(y)
y = Dense(units=nb_classes, activation='softmax', kernel_initializer='he_normal')(y)

model1 = Model(inputs=x, outputs=y, name='model1')
model1.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
model1.fit(X_train,Y_train,nb_epoch=10,batch_size=32)
loss,accuracy=model1.evaluate(X_test,Y_test)
print('test loss',loss)
print('accuracy',accuracy)

這是實驗結果: