导入错误:图像转换需要SciPy。安装SciPy。

5

我正在使用迁移学习通过imagedatagenerator从医学图像中提取特征。我正在使用pillow版本2.8.0,图像为单通道且为jpeg格式。我的IDE上安装了SciPy版本1.5.4。代码如下:

import os, glob
import numpy as np
import matplotlib.pyplot as plt
import cv2
import nibabel as nib

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow import keras
from keras.models import load_model
from keras import backend as K
from keras.applications.vgg16 import VGG16

import os
import cv2
from PIL import Image
import numpy as np
import scipy
from matplotlib import pyplot as plt


batch_size = 64
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        '/home/idu/Desktop/COV19D/train/', 
        target_size=(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)

曾经它会抛出错误 __array__() 接收 1 个位置参数,但给出了 2 个? 但是将 pillow 版本降级到上面提到的版本后,问题已修复。现在,然而,最后的代码会抛出错误:
Traceback (most recent call last):

  File "<ipython-input-120-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 238, in _get_batches_of_transformed_samples
    x = self.image_data_generator.apply_transform(x, params)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py", line 874, in apply_transform
    order=self.interpolation_order)

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/affine_transformations.py", line 281, in apply_affine_transform
    raise ImportError('Image transformations require SciPy. '

ImportError: Image transformations require SciPy. Install SciPy.

我在我的IDE上安装并检查了SciPy版本,看起来一切正常,版本号为'1.5.4'。如何解决这个错误?谢谢。


1
你导入了吗?请在此处发布您的完整代码。 - Ghost Ops
1
是的,我已经导入了scipy库。完整的代码可以在上面看到。谢谢! - Kenan Morani
1
升级pillow,然后显示错误行,我认为scipy需要更新pillow。 - Ghost Ops
1
当pillow已经更新时,错误可以在这里看到https://stackoverflow.com/questions/68880452/img-to-array-throws-the-error-array-takes-1-positional-argument-but-2-were。带有错误的完整代码可以在这里看到https://stackoverflow.com/questions/68291947/typeerror-array-takes-1-positional-argument-but-2-were-given。谢谢您的时间! - Kenan Morani
3个回答

3

我在使用Jupyter Notebook时也遇到过这个问题。我关闭了内核并重新启动它,问题得到了解决。


这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Muhammad Mohsin Khan

1

这个问题通常是由于TensorFlow版本引起的。我相信您当前的版本必须是2.6或类似的版本,如果您安装tensorflow == 2.4,则可以解决此问题。


0

我有同样的问题,看起来你没有安装scipy,你可以尝试使用pip进行安装,如果你有一些虚拟环境,比如anoconda,你可能需要打开一个系统终端(例如在Windows中的cmd)来更改你的环境,并在该环境中进行安装。 我为什么这么说呢?因为我最初在pycharm的终端中安装它时,它没能成功,但当我在系统终端中尝试时,问题得到了解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接