我正在使用目录中的流来获取图像并创建一个生成器,然后我在predict_generator中使用它来预测概率和类。问题在于,当我预测两者时,虽然我没有在任何地方使用shuffle参数,但标签会被洗牌。如何将正确的类分配给正确的标签?以下是我的完整代码:
码
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)
类的输出
[4 4 4 4 0 4 1 0 4 1 3 4]
输出文件名
['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']
默认情况下,shuffle设置为True,将生成器更改为false,如下所示:
validation_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(path, target_size=(img_width, img_height), batch_size=6, shuffle=False)