使用InceptionV3一次性将多个图像分类

我正在尝试使用InceptionV3在一个目录中一次对多个图像进行分类。
我能够使用目录方法中的流程提取瓶颈特征,并以数组的形式获得预测结果。

我的问题是我如何知道每个预测所指的图像,因为它们在数组中是不规则的。
我尝试使用generator_top.classes提取类,但是它从图像所在目录的名称中提取它们。
我看到预测都是正确的(图像上的那些对象是从原始数组中获得的)。
我只会了解如何比较它们。

此外,我在大型测试样本上测试图像时使用了这种方法(有每个类的文件夹和图像),并且数组中的所有预测都按测试目录中的文件夹顺序显示 但是当我尝试从同一目录中使用不同的类来执行此操作时,我无法将预测结果与图像进行比较。

from keras import applications

base_model = applications.InceptionV3(include_top=False,weights='imagenet')

res_model = Sequential()
res_model.add(GlobalAveragePooling2D(input_shape=train_data.shape[1:]))
res_model.add(Dense(17,activation='softmax'))
datagen=ImageDataGenerator(rescale=1./255)


generator = datagen.flow_from_directory(  
    test_path,target_size=(img_width,img_height),batch_size=batch_size,class_mode=None,shuffle=False)

nb_test_samples = len(generator.filenames)  #17
predict_size_test = int(math.ceil(nb_test_samples/batch_size))   #2  
bottleneck_features_test = base_model.predict_generator( generator,predict_size_test,verbose=1) 

np.save('bottleneck_features_test_10000inc.npy',bottleneck_features_test)

generator = datagen.flow_from_directory(  
    test_path,shuffle=False)  

nb_test_samples = len(generator_top.filenames)  

test_data = np.load('bottleneck_features_test_10000inc.npy')  

test_labels = generator_top.classes
test_labels = to_categorical(test_labels,num_classes=17) 

tr_predictions=[np.argmax(res_model.predict(np.expand_dims(feature,axis=0)))for feature in test_data]
qq579694 回答:使用InceptionV3一次性将多个图像分类

您可以获取所生成文件的文件名,并将其与预测结果进行映射。

expect(router).toHaveBeenCalledWith('/home')

文件名数组将具有文件名,并使用python中的zip之类的功能来映射文件名和预测结果。

注意1-

可用
datagen = ImageDataGenerator()
gen = datagen.flow_from_directory(...)
for i in gen:
idx = (gen.batch_index - 1) * gen.batch_size
filenames = gen.filenames[idx : idx + gen.batch_size]

在生成器中,因为在读取后数据可能会被随机播放,您将无法跟踪随机播放。

注释2-此答案的灵感来自 Keras flowFromDirectory get file names as they are being generated

本文链接:https://www.f2er.com/3161024.html

大家都在问