Bia
Bia

Reputation: 305

ResNet50 network in Keras functional API (python)

I want to transform the code below in Keras functional API. This code worked fine when I trained it(with a softmax layer in the end).

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()

This is what I came up with using the tutorial from Keras functional API:

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

However, this error appears when I try to create the variable first_dense:

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

Upvotes: 3

Views: 888

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

Your ResNet model should receive an input from an Input layer and then be connected to the following layers like in the example below

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)

Upvotes: 2

Related Questions