簡體   English   中英

在預訓練模型之后添加完全連接的層

[英]Adding fully connected layers after pretrained model

我是ConvNets和Python的新手,想實現以下功能:

我想使用預訓練的vgg16模型,並在其后添加3個完全連接的層,並在末尾進行L2-歸一化。

所以Data-> VGG16-> FC(1x4096)-> FC(1x4096)-> FC(1x3)-> L2-Norm->輸出

第一個和第二個FC獲得一個數組1x4096,最后一個FC獲得一個執行L2-Norm的數組1x3。

誰能給我一個提示,怎么做?

我發現我可以像這樣加載模型:

model_vgg19 = models.vgg19(pretrained =真)

但是在那之后如何添加FC和L2-Norm? 我如何通過模型獲得測試數據?

我引用的是Keras#3465中提到的示例

在Keras框架中,如果在加載預先訓練的模型時提及include_top = False ,則它將不包含最終的分類層。 您可以在最后添加您的自定義FC層,如下例所示:

#load vgg16 without dense layer and with theano dim ordering
base_model = VGG16(weights = 'imagenet', include_top = False, input_shape = (3,224,224))

#number of classes in your dataset e.g. 20
num_classes = 20

x = Flatten()(base_model.output)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
predictions = Dense(num_classes, activation = 'softmax')(x)

#create graph of your new model
head_model = Model(input = base_model.input, output = predictions)

#compile the model
head_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

head_model.summary()
.
.
.
#train your model on data
head_model.fit(x, y, batch_size = batch_size, verbose = 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM