2

我正在为图像分类任务进行迁移学习。训练生成器如下:

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')

转移的型号代码如下:

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)

最后一个命令抛出错误:

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

如何克服此错误来进行特征提取?谢谢你。

4

1 回答 1

0

我遇到了同样的错误。我试图将 tensorflow 降级到 2.4,但没有奏效。我将我的 python 版本从 3.10.2 降级到 3.9.9 并使用以下命令重新安装了 scipy python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
这个命令解决了我的问题。

于 2022-02-15T20:53:07.763 回答