Valueerror: Unknown Layer: Functional
Solution 1:
The solution to this error is very simple, ex. the reason is that you have trained the model on version '2.3.0' of Tensorflow & '2.4.3' of Keras (On Colab or local). and now you are accessing the saved model(.h5) via another version of Keras & TensorFlow. It will give you the error. The solution is that re-trained model with upgraded versions or downgrades your TF&Keras to the same version as on which model is trained.
Solution 2:
Rebuilt the network from scratch:
image_size = (212, 212)
batch_size = 32
data_augmentation = keras.Sequential(
[
layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
layers.experimental.preprocessing.RandomRotation(0.8),
]
)
def make_model(input_shape, num_classes):
inputs = keras.Input(shape=input_shape)
# Image augmentation block
x = data_augmentation(inputs)
# Entry block
x = layers.experimental.preprocessing.Rescaling(1.0 / 255)(x)
x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(64, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
previous_block_activation = x # Set aside residual
for size in [128, 256, 512, 728]:
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, strides=2, padding="same")(x)
# Project residual
residual = layers.Conv2D(size, 1, strides=2, padding="same")(
previous_block_activation
)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
x = layers.SeparableConv2D(1024, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.GlobalAveragePooling2D()(x)
if num_classes == 2:
activation = "sigmoid"
units = 1
else:
activation = "softmax"
units = num_classes
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(units, activation=activation)(x)
return keras.Model(inputs, outputs)
model = make_model(input_shape=image_size + (3,), num_classes=2)
keras.utils.plot_model(model, show_shapes=False)
Loaded the weights:
model.load_weights('save_at_47.h5')
And ran a prediction on an image:
# Running inference on new data
img = keras.preprocessing.image.load_img(
"le_image.jpg", target_size=image_size
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create batch axis
predictions = model.predict(img_array)
score = predictions[0]
print(
"This image is %.2f percent negative and %.2f percent positive."
% (100 * (1 - score), 100 * score)
)
Solution 3:
I had the same issue when i was on tf 2.3.0, i downgraded to tf 2.2.0 and it worked
Solution 4:
The following trick can be useful if you are using older TF (in my case 2.1.0) and trying to unpack h5 file packed by newer TF (e.g. 2.4.1) and facing this error.
your_model = tf.keras.models.load_model(
path_to_h5,
custom_objects={'Functional':tf.keras.models.Model})
Solution 5:
I faced the same problem when training model with tf 2.3
on colab and load them with tf 2.2
in my local machine. The solution is to upgrade TensorFlow with this command:
pip3 install --upgrade tensorflow
Post a Comment for "Valueerror: Unknown Layer: Functional"