keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.11k stars 19.36k forks source link

Unable to make two instances of the MobileNetV3 within the same model #19689

Closed DLumi closed 3 weeks ago

DLumi commented 3 weeks ago

I'm trying to use two distinct instances of mobilenet as sub-modules of one bigger model.

This is the error I receive. ValueError: The name "MobilenetV3small" is used 2 times in the model. All operation names should be unique.

For some weird reason, Keras does not handle name uniqueness automatically, nor does it give you the option to specify model name. I've tested it on MobileNetV3Small specifically, but I guess it's the case for all other models within the applications API.

See the code in a colab notebook below:

https://colab.research.google.com/drive/11R7m0yULUPhlN52ligdsPK18Mh6vKUCq?usp=sharing

P.S. I've tried to interface with _name attribute for each layer of the model, but it gets even worse from there, as it renders you unable to save the model throwing even more errors at you.

jeffcarp commented 3 weeks ago

Thanks for the repro! Here's a smaller one:

x = keras.Input((None, None, 3))
out1 = keras.layers.Flatten(name='a')(x)
out2 = keras.layers.Flatten(name='a')(x)
keras.Model(inputs=x, outputs=[out1, out2])

I think when the layer or model name is hard-coded in a functional model (like it is here for MobileNetV3), it won't be auto-deduplicated. I think the fix here is to remove the hard-coded name.

google-ml-butler[bot] commented 3 weeks ago

Are you satisfied with the resolution of your issue? Yes No