tfjs: Error import Keras Mobilenet model: Unknown layer: BatchNormalizationV1

Hi everyone

I have a problem when run keras mobinet model in tensorflow js.

Browser raise exception:

errors.ts:48 Uncaught (in promise) Error: Unknown layer: BatchNormalizationV1. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
    at new t (errors.ts:48)
    at deserializeKerasObject (generic_utils.ts:239)
    at deserialize (serialization.ts:31)
    at u (container.ts:1335)
    at t.fromConfig (container.ts:1362)
    at deserializeKerasObject (generic_utils.ts:274)
    at deserialize (serialization.ts:31)
    at models.ts:287
    at common.ts:14
    at Object.next (common.ts:14)

My model train by python with keras application mobilenet

mobileModel = keras.applications.mobilenet.MobileNet(weights=None,classes=4, input_shape=(150,150,3))
mobileModel.compile(optimizer=tf.keras.optimizers.Adam(0.01), 
              loss='binary_crossentropy',
              metrics=['accuracy'])

ref: https://keras.io/applications/#mobilenet

There is my h5 file: https://drive.google.com/file/d/10YmV7jfR0ooPxzXUbD1s1Y0X1FjRLeyz/view?usp=sharing

TensorFlow.js version: 0.15.1 Browser: chrome Version 72.0.3626.109 (Official Build) (64-bit)

Thank you very much

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 30 (1 by maintainers)

Most upvoted comments

It is fixed but now we get a different error

ValueError: Improperly formatted model config.

😦 so frustrating

@RadEdje You can replace BatchNormalizationV1 to BatchNormalization in your model.json. But I not sure about accuracy of model after that. You should turn back to tensorflow 1.12.x and tensorflowjs 0.8.5 (python). For javascript site, >0.13. But not sure that accuracy still same with python. You must re-evaluate in javascript. As my experience, it not the same. I dont know that because of my code or tensorflow-converter. So be careful with it, and please confirm if your model is ok or not.

thanks for the quick reply.

i’m NOW stuck with that second type of error (the one that gets past the batchNormalizationV1 bug).

i’m now at the

Error: Improperly formatted model config for layer

Any suggestions on how to fix this?

How do I force the version of tensorflowjs?

referring to your suggestion earlier to use version 0.8.5

tensorflowjs 0.8.5 (python)

do I just do the following?

pip install tensorflowjs@0.8.5

thanks again.

@SanthoshRajendiran For tflite-related problems, please file issues at https://github.com/tensorflow/tensorflow/issues

For TensorFlow.js, we are considering adding a logic in our converter to take care of the wrong class names such as BatchNormalizationV1.

Just to be clear, the final release of tensorflow 1.13 (1.13.0) that’s coming out soon should fix this issue.