'I can't load my nn model that I've trained and saved

I used transfer learning to train the model. The fundamental model was efficientNet. You can read more about it here

from tensorflow import keras
from keras.models import Sequential,Model
from keras.layers import Dense,Dropout,Conv2D,MaxPooling2D, 
Flatten,BatchNormalization, Activation
from keras.optimizers import RMSprop , Adam ,SGD
from keras.backend import sigmoid

Activation function

class SwishActivation(Activation):

def __init__(self, activation, **kwargs):
    super(SwishActivation, self).__init__(activation, **kwargs)
    self.__name__ = 'swish_act'

def swish_act(x, beta = 1):
    return (x * sigmoid(beta * x))

from keras.utils.generic_utils import get_custom_objects
from keras.layers import Activation
get_custom_objects().update({'swish_act': SwishActivation(swish_act)})

Model Definition

model = enet.EfficientNetB0(include_top=False, input_shape=(150,50,3), pooling='avg', weights='imagenet')

Adding 2 fully-connected layers to B0.

x = model.output

x = BatchNormalization()(x)
x = Dropout(0.7)(x)

x = Dense(512)(x)
x = BatchNormalization()(x)
x = Activation(swish_act)(x)
x = Dropout(0.5)(x)

x = Dense(128)(x)
x = BatchNormalization()(x)
x = Activation(swish_act)(x)

x = Dense(64)(x)

x = Dense(32)(x)

x = Dense(16)(x)

# Output layer
predictions = Dense(1, activation="sigmoid")(x)

model_final = Model(inputs = model.input, outputs = predictions)

model_final.summary()

I saved it using:

    model.save('model.h5')

I get the following error trying to load it:

    model=tf.keras.models.load_model('model.h5')
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-12-e3bef1680e4f> in <module>()
          1 # Recreate the exact same model, including its weights and the optimizer
    ----> 2 model = tf.keras.models.load_model('PhoneDetection-CNN_29_July.h5')
          3 
          4 # Show the model architecture
          5 model.summary()
    
    10 frames
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
        319   cls = get_registered_object(class_name, custom_objects, module_objects)
        320   if cls is None:
    --> 321     raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
        322 
        323   cls_config = config['config']
    
    ValueError: Unknown layer: FixedDropout
```python



Solution 1:[1]

I was getting the same error while trying to do the inference by loading my saved model. Then i just imported the effiecientNet library in my inference notebook as well and the error was gone. My import command looked like:

import efficientnet.keras as efn

(Note that if you havent installed effiecientNet already(which is unlikely), you can do so by using !pip install efficientnet command.)

Solution 2:[2]

I had this same issue with a recent model. Researching the source code you can find the FixedDropout Class. I added this to my inference code with import of backend and layers. The rate should also match the rate from your efficientnet model, so for the EfficientNetB0 the rate is .2 (others are different).

from tensorflow.keras import backend, layers
class FixedDropout(layers.Dropout):
        def _get_noise_shape(self, inputs):
            if self.noise_shape is None:
                return self.noise_shape

            symbolic_shape = backend.shape(inputs)
            noise_shape = [symbolic_shape[axis] if shape is None else shape
                           for axis, shape in enumerate(self.noise_shape)]
            return tuple(noise_shape)
model = keras.models.load_model('model.h5', custom_objects={'FixedDropout':FixedDropout(rate=0.2)})

Solution 3:[3]

I was getting the same error. Then I import the below code. then it id working properly

import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.metrics import confusion_matrix
import itertools
import os, glob
from tqdm import tqdm
from efficientnet.tfkeras import EfficientNetB4

if you don't have to install this. !pip install efficientnet. If you have any problem put here.

Solution 4:[4]

In my case, I had two files train.py and test.py. I was saving my .h5 model inside train.py and was attempting to load it inside test.py and got the same error. To fix it, you need to add the import statements for your efficientnet models inside the file that is attempting to load it as well (in my case, test.py).

from efficientnet.tfkeras import EfficientNetB0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hammad
Solution 2 Amanda Kimball
Solution 3 Dropzz
Solution 4 Kripa Jayakumar