在向MobileNet模型添加分类器时遇到问题

我有以下代码,在尝试添加自己的分类器时遇到了错误。

import kerasfrom keras import layers,Modelfrom keras.layers import Input,GlobalAveragePooling2D,Flatten,DenseMobileNetV2_model= tf.keras.applications.MobileNetV2(input_shape=None, alpha=1.0, include_top=False, weights='imagenet')#MobileNetV2_model.summary()x= MobileNetV2_model.outputx = 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: 无法将符号化的Keras输入/输出转换为numpy数组。此错误可能表明您试图将符号值传递给不支持的NumPy调用。或者,您可能试图将Keras符号输入/输出传递给未注册调度的TF API,阻止Keras自动将API调用转换为函数模型中的lambda层。

回答:

您绝不应该混合使用kerastf.keras。您可以参考下面的工作代码

import tensorflow as tffrom tensorflow.keras import layers, ModelMobileNetV2_model= tf.keras.applications.MobileNetV2(input_shape=(224,224,3), alpha=1.0, include_top=False, weights='imagenet')#MobileNetV2_model.summary()x= MobileNetV2_model.outputx = 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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注