tensorflow: TypeError: __array__() takes 1 positional argument but 2 were given
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code: yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOs Big Sur
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): v2.4.0-49-g85c8b2a817f 2.4.1
- Python version: 3.8.7
Describe the current behavior
I’m trying to assign the return value of get_value()
to a numpy array within a method decorated using tf.function
. It works only if I call the method directly. However, if I use the result of tf.numpy_function()
(in case3
), I get an error.
Describe the expected behavior
I don’t know, but the error is not doing any good explaining why this is happening
Standalone code to reproduce the issue Please find the notebook here which contains the following:
import numpy as np
import tensorflow as tf
class Foo:
def __init__(self):
self.storage = np.zeros((10, 3, 1))
@staticmethod
def get_value():
return np.array([[1], [2], [3]])
@tf.function
def case1(self): # works
self.storage[0] = self.get_value()
def case2(self): # works
self.storage[0] = tf.numpy_function(self.get_value, inp=[], Tout=tf.float32)
@tf.function # fails
def case3(self):
self.storage[0] = tf.numpy_function(self.get_value, inp=[], Tout=tf.float32)
if __name__ == '__main__':
foo = Foo()
foo.case1()
foo.case2()
foo.case3()
Error:
2021-02-01 23:11:35.422101: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-01 23:11:35.422548: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-02-01 23:11:35.439501: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Traceback (most recent call last):
File "/Users/emadboctor/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py", line 29, in <module>
foo.case3()
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 725, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3196, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3887, in bound_method_wrapper
return wrapped_fn(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
/Users/emadboctor/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py:22 case3 *
self.storage[0] = tf.numpy_function(self.get_value, inp=[], Tout=tf.float32)
TypeError: __array__() takes 1 positional argument but 2 were given
Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (5 by maintainers)
I found that was caused by PIL lib. I found the numpy array function description here
so, it will invoke the
__array__
of PIL object. I solved this problems by downgrade PIL from 8.3.0 to 8.2.0. Maybe this will help you.Thanks! I had suspected something like this, so I had done a
pip install --upgrade --force-reinstall tensorflow
already. Interestingly, thepillow
package was unaffected by this change, as it is not even listed in thetensorflow
dependency tree.pip install "pillow!=8.3.0"
does indeed fix this for now, and the root issue is tracked in https://github.com/python-pillow/Pillow/issues/5571.All Possible solution added [Solved] TypeError: method() takes 1 positional argument but 2 were given
Upgrade to 8.3.1 is work too.
Thanks alooooot !!
Just came by to say that we too were seeing issues with this and a downgrade of pillow to 8.2.0 has worked around the issue for now.
Able to reproduce the error for a similar case as shown below:
from PIL import Image img_data = np.random.random(size=(100, 100, 3)) img = tf.keras.preprocessing.image.array_to_img(img_data) array = tf.keras.preprocessing.image.img_to_array(img)
TypeError Traceback (most recent call last) <ipython-input-115-e13b4c9f83c3> in <module> 2 img_data = np.random.random(size=(100, 100, 3)) 3 img = tf.keras.preprocessing.image.array_to_img(img_data) ----> 4 array = tf.keras.preprocessing.image.img_to_array(img)
~\anaconda3\envs\tf-latest-gpu\lib\site-packages\tensorflow\python\keras\preprocessing\image.py in img_to_array(img, data_format, dtype) 226 dtype = backend.floatx() 227 kwargs[‘dtype’] = dtype –> 228 return image.img_to_array(img, data_format=data_format, **kwargs) 229 230
~\anaconda3\envs\tf-latest-gpu\lib\site-packages\keras_preprocessing\image\utils.py in img_to_array(img, data_format, dtype) 307 # or (channel, height, width) 308 # but original PIL image has format (width, height, channel) –> 309 x = np.asarray(img, dtype=dtype) 310 if len(x.shape) == 3: 311 if data_format == ‘channels_first’:
TypeError: array() takes 1 positional argument but 2 were given
See also #46563 for a little more detail about what causes this error in general.