question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Unknown layer: TensorFlowOpLayer" in Python Keras converted model using Python indexing

See original GitHub issue

TensorFlow.js version

Python: 1.3.1.1 Javascript: 1.3.0

Browser version

Chromium Version 78.0.3904.70 (Official Build) Built on Ubuntu , running on LinuxMint 19.2 (64-bit) Also tested on Firefox

Describe the problem or feature request

Failing to load model in tfjs when using a tensorflow keras model converted from Python to tfjs when the python model contains indexing (and more generally basic tensorflow ops).

Code to reproduce the bug / link to feature request

from tensorflow.keras import layers
from tensorflow import keras

inputs = keras.Input(shape=[10,])
outputs = layers.Dense(20)(inputs)
outputs = outputs[:, 3]
model = keras.models.Model(inputs=inputs, outputs=outputs)

model.compile(loss='sparse_categorical_crossentropy',
              optimizer=keras.optimizers.RMSprop())

model.summary()
model.save('test_model.h5')

Export done with

> tensorflowjs_converter --input_format keras test_model.h5 jsmodel/

Loading the tfjs model with const model = await tf.loadLayersModel(url); throws the following error:

Error: Unknown layer: TensorFlowOpLayer. 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:242)
    at deserialize (serialization.ts:31)
    at l (container.ts:1197)
    at t.fromConfig (container.ts:1225)
    at deserializeKerasObject (generic_utils.ts:277)
    at deserialize (serialization.ts:31)
    at models.ts:295
    at common.ts:14
    at Object.next (common.ts:14)`

This is due to the tf_op_layer_strided_slice in the model definition:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 10)]              0         
_________________________________________________________________
dense (Dense)                (None, 20)                220       
_________________________________________________________________
tf_op_layer_strided_slice (T [(None,)]                 0         
=================================================================
Total params: 220
Trainable params: 220
Non-trainable params: 0

Removing the corresponding line removes the error. Thanks a lot!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
nsthoratcommented, Jan 31, 2020

It looks like outputs = outputs[:, 3] is the problem which adds a TensorFlow strided slice (and thus adds a layer which cannot be serialized automatically). You can either:

  1. Remove the slice, let the output be the full output and slice after.
  2. Register a a custom op with that name and the implementation of that custom layer would be the tensorflow.js version of outputs[:, 3] using tf.slice. See the details of custom layers here: https://www.tensorflow.org/js/guide/models_and_layers#custom_layers

If you don’t need the layers model in JavaScript (e.g. you don’t need training in the browser or transfer learning) you could convert the model using a graph model during conversion which will be able to serialize that strided slice.

Hope this helps.

0reactions
rthadurcommented, Feb 13, 2020

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: Unknown layer: Functional
The solution to this error is very simple, ex. the reason is that you have trained the model on version '2.3.0' of Tensorflow...
Read more >
Unknown layer ERROR in importing Keras model to ...
Hello, I'm pretty new to TF and just started by working around this tutorial: “pose classification in keras”. I am using my own...
Read more >
Release 2.12.0
Returns the current metrics values of the model as a dict. Added group normalization layer tf.keras.layers.GroupNormalization . Added weight decay support for ......
Read more >
Custom Layers in Core ML
In this post I'll show how to convert a Keras model with a custom ... It requires Python 2, Keras with TensorFlow, coremltools,...
Read more >
未知层:TensorFlowoplayer”在Python Keras转换模型中
"Unknown layer: TensorFlowOpLayer" in Python Keras converted model using Python indexing. tensorflow.js版本. Python:1.3.1.1 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found