I’m quite newbie to tensorflow so pls help me. I build a tranferlearning model with MobileNetV3Large from Tensorflow with the first layer is InputLayer(224,224,3), then MobileNetV3Large and some layers for classification. Here are the structure:
Now I want to make the CAM model to see what my model saw and keep getting errors like this:
cam_model = Model(inputs=epoch70_model.inputs[0],outputs=(epoch70_model.layers[-6].output,epoch70_model.layers[-1].output))
KeyError: 'Exception encountered when calling Functional.call().\n\n\x1b[1m133568923055728\x1b[0m\n\nArguments received by Functional.call():\n • inputs=tf.Tensor(shape=(32, 224, 224, 3), dtype=float32)\n • training=False\n • mask=None'
I believe the problem is on epoch70_model.layers[-6].output as I remove this output the model working fine. But I cannot figure out why there is a input shape (32, 224, 224, 3) and how to solve this. The model training and predicing fine with dataset has batch size (256, 224, 224, 3).
Thank for your mention, I have find out my problem. To summarize it, I build my model this way:
IMG_SHAPE =(224, 224, 3)
base_model = tf.keras.applications.MobileNetV3Large(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet',
include_preprocessing=False)
base_model.trainable = False
def feature_extractor(inputs):
feature_extractor = base_model(inputs)
return feature_extractor
def classifier(inputs):
x = GlobalAveragePooling2D()(inputs)
x = Dense(128, activation="silu")(x)
x = Dropout(0.2)(x)
x = BatchNormalization()(x)
x = Dense(1, activation="sigmoid", name="classification")(x)
return x
def final_model():
inputs = tf.keras.layers.Input(shape=(224,224,3))
mobilev3_feature_extractor = feature_extractor(inputs)
classification_output = classifier(mobilev3_feature_extractor)
model = tf.keras.Model(inputs=inputs, outputs = classification_output)
return model
model = final_model()
model.summary()
And it looks like MobileNetV3 is embedded as a layer within a new model make the error. Instead I find the solution to rather extend the MobileNetV3 model and it working:
IMG_SHAPE =(224, 224, 3)
inp = tf.keras.layers.Input(shape=(IMG_SHAPE))
base_model = tf.keras.applications.MobileNetV3Large(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet',
include_preprocessing=False,
input_tensor=inp)
base_model.trainable = False
last_layer = base_model.get_layer('activation_19')
x = GlobalAveragePooling2D()(last_layer.output)
x = Dense(128, activation="silu")(x)
x = Dropout(0.2)(x)
x = BatchNormalization()(x)
x = Dense(1, activation="sigmoid", name="classification")(x)
model = tf.keras.models.Model(inputs = inp, outputs = x)
model.summary()