2

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

5
  • You need to tell load_model where to find FixedDropout via the custom_objects argument.
    – jkr
    Aug 3, 2020 at 5:07
  • I used model.get_config after training, it gave me a long dictionary of configurations of my model, and then I saved the config in a pickle file. Afterward, I used that for custom_object but it gave the same error. Aug 4, 2020 at 8:38
  • Can you please share the code that creates the model? There should be a reference to FixedDropout there. You need to pass in custom_objects={"FixedDropout": FixedDropout}
    – jkr
    Aug 4, 2020 at 13:21
  • I provided the code for the creation of the model and the custom-made activation fcn. As you proposed @jakub I used custom_objects={"FixedDropout": FixedDropout} but it gave : NameError: name 'FixedDropout' is not defined. If you check the link I mentioned initially at my post you'll notice that FixedDropout is used 9 times in the structure of the model. Aug 5, 2020 at 6:28
  • @AliBesharati did you ever find a solution for this? I have exactly the same problem!
    – BoomBoxBoy
    Jan 5, 2021 at 20:25

4 Answers 4

5

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.)

1

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
1

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)})
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.