Open m1kita opened 7 years ago
I also encountered the same issue on Mac environment. I resolved it by executing the following command.
"$ pip install pillow"
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
I am having the same issue. I have pillow installed already, running command "conda install pillow", requested package already installed. Any Solutions?
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.
@schmudde I was installing pillow in wrong environment. if you are using tensorflow background first " conda activate tensorflow" then run "conda install pillow"
thank you so much ,you're suggestion worked.
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.
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.
r Sys.Date()
"
output: html_documentrm(list = ls()) #remove all past worksheet variables
pckg <- c("dplyr", "tidyr","RColorBrewer", "ggplot2","curl", "caret", "RCurl","parallel","snowfall","rmarkdown","jpeg", "knitr", "tictoc", "EBImage", "keras","tensorflow", "magick")
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]))
} }
install_tensorflow()
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/"
set.seed(333)
trapdir<-paste0(basedir, "Animals/Empty/")
ratdir<-paste0(basedir, "Animals/Rats/")
mousedir<-paste0(basedir, "Animals/Mice/")
pigdir<-paste0(basedir, "Animals/Pigs/")
birdir<-paste0(basedir, "Animals/Birds/")
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)]
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)
for (testpic in testindex){ pic <- readImage(testpic) pic<- resize(pic, 256, 256)
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) } }
names_list <- c("Empty", "Birds", "Rats", "Mice", "Pigs")
output_n <- length(names_list)
img_width <- 256 img_height <- 256 target_size <- c(img_width, img_height)
channels <- 3
train_image_files_path <-traindir valid_image_files_path <-testdir
train_data_gen <- image_data_generator(traindir, rescale = 1/255 #,
#width_shift_range = 0.2,
#height_shift_range = 0.2,
#shear_range = 0.2,
#zoom_range = 0.2,
#horizontal_flip = TRUE,
#fill_mode = "nearest"
)
valid_data_gen <- image_data_generator(testdir, rescale = 1/255 )
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)
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") )
train_samples <- train_image_array_gen$n
valid_samples <- valid_image_array_gen$n
batch_size <- 32 epochs <- 10
model <- keras_model_sequential()
model %>% layer_conv_2d(filter = 32, kernel_size = c(3,3), padding = "same", input_shape = c(img_width, img_height, channels)) %>% layer_activation("relu") %>%
layer_conv_2d(filter = 16, kernel_size = c(3,3), padding = "same") %>% layer_activation_leaky_relu(0.5) %>% layer_batch_normalization() %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>% layer_dropout(0.25) %>%
layer_flatten() %>% layer_dense(100) %>% layer_activation("relu") %>% layer_dropout(0.5) %>%
layer_dense(output_n) %>% layer_activation("softmax")
model %>% compile( loss = "categorical_crossentropy", optimizer = optimizer_rmsprop(lr = 0.0001, decay = 1e-6), metrics = "accuracy" )
hist <- model %>% fit_generator(
train_image_array_gen,
steps_per_epoch = as.integer(train_samples / batch_size), epochs = epochs,
validation_data = valid_image_array_gen, validation_steps = as.integer(valid_samples / batch_size),
verbose = 1, callbacks = list(
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")
) )
To execute VGG16_demo.py, PIL is needed.