我想将下面的代码转换为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)