简体   繁体   中英

Classify multiple images in one go with InceptionV3

I'm trying to classify several images at once in one directory using InceptionV3.
I was able to extract bottleneck features using the flow from directory method and get the results of the predictions as an array.

My question is how do I know which image each prediction refers to, since they are out of order in the array.
I tried to extract classes using generator_top.classes, but it takes them from the name of the directory where the images are.
I see that the predictions are all correct (those objects that are on the images, those are obtained in the original array).
I would only understand how to compare them.

In addition, I used this approach when testing images on a large test sample (there were folders with each class and images in them) and all the predictions in the array were displayed in order of folders in test dir but when I try to do this from the same directory with different classes, I cannot compare the predictions with the images.

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,  
    target_size=(img_width, img_height),  
    batch_size=batch_size,  
    class_mode=None,  
    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]

You can get the filenames of the files generated, and map it with the predicted results.

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]

The filenames array will have the filenames, use functions like zip in python, to map the filenames and the predicted results.

Note 1 - This will work when

shuffle=False

in the generator, because the data may be shuffled after reading and you wont be able to track the shuffle.

Note 2 - this answer is inspired from Keras flowFromDirectory get file names as they are being generated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM