我正在尝试使用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]