tensorflowkerastransfer-learningmobilenet

Keras and tensorflow conflict when transfer learning on MobileNetV3


I'm trying to do transfer learning with MobileNetV3 in Keras but I'm having some issues.

from keras.models import Model
from keras.layers import GlobalMaxPooling2D, Dense, Dropout
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from tensorflow.keras.applications import MobileNetV3Small
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

pretrained_model = MobileNetV3Small(input_shape=(224,224,3),
                                    weights="imagenet",
                                    include_top=False)

# freeze all layers except the last one
for layer in pretrained_model.layers:
    layer.trainable = False
pretrained_model.layers[-1].trainable = True

# combine the model with some extra layers for classification
last_output = pretrained_model.layers[-1].output
x = GlobalMaxPooling2D()(last_output)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(pretrained_model.input, x)

I get this error when I try to make the Dense layer:

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.

but it's fixed by adding the following code snippet:

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

When I include the code fix above, I get this error when I call model.fit():

FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Could not find variable Conv_1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Conv_1_2/kernel/N10tensorflow3VarE does not exist.
     [[{{node Conv_1_2/Conv2D/ReadVariableOp}}]]
     [[_arg_dense_12_target_0_1/_100]]
  (1) Failed precondition: Could not find variable Conv_1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Conv_1_2/kernel/N10tensorflow3VarE does not exist.
     [[{{node Conv_1_2/Conv2D/ReadVariableOp}}]]
0 successful operations.
0 derived errors ignored.

How can I fix these issues and train the model?


Solution

  • From comments

    Don't mix tf.keras and standalone keras. They are not compatible. Only use one of them (paraphrased from Frightera)

    Working code as shown below

    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import GlobalMaxPooling2D, Dense, Dropout
    from tensorflow.keras.optimizers import Adam
    from tensorflow.keras.callbacks import ModelCheckpoint
    from tensorflow.keras.applications import MobileNetV3Small
    import numpy as np
    from tqdm import tqdm
    from PIL import Image, ImageFile
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    
    pretrained_model = MobileNetV3Small(input_shape=(224,224,3),
                                        weights="imagenet",
                                        include_top=False)
    
    # freeze all layers except the last one
    for layer in pretrained_model.layers:
        layer.trainable = False
    pretrained_model.layers[-1].trainable = True
    
    # combine the model with some extra layers for classification
    last_output = pretrained_model.layers[-1].output
    x = GlobalMaxPooling2D()(last_output)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(1, activation='sigmoid')(x)
    model = Model(pretrained_model.input, x)