2

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.

Kenan Morani
  • 141
  • 1
  • 9
  • This is the line with the error: `feature_extractor=VGG_model.predict(train_generator)` – Mad Physicist Jul 07 '21 at 19:23
  • The error is saying that you need to pass in no arguments or only keyword arguments. You'll have to read the docs to figure out which. – Mad Physicist Jul 07 '21 at 19:24
  • Agreed with @MadPhysicist . The error is that somewhere internally there is a call to `img_to_array()` that is missing the `dtype` argument. My guess would be you need to pass a `dtype=float` or `dtype=int` argument in the `**kwargs` of `train_datagen.flow_from_directory()`. – miravalls Jul 07 '21 at 19:32
  • Thank you for your explanation and help. I added dtype argument (dtype=int) and (dtype=float) with train_datagen = ImageDataGenerator(rescale=1./255, dtype=int). flow from directory takes no dtype argument or so I have found. The problem has not been solved yet? – Kenan Morani Aug 08 '21 at 12:54

1 Answers1

0

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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54