1. 程式人生 > >keras函式式模型和序貫模型的使用

keras函式式模型和序貫模型的使用

文章目錄

函式式模型和序貫模型

在 Keras 中有兩類主要的模型:Sequential 順序模型 和 使用函式式 API 的 Model 類模型。
序貫模型是多個網路層的線性堆疊,是函式式模型的簡略版,為最簡單的線性、從頭到尾的結構順序,不發生分叉。

函式式模型比序貫模型要複雜,更靈活,可以同時/分階段輸入變數,分階段輸出想要的模型。Keras函式式模型介面是使用者定義多輸出模型、非迴圈有向模型或具有共享層的模型等複雜模型的途徑。一句話,只要你的模型不是類似VGG一樣一條路走到黑的模型,或者你的模型需要多於一個的輸出,那麼你總應該選擇函式式模型。函式式模型是最廣泛的一類模型,序貫(Sequential)模型只是它的一種特殊情況。

序貫模型例項

4層cnn

dr = 0.5 # dropout rate (%)
model = models.Sequential()
model.add(Reshape(in_shp + [1], input_shape=in_shp))#在網路第一步做了變換,in_shp---in_shp+[1],也可以對輸入資料reshape
model.add(Conv2D(160, (5, 5), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
model.add(Conv2D(160, (5, 5), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
model.add(Dropout(0.5))

model.add(Conv2D(64, (3, 3), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
model.add(Conv2D(64, (3, 3), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(128, kernel_initializer="glorot_normal", activation="relu", name="dense1"))
model.add(Dropout(dr))
# model.add(Dense(1, activation='sigmoid'))

model.add(Dense(len(classes), kernel_initializer="he_normal", name="dense2"))
model.add(Activation('softmax'))
model.add(Reshape([len(classes)]))


model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])

函式式模型例項

雙通道cnn

######tcnn model####
model = models.Sequential()
model.add(Reshape(in_shp + [1], input_shape=in_shp))
input=Input(shape=in_shp + [1])

####up channel##
# with tf.variable_scope("y_pred",reuse=None):
cnnup=Conv2D(160, (5, 5), activation='relu')(input)
# BatchNormalization(epsilon=1e-6, weights=None)
cnnup=Conv2D(160, (5, 5), activation='relu')(cnnup)
cnnup = Dropout(dr)(cnnup)
# cnnup = BatchNormalization(epsilon=1e-6, weights=None)(cnnup)
cnnup=Conv2D(64, (3, 3), activation='relu')(cnnup)
cnnup = Dropout(dr)(cnnup)
# cnnup = BatchNormalization(epsilon=1e-6, weights=None)(cnnup)
cnnup=Conv2D(64, (3, 3), activation='relu')(cnnup)
# cnnup = BatchNormalization(epsilon=1e-6, weights=None)(cnnup)

print cnnup

####down channel##
cnndown=Conv2D(160, (7, 7), activation='relu')(input)
cnndown = Dropout(dr)(cnndown)
# cnndown = BatchNormalization(epsilon=1e-6, weights=None)(cnndown)
cnndown=Conv2D(64, (5, 5), activation='relu')(cnndown)
cnndown = Dropout(dr)(cnndown)
# cnndown = BatchNormalization(epsilon=1e-6, weights=None)(cnndown)
cnndown=Conv2D(32, (3, 3), activation='relu')(cnndown)
# cnndown = BatchNormalization(epsilon=1e-6, weights=None)(cnndown)

print cnndown

cnnout=concatenate([cnndown,cnnup])
fl=Flatten()(cnnout)
output=Dense(128,kernel_initializer="glorot_uniform", activation='relu',name="dense1")(fl)
output = Dropout(dr, name='dense_drp')(output)
output=Dense(len(classes),kernel_initializer="glorot_normal", activation='softmax',name="dense2")(output)

model1=Model(input,output)
model.add(model1)

model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])

混合模型例項

雙通道cnn

dr = 0.5 # dropout rate (%)
######tcnn model###
input=Input(shape=in_shp)

####up channel##############################
up_model = Sequential()
up_model.add(Reshape(in_shp + [1], input_shape=in_shp))#網路第一層reshape
up_model.add(Conv2D(160, (5, 5), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
up_model.add(Conv2D(160, (5, 5), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
up_model.add(Dropout(0.5))

up_model.add(Conv2D(64, (3, 3), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
up_model.add(Conv2D(64, (3, 3), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
up_model.add(Dropout(0.5))
up_model.add(Flatten())
up_model.summary()
# plot_model(up_model, to_file='upmodel.png',show_shapes=True)

cnnup = up_model(input)

####down channel##################################
down_model = Sequential()
down_model.add(Reshape(in_shp + [1], input_shape=in_shp))
down_model.add(Conv2D(128, (7, 7), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
# down_model.add(Conv2D(160, (5, 5), activation='relu'))
# down_model.add(BatchNormalization(epsilon=1e-6, weights=None))
down_model.add(Dropout(0.5))

down_model.add(Conv2D(64, (5, 5), activation='relu'))
BatchNormalization(epsilon=1e-6, weights=None)
down_model.add(Dropout(0.5))

down_model.add(Flatten())
cnndown = down_model(input)
print cnndown
cnnout=concatenate([cnndown,cnnup])
print cnnout

output=Dense(128,kernel_initializer="glorot_uniform", activation='relu',name="dense1")(cnnout)
output = Dropout(dr, name='dense_drp')(output)
output=Dense(len(classes),kernel_initializer="glorot_normal", activation='softmax',name="dense2")(output)

model=Model(input,output)

model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])

官方例項

from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.layers import Input, LSTM, Embedding, Dense
from keras.models import Model, Sequential

# 首先,讓我們用 Sequential 來定義一個視覺模型。
# 這個模型會把一張影象編碼為向量。
vision_model = Sequential()
vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
vision_model.add(Conv2D(64, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(128, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Flatten())

# 現在讓我們用視覺模型來得到一個輸出張量:
image_input = Input(shape=(224, 224, 3))
encoded_image = vision_model(image_input)

# 接下來,定義一個語言模型來將問題編碼成一個向量。
# 每個問題最長 100 個詞,詞的索引從 1 到 9999.
question_input = Input(shape=(100,), dtype='int32')
embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input)
encoded_question = LSTM(256)(embedded_question)

# 連線問題向量和影象向量:
merged = keras.layers.concatenate([encoded_question, encoded_image])

# 然後在上面訓練一個 1000 詞的邏輯迴歸模型:
output = Dense(1000, activation='softmax')(merged)

# 最終模型:
vqa_model = Model(inputs=[image_input, question_input], outputs=output)