title: Output order in Keras predict_generator
body: <p>I have followed online tutorial about image recognition using Keras in R ending up with the following code:</p>
  
  <pre><code>library(keras)
  
  view_list &lt;- c("Inside", "Outside")
  output_n &lt;- length(view_list)
  
  # image size to scale down to (original images are 100 x 100 px)
  img_width &lt;- 20
  img_height &lt;- 20
  target_size &lt;- c(img_width, img_height)
  
  # RGB = 3 channels
  channels &lt;- 3
  
  train_image_files_path &lt;- "C:/Users/Tomek/Desktop/Photos"
  valid_image_files_path &lt;- "C:/Users/Tomek/Desktop/Photos valid"
  test_image_files_path &lt;- "C:/Users/Tomek/Desktop/Photos test"
  
  # optional data augmentation
  train_data_gen = image_data_generator(rescale = 1/255 )
  
  # Validation data shouldn't be augmented! But it should also be scaled.
  valid_data_gen &lt;- image_data_generator(rescale = 1/255)  
  test_data_gen &lt;- image_data_generator(rescale = 1/255) 
  
  # training images
  train_image_array_gen &lt;- flow_images_from_directory(train_image_files_path, 
                                                      train_data_gen,
                                                      target_size = target_size,
                                                      class_mode = "categorical",
                                                      classes = view_list,
                                                      seed = 42)
  
  
  # validation images
  valid_image_array_gen &lt;- flow_images_from_directory(valid_image_files_path, 
                                                      valid_data_gen,
                                                      target_size = target_size,
                                                      class_mode = "categorical",
                                                      classes = view_list,
                                                      seed = 42)
  
  # test images
  test_image_array_gen &lt;- flow_images_from_directory(test_image_files_path, 
                                                     test_data_gen,
                                                     target_size = target_size,
                                                     class_mode = "categorical",
                                                     classes = view_list,
                                                     seed = 42)
  
  cat("Number of images per class:")
  table(factor(train_image_array_gen$classes))
  train_image_array_gen$class_indices
  
  views_classes_indices &lt;- train_image_array_gen$class_indices
  save(views_classes_indices, file = "C:/Users/Tomek/Desktop/views_classes_indices.RData")
  
  # number of training samples
  train_samples &lt;- train_image_array_gen$n
  # number of validation samples
  valid_samples &lt;- valid_image_array_gen$n
  # number of test samples
  test_samples &lt;- test_image_array_gen$n
  
  # define batch size and number of epochs
  batch_size &lt;- 1
  epochs &lt;- 10
  
  # initialise model
  model &lt;- keras_model_sequential()
  
  # add layers
  model %&gt;%
    layer_conv_2d(filter = 32, kernel_size = c(3,3), padding = "same", input_shape = c(img_width, img_height, channels)) %&gt;%
    layer_activation("relu") %&gt;%
  
    # Second hidden layer
    layer_conv_2d(filter = 16, kernel_size = c(3,3), padding = "same") %&gt;%
    layer_activation_leaky_relu(0.5) %&gt;%
    layer_batch_normalization() %&gt;%
  
    # Use max pooling
    layer_max_pooling_2d(pool_size = c(2,2)) %&gt;%
    layer_dropout(0.25) %&gt;%
  
    # Flatten max filtered output into feature vector 
    # and feed into dense layer
    layer_flatten() %&gt;%
    layer_dense(100) %&gt;%
    layer_activation("relu") %&gt;%
    layer_dropout(0.5) %&gt;%
  
    # Outputs from dense layer are projected onto output layer
    layer_dense(output_n) %&gt;% 
    layer_activation("softmax")
  
  # compile
  model %&gt;% compile(
    loss = "categorical_crossentropy",
    optimizer = optimizer_rmsprop(lr = 0.0001, decay = 1e-6),
    metrics = "accuracy"
  )
  
  summary(model)
  
  # fit
  hist &lt;- model %&gt;% 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 = 2,
    callbacks = list(
      # save best model after every epoch
      callback_model_checkpoint("C:/Users/Tomek/Desktop/views_checkpoints.h5", save_best_only = TRUE),
      # only needed for visualising with TensorBoard
      callback_tensorboard(log_dir = "C:/Users/Tomek/Desktop/keras/logs")
    )
  )
  plot(hist)
  
  #prediction
  a &lt;- model %&gt;% predict_generator(test_image_array_gen, steps = 5, verbose = 1, workers = 1)
  a &lt;- round(a, digits = 4)
  </code></pre>
  
  <p>The classification model (with two output classes) seems to work quite nicely. The accuracy on the train and the validation sets is equal to ~99% and ~95% respectively. However, I am not sure about the results of predictions on the test set. It looks like the predictions for observations are shuffled and I am not able to find a way to check which prediction refers to which image(observation). I have seen some threads on that issue: <a href="https://github.com/keras-team/keras/issues/3477" rel="nofollow noreferrer">github</a> <a href="https://medium.com/difference-engine-ai/keras-a-thing-you-should-know-about-keras-if-you-plan-to-train-a-deep-learning-model-on-a-large-fdd63ce66bd2" rel="nofollow noreferrer">medium 1</a> <a href="https://medium.com/@vijayabhaskar96/tutorial-on-keras-flow-from-dataframe-1fd4493d237c" rel="nofollow noreferrer">medium 2</a>.</p>
  
  <p>Nevertheless, I am really new to Keras and Python and I have hard time applying the suggested solutions in R. What is the easiest way to track which prediction refers to which image from the test set in predict_generator output?</p>