tensorflow: TFlite conversion of tf.keras model fails

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: No
  • TensorFlow installed from (source or binary): pip install
  • TensorFlow version (use command below):v2.0.0-beta1-0-g8e423e3d56 2.0.0-beta1
  • Python version: 3.6.8
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source): 7.4.0
  • CUDA/cuDNN version: No
  • GPU model and memory: Nvidia titan-Xp

Describe the current behavior

import tensorflow as tf
model = tf.keras.models.load_model('keras_model.h5')
model.summary()
Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 1048576)]    0
__________________________________________________________________________________________________
embedding (Embedding)           (None, 1048576, 8)   2056        input_1[0][0]
__________________________________________________________________________________________________
conv1d (Conv1D)                 (None, 2097, 128)    512128      embedding[0][0]
__________________________________________________________________________________________________
conv1d_1 (Conv1D)               (None, 2097, 128)    512128      embedding[0][0]
__________________________________________________________________________________________________
multiply (Multiply)             (None, 2097, 128)    0           conv1d[0][0]
                                                                 conv1d_1[0][0]
__________________________________________________________________________________________________
global_max_pooling1d (GlobalMax (None, 128)          0           multiply[0][0]
__________________________________________________________________________________________________
dense (Dense)                   (None, 128)          16512       global_max_pooling1d[0][0]
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 1)            129         dense[0][0]
==================================================================================================
Total params: 1,042,953
Trainable params: 1,042,953
Non-trainable params: 0
__________________________________________________________________________________________________
converter = tf.lite.TFLiteConverter.from_keras_model(model)

The converter fails to convert the model

>>> converter.convert()
2019-09-26 14:39:27.048354: I tensorflow/core/grappler/devices.cc:60] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA support)
2019-09-26 14:39:27.048553: I tensorflow/core/grappler/clusters/single_machine.cc:359] Starting new session
2019-09-26 14:39:27.065544: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:716] Optimization results for grappler item: graph_to_optimize
2019-09-26 14:39:27.066324: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:718]   function_optimizer: function_optimizer did nothing. time = 0.002ms.
2019-09-26 14:39:27.066655: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:718]   function_optimizer: function_optimizer did nothing. time = 0ms.
Traceback (most recent call last):
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 427, in import_graph_def
    graph._c_graph, serialized, options)  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input 0 of node model/embedding/embedding_lookup was passed float from model/embedding/embedding_lookup/Read/ReadVariableOp/resource:0 incompatible with expected resource.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 348, in convert
    self._funcs[0])
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py", line 252, in convert_variables_to_constants_v2
    new_output_names)
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/eager/wrap_function.py", line 607, in function_from_graph_def
    wrapped_import = wrap_function(_imports_graph_def, [])
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/eager/wrap_function.py", line 585, in wrap_function
    collections={}),
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 716, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/eager/wrap_function.py", line 80, in __call__
    return self.call_with_variable_creator_scope(self._fn)(*args, **kwargs)
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/eager/wrap_function.py", line 86, in wrapped
    return fn(*args, **kwargs)
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/eager/wrap_function.py", line 605, in _imports_graph_def
    importer.import_graph_def(graph_def, name="")
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/home/sridhar/PE_CSV/malenv3/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 431, in import_graph_def
    raise ValueError(str(e))
ValueError: Input 0 of node model/embedding/embedding_lookup was passed float from model/embedding/embedding_lookup/Read/ReadVariableOp/resource:0 incompatible with expected resource.

I’m able to use the model on a python based inference engine. I’m trying to just compress the model to deploy it on smaller setup and consume via a c/c++ wrapper.

About this issue

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

Most upvoted comments

Closing this issue because the code snippet in https://github.com/tensorflow/tensorflow/issues/32849#issuecomment-539646714 works on the nightly (pip install tf-nightly).

@codeyman Please request to reopen if you experience additional problems.

@jvishnuvardhan Yeah, no issues while training. Note that is the standard malconv model: https://arxiv.org/pdf/1710.09435.pdf

@codeyman I could reproduce the issue. I think this is more related to embedding. Were you able to train this model without any issues? Thanks!