Mask_RCNN icon indicating copy to clipboard operation
Mask_RCNN copied to clipboard

Unable to convert .h5 model to ONNX for inferencing through any means

Open pallavimohansaab opened this issue 4 years ago • 4 comments

I built a custom MaskRCNN model in .h5 using this code. I managed to save the full model and not the weights alone using model.keras_model.save(), and assume it worked correctly.

I need to convert this model to ONNX to inference in Unity Barracuda, and I have been hitting several errors along the way. I tried:

  1. .h5 to ONNX using this tutorial and the keras2onnx package, and I hit an error at:

    model = load_model('model.h5')
    
    Error:
    ValueError: Unknown layer: BatchNorm
    
  2. I tried defining custom layers using this GitHub code:

     model = keras.models.load_model(r'model.h5', custom_objects={'BatchNorm':BatchNorm, 
    'tf':tf, 'ProposalLayer':ProposalLayer, 
    'PyramidROIAlign1':PyramidROIAlign1, 'PyramidROIAlign2':PyramidROIAlign2,
    'DetectionLayer':DetectionLayer}, compile=False)
    
    Error:
    ValueError: No model found in config file.
    ValueError: Unknown layer: PyramidROIAlign
    
  3. .h5 to .pb (frozen graph) and .pbtxt, and then from .pb to ONNX using tf2onnx after finding input and output nodes (seems to be only one of each?):

    assert d in name_to_node, "%s is not in graph" % d
    AssertionError: output0 is not in graph
    

It seems that keras.models.load_model() throws the first two errors - wondering if there is a way I can work with the .pb/.pbtxt model, or a way around without using load_model(), or a way to solve the load_model() issue?

Is there a way to convert my custom .h5 model to ONNX through any direct/indirect means? I have been stuck on this for days! I find no issue here seems to have a solution that works.

Thanks in advance.

pallavimohansaab avatar Sep 01 '21 03:09 pallavimohansaab

Any progress so far ?

ghost avatar Sep 10 '21 10:09 ghost

Hi there @pallavimohansaab and @CodeMonkey3435 did you manage to solve this by now? I am currently stuck at this very issue and would be more than happy to read about a solution...

Thanks in advance! :)

EyGy avatar May 27 '22 12:05 EyGy

No I have never solved this. If it does not work, it is probably because some layers just cannot be converted so I would not poor too much effort into this. A workaround for me was to use an entirely different model that does not do instance segmentation but only semantic segmentation (I think it was U Net). I was able to convert it to a tflite model, since the actual goal was to run it on an NPU and it reached incredible speeds. Sorry, I guess this is really not what you are looking for but I REALLY spent a lot of time figuring this out.

ghost avatar May 27 '22 13:05 ghost

Thank you for the answer! I am hard stuck for 2 days on trying to get my custom MaskRCNN running in openCV and hoped going over onnx would be a possible workaround. I do need instance segmentation but i guess i am gonna look for another Model as well, since this seems to be an utterly hopeless effort. So thank you :)

EyGy avatar May 27 '22 15:05 EyGy