tensorflow - 将分类器添加到 MobileNet 模型时出现检索错误
问题描述
我有以下代码,当我尝试添加自己的分类器时出现错误。
import keras
from keras import layers,Model
from keras.layers import Input,GlobalAveragePooling2D,Flatten,Dense
MobileNetV2_model= tf.keras.applications.MobileNetV2(input_shape=None, alpha=1.0, include_top=False,
weights='imagenet')
#MobileNetV2_model.summary()
x= MobileNetV2_model.output
x = layers.GlobalAveragePooling2D()(x)
final_output=layers.Dense(2, activation='sigmoid')(x)
model = keras.Model(inputs=MobileNetV2.input, outputs = final_output)
model.compile(optimizer="adam", loss='BinaryCrossentropy', metrics=['accuracy'],loss_weights=0.1)
错误
TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that
you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying
to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.
解决方案
你不应该混合keras
和tf.keras
。您可以参考工作代码,如下所示
import tensorflow as tf
from tensorflow.keras import layers, Model
MobileNetV2_model= tf.keras.applications.MobileNetV2(input_shape=(224,224,3), alpha=1.0, include_top=False, weights='imagenet')
#MobileNetV2_model.summary()
x= MobileNetV2_model.output
x = layers.GlobalAveragePooling2D()(x)
final_output=layers.Dense(2, activation='sigmoid')(x)
model = Model(inputs=MobileNetV2_model.input, outputs = final_output)
model.compile(optimizer="adam", loss='BinaryCrossentropy', metrics=['accuracy'],loss_weights=0.1)
推荐阅读
- html - XSLT 通用解决方案,用于从 XML 中获取分层 html 表
- scala - scala中的理解半并行调用
- javascript - 使用 puppeteer 打印 PDF 时,如何获取打印元素的页面?
- python - Python package cannot find one of its modules after installation
- python - 逻辑索引python
- c# - 为什么 TextInfo.ToTitleCase 在字母全部为大写的字符串上无法正常工作?
- visual-c++ - 应用程序无法正确启动 (0xc000007b)。WAMP 服务器错误
- c# - 在不同的列上使用 LINQ 连接三个表并在最后打印总和
- python - 在 python 中有多个循环会提高脚本效率吗?
- c# - WebView2 MouseEnter 事件处理程序永远不会触发