'TypeError: __array__() takes 1 positional argument but 2 were given?

I am working on transfer learning for an image classification task. The training generator is as follows:

train_generator = train_datagen.flow_from_directory(
        '/home/idu/Desktop/COV19D/train/', 
        color_mode = "grayscale",
        target_size=(512, 512),  # All images are 512 * 512
        batch_size=batch_size,
        classes = ['covid','non-covid'],
        class_mode='binary')

The transferred model code is as follows:

SIZE = 512
VGG_model = VGG16(include_top=False, weights=None, input_shape=(SIZE, SIZE, 1))
for layer in VGG_model.layers:
    layer.trainable = False

feature_extractor=VGG_model.predict(train_generator)

The last command throws the error:

Traceback (most recent call last):

  File "<ipython-input-28-b9bad68819ec>", line 1, in <module>
    feature_extractor=VGG_model.predict(train_generator)

  File "/home/idu/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1681, in predict
    steps_per_execution=self._steps_per_execution)

  File "/home/idu/.local/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 1348, in get_data_handler
    return DataHandler(*args, **kwargs)

  File "/home/idu/.local/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 1150, in __init__
    model=model)

  File "/home/idu/.local/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 793, in __init__
    peek, x = self._peek_and_restore(x)

  File "/home/idu/.local/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 850, in _peek_and_restore
    peek = next(x)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 104, in __next__
    return self.next(*args, **kwargs)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 116, in next
    return self._get_batches_of_transformed_samples(index_array)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/iterator.py", line 231, in _get_batches_of_transformed_samples
    x = img_to_array(img, data_format=self.data_format)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 309, in img_to_array
    x = np.asarray(img, dtype=dtype)

  File "/home/idu/.local/lib/python3.6/site-packages/numpy/core/_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)

TypeError: __array__() takes 1 positional argument but 2 were given

How can I overcome this error to do the feature exctraction? Thank you.



Solution 1:[1]

I tried to downgrade tensorflow to 2.4, but that did not work. I downgraded my python version from 3.10.2 to 3.9.9 and re-installed scipy using the following command: python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose.
This command solved the issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yunnosch