tensorflow: Cannot convert a Tensor of dtype resource to a NumPy array

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Google Colab
  • TensorFlow installed from (source or binary): Google Colab
  • TensorFlow version (or github SHA if from source): 2.3.0

Command used to run the converter or code if you’re using the Python API Colab is here: https://colab.research.google.com/drive/1X0MhpbPbguscxwY-UDyp9d0OsL5hANR2?usp=sharing

tfliteModel = tf.lite.TFLiteConverter.from_keras_model(model).convert()

The output from the converter invocation

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-11-ef48ab45d342> in <module>()
----> 1 tfliteModel = tf.lite.TFLiteConverter.from_keras_model(model).convert()
      2 
      3 with tf.io.gfile.GFile('my_model.tflite', 'wb') as f:
      4     f.write(tfliteModel)

6 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self)
    807     frozen_func, graph_def = (
    808         _convert_to_constants.convert_variables_to_constants_v2_as_graph(
--> 809             self._funcs[0], lower_control_flow=False))
    810 
    811     input_tensors = [

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py in convert_variables_to_constants_v2_as_graph(func, lower_control_flow, aggressive_inlining)
   1101       func=func,
   1102       lower_control_flow=lower_control_flow,
-> 1103       aggressive_inlining=aggressive_inlining)
   1104 
   1105   output_graph_def, converted_input_indices = _replace_variables_by_constants(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py in __init__(self, func, lower_control_flow, aggressive_inlining, variable_names_whitelist, variable_names_blacklist)
    802         variable_names_whitelist=variable_names_whitelist,
    803         variable_names_blacklist=variable_names_blacklist)
--> 804     self._build_tensor_data()
    805 
    806   def _build_tensor_data(self):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/convert_to_constants.py in _build_tensor_data(self)
    821         data = map_index_to_variable[idx].numpy()
    822       else:
--> 823         data = val_tensor.numpy()
    824       self._tensor_data[tensor_name] = _TensorData(
    825           numpy=data,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in numpy(self)
   1061     """
   1062     # TODO(slebedev): Consider avoiding a copy for non-CPU or remote tensors.
-> 1063     maybe_arr = self._numpy()  # pylint: disable=protected-access
   1064     return maybe_arr.copy() if isinstance(maybe_arr, np.ndarray) else maybe_arr
   1065 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _numpy(self)
   1029       return self._numpy_internal()
   1030     except core._NotOkStatusException as e:  # pylint: disable=protected-access
-> 1031       six.raise_from(core._status_to_exception(e.code, e.message), None)  # pylint: disable=protected-access
   1032 
   1033   @property

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.

Also, please include a link to the saved model or GraphDef

https://drive.google.com/file/d/1iXmNGjMTPoqzBRpeNmTjarGGEIBK8Jdr/view?usp=sharing

Any other info / logs I can’t convert a model which contains tf.keras.layers.experimental.preprocessing.TextVectorization to tf.lite. I’ve got this error: InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.

Please see this colab: https://colab.research.google.com/drive/1X0MhpbPbguscxwY-UDyp9d0OsL5hANR2?usp=sharing

Can you please help me

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 32 (15 by maintainers)

Most upvoted comments

Could you try the conversion with from_saved_model within tf-nightly version? You can avoid the above error through the from_saved_model with tf-nightly version.

@abattery now I have the same problem, I tested my code with TF 2.2 and TF-nightly 2.5, but encountered the same problem.

frozen_func = convert_variables_to_constants_v2(full_model)  
frozen_func.graph.as_graph_def()

output:

  File "frozen.py", line 201, in h5_to_pb
    frozen_func = convert_variables_to_constants_v2(full_model)
  File "/home/.local/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py", line 1078, in convert_variables_to_constants_v2
    aggressive_inlining=aggressive_inlining)
  File "/home/.local/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py", line 807, in __init__
    self._build_tensor_data()
  File "/home/.local/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py", line 826, in _build_tensor_data
    data = val_tensor.numpy()
  File "/home/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1072, in numpy
    maybe_arr = self._numpy()  # pylint: disable=protected-access
  File "/home/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1040, in _numpy
    six.raise_from(core._status_to_exception(e.code, e.message), None)  # pylint: disable=protected-access
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array

Can you give me some advice, thank you very much!