Arsenal Fanatic
Arsenal Fanatic

Reputation: 3813

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

Upvotes: 11

Views: 21899

Answers (4)

Basma Elshoky
Basma Elshoky

Reputation: 149

The error because of the path have sub directory 'category' such as cat and dogs. You should create a new directory that will contain all images. Example dataset contains:

  1. ../input.../train/
    autistic/
  • image1.jpg
  • image2.jpg
  1. ../input.../train/
    non_autistic/
  • image1.jpg
  • image2.jpg

Copy all image to one diectory/folder:

from distutils.dir_util import copy_tree
toDir = "AllTrain"
fromdir = "../input/autistic-children-data-set/train/autistic"
copy_tree(fromdir ,toDir)
fromdirNon = "../input/autistic-children-data-set/train/non_autistic"
copy_tree(fromdirNon ,toDir)

Add lable to each category:

filenames = []
categories = []
Train_autistic = os.listdir("../input/autistic-children-data-set/train/autistic/")
for filename in Train_autistic :
        categories.append(1)
filenames.extend(Train_autistic )

Train_non_autistic = os.listdir("../input/autistic-children-data-set/train/non_autistic/")
for filename in Train_non_autistic :
        categories.append(0)
filenames.extend(Train_non_autistic )

train_df = pd.DataFrame({
    'filename': filenames,
    'category': categories
})

train_df["category"] = train_df["category"].replace({0: 'non_autistic', 1: 'autistic'}) 

then use:

train_generator = train_datagen.flow_from_dataframe(
    train_df, "AllTrain/", 
    x_col='filename',
    y_col='category',
    target_size=IMAGE_SIZE,
    class_mode='categorical',
    batch_size=batch_size
)

ineasted of:

train_generator = train_datagen.flow_from_dataframe(
    train_df, "../input/autistic-children-data-set/train", 
    target_size=IMAGE_SIZE,
    class_mode='binary',
    batch_size=batch_size
)

Upvotes: 2

Kamini Salunke
Kamini Salunke

Reputation: 11

Its Just about your file path look here is my file for training images =

C:/Users/Admin/python/Dataset/training_set/data

here is my file for test images =

C:/Users/Admin/python/Dataset/test_set/data and in data folder of each path i have put my images.

but now , if you are giving this in a command , you need to give it as:

test_set = train_datagen.flow_from_directory('C:/Users/Admin/python/Dataset/test_set',target_size=(435,116),batch_size=4,class_mode='binary')

and

test_set = train_datagen.flow_from_directory('C:/Users/Admin/python/Dataset/test_set',target_size=(435,116),batch_size=4,class_mode='binary')

Do not mention 'data' folder in this path. this will solve the issue

Upvotes: 1

Pvic
Pvic

Reputation: 101

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

Upvotes: 4

Mike Ivanov
Mike Ivanov

Reputation: 979

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.

Upvotes: 16

Related Questions