tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Provided weight data has no target variable: batch_normalization

Open rajeev-samalkha opened this issue 8 years ago • 28 comments

To get help from the community, check out our Google group.

TensorFlow.js version

0.13

Browser version

Chrome Version 69.0.3497.100

Describe the problem or feature request

I converted a Keras model to tfjs using python utility with no errors. But when I try to load the model in tfjs, I get the following error:

[email protected]:2 Uncaught (in promise) Error: Provided weight data has no target variable: batch_normalization_1_2/gamma
    at new t ([email protected]:2)
    at loadWeightsFromNamedTensorMap ([email protected]:2)
    at t.loadWeights ([email protected]:2)
    at [email protected]:2
    at [email protected]:2
    at Object.next ([email protected]:2)
    at i ([email protected]:2)

Code to reproduce the bug / link to feature request

Running it on local machine. model = await tf.loadModel(<path_to_model.json>)

rajeev-samalkha avatar Oct 03 '18 09:10 rajeev-samalkha

Hi Can you please share your (original) model and the commands used to convert & load? Thanks

bileschi avatar Oct 04 '18 13:10 bileschi

If that weight is an extra one that is lying around for some reason but is not actually needed, you can call tf.loadModel(..., strict=false) to disable the error.

Of course, if the weight is needed, doing this would leave you with a broken model. In that case, as @bileschi said, we'd need to see the original Keras model to determine whether there is a conversion bug.

davidsoergel avatar Oct 05 '18 15:10 davidsoergel

@rajeev-samalkha is this issue resolved? If so feel free to close. Thankyou.

bileschi avatar Oct 09 '18 15:10 bileschi

Folks, sorry for delayed response.

When I take out Batch Norm layer then it seems to work fine (I had to retrain the model). Is there any difference between Batch norm in tf.keras and tfjs. I have used tensorflowjs.converter utility. The model in itself is quite simple with few Con2D layers interspersed by Max Pool/Dropout.

Regards Rajeev

rajeev-samalkha avatar Oct 10 '18 11:10 rajeev-samalkha

TFJS BatchNorm maintains 4 up to weights: gamma, beta, movingMean, and movingVariance which matches those in keras-team/keras

https://github.com/keras-team/keras/blob/master/keras/layers/normalization.py#L93

I wonder if tf.keras is saving additional tensors, possibly for optimization or related to the momentum for training?

Looking at the tensorflow/keras implementation, I see that it is somewhat more complex: including a 'fused' batch-norm implementation that reaches into c. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/layers/normalization.py

Can you list out the weights in your model from the python code?

bileschi avatar Oct 10 '18 13:10 bileschi

Do you need weights for all the layers or just BatchNorm one.

rajeev-samalkha avatar Oct 10 '18 14:10 rajeev-samalkha

Just the names and sizes of the batch_norm related weights should be fine. No need to provide the actual values.

On Wed, Oct 10, 2018 at 10:51 AM rajeev-samalkha [email protected] wrote:

Do you need weights for all the layers or just BatchNorm one.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tfjs/issues/755#issuecomment-428602906, or mute the thread https://github.com/notifications/unsubscribe-auth/AAhZTn53dfvl1D88ywKN59zRuoDWERpgks5ujglrgaJpZM4XFl_7 .

-- Stan Bileschi Ph.D. | SWE | [email protected] | 617-230-8081

bileschi avatar Oct 10 '18 15:10 bileschi

I get the same error while loading a keras converted model on browser. following error pops up embeddings_issue

As for the model weights: model_issue_embedding

Note: Some of my converted model tend to work fine on browser having embeddings in it, but sometimes this error shows up. @bileschi @davidsoergel kindly post a proper fix for this issue.

Raza25 avatar Oct 24 '18 05:10 Raza25

Same error here, Someone please help.

[email protected]:2 Uncaught (in promise) Error: Provided weight data has no target variable: conv2d/kernel at new t ([email protected]:2) at loadWeightsFromNamedTensorMap ([email protected]:2) at t.loadWeights ([email protected]:2) at [email protected]:2 at [email protected]:2 at Object.next ([email protected]:2) at i ([email protected]:2)

image

image

ashsharma28 avatar Oct 24 '18 08:10 ashsharma28

I hit the same error without batch norm. Appreciate your help.

capture

rajeev-tbrew avatar Oct 25 '18 10:10 rajeev-tbrew

Folks

I think I found why we are getting this error. The error can happen for any layer. Steps to reproduce the error:

  1. Load the model in tensorFlow using tf.keras.
  2. Load the same model again (basically load the model more than once).
  3. Use tfjs.converters to convert keras model and you get this error.

It seems every layer name changes in model.json file (it will be different than model.summary name). For example, one of the layer in my model was 'conv2d_6' but it got named as 'conv2d_6_2' when I loaded the model twice. But it seems actual weights (assuming in shard file) still expect 'conv2d_6 in my case.

So till we get a fix, pls make sure you load your model only once before doing tfjs conversion. Hope this helps.

rajeev-tbrew avatar Oct 25 '18 11:10 rajeev-tbrew

I have tried that too by loading the model exactly once but still, the same error prevails.

hardikmodi1 avatar Oct 29 '18 07:10 hardikmodi1

Not sure if this helps, but when I converted my h5 model using the python code `import tensorflowjs as tfjs from keras.models import load_model

modelk = load_model('./input/model.h5')
tfjs.converters.save_keras_model(modelk, './output/')`

I would receive the following error:

errors.ts:48 Uncaught (in promise) Error: Provided weight data has no target variable: dense_1_7/kernel at new t (errors.ts:48) at loadWeightsFromNamedTensorMap (container.ts:190) at t.loadWeights (container.ts:759) at models.ts:285 at index.ts:79 at Object.next (index.ts:79) at i (index.ts:79)

But if I convert the h5 model using the tensorflowjs_converter command line tool my tfjs json model file will load without any problems.

stephenrt42 avatar Nov 24 '18 04:11 stephenrt42

model summary

image

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const path = require('path');

async function load(){
  await tf.loadModel(`file://${path.join('xxx', 'model.json')}`);
}

load()

error

(node:72812) UnhandledPromiseRejectionWarning: Error: Provided weight data has no target variable: conv2d_10_1/kernel

desenmeng avatar Dec 17 '18 02:12 desenmeng

@demohi Can you try using setting the strict argument to false, i.e.,

  await tf.loadModel(`file://${path.join('xxx', 'model.json')}`, false);

Also, this might be a bug in loadModel. Can you provide the weight and JSON file to us so we may try reproducing this issue on our end? Thanks.

caisq avatar Dec 17 '18 03:12 caisq

@caisq Thank you for your reply. It works.

you can convert this keras model to fix the bug.

desenmeng avatar Dec 17 '18 04:12 desenmeng

@demohi I'm looking into this issue now. It seems the cause to do with the following fact:

  • The model has a layer with the name conv2d_10, however
  • One of the weights for that layer is named conv2d_10_1/kernel in the model.h5 file. So there is the extra suffix _1

This is the reason why the weight loading fails and you get the error. Can you tell me a little about how the model is saved from Python side? Is it possible that there are multiple instances of the model existing in Python memory?

I think we need to fix this issue regardless of what happens on the Python side, as Python Keras / TensorFlow can load this sort of model correctly. But I just want to understand the conditions under which this kind of name mismatches happen. Thanks.

caisq avatar Dec 22 '18 04:12 caisq

@caisq

I use the colab to train this model with keras.

// keras model
model.save('xxx.h5')

desenmeng avatar Dec 22 '18 05:12 desenmeng

@demohi At the risk of asking too much, I wonder whether you could try running the same code but from a Python file (or reset the state of the CoLab kernel and run the code from scratch, making sure that each code block is run only once.) I expect the name mismatch to disappear in those cases.

Again, don't feel obliged to try that. But if you do have time to try it and let me know, it would be wonderful.

We'll work on a fix in the meantime.

caisq avatar Dec 22 '18 05:12 caisq

I am facing the same issue. It appears to happen when I run the tensorflowjs_converter command (with os.system) while having the model loaded from the same input keras .h5 file. If I run tensorflowjs_converter separately after the python program is over from the shell, it looks to work fine

ekchatzi-pointr avatar Dec 24 '18 01:12 ekchatzi-pointr