asataniAIR / Image_DL_Tutorial

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

array_to_img requires PIL #2

Open m1kita opened 7 years ago

m1kita commented 7 years ago

To execute VGG16_demo.py, PIL is needed.

Traceback (most recent call last): File "VGG16_Demo.py", line 27, in img = image.load_img(filename, target_size=(224, 224)) File "C:\Users\h04033\Anaconda3\envs\AIR-Tutorial\lib\site-packages\keras\preprocessing\image.py", line 318, in load_img raise ImportError('Could not import PIL.Image. ' ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.

ks6088ts commented 7 years ago

I also encountered the same issue on Mac environment. I resolved it by executing the following command.

"$ pip install pillow"

syedfaizalex commented 6 years ago

it is giving same error even after install pillow by the way i am using anaconda so installed pillow using following command conda install pillow

jalilahmed commented 6 years ago

I am having the same issue. I have pillow installed already, running command "conda install pillow", requested package already installed. Any Solutions?

schmudde commented 6 years ago

I'm seeing the exact same issue @jalilahmed. I'll let you know if I find the solution. Please post here if you find it first.

jalilahmed commented 6 years ago

@schmudde I was installing pillow in wrong environment. if you are using tensorflow background first " conda activate tensorflow" then run "conda install pillow"

Mrinalini97 commented 6 years ago

thank you so much ,you're suggestion worked.

adrianlievano commented 6 years ago

Hey guys -

I installed pillow (using a Mac) using pip install pillow. Then, I also tried doing conda activate tensorflow, conda install pillow, but no luck. I keep getting the error, raise IOError("cannot identify image file %r" and raise ImportError('Could not import PIL.Image. ' 386 'The use of array_to_img requires PIL.') --> 387 img = pil_image.open(path).

I thought it had to do with my image files (initially all were pngs, but I converted them to jpgs). But, it just doesn't seem to be working. I'm trying to convert all these image files into tensors of shape (N, 224, 224, 3), where N is the number of images in the dataset.

When I run, train_files? (which is the supposed to be a list of image file paths), some of them actually show missing extensions (there's no .jpg or .png behind them). Check it out:

'birdImages/train/04.Osprey/.DS_Store', 'birdImages/train/01.Great_blue_heron/Great-Blue-Heron-3 DChristian.jpg', 'birdImages/train/05.Red_tailed_hawk/Red-tailed Hawk 2 DChristian.jpg', 'birdImages/train/04.Osprey/Osprey Joanne.jpg', 'birdImages/train/04.Osprey/Osprey 2 LK.jpg',

It seems that it's making my code crash. Does anyone know how to fix this?

-Update: Turns out, my data also contains the folder that holds the images. That's why it doesn't have a .jpg extension behind it.

marireeves commented 5 years ago

I am trying to adapt code I found here (https://shirinsplayground.netlify.com/2018/06/keras_fruits/ ) to fit a neural network to 5 categories of images.

I am having this ( 'The use of array_to_img requires PIL.') problem trying to run Keras in R Studio (v. 1.1.456 and R version 3.6.1)- - and have tried installing pillow every which way I can find on stack Overflow, while in R and in Conda and in Python.

I have been launching R-Studio from a conda environment where I check that I have Pillow installed. I have tried on newer and older versions of conda, by uninstalling the newer version of conda that was having compatibilty problems, and downgraded my anaconda to version 2. . I have downgraded my Python to 2.7, because of incompatibilities between the newer python and tensorflow just this week (Python 3.7). I have tried running the code that seems to work for everyone else on the web from my anaconda prompt to (pip install pillow ... and it tells me the requirement is already satisfied). When I install tensorflow using the install_tensorflow() function in R, it reports that it is installing Pillow v. 6.0.1.

And still the error when I try and run the model_fit_generator section, 'The use of array_to_img requires PIL.'

Error in py_call_impl(callable, dots$args, dots$keywords) : ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.

Do the image data generators work for others? Are other people having this glitch? Here is my full code below:

title: "Deep Neural Network Photo Analysis" author: "Mari K Reeves" date: "r Sys.Date()" output: html_document

rm(list = ls()) #remove all past worksheet variables

Load Packages

Read in Base Packages ---------------------------------------------------

pckg <- c("dplyr", "tidyr","RColorBrewer", "ggplot2","curl", "caret", "RCurl","parallel","snowfall","rmarkdown","jpeg", "knitr", "tictoc", "EBImage", "keras","tensorflow", "magick")

READING IN PACKAGES

for(i in 1:length(pckg)){ if ((!pckg[i] %in% installed.packages())==T) { if(pckg[i] == 'EBImage'){ if (!requireNamespace("BiocManager", quietly = TRUE)){ install.packages("BiocManager", repos="http://cran.us.r-project.org") BiocManager::install("EBImage") }

} 
install.packages(pckg[i], repos="http://cran.us.r-project.org",
                 dependencies = T)
print(pckg[i])
do.call("library", list(pckg[i]))

if(pckg[i] == "tensorflow"){
  install_tensorflow()
}

}else{

print(pckg[i])
do.call("library", list(pckg[i]))

} }

installing tensorflow again, because it doesn't seem to work in my loop above...

install_tensorflow()

Some helpful resources: https://shirinsplayground.netlify.com/2018/06/keras_fruits/ and https://blogs.rstudio.com/tensorflow/posts/2018-10-11-activations-intro/

Set up workspace and identify data directories

basedir<-"C:/Users/marireeves/Documents/RatTrapFever/Data/" outdir<-"C:/Users/marireeves/Documents/RatTrapFever/Data/OutDir/" traindir<-"C:/Users/marireeves/Documents/RatTrapFever/Data/19_07_11_Out/Training/" testdir<-"C:/Users/marireeves/Documents/RatTrapFever/Data/19_07_11_Out/Validation/"

Make Lists of All Training and Test Pics to subset

set.seed(333)

for empty frames

trapdir<-paste0(basedir, "Animals/Empty/")

and for photos with rats

ratdir<-paste0(basedir, "Animals/Rats/")

and for photos with mice

mousedir<-paste0(basedir, "Animals/Mice/")

with pigs

pigdir<-paste0(basedir, "Animals/Pigs/")

humans - not enough now

peopledir<-paste0(basedir, "Animals/Humans/")

birds

birdir<-paste0(basedir, "Animals/Birds/")

read in pictures as images, reduce resolution, write them out as smaller files, and transform them to arrays

Do this for the training and test sets for the rats and the empty frames

Send photos to training and validation folders randomly

first, make a list of all the photo groups

folderlist<-c(trapdir, ratdir, mousedir, pigdir, birdir)#peopledir,

for (myfolder in folderlist){

mypics <- list.files(myfolder, pattern = "JPG" , full.names = T) mypics<-sample(mypics, 102, replace = T)#when more pics, set replace to F mypics<-na.omit(mypics) trainsize<-round(length(mypics)*0.8, digits = 0) trainindex<-sample(mypics, trainsize, replace = F) testindex<-mypics[!(mypics %in% trainindex)]

give things names based on the name of the infolders

foldername<-as.character(myfolder) minifolder<-sub("C:/Users/marireeves/Documents/RatTrapFever/Data/Animals/", "", myfolder) foldername<-sub("C:/Users/marireeves/Documents/RatTrapFever/Data/Animals/", "", myfolder) foldername<-sub("/", "_", foldername) testname<-paste0(foldername, "test") trainname<-paste0(foldername, "train") afolder<-paste0( as.character(myfolder)) trainfolder<-sub("C:/Users/marireeves/Documents/RatTrapFever/Data/Animals/", traindir, afolder) testfolder<-sub("C:/Users/marireeves/Documents/RatTrapFever/Data/Animals/", testdir, afolder)

make this send photos to training and validation folders

for (testpic in testindex){ pic <- readImage(testpic) pic<- resize(pic, 256, 256)

give smaller pic a name and write it out as a jpg

picid<-as.character(testpic)
#This sub statement is not working!
picid<-sub(afolder, "", picid)
picname<-sub(".JPG", "_256x256.jpg", picid)
#picname<-sub(minifolder, "", picname)
outpic<-sub(afolder, testfolder, picname)
outpic<-paste0(testfolder, outpic)
writeJPEG(pic, target = outpic)

}

for (trainpic in trainindex){ tpic<-readImage(trainpic) tpic<-resize(tpic, 256, 256) pictid<-as.character(trainpic) pictname<-sub(afolder, "", pictid) picntname<-sub(".JPG", "_256x256.jpg", pictid) outpict<-sub(afolder, trainfolder, pictname) outpict<-paste0(trainfolder, outpict) writeJPEG(tpic, target = outpict) } }

list of animals

names_list <- c("Empty", "Birds", "Rats", "Mice", "Pigs")

number of output classes (i.e. fruits)

output_n <- length(names_list)

image size to scale down to (original images are 100 x 100 px)

img_width <- 256 img_height <- 256 target_size <- c(img_width, img_height)

RGB = 3 channels

channels <- 3

path to image folders

train_image_files_path <-traindir valid_image_files_path <-testdir

Set up the image data generator functions

optional data augmentation

train_data_gen <- image_data_generator(traindir, rescale = 1/255 #,

rotation_range = 40,

                                   #width_shift_range = 0.2,
                                   #height_shift_range = 0.2,
                                   #shear_range = 0.2,
                                   #zoom_range = 0.2,
                                   #horizontal_flip = TRUE,
                                   #fill_mode = "nearest"

)

Validation data shouldn't be augmented! But it should also be scaled.

valid_data_gen <- image_data_generator(testdir, rescale = 1/255 )

load images into memory and resize them

use_condaenv("r-tensorflow")

training images

train_image_array_gen <- flow_images_from_directory(train_image_files_path, train_data_gen, target_size = target_size, class_mode = "categorical", classes = names_list, seed = 333)

validation images

valid_image_array_gen <- flow_images_from_directory(valid_image_files_path, valid_data_gen, target_size = target_size, class_mode = "categorical", classes = names_list, seed = 333)

cat("Number of images per class:")

table(factor(train_image_array_gen$classes))

cat("\nClass label vs index mapping:\n")

train_image_array_gen$class_indices

names_classes_indices <- train_image_array_gen$class_indices save(names_classes_indices, file = paste0(basedir, "names_classes_indices.RData") )

number of training samples

train_samples <- train_image_array_gen$n

number of validation samples

valid_samples <- valid_image_array_gen$n

define batch size and number of epochs

batch_size <- 32 epochs <- 10

set model parameters and run fit model

Use this next line if you don't want the model to keep adding new layers each time you

run the code:

k_clear_session()

initialise model

model <- keras_model_sequential()

add layers

model %>% layer_conv_2d(filter = 32, kernel_size = c(3,3), padding = "same", input_shape = c(img_width, img_height, channels)) %>% layer_activation("relu") %>%

Second hidden layer

layer_conv_2d(filter = 16, kernel_size = c(3,3), padding = "same") %>% layer_activation_leaky_relu(0.5) %>% layer_batch_normalization() %>%

Use max pooling

layer_max_pooling_2d(pool_size = c(2,2)) %>% layer_dropout(0.25) %>%

Flatten max filtered output into feature vector

and feed into dense layer

layer_flatten() %>% layer_dense(100) %>% layer_activation("relu") %>% layer_dropout(0.5) %>%

Outputs from dense layer are projected onto output layer

layer_dense(output_n) %>% layer_activation("softmax")

compile

model %>% compile( loss = "categorical_crossentropy", optimizer = optimizer_rmsprop(lr = 0.0001, decay = 1e-6), metrics = "accuracy" )

This below is the part that fails~~~~~~~~~

fit

hist <- model %>% fit_generator(

training data

train_image_array_gen,

epochs

steps_per_epoch = as.integer(train_samples / batch_size), epochs = epochs,

validation data

validation_data = valid_image_array_gen, validation_steps = as.integer(valid_samples / batch_size),

print progress

verbose = 1, callbacks = list(

save best model after every epoch

callback_model_checkpoint("C:/Users/marireeves/Documents/RatTrapFever/Data/OutDir/fruits_checkpoints.h5", save_best_only = TRUE),
# only needed for visualising with TensorBoard
callback_tensorboard(log_dir = "C:/Users/marireeves/Documents/RatTrapFever/Data/OutDir/keras/logs")

) )