5

I am using transfer learning for feature extraction of medical images through imagedatagenerator. I am using pillow version 2.8.0, images are one-channeled and of jpeg format. SciPy version 1.5.4 shows to be installed on my IDE. The code is as:

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

The transfer learning model 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)

It used to throw the error __array__() takes 1 positional argument but 2 were given? But that was fixed by downgrading the pillow version to the version mentioned above. Now, however, the last code throws the error:

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.

I have installed and checked the SciPy version on my IDE and it seems to be fine, version '1.5.4'. How can I fix this error? Thank you.

Kenan Morani
  • 141
  • 1
  • 9
  • 1
    did u import it? post your full code here pls – Ghost Ops Aug 22 '21 at 13:35
  • 1
    Yes. I have imported the scipy library. The full code can be seen above now. Thank you! – Kenan Morani Aug 22 '21 at 14:03
  • 1
    upgrade the pillow and then show the line of error, i think scipy needs updated pillow – Ghost Ops Aug 22 '21 at 14:31
  • 1
    The error, when pillow is up to date, can be seen here https://stackoverflow.com/questions/68880452/img-to-array-throws-the-error-array-takes-1-positional-argument-but-2-were. The full code with the error can be seen here https://stackoverflow.com/questions/68291947/typeerror-array-takes-1-positional-argument-but-2-were-given. Thank you for your time! – Kenan Morani Aug 22 '21 at 15:09

3 Answers3

3

I also have the problem when I use jupyter notebook. I shutdown the kernel and restart it. The problem is solved.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31445088) – Muhammad Mohsin Khan Apr 04 '22 at 12:37
1

This problem is usually because of TensorFlow Version. I believe your current version must be 2.6 or something like this if you install tensorflow==2.4 this issue will be solved.

Phoenix
  • 1,343
  • 8
  • 10
0

I have same problem, it seems you don't install scipy, you can try pip to install it, and if you have some virtual env, like anoconda, you may need to open a system terminal like cmd in windows to change your env, and install in the environment. why I say that, because I install it in pycharm's terminal at first, but it didn't work, but when I try in system terminal, the problem has been solve.

Youmi
  • 1