Repository: keras-team/keras

Issue:
Using flow_from_dataframe with predict_generator produces wrong ordering of filenames
flow_from_dataframe is a very useful function allowing flexibility that flow_from_directory lacks, for e.g, regression tasks.
But the problem with this function is it processes the dataframe's (filename) column in sorted way.
for example
filename  y
a              3.2
d              2.2
c               5.2

in principle, the generator should process files in this way:
a,d,c
but the current implementation processes file in alphabtic order
a,c,d

this is not so convenient for predict_generator function
Using flow_from_dataframe with predict_generator produces wrong ordering of filenames
flow_from_dataframe is a very useful function allowing flexibility that flow_from_directory lacks, for e.g, regression tasks.
But the problem with this function is it processes the dataframe's (filename) column in sorted way.
for example
filename  y
a              3.2
d              2.2
c               5.2

in principle, the generator should process files in this way:
a,d,c
but the current implementation processes file in alphabtic order
a,c,d

this is not so convenient for predict_generator function


Discussion:
Have you set `shuffle=False` ?
I have the same problem. 

This is where it originates from:

_keras-preprocessing/keras_preprocessing/image.py_
```
def _iter_valid_files(directory, white_list_formats, follow_links):
    """Iterates on files with extension in `white_list_formats` contained in `directory`.

    # Arguments
        directory: Absolute path to the directory
            containing files to be counted
        white_list_formats: Set of strings containing allowed extensions for
            the files to be counted.
        follow_links: Boolean.

    # Yields
        Tuple of (root, filename) with extension in `white_list_formats`.
    """
    def _recursive_list(subpath):
        return sorted(os.walk(subpath, followlinks=follow_links),
                      key=lambda x: x[0])

    for root, _, files in _recursive_list(directory):
        for fname in sorted(files):
            for extension in white_list_formats:
                if fname.lower().endswith('.tiff'):
                    warnings.warn('Using \'.tiff\' files with multiple bands '
                                  'will cause distortion. '
                                  'Please verify your output.')
                if fname.lower().endswith('.' + extension):
                    yield root, fname
```

The filenames from the dataframe are getting sorted when they are read out.

Either this should be fixed (remove sorted) or it should be documented.
@Dref360 I labeled this as a bug. Can you confirm?
Not a bug, but it should be documented.
Without the sorted, we could get two different lists since os.walk is not deterministic.
Have you set `shuffle=False` ?
I have the same problem. 

This is where it originates from:

_keras-preprocessing/keras_preprocessing/image.py_
```
def _iter_valid_files(directory, white_list_formats, follow_links):
    """Iterates on files with extension in `white_list_formats` contained in `directory`.

    # Arguments
        directory: Absolute path to the directory
            containing files to be counted
        white_list_formats: Set of strings containing allowed extensions for
            the files to be counted.
        follow_links: Boolean.

    # Yields
        Tuple of (root, filename) with extension in `white_list_formats`.
    """
    def _recursive_list(subpath):
        return sorted(os.walk(subpath, followlinks=follow_links),
                      key=lambda x: x[0])

    for root, _, files in _recursive_list(directory):
        for fname in sorted(files):
            for extension in white_list_formats:
                if fname.lower().endswith('.tiff'):
                    warnings.warn('Using \'.tiff\' files with multiple bands '
                                  'will cause distortion. '
                                  'Please verify your output.')
                if fname.lower().endswith('.' + extension):
                    yield root, fname
```

The filenames from the dataframe are getting sorted when they are read out.

Either this should be fixed (remove sorted) or it should be documented.
@Dref360 I labeled this as a bug. Can you confirm?
Not a bug, but it should be documented.
Without the sorted, we could get two different lists since os.walk is not deterministic.

Resolving patch:
diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py
--- a/keras/preprocessing/image.py
+++ b/keras/preprocessing/image.py
@@ -102,6 +102,7 @@ class DirectoryIterator(image.DirectoryIterator, Iterator):
             `None`: no targets get yielded (only input images are yielded).
         batch_size: Integer, size of a batch.
         shuffle: Boolean, whether to shuffle the data between epochs.
+            If set to False, sorts the data in alphanumeric order.
         seed: Random seed for data shuffling.
         data_format: String, one of `channels_first`, `channels_last`.
         save_to_dir: Optional directory where to save the pictures


Test patch: