ValueError: Unknown layer: BatchNorm while loading .h5 saved model

2,637 views
Skip to first unread message

ulha...@gmail.com

unread,
Jun 17, 2020, 6:41:37 AM6/17/20
to Keras-users
Hi,
I am using MaskRCNN trained on coco dataset. I saved the complete model(not only weights) using.

model.keras_model.save('new.h5')

when I load model again using load_model() it shows error

ValueError: Unknown layer: BatchNorm

Can anyone help me with this? \
Thanks

Lance Norskog

unread,
Jun 17, 2020, 5:31:20 PM6/17/20
to ulha...@gmail.com, Keras-users
Which version of Keras and Tensorflow?

Can you post the source?

Cheers,

Lance

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/c61cde6f-5642-4897-8319-831774b15ea5o%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

Matias Valdenegro

unread,
Jun 17, 2020, 5:51:39 PM6/17/20
to keras...@googlegroups.com

You are obviously loading a model that has a custom Batch Normalization layer, as the Keras layer is called BatchNormalization (not BatchNorm), so you should follow instructions from whatever code/library you are using on how to load their models.


Or you could also mention what library/code you are using.

ulha...@gmail.com

unread,
Jun 18, 2020, 12:29:51 AM6/18/20
to Keras-users
using Tensorflow 1.15 and keras 2.0+ (not using tensorflow 2.x as I am using Mask RCNN and it was written in tensorflow 1.x)


On Thursday, June 18, 2020 at 2:31:20 AM UTC+5, Lance Norskog wrote:
Which version of Keras and Tensorflow?

Can you post the source?

Cheers,

Lance

On Wed, Jun 17, 2020 at 3:41 AM <ulha...@gmail.com> wrote:
Hi,
I am using MaskRCNN trained on coco dataset. I saved the complete model(not only weights) using.

model.keras_model.save('new.h5')

when I load model again using load_model() it shows error

ValueError: Unknown layer: BatchNorm

Can anyone help me with this? \
Thanks

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras...@googlegroups.com.

ulha...@gmail.com

unread,
Jun 18, 2020, 12:31:48 AM6/18/20
to Keras-users
I don't know if the model has a custom batch norm layer or not. I was using Mask RCNN architecture available on this link.

Lance Norskog

unread,
Jun 18, 2020, 1:59:06 PM6/18/20
to ulha...@gmail.com, Keras-users
Yes, that file has several custom Layer classes. You need to import that file and use the Keras Custom Objects feature to register the names of the classes. (I have not used this feature.)


Cheers,

Lance Norskog

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/6817a12d-c2e6-432f-87c8-d915c8b6510fo%40googlegroups.com.

Aruna Kote

unread,
Aug 25, 2021, 5:11:12 AM8/25/21
to Keras-users
I am trying to convert keras h5 format to tensorflow saved_model format inorder convert mrcnn model to mlir(tf dialect).


import tensorflow as tf
model = tf.keras.models.load_model('model.h5',custom_objects={'BatchNorm': tf.keras.layers.BatchNormalization})
model.save()


But I am getting error as Mask-RCNN-TF2-master/mrcnn/model.py", line 857, in <lambda>
    lambda t: tf.reshape(t, [tf.shape(t)[0], -1, 2]))(x)
NameError: name 'tf' is not defined

JUAN PABLO PEÑA ARENALES

unread,
Oct 28, 2021, 11:23:46 PM10/28/21
to Keras-users
Hi Aruna,

did you get how to solve it? I am running into the same and I have no idea how.

Thanks

Reply all
Reply to author
Forward
0 new messages