[英]How to use a pretrained keras model to be a parameter to a Model's add function?
I am applying the pretrained model tutorial from Deep Learning with python to a dataset on kaggle.我正在将 Deep Learning with python 中的预训练模型教程应用于 kaggle 上的数据集。 Below is my CNN architecture code, though simple I am getting this error:下面是我的 CNN 架构代码,虽然很简单,但我收到了这个错误:
TypeError: The added layer must be an instance of class Layer.类型错误:添加的图层必须是类图层的实例。 Found: keras.engine.training.Model object at 0x7fdb6a780f60发现:keras.engine.training.Model 对象位于 0x7fdb6a780f60
I have been able to do this while just using native keras but I run into problems when trying to utilize with tensorflow 2.0我已经能够在仅使用本机 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()
You need to switch to the functional API since the sequential model only accepts layers:您需要切换到函数式 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()
Notice how you can use a model as a layer in the functional API.请注意如何将模型用作功能 API 中的层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.