[英]How to add layers to the bottom of a pretrained model in keras' functional api?
[英]How to use a pretrained keras model to be a parameter to a Model's add function?
我正在將 Deep Learning with python 中的預訓練模型教程應用於 kaggle 上的數據集。 下面是我的 CNN 架構代碼,雖然很簡單,但我收到了這個錯誤:
類型錯誤:添加的圖層必須是類圖層的實例。 發現:keras.engine.training.Model 對象位於 0x7fdb6a780f60
我已經能夠在僅使用本機 keras 時做到這一點,但是在嘗試使用 tensorflow 2.0 時遇到了問題
from keras.applications.vgg16 import VGG16
base = VGG16(weights='../input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
input_shape=(150,225,3))
model = models.Sequential()
model.add(base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
base.summary()
您需要切換到函數式 API,因為順序模型只接受層:
from keras.applications.vgg16 import VGG16
base = VGG16(weights='../input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
include_top=False,
input_shape=(150,225,3))
in = Input(shape=(150,225,3))
base_out = base(in)
out = Flatten()(base_out)
out = Dense(256, activation='relu')
out = Dense(1, activation='sigmoid')
model = Model(in, out)
model.summary()
請注意如何將模型用作功能 API 中的層。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.