ZFTurbo / segmentation_models_3D

Set of models for segmentation of 3D volumes
MIT License
122 stars 26 forks source link

Unable to load mobilenetv2 version #20

Open karllandheer opened 1 year ago

karllandheer commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

karllandheer commented 1 year ago

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 commented 1 year ago

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

karllandheer commented 1 year ago

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:

https://github.com/keras-team/keras/issues/6977

ZFTurbo commented 1 year ago

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 commented 1 year ago

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