Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrained MobileNet and failed to load it for prediction #7431

Closed
rtao opened this issue Jul 26, 2017 · 20 comments
Closed

Retrained MobileNet and failed to load it for prediction #7431

rtao opened this issue Jul 26, 2017 · 20 comments

Comments

@rtao
Copy link

rtao commented Jul 26, 2017

TF: 1.2.1-gpu
Keras: 2.0.6

I am trying to retrain the MobileNet modle from the latest version using the following script,

Training.py
https://gist.github.com/rtao/50eb8c96b06f4deddec2b7888da1d062

And when I try to load it back with the following, I got an error of "ValueError: Unknown activation function:relu6"

Predict.py
https://gist.github.com/rtao/c6773e7e430552a54a3812d5fae91bfe

@fchollet
Copy link
Member

fchollet commented Jul 26, 2017 via email

@drscotthawley
Copy link

drscotthawley commented Oct 7, 2017

Just to make @fchollet's comment explicit for fellow n00bs like me...this works:

from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    model = load_model('weights.hdf5')

@bkou
Copy link

bkou commented Mar 27, 2018

If your keras is at "from tensorflow.python import keras" instead of at "import keras" then here is what worked for me:

from tensorflow.python.keras._impl.keras.utils.generic_utils import CustomObjectScope
from tensorflow.python.keras._impl.keras.applications import mobilenet
from tensorflow.python.keras._impl.keras.models import load_model
with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
    model = load_model('weights.hdf5')

@edumucelli
Copy link
Contributor

edumucelli commented Jun 26, 2018

Is still possible to import mobilenet.DepthwiseConv2D and mobilenet.relu6 on Keras 2.2.0? I have seen that DepthwiseConv2D is now on keras.layers, but not sure about relu6. Where can I find it?

@koichirokamoto
Copy link

I have found relu6 at here.

@flyfj
Copy link

flyfj commented Jul 16, 2018

had same issue with keras 2.2.0, no relu6 is found.

@flyfj
Copy link

flyfj commented Jul 16, 2018

i downgraded to 2.1.4, it works fine. noticed pr on removing relu6 in 2.2, but the pretrained model still has it.

@haitham-b
Copy link

im using Keras 2.2 with tensorflow 1.9
trying to load a mobilenet and mobilenetV2 models that were generated with tensorflow 1.8.
im able to find relu6 in (from keras_applications import mobilenet as mn) , but can't find
DepthwiseConv2D.

any idea how the import for DepthwiseConv2D should look like ?

@edumucelli
Copy link
Contributor

edumucelli commented Jul 25, 2018

Found them,

from keras.layers import DepthwiseConv2D
from keras_applications.mobilenet import relu6

Just for information, before they were imported as

from keras.applications.mobilenet import DepthwiseConv2D, relu6

@warren30815
Copy link

warren30815 commented Aug 16, 2018

In Keras2.2, modified to
from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.layers.ReLU(6.),'DepthwiseConv2D': keras.layers.DepthwiseConv2D}):
model = load_model('weights.hdf5')

@SteveIb
Copy link

SteveIb commented Oct 14, 2018

Im still facing the same issue!
Keras = 2.2.0, TF = 1.9?

@mrgloom
Copy link

mrgloom commented Oct 18, 2018

How to convert MobileNet from Keras 2.1.3 to CoreML?

from keras.applications import MobileNet
from keras.applications.mobilenet import relu6
from keras.applications.mobilenet import DepthwiseConv2D

import coremltools.converters.keras as k

def save_model():
    model = MobileNet(input_shape=(128,128,3), include_top=False)
    model.save('temp.h5')

def convert():
    model = k.convert('temp.h5',
                      input_names=['input'],
                      output_names=['output'],
                      model_precision='float16',
                      add_custom_layers = True,
                      custom_conversion_functions={'relu6': relu6, 'DepthwiseConv2D': DepthwiseConv2D})
    model.save('temp.model')

save_model()
convert()

This also gives ValueError: Unknown activation function:relu6

@mrgloom
Copy link

mrgloom commented Oct 18, 2018

Here is solution based on apple/coremltools#38

from keras.applications import MobileNet
import keras

import coremltools.converters.keras as k
from keras.utils.generic_utils import CustomObjectScope

def save_model():
    model = MobileNet(input_shape=(128,128,3), include_top=False)
    model.save('temp.h5')

def convert():
    with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,
                            'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
        model = k.convert("temp.h5",
                          input_names=['input'],
                          output_names=['output'],
                          model_precision='float16')

    model.save('temp.mlmodel')

save_model()
convert()

And here is as solution for keras to tflite
tensorflow/tensorflow#17289

@SteveIb
Copy link

SteveIb commented Oct 25, 2018

In Keras2.2, modified to
from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.layers.ReLU(6.),'DepthwiseConv2D': keras.layers.DepthwiseConv2D}):
model = load_model('weights.hdf5')

I have tried
with CustomObjectScope({'relu6': keras.layers.ReLU(6.),'DepthwiseConv2D': keras.layers.DepthwiseConv2D}):
model = load_model('****.hdf5')

but I got the following error:
ValueError: axes don't match array

my TF is 1.11 my keras is 2.2.4, python 2.7.
Im trying to convert the model on the same machine and environment i have trained on.
any suggestions?

@poria-cat
Copy link

in version >= 2.2.4, you can write like this:

from keras.layers import DepthwiseConv2D, ReLU
relu6 = ReLU(6.)
....
return Activation(relu6)(x)

@sonfire186
Copy link

in version >= 2.2.4, you can write like this:

from keras.layers import DepthwiseConv2D, ReLU
relu6 = ReLU(6.)
....
return relu6(x)

@hernandezfelipe
Copy link

I am having some trouble with this error and I don't know what to do. I am having a hard time because the way I am saving my trained model is different.

What should I do?

Saving

model_json = model.to_json()
with open("model_pretrained.json", "w") as json_file:
json_file.write(model_json)

model.save_weights("model_pretrained.h5")
print("Saved model to disk")

Loading

json_file = open('model_pretrained.json', 'r')
loaded_model_json = json_file.read()
json_file.close()

loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_pretrained.h5")

ValueError: Unknown activation function:relu6

@ymodak
Copy link
Collaborator

ymodak commented Mar 22, 2019

Closing this issue since its resolved. Feel free to reopen if have further problems. Thanks!

@ymodak ymodak closed this as completed Mar 22, 2019
@Ezra521
Copy link

Ezra521 commented Apr 24, 2019

I am having some trouble with this error and I don't know what to do. I am having a hard time because the way I am saving my trained model is different.

What should I do?

Saving

model_json = model.to_json()
with open("model_pretrained.json", "w") as json_file:
json_file.write(model_json)

model.save_weights("model_pretrained.h5")
print("Saved model to disk")

Loading

json_file = open('model_pretrained.json', 'r')
loaded_model_json = json_file.read()
json_file.close()

loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_pretrained.h5")

ValueError: Unknown activation function:relu6

i don't know,

@Ruomei
Copy link

Ruomei commented Aug 11, 2020

TF: 1.2.1-gpu
Keras: 2.0.6

I am trying to retrain the MobileNet modle from the latest version using the following script,

Training.py
https://gist.github.com/rtao/50eb8c96b06f4deddec2b7888da1d062

And when I try to load it back with the following, I got an error of "ValueError: Unknown activation function:relu6"

Predict.py
https://gist.github.com/rtao/c6773e7e430552a54a3812d5fae91bfe

@rtao
I am wondering whether you have managed to reproduce the accuracy results after training mobilenet from scratch.

@fchollet
Isn't the graph we import from keras applications only for inference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests