Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error importing a keras model: Unknown layer: Dot #218

Closed
huguesg12 opened this issue Apr 23, 2018 · 17 comments
Closed

Error importing a keras model: Unknown layer: Dot #218

huguesg12 opened this issue Apr 23, 2018 · 17 comments
Assignees

Comments

@huguesg12
Copy link

0.9.0, (and also 0.8.0)

Firefox & Chrome

I tried implementing the 'How to import a Keras Model into TensorFlow.js' tutorial, but could not manage to import my model, and got an: 'Uncaught (in promise) Error: Constructing tensor of shape (28) should match the length of values (22)' which I cannot make sense of. In particular, 28 is the actual number of output units of my model, but I really do not understand where that 22 comes from.
The test page is at: http://huguesg.fr/incidents/test.html
The model.json file is: http://huguesg.fr/incidents/model.json and the 3 shards are also in the same folder.
I tried creating the files both with the tfjs.converters.save_keras_model function or the tensorflowjs_converter function after saving the model as .h5, but neither worked.

@bileschi
Copy link
Collaborator

I am able to reproduce this issue. Looking into it.

@huguesg12
Copy link
Author

Thanks. If necessary, I also dropped the .h5 file at http://huguesg.fr/incidents/onehot29model.h5

@bileschi
Copy link
Collaborator

The issue seems to be related to missing bytes in the weights files.
The manifiest claims that the first groupBuffer contains two weights, "dense_1/kernel" with size [64, 28], and "dense_1/bias" with size [28]. It expects the first 7168 = 4 * 64 * 28 bytes to be the kernel, and the next 112 = 4 * 28 bytes to be the bias. Thus, it's looking for 7280 = 7168 + 112 bytes in the first weights file. However, the file 'group1-shard1of1' only contains 7256 bytes. This is representing as a shape error when building the tensor.

Now, the mystery is why the weights file is fewer bytes than we expect! My next step is to look at the .h5 file to see if it's contains any clues.

@bileschi
Copy link
Collaborator

h5py seems to indicate that the weights are the correct sizes.

I then tried converting locally, and it seems to have created a group1 file with the correct size. Can you try making sure your converter is up to date (pip install) and running again on your .h5 file?

$ tensorflowjs_converter --input_format keras ./onehot29model.h5 .

...

$ \ls -al
total 832
drwxr-xr-x   7 bileschi  eng     224 Apr 23 16:57 .
drwxr-xr-x+ 48 bileschi  eng    1536 Apr 23 16:55 ..
-rw-r--r--   1 bileschi  eng    7280 Apr 23 16:57 group1-shard1of1
-rw-r--r--   1 bileschi  eng    6464 Apr 23 16:57 group2-shard1of1
-rw-r--r--   1 bileschi  eng   82944 Apr 23 16:57 group3-shard1of1
-rw-r--r--   1 bileschi  eng    3232 Apr 23 16:57 model.json
-rw-r--r--@  1 bileschi  eng  318256 Apr 23 16:56 onehot29model.h5

@huguesg12
Copy link
Author

huguesg12 commented Apr 23, 2018 via email

@Rafi993
Copy link

Rafi993 commented Apr 24, 2018

I do have similar issue. After importing when I run the code it says

tfjs@0.9.0:1 Uncaught (in promise) Error: Unknown layer: Dot
    at new t (tfjs@0.9.0:1)
    at Object.n.deserializeKerasObject (tfjs@0.9.0:1)
    at Object.n.deserialize (tfjs@0.9.0:1)
    at p (tfjs@0.9.0:1)
    at t.fromConfig (tfjs@0.9.0:1)
    at Object.n.deserializeKerasObject (tfjs@0.9.0:1)
    at Object.n.deserialize (tfjs@0.9.0:1)
    at tfjs@0.9.0:1
    at r (tfjs@0.9.0:1)
    at Object.next (tfjs@0.9.0:1)

@Rafi993
Copy link

Rafi993 commented Apr 24, 2018

I did try updating tfjs to version 0.10.2. I do seem to get the following error now. It would be really helpful if anyone can solve this. It is driving me nuts


tf.js:531 Uncaught (in promise) Error: No backend found in registry.
    at Environment.getBestBackendType (tf.js:531)
    at Environment.evaluateFeature (tf.js:547)
    at Environment.get (tf.js:522)
    at Environment.initDefaultBackend (tf.js:634)
    at Environment.get [as engine] (tf.js:626)
    at Object.Tracking.tidy (tf.js:7701)
    at Object.descriptor.value [as randomUniform] (tf.js:4359)
    at Object.randomUniform (tf.js:9847)
    at RandomUniform.apply (tf.js:15275)
    at Embedding.Layer.addWeight (tf.js:11977)
Environment.getBestBackendType @ tf.js:531
Environment.evaluateFeature @ tf.js:547
Environment.get @ tf.js:522
Environment.initDefaultBackend @ tf.js:634
get @ tf.js:626
Tracking.tidy @ tf.js:7701
descriptor.value @ tf.js:4359
randomUniform @ tf.js:9847
RandomUniform.apply @ tf.js:15275
Layer.addWeight @ tf.js:11977
Embedding.build @ tf.js:16635
(anonymous) @ tf.js:11891
nameScope @ tf.js:11151
nameScope @ tf.js:10364
Layer.apply @ tf.js:11883
Sequential.add @ tf.js:19825
Sequential.fromConfig @ tf.js:19992
deserializeKerasObject @ tf.js:20582
deserialize @ tf.js:19125
processLayer @ tf.js:12885
Container.fromConfig @ tf.js:12902
deserializeKerasObject @ tf.js:20582
deserialize @ tf.js:19125
(anonymous) @ tf.js:19745
step @ tf.js:19720
(anonymous) @ tf.js:19701
(anonymous) @ tf.js:19695
__awaiter @ tf.js:19691
modelFromJSON @ tf.js:19735
(anonymous) @ tf.js:19786
step @ tf.js:19720
(anonymous) @ tf.js:19701
fulfilled @ tf.js:19692
Promise.then (async)
(anonymous) @ index.js:68

@Rafi993
Copy link

Rafi993 commented Apr 24, 2018

This is the model I have been trying to convert https://github.com/priya-dwivedi/Deep-Learning/blob/master/qa_chat_bot/model6.h5

@dsmilkov
Copy link
Contributor

Hi @Rafi993 , regarding the "No backend found in registry." error - there was a problem with 0.10.2. We just released 0.10.3 and that problem should (hopefully) go away.

@Rafi993
Copy link

Rafi993 commented Apr 24, 2018

thanks @dsmilkov but after updating to 0.10.3 I have the error


Uncaught (in promise) Error: Unknown layer: Dot
    at new t (tfjs@0.10.3:1)
    at Object.n.deserializeKerasObject (tfjs@0.10.3:1)
    at Object.n.deserialize (tfjs@0.10.3:1)
    at p (tfjs@0.10.3:1)
    at t.fromConfig (tfjs@0.10.3:1)
    at Object.n.deserializeKerasObject (tfjs@0.10.3:1)
    at Object.n.deserialize (tfjs@0.10.3:1)
    at tfjs@0.10.3:1
    at r (tfjs@0.10.3:1)
    at Object.next (tfjs@0.10.3:1)

This was the trained model https://gist.github.com/Rafi993/aff598b4c06a4a70a4893f4ee62c25f4#file-trained-model-L140 . the error was in Line 140 "class_name" with value "Dot"

@huguesg12
Copy link
Author

huguesg12 commented Apr 24, 2018 via email

@bileschi
Copy link
Collaborator

I was able to load the model using 0.10.3.

Can you report the size of the weight shards created by converter? Can you also make sure that your browser is not caching older, incorrect values of the weights?

The sizes converter created here are:

-rw-r--r--   1 bileschi primarygroup    7280 Apr 24 10:13 group1-shard1of1
-rw-r--r--   1 bileschi primarygroup    6464 Apr 24 10:13 group2-shard1of1
-rw-r--r--   1 bileschi primarygroup   82944 Apr 24 10:13 group3-shard1of1

You can force reloading of the cached elements in chrome with ctl + shift + r .

@huguesg12
Copy link
Author

huguesg12 commented Apr 24, 2018 via email

@caisq caisq changed the title Error importing a keras model Error importing a keras model: Unknown layer: Dot Sep 27, 2018
@caisq caisq self-assigned this Sep 28, 2018
@caisq
Copy link
Collaborator

caisq commented Sep 28, 2018

Began working on this. Will send a PR soon.

@freekoy
Copy link

freekoy commented Nov 6, 2018

Same problem?How to solve it?
`
io_utils.ts:116 Uncaught (in promise) RangeError: byte length of Float32Array should be a multiple of 4
at new Float32Array ()
at o (io_utils.ts:116)
at Object.decodeWeights (io_utils.ts:79)
at e. (frozen_model.ts:109)
at exports_regularizers.ts:47
at Object.next (exports_regularizers.ts:47)
at s (exports_regularizers.ts:47)

o @ io_utils.ts:116
decodeWeights @ io_utils.ts:79
(anonymous) @ frozen_model.ts:109
(anonymous) @ exports_regularizers.ts:47
(anonymous) @ exports_regularizers.ts:47
s @ exports_regularizers.ts:47
Promise.then (async)
c @ coco-ssd.esm.js:2
(anonymous) @ coco-ssd.esm.js:2
__awaiter @ coco-ssd.esm.js:2
load @ coco-ssd.esm.js:2
window.onload @ index.js:26
load (async)
parcelRequire.index.js.@tensorflow-models/coco-ssd @ index.js:26
newRequire @ demo.e31bb0bc.js:49
(anonymous) @ demo.e31bb0bc.js:81
(anonymous) @ demo.e31bb0bc.js:107
`

@YajJackson
Copy link

Also running into this issue when running tf.loadModel('./tfjs-models/MOBILENET/model.json');
MOBILENET/ was generated from tfjs.converters.save_keras_model(mobilenet, './tfjs-models/MOBILENET')

Uncaught (in promise) RangeError: byte length of Float32Array should be a multiple of 4
    at new Float32Array (<anonymous>)
    at o (io_utils.ts:116)
    at Object.decodeWeights (io_utils.ts:79)
    at models.ts:287
    at index.ts:68
    at Object.next (index.ts:68)
    at o (index.ts:68)

Any help would be much appreciated.

@dsmilkov dsmilkov reopened this Nov 20, 2018
@caisq
Copy link
Collaborator

caisq commented Nov 20, 2018

I don't think the issue that @YajJackson and @freekoy is running into is related to the Dot layer. The issue is being tracked in #924

Closing this PR again.

@caisq caisq closed this as completed Nov 20, 2018
nsthorat pushed a commit that referenced this issue Aug 19, 2019
nsthorat pushed a commit that referenced this issue Aug 19, 2019
* 0.3.x: Update the publish-npm script to allow publishing from the release branch. (#203)

DEV

* Upgrade 0.3.x to 0.15.3 (#210)


<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/210)
<!-- Reviewable:end -->

* Fix win GPU packaging. (#208) (#211)

Turns out that the windows GPU builds for TensorFlow 1.12 lack the directory structure and eager headers. A bug has been filed with core TF - but we should bake in some fixes for this.

This PR simply refactors the downloading logic to a new file. I'd like to use this logic in the node-gles package as well (maybe worth releasing as a stand-alone package in the future).

After the refactoring, I check the directory structure in Windows. If the folder structure is missing, but the required tensorflow.dll exists - I re-create the directory structure, move and download the proper header files.

The screenshot below shows the contents of TF 1.12 Windows GPU:
![capture](https://user-images.githubusercontent.com/306276/53048799-719f4b80-344a-11e9-9004-3eef2446a246.PNG)

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/208)
<!-- Reviewable:end -->

* Bump 0.3.1

* Add TensorBoard callback for model training: tf.node.tensorBoard() (#202) (#213)

FEATURE

See example screenshot:
![image](https://user-images.githubusercontent.com/16824702/52491877-19d52a80-2b96-11e9-8c24-5a403c2450d3.png)

Fixes #686

* [0.3.x] Upgrade nyc package fo fix lodash security issue. (#218) (#219)

Bump for 0.3.x so we can get a security release spun.

https://github.com/tensorflow/tfjs-node/network/alert/yarn.lock/lodash/open

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/219)
<!-- Reviewable:end -->

* Bump to 0.3.2

* Upgrade TS libraries and change binding from typings file to plain
TypeScript definition.

* Upgrade TS dependencies

* save

* Fix deps-stage

* save

* Revert TS changes and keep binary staging fixes.
nsthorat pushed a commit that referenced this issue Aug 19, 2019
* 0.3.x: Update the publish-npm script to allow publishing from the release branch. (#203)

DEV

* Upgrade 0.3.x to 0.15.3 (#210)


<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/210)
<!-- Reviewable:end -->

* Fix win GPU packaging. (#208) (#211)

Turns out that the windows GPU builds for TensorFlow 1.12 lack the directory structure and eager headers. A bug has been filed with core TF - but we should bake in some fixes for this.

This PR simply refactors the downloading logic to a new file. I'd like to use this logic in the node-gles package as well (maybe worth releasing as a stand-alone package in the future).

After the refactoring, I check the directory structure in Windows. If the folder structure is missing, but the required tensorflow.dll exists - I re-create the directory structure, move and download the proper header files.

The screenshot below shows the contents of TF 1.12 Windows GPU:
![capture](https://user-images.githubusercontent.com/306276/53048799-719f4b80-344a-11e9-9004-3eef2446a246.PNG)

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/208)
<!-- Reviewable:end -->

* Bump 0.3.1

* Add TensorBoard callback for model training: tf.node.tensorBoard() (#202) (#213)

FEATURE

See example screenshot:
![image](https://user-images.githubusercontent.com/16824702/52491877-19d52a80-2b96-11e9-8c24-5a403c2450d3.png)

Fixes #686

* [0.3.x] Upgrade nyc package fo fix lodash security issue. (#218) (#219)

Bump for 0.3.x so we can get a security release spun.

https://github.com/tensorflow/tfjs-node/network/alert/yarn.lock/lodash/open

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/tensorflow/tfjs-node/219)
<!-- Reviewable:end -->

* Bump to 0.3.2

* Upgrade TS libraries and change binding from typings file to plain
TypeScript definition.

* Upgrade TS dependencies

* save

* Fix deps-stage

* save

* Revert TS changes and keep binary staging fixes.

* Don't use a definition file for the bindings.

This causes many issues and doesn't help with redistribution. It looks
like exporting a local definition file on top of what else is exported
is not a common supported TypeScript use case. This fix simply moves
defnitions into a normal TypeScript file.

This should fix: #1092

* save

* Add typescript integration project.

* save

* Add license
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants