GlobalAveragePooling2D : Transfer Learning

while executing this i am getting error -

code:

av1 =GlobalAveragePooling2D()(model.output)
fc1 = Dense(256, activation=‘relu’)(av1)
d1 = Dropout(0.5)(fc1)
fc2 = Dense(4,activation=‘softmax’)(d1)
model_new = Model(inputs=model.input,outputs=fc2)
model_new.summary()

###error:
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.

hey @palashrai31 ,
can you share me your code ,
its something related to your first line. model.outputs

i need to check this in code.

CODE LINK:

https://colab.research.google.com/drive/1YusKEoi4ie_RZS1RG8M_sjFc54b99_6P?usp=sharing

don’t directly call model output here ,
prefer doing it like this ,
like
input = Input(…)
base_model = ResNet50(include_top = False,weights = ‘imagenet’,input_shape = (224,224,3))
output = base_model(input)
av1 =GlobalAveragePooling2D()(output)
fc1 = Dense(256, activation=‘relu’)(av1)
d1 = Dropout(0.5)(fc1)
fc2 = Dense(4,activation=‘softmax’)(d1)
model_new = Model(inputs=input,outputs=fc2)

this will work.

sir do we have to pass X_train as argument in
input=input(…)
I am bit confused.

input=Input(shape=(224,224,3))

base_model = ResNet50(include_top = False,weights = ‘imagenet’,input_shape = (224,224,3))

output = base_model(input)

av1 =GlobalAveragePooling2D()(output)

fc1 = Dense(256, activation=‘relu’)(av1)

d1 = Dropout(0.5)(fc1)

fc2 = Dense(4,activation=‘softmax’)(d1)

model_new = Model(inputs=input,outputs=fc2)

model_new.summary()

writing this code gives multiple warnings :
like -WARNING:tensorflow:
The following Variables were used a Lambda layer’s call (tf.nn.convolution_424), but
are not present in its tracked objects:
<tf.Variable ‘conv1_conv/kernel:0’ shape=(7, 7, 3, 64) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow:
The following Variables were used a Lambda layer’s call (tf.nn.bias_add_424), but
are not present in its tracked objects:
<tf.Variable ‘conv1_conv/bias:0’ shape=(64,) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer.

Instead of importing from keras .
try use tensorflow.keras …

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.