0

i am using flow from directory to fetch the images and create a generator which i am then using in a predict_generator for predicting probabilities and classes. The problem is that when i predict both, the labels get shuffled although i am not using the shuffle argument anywhere. How can i assign the correct class to the correct label? Below is my complete code:

code

from __future__ import division
import numpy as np
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten

top_model_weights_path = '/home/rehan/ethnicity.071217.23-0.28.hdf5'
path = "/home/rehan/countries/pakistan/guys/test/"
img_width, img_height = 139, 139

confidence = 0.8

model = applications.InceptionResNetV2(include_top=False, weights='imagenet',
                                       input_shape=(img_width, img_height, 3))

print("base pretrained model loaded")

validation_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(path, target_size=(img_width, img_height),
                                                        batch_size=6)

print("generator built")
print(validation_generator.filenames)

features = model.predict_generator(validation_generator)

print("features found")

model = Sequential()
model.add(Flatten(input_shape=(3, 3, 1536)))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax'))
model.load_weights(top_model_weights_path)
print("top model loaded")
prediction_proba = model.predict_proba(features)
prediction_classes = model.predict_classes(features)

print(prediction_proba)
print(prediction_classes)

output of classes

[4 4 4 4 0 4 1 0 4 1 3 4]

output of file names

['test1/pakistan_guys_19_0327850289.jpg', 'test1/pakistan_guys_19_0328320258.jpg', 'test1/pakistan_guys_19_0328792595.jpg', 'test1/pakistan_guys_19_0329098521.jpg', 'test1/pakistan_guys_19_0330327554.jpg', 'test1/pakistan_guys_19_0331605496.jpg', 'test1/pakistan_guys_19_0340513245.jpg', 'test1/pakistan_guys_19_0340525097.jpg', 'test1/pakistan_guys_19_0340536960.jpg', 'test1/pakistan_guys_19_0340551769.jpg', 'test1/pakistan_guys_19_0341250408.jpg', 'test1/pakistan_guys_19_0341327910.jpg']
Rehan Aziz
  • 123
  • 12

1 Answers1

0

By default shuffle is set to True, change your generator to false like this:

validation_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(path, target_size=(img_width, img_height), batch_size=6, shuffle=False)
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55