tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.52k stars 1.94k forks source link

tfjs-converter keras model to js Unknown layer:BatchNorm #1752

Closed aohan237 closed 5 years ago

aohan237 commented 5 years ago

i use the converter to transform keras model to js.

but it shows errors as follows:

Unknown layer: BatchNorm. 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().

what should i do to make this?

how can i make the transformed model work? is there any guide or advice or something else for me to follow? cause the js-doc never says about what to do when error occurs

thanks

kangyizhang commented 5 years ago

hi @aohan237

what is the output_format you are using? tfjs_graph_model does not support some layer types. You can try to convert the keras model to tfjs_layers_model if you haven't.

aohan237 commented 5 years ago

@kangyizhang

the output_format is model.json, which is the converter generates

just follow the instructions save the entire model https://www.tensorflow.org/tutorials/keras/save_and_restore_models#save_the_entire_model

You can try to convert the keras model to tfjs_layers_model if you haven't.

how to do this? convert the layer in keras to some stand keras layer, which tfjs have? and then save the model, then convert to js model?

kangyizhang commented 5 years ago

@aohan237

What I mean that when using tensorflowjs_converter pip package to convert your keras model to tfjs format model, there are a list of arguments (https://github.com/tensorflow/tfjs-converter#step-1-converting-a-tensorflow-savedmodel-tensorflow-hub-module-keras-hdf5-or-tfkeras-savedmodel-to-a-web-friendly-format).

You can define input_format and output_format. I think your input_format is keras, and you can set the output format as tfjs_layers_model. You can find more details in the readme.

aohan237 commented 5 years ago

@kangyizhang

ouput format is not the problem.

python model has some custom layers. which tfjs cant know. so load model will be failed.

i must impl these layers in js and register them, so that tfjs can load the model.

kangyizhang commented 5 years ago

thanks @aohan237 for the update. So it seems like https://github.com/tensorflow/tfjs/issues/1753#issuecomment-512640437 is the root cause. I'll close this issue and use the other one for tracking.

aohan237 commented 5 years ago

@kangyizhang some updates. tfjs has a few functions lost compared to tfpy, which makes it very difficulty to do this

kangyizhang commented 5 years ago

@aohan237 can you file another issue and list the missing ops?

aohan237 commented 5 years ago

of course.i will list the missing ops in that issue

PierreNoll commented 3 years ago

I replaced in model.json BatchNorm by BatchNormalization and it seems to work https://github.com/tensorflow/tfjs/blob/31fd388daab4b21c96b2cb73c098456e88790321/tfjs-layers/src/layers/normalization.ts#L255