Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Image data generator throwing no files found error?

I unable to run simple data generator code from keras

import os
import keras as K
from keras.preprocessing.image import ImageDataGenerator

def save_images_from_generator(maximal_nb_of_images, generator):
    nb_of_images_processed = 0
    for x, _ in generator:
        nb_of_images += x.shape[0]
        if nb_of_images <= maximal_nb_of_images:
            for image_nb in range(x.shape[0]):
                your_custom_save(x[image_nb]) # your custom function for saving images
        else:
            break

Gen=ImageDataGenerator(featurewise_center=True,
    samplewise_center=False,
    featurewise_std_normalization=False,
    samplewise_std_normalization=False,
    zca_whitening=True,
    rotation_range=90,
    width_shift_range=0.2,
    height_shift_range=0.1,
    shear_range=0.5,
    zoom_range=0.2,
    channel_shift_range=0.1,
    fill_mode='nearest',
    cval=0.,
    horizontal_flip=True,
    vertical_flip=True,
    rescale=None,
    preprocessing_function=None)


if __name__ == '__main__':
    save_images_from_generator(40,Gen.flow_from_directory('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input', target_size=(150, 150),class_mode=None,save_prefix='augm',save_to_dir='C:\\Users\\aanilil\\PycharmProjects\\untitled\\im_output\\'))

Output

Using TensorFlow backend.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 1578, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 1015, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/aanilil/PycharmProjects/untitled/generate_data_from_folder.py", line 35, in <module>
    save_images_from_generator(40,Gen.flow_from_directory('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input', target_size=(150, 150),class_mode=None,save_prefix='augm',save_to_dir='C:\\Users\\aanilil\\PycharmProjects\\untitled\\im_output\\'))
  File "C:/Users/aanilil/PycharmProjects/untitled/generate_data_from_folder.py", line 7, in save_images_from_generator
    for x, _ in generator:
  File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 727, in __next__
    return self.next(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 950, in next
    index_array, current_index, current_batch_size = next(self.index_generator)
  File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 710, in _flow_index
    current_index = (self.batch_index * batch_size) % n
ZeroDivisionError: integer division or modulo by zero

When I do a os. listdir I get an output like so

os.listdir('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input') 
['download (1).png', 'download.jpg', 'download.png', 'images.jpg']

So there are images in the input folder and It still throws an error assoiciated to no files found

like image 914
Arsenal Fanatic Avatar asked Apr 10 '17 08:04

Arsenal Fanatic


People also ask

Is there a memory leak in keras?

Also, as mentioned already, there indeed are (you can find some of them, fe. here is an open one) a few issues with Keras memory leak when using the fit and fit_generator methods. This is common when running 32bit if the float precision is too high. Are you running 32bit? You may also consider casting or rounding the array.

What is Keras and why should you use it?

This is where Keras shines and provides these training abstractions which allow you to quickly train your models. This is very good for rapid prototyping. And the training samples would be generated on the fly using multi-processing [if it is enabled] thereby making the training faster. I’ll explain the arguments being used. [2]

How do I create an imagedatagenerator?

This involves the ImageDataGenerator class and few other visualization libraries. There are two main steps involved in creating the generator. Instantiate ImageDataGenerator with required arguments to create an object Use the appropriate flow command (more on this later) depending on how your data is stored on disk.


Video Answer


2 Answers

Keras assumes that images are stored in a folder tree with one separate subfolder per class, like this:

  • some/path/
    • class1/
      • image1.jpg
      • image2.jpg
    • class2/
      • image3.jpg
      • etc
    • etc

So, in your case the solution is to create a subfolder under 'C:\Users\aanilil\PycharmProjects\untitled\images_input' and move the images there. Of course, you'll need more than one class subfolder for training a classifier, if that is your goal.

like image 175
Mike Ivanov Avatar answered Nov 16 '22 04:11

Mike Ivanov


Another possibility, if you have no classes pre defined, is to put all the images in a sub folder from your image folder e.g:

flow_from_directory(directory = "/path/images/",…)

Your actual data inside images/data

like image 44
Pvic Avatar answered Nov 16 '22 02:11

Pvic