在Keras函数式API中使用ResNet50网络(Python)

我想将下面的代码转换为Keras函数式API。当我训练它时(最后添加了一个softmax层),这段代码运行良好。

Resnet = ResNet50(include_top=False,                 weights='imagenet', input_shape=(224, 224, 3))image_model = tf.keras.Sequential(Resnet)image_model.add(layers.GlobalAveragePooling2D())#image_model.summary()

这是我根据Keras函数式API教程编写的代码:

first_input = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3))first_dense = layers.GlobalAveragePooling2D()(first_input)

然而,当我尝试创建变量first_dense时,出现了以下错误:

Inputs to a layer should be tensors. Got: <tensorflow.python.keras.engine.functional.Functionalobject at 0x000002566CE37520>

回答:

您的ResNet模型应该从一个Input层接收输入,然后连接到下面的层,就像下面的例子一样

resnet = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3))inp = Input((224,224,3))x = resnet(inp)x = GlobalAveragePooling2D()(x)out = Dense(3, activation='softmax')(x)model = Model(inp,out)

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中创建了一个多类分类项目。该项目可以对…

发表回复

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