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

Keras mobilenet can't be imported (missing Relu6 / DepthwiseConv2D) #38

Closed
luke14free opened this issue Oct 8, 2017 · 24 comments
Closed
Assignees
Labels
bug Unexpected behaviour that should be corrected (type)

Comments

@luke14free
Copy link

luke14free commented Oct 8, 2017

I have a custom trained MobileNet network from Keras and I bump into an issue about CoreML Tools not recognizing Relu6 as an activation function.

my keras model is something like:

import keras
from keras.layers.core import Flatten
initial_model = keras.applications.mobilenet.MobileNet(input_shape=(size, size, 3), include_top=False, weights='imagenet', classes=2)
last = initial_model.output
x = Flatten()(last)
preds = Dense(2, activation='softmax')(x)

model = Model(initial_model.input, preds)

And after training I try to convert it with:

from keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
      convert = coremltools.converters.keras.convert("model.h5", input_names=['img'], image_input_names=['img'], class_labels=['class1', 'class2'])

But this raises:

RuntimeError: Unsupported option activation=relu6 in layer Dense(conv1_relu)

Because coreML Tools doesn't know what DepthwiseConv2D and Relu6 are.

@srikris srikris added the bug Unexpected behaviour that should be corrected (type) label Oct 8, 2017
@luke14free
Copy link
Author

Actually the only missing portion is the ReLU6, the DepthwiseConv2D is already there.

@slin07
Copy link
Contributor

slin07 commented Oct 10, 2017

PR#44 is up, should fix this issue.

@luke14free
Copy link
Author

@slin07 yes! I tested with #44 and I am properly able to convert the model and it works quite well in my dummy frame categorizer app. 👍

@slin07
Copy link
Contributor

slin07 commented Oct 11, 2017

PR #44 has been merged. Closing this issue.

@slin07 slin07 closed this as completed Oct 11, 2017
@seantempesta
Copy link

So I just ran into something similar:

ValueError: Unknown activation function:relu6

And I just built from source. @luke14free did you have to do anything else to convert your model?

@slin07
Copy link
Contributor

slin07 commented Nov 14, 2017

Can you provide (1) trace stack for this error and (2) the Keras version you're using?

@luke14free
Copy link
Author

@seantempesta are you doing this?

from keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
     ...

@seantempesta
Copy link

Full stack trace below. I was loading just the keras .h5 file.

(pythonenv) bash-3.2$ python coreml.py 
WARNING:root:Keras version 2.0.8 detected. Last version known to be fully compatible of Keras is 2.0.6 .
WARNING:root:TensorFlow version 1.4.0 detected. Last version known to be fully compatible is 1.2.1 .
Traceback (most recent call last):
  File "coreml", line 10, in <module>
    image_scale=1/255.)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 505, in convert
    predicted_probabilities_output = predicted_probabilities_output)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 161, in _convert
    model = _keras.models.load_model(model)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/models.py", line 239, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/models.py", line 313, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/engine/topology.py", line 2487, in from_config
    process_layer(layer_data)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/engine/topology.py", line 2473, in process_layer
    custom_objects=custom_objects)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/engine/topology.py", line 1252, in from_config
    return cls(**config)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/layers/core.py", line 283, in __init__
    self.activation = activations.get(activation)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/activations.py", line 95, in get
    return deserialize(identifier)
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/activations.py", line 87, in deserialize
    printable_module_name='activation function')
  File "/Users/sean/src/tmp/Corellian/CoreML/mlvirtualenv/pythonenv/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 159, in deserialize_keras_object
    ':' + function_name)
ValueError: Unknown activation function:relu6

@seantempesta
Copy link

I ended up completing the conversion by just converting the model after building it in Keras and loading the weights. I can't post the entire file, but here's the gist:

##############################################################
############      LET'S DEFINE OUR MODEL      #################
###############################################################
modelBase = applications.MobileNet(weights='imagenet',include_top=False, input_shape = (img_width, img_height, 3))
...
model.load_weights('mobilenet-weights.h5')
import coremltools
coreml_model = coremltools.converters.keras.convert(model)

@lahayeg
Copy link

lahayeg commented Nov 27, 2017

Hi @seantempesta does not seems to work for me. Please provide more sample if possible

@greis
Copy link

greis commented Nov 29, 2017

This seems to fix the issue for me keras-team/keras#7431 (comment)

@tatsuya-ogawa
Copy link

@seantempesta You are probably using coremltools from pip.
It seems that it has not been released yet so you need to build it yourself.

@seantempesta
Copy link

@tatsuya-ogawa: No, I built it from source. Granted that was 19 days ago, so I don't know if you've merged a fix recently?

@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')

@schliffen
Copy link

with tensorflow 1.9 and keras 2.2.0 this is what I did and it worked completely:
from tensorflow.python.keras.models import load_model
from keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.applications import mobilenet
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
loaded_model.load_weights('model_256.h5')

for complete model you should import your model separately, e.g. using model_from_json

@SteveIb
Copy link

SteveIb commented Oct 25, 2018

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?

@schliffen
Copy link

I suggest for conversion use Keras 2.1.6 to import relu6- import relu6 from "keras.applications.mobilenet". the rest is just as I explained above. (tensorflow version does not matter)

@SteveIb
Copy link

SteveIb commented Oct 28, 2018

I suggest for conversion use Keras 2.1.6 to import relu6- import relu6 from "keras.applications.mobilenet". the rest is just as I explained above. (tensorflow version does not matter)

Hi, i downgraded keras to 2.1.6

My code:

import keras

from keras.models import model_from_json

with open('model.json','r') as f:
json = f.read()
loaded_model = model_from_json(json)

from tensorflow.python.keras.models import load_model
from keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.applications import mobilenet
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
loaded_model.load_weights('**********.hdf5')

Im getting the following error:

ValueError: Unknown layer: ReLU

@schliffen
Copy link

schliffen commented Oct 28, 2018 via email

@SteveIb
Copy link

SteveIb commented Oct 28, 2018

You should train it again, with this version

On Oct 28, 2018 10:02 AM, "SteveIb" @.> wrote: I suggest for conversion use Keras 2.1.6 to import relu6- import relu6 from "keras.applications.mobilenet". the rest is just as I explained above. (tensorflow version does not matter) Hi, i downgraded keras to 2.1.6 My code: import keras from keras.models import model_from_json with open('model.json','r') as f: json = f.read() loaded_model = model_from_json(json) from tensorflow.python.keras.models import load_model from keras.applications.mobilenet import MobileNet from tensorflow.python.keras.applications import mobilenet from tensorflow.python.keras.utils.generic_utils import CustomObjectScope with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}): loaded_model.load_weights('*******.hdf5') Im getting the following error: ValueError: Unknown layer: ReLU — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#38 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/ADmxxrE7eeFO5AtZZZeCvZPnRmx3HzC1ks5upWSlgaJpZM4Pxqxu .

I trained again.
File "toTfLite.py", line 26, in
loaded_model = model_from_json(json)

ValueError: Unknown activation function:relu6

@SteveIb
Copy link

SteveIb commented Oct 28, 2018

I'm using this library to train on MobileNet
https://github.com/experiencor/keras-yolo2

@fasecity
Copy link

Had this same issue: my fix:

from pathlib import Path #from tf.keras.models.model_from_json import model_from_json

# Load the json file that contains the model's structure
f = Path('D:\\Class\\mytflite\\Model_forker\\my_model.json')
model_structure = f.read_text()


from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
from tensorflow.python.keras.applications import mobilenet
from tensorflow.python.keras.models import load_model
with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
    # Recreate the Keras model object from the json data
    model = tf.keras.models.model_from_json(model_structure)
    # Re-load the model's trained weights
    model.load_weights('D:\Class\mytflite\Model_forker\my_model.h5')

@prabhanjan215
Copy link

Am facing the issue still

from tensorflow.python.keras.models import load_model
from keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.applications import mobilenet
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):transfer_model = load_model('modeltransfer1.h5')

AttributeError: module 'tensorflow.python.keras.applications.mobilenet' has no attribute 'relu6'

Please help on this

@fasecity
Copy link

fasecity commented Jan 9, 2019

Am facing the issue still

from tensorflow.python.keras.models import load_model
from keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.applications import mobilenet
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):transfer_model = load_model('modeltransfer1.h5')

AttributeError: module 'tensorflow.python.keras.applications.mobilenet' has no attribute 'relu6'

Please help on this

what worked for me is loading the weights and json individually.
` ...
from tensorflow.python.keras.models import load_model
with CustomObjectScope({'relu6': mobilenet.relu6,'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
#------------------------here----------------------------------------------------

Recreate the Keras model object from the json data

model = tf.keras.models.model_from_json(model_structure) #<---- comes from the model.json
# Re-load the model's trained weights
model.load_weights('modeltransfer1_weihts.h5'')`

Birch-san pushed a commit to Birch-san/coremltools that referenced this issue Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behaviour that should be corrected (type)
Projects
None yet
Development

No branches or pull requests