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.
- I saw this issue ( https://github.com/tensorflow/tensorflow/issues/32693 ) and tried to use
converter.experimental_new_converter = True
– it did not help - I saw this answer ( https://stackoverflow.com/questions/59962509/valueerror-cannot-convert-a-tensor-of-dtype-resource-to-a-numpy-array ) and tried to use a custom layer – it did not help
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)
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.
output:
Can you give me some advice, thank you very much!