asataniAIR / Image_DL_Tutorial

画像に対するDeep Learningの応用チュートリアル
MIT License
5 stars 3 forks source link

ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL + tenserflow + virtual machine #4

Open mahernadar opened 7 years ago

mahernadar commented 7 years ago

Dear Gents,

I am trying to fine-tune the VGG16 model in order to classify 5 classes, inspired by the link : https://gist.github.com/fchollet/7eb39b44eb9e16e59632d25fb3119975.

When i used to run it on my laptop, i was not encountering any problems.

However, i am currently using a virtual machine in order to access some university servers (it's faster as you may imagine).

The problem is that now i am encountering this Error:

Epoch 1/50 Exception in thread Thread-18: Traceback (most recent call last): File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner self.run() File "/usr/lib/python3.4/threading.py", line 868, in run self._target(*self._args, *self._kwargs) File "/home/mnadar/tensorflow/lib/python3.4/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py", line 615, in data_generator_task generator_output = next(self._generator) File "/home/mnadar/tensorflow/lib/python3.4/site-packages/tensorflow/contrib/keras/python/keras/preprocessing/image.py", line 804, in next return self.next(args, **kwargs) File "/home/mnadar/tensorflow/lib/python3.4/site-packages/tensorflow/contrib/keras/python/keras/preprocessing/image.py", line 1063, in next target_size=self.target_size) File "/home/mnadar/tensorflow/lib/python3.4/site-packages/tensorflow/contrib/keras/python/keras/preprocessing/image.py", line 361, in load_img raise ImportError('Could not import PIL.Image. ' ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.


ValueError Traceback (most recent call last)

in () 86 epochs=epochs, 87 validation_data=validation_generator, ---> 88 validation_steps=nb_validation_samples // batch_size) 89 90 model.save('my_model_5_classes_fine_tuning.h5') ~/tensorflow/lib/python3.4/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_q_size, workers, pickle_safe, initial_epoch) 1855 raise ValueError('output of generator should be ' 1856 'a tuple `(x, y, sample_weight)` ' -> 1857 'or `(x, y)`. Found: ' + str(generator_output)) 1858 if len(generator_output) == 2: 1859 x, y = generator_output # pylint: disable=unpacking-non-sequence ValueError: output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: None **here is my code:** import h5py from PIL import Image from tensorflow.contrib.keras.python.keras import applications from tensorflow.contrib.keras.python.keras.preprocessing.image import ImageDataGenerator from tensorflow.contrib.keras.python.keras import optimizers from tensorflow.contrib.keras.python.keras.models import Sequential from tensorflow.contrib.keras.python.keras.models import Model from tensorflow.contrib.keras.python.keras.layers import Dropout, Flatten, Dense weights_path = 'vgg16_weights.h5' top_model_weights_path ='bottleneck_fc_model_5_classes.h5' img_width, img_height = 150, 150 train_data_dir = '/home/mnadar/Maher_Thesis/training2/training2' validation_data_dir = '/home/mnadar/Maher_Thesis/validation2/validation2' nb_train_samples = 50000 nb_validation_samples = 10000 epochs = 50 batch_size = 20 base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150,150,3)) print('Model loaded.') top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(5, activation='softmax')) top_model.load_weights(top_model_weights_path) model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) for layer in model.layers[:15]: layer.trainable = False model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # prepare data augmentation configuration train_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) validation_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') model.summary() model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size) model.save('my_model_5_classes_fine_tuning.h5') **Note: i already installed "Pillow", for when i try to install "PIL", i get:** Could not find a version that satisfies the requirement pil (from versions: ) No matching distribution found for pil **Can anyone help me with this issue please? :)** **Thanks!!**
zkfly1991 commented 6 years ago

I encounter the exact same problem! DId you manage to find the solution? Thanks

syedfaizalex commented 6 years ago

I also encounter the exact same prolem , is anyone have solution ?

namish800 commented 6 years ago

I am having the same error . is there any solution?

snafu4 commented 6 years ago

One of these did the trick:

pip install --upgrade tensorflow keras numpy pandas sklearn pillow

I would assume tensorflow or keras.

syedfaizalex commented 6 years ago

I think something is related to the O/S , which os you're using ?

On Feb 4, 2018 5:48 AM, "Neil Greisman" notifications@github.com wrote:

One of these did the trick:

pip install --upgrade tensorflow keras numpy pandas sklearn pillow

I would assume tensorflow or keras.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/asataniAIR/Image_DL_Tutorial/issues/4#issuecomment-362868430, or mute the thread https://github.com/notifications/unsubscribe-auth/AgwmTjPeC7KPmQj1TKD45EwRJGNApZgXks5tRPdNgaJpZM4OQBWt .

snafu4 commented 6 years ago

My answer above was not clear. I was getting the same error message and fixed it with the pip upgrade above (Ubuntu).

samra-irshad commented 6 years ago

anyone got the solution so far?

SuekyeongNam commented 6 years ago

I solved this problem. I just did not have sklearn package. So I enter 'pip install --upgrade tensorflow keras numpy pandas sklearn pillow' on anaconda prompt, and add 'from sklearn.preprocessing import LabelEncoder' in python code instead 'from PIL import Image'. It works to me!

salvadorrueda commented 6 years ago

In my case pip install pillow was the solution. o_O!

RavenPillmann commented 6 years ago

I originally was still having a similar issue after running pip install pillow until I realized I had to deactivate and reactivate my environment, for the case anyone else forgets to do this step

mrugankray commented 6 years ago

pip install --upgrade tensorflow keras numpy pandas sklearn pillow This worked for me.

NicolasFerland commented 6 years ago

I did pip install --upgrade tensorflow keras numpy pandas sklearn pillow and after import numpy as np stopped working. I got the error

ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Thus I recommend not to use this command.

If you have the same problem as me, I suggest you do conda list --revisions conda install --revision [revision number] where revision number is the number before the last command.

Purva-19 commented 6 years ago

I did as @SuekyeongNam directed and it worked for me too.

Thanks.

SushantPupneja commented 6 years ago

uninstall pil or pillow both then install pillow package and re-run the environment and notebook worked for me.

amitmeel commented 6 years ago

I'm still getting the same error as above described. I tried all the method like:

  1. pip install --upgrade tensorflow keras numpy pandas sklearn pillow
  2. uninstall the pillow using conda remove pillow and install using pip install pillow
  3. even tried with from sklearn.preprocessing import LabelEncode

None of the above methods worked for me. if anybody found any solution plz let me know.

jaydevtrivedi commented 5 years ago

'pip install --upgrade tensorflow keras numpy pandas sklearn pillow' This one worked! Thanks!

manualbashing commented 5 years ago

I originally was still having a similar issue after running pip install pillow until I realized I had to deactivate and reactivate my environment, for the case anyone else forgets to do this step

That is exactly, what solved it for me!

talhaanwarch commented 5 years ago

deactivating and activating work for me, i think so

linzhenyuyuchen commented 5 years ago

pip install --upgrade tensorflow keras numpy pandas sklearn pillow conda deactivate conda activate tfenv

Akshaykumarcp commented 4 years ago

pip install --upgrade tensorflow keras numpy pandas sklearn pillow conda deactivate conda activate tfenv

Worked For Me. Thank you

ghimireadarsh commented 4 years ago

In your environment - pip install pillow Restart kernel. (Works)

vivekanandan-k commented 4 years ago

deactivate and activating the environment has worked for me

ibrahim9496 commented 3 years ago

I installed Matplotlib on my Virtual Environment while still running the notebook in the background. Matplotlib imported without any errors on that notebook, but this error keep coming.

"ImportError: Could not import PIL.Image. The use of array_to_img requires PIL"

Simply Deactivating and then activating that virtual environment worked for me.

callmekofi commented 2 years ago

Initially, I encountered the same issue after running pip install pillow. This worked for me pip install --upgrade tensorflow keras numpy pandas sklearn pillow

Ross-Fan commented 1 year ago

I have got this problem in 2023, ye~~ , 2023 with keras+tf in 2.8.0 I found some of the solution is to setup pillow, such as pip3 install pillow, I tried ,but it didn't work another solution is , en..., build the load_img function from PIL import Image

def load_img(img_path, target_size): image = Image.open(img_path) resize_img = image.resize(target_size) return resize_img

img = image.load_img(img_path, target_size=(224, 224)). => transit to img =load_img(img_path, target_size=(224, 224))

It works!