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

Unable to load mobilenetv2 version #20

Open
karllandheer opened this issue Nov 2, 2022 · 9 comments
Open

Unable to load mobilenetv2 version #20

karllandheer opened this issue Nov 2, 2022 · 9 comments

Comments

@karllandheer
Copy link

Hello, when I try to load a model which used a mobilenetv2 backbone:

model = tensorflow.keras.models.load_model(MobileNetModel.h5',compile=False)

I get this issue:

ValueError: Unknown layer: DepthwiseConv3D. Please ensure this object is passed to the custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

Any ideas on how to fix this?

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 2, 2022

You must first create the model after load weights:

from classification_models_3D.tfkeras import Classifiers
modelPoint, preprocess_input = Classifiers.get('mobilenetv2')
model = modelPoint(input_shape=(128, 128, 128, 3), include_top=False, weights=None)
model.load_weights('MobileNetModel.h5')

You can try like this, but I see it doens't work for me:

from classification_models_3D.models._DepthwiseConv3D import DepthwiseConv3D
model = tensorflow.keras.models.load_model(
    'MobileNetModel.h5',
    custom_objects={'DepthwiseConv3D': DepthwiseConv3D}
)

@karllandheer
Copy link
Author

Hello, thank you for your prompt response. If I try your code (modified to my shape):

from classification_models_3D.tfkeras import Classifiers
modelPoint, preprocess_input = Classifiers.get('mobilenetv2')
model = modelPoint(input_shape=(32, 128, 128, 1), weights=None, encoder_weights=None)
model.load_weights('MobileNetModel.h5')

I get this error:

You are trying to load a weight file containing 125 layers into a model with 105 layers.

My model has been instantiated with this code:

model = sm.Unet('mobilenetv2',weights=None,input_shape=input_model_shape,encoder_weights=None)

Could I just use the above line, and do model.load_weights('MobileNetModel.h5')? I ask because when I tried this my model spits out all 0s, even though the training IOU is ~0.7. Wondering if perhaps this method of loading weights isn't quite right?

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 2, 2022

You didn't say in first message which model you used.

Try this:

model = sm.Unet('mobilenetv2',weights=None,input_shape=input_model_shape,encoder_weights=None)
model.load_weights('MobileNetModel.h5')

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 2, 2022

Check if during training and inference you have the same preprocessing of volumes.

@karllandheer
Copy link
Author

Hello, I have identical preprocessing of volumes. For example if I run this:

Screen Shot 2022-11-02 at 12 47 31 PM

But when I run model.evaluate I get this (this agrees with what I get when I use model.predict):

MicrosoftTeams-image

Not sure if this is related to segmentation_models_3D or keras more broadly

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 2, 2022

Unfortunately I don't know why result is like this... (

@karllandheer
Copy link
Author

Interesting, thank you for your response. Have you trained models with mobilenetv2? Perhaps it's an inherent bug related to this model. This seems to be exactly the issue I am getting:

keras-team/keras#6977

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 2, 2022

No I never tried mobilenetv2 model for 3D tasks. I propose you to try resnet18_3D first. It also not so large and may be you avoid this problem.

@karllandheer
Copy link
Author

OK, I will try that and get back to you. I tried resnet18 first but it was training slow so I swapped, but maybe this is an inherent issue to mobilenet

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

2 participants