# How to predict class label from class probability given by predict_generator for testdata?

While using Keras' flow_from_directory method to train my model on a multi-class image classification problem, the predict_generator function gives the class probabilities.

So, my query is how to get the corresponding class-labels for those class probabilities?

You just take the class with the maximum probability. This can be done using numpy argmax function.

• y_prob = model.predict(x) y_classes = y_prob.argmax(axis=-1) May 26 '18 at 14:38

I realize this question was raised some time back but came across this and thought will share what I have done

It is better to use predict_classes function from the keras model rather than predict_generator - I have run into issues while using this with the time it takes to complete. However, the input data to this function will have to be an array which means we will have to use :


yFit=model.predict_classes(data)

#post which use the train generator to map the labels
#back to actual names of the classes

label_map = (train_generator.class_indices)
label_map = dict((v,k) for k,v in label_map.items()) #flip k,v
predictions = [label_map[k] for k in yFit]



Just to simplify things to receive the class label try:

predict_img = '../Directory'



Converting to a numpy array

predict_img = image.img_to_array(predict_paper_1)

predict_img = np.expand_dims(predict_img,axis=0) # position from where converted image array is read

yFit=model.predict_classes(predict_img)

#post which use the trained generator to map the labels
#back to actual names of the classes

label_map = (train_gen.class_indices)

label_map = dict((v,k) for k,v in label_map.items()) #flip k,v

predictions = [label_map[k] for k in yFit[:,0]] # YFIT[:,0] INSURES THAT THE ARRAY IS READ IN THE RIGHT SHAPE

print(predictions)