How to know the corresponding 'class' in the predictions output in multiple class classification

In lesson 1, we have these:

batches, preds = vgg.test(path+‘test’, batch_size = batch_size*2)
isdog = preds[:,1]

Where to control the 2nd value in each “preds” to be the dog’s class? In a multiple class classification, how to know which class refers to which category?

For example,


pred[0] = [0.2, 0, 5, 0.3]

How to make sure class ‘a’ corresponds to 0.2, ‘b’ -> 0.5 and ‘c’->0.3?

@martin You can use batches.class_indices to see the mapping

{'cats': 0, 'dogs': 1}
1 Like

Thank you. @rachel

Actually, I found that:

batches, preds = vgg.test(path+‘test’, batch_size = batch_size*2)
filenames = batches.filenames

In this code, batches.class_indices only contains:

dict:{‘unknown’: 0}, since in the ‘test’ directory, there is only one class, which is ‘unknown’. Other classes information in the training data is not available.

Thanks for the answer, I am strting to grasp it.

I tried adding a 3rd folder to the catdogsproyect, so I make a ‘bears’ folder in the sample train and validation, the thing is, I trained the network, and I want to test the prediccions (because I am working with very few images the accuracy is awfull but that is not what I am looking for in this moment)

So, in my I have this code

batch_size = 64

#Instanciamos el modelo pre entrenado
vgg = Vgg16()

batches = vgg.get_batches('datos/mygato/', batch_size=batch_size)

#Si quiero solo porcentajes
#batches = vgg.get_batches('datos/mygato/', batch_size=batch_size, class_mode=None)

#batches = vgg.test('datos/mygato/', batch_size=batch_size)
imgs,labels = next(batches)

filenames = batches.filenames

pred = vgg.predict(imgs)

print("La prediccion dio "+str(pred))

print("Los nombres"+str(filenames))

print("Los indices son :")

print("Si quiero solo el vector"+str(pred[:2]))

My output is

Found 4 images belonging to 3 classes.
La prediccion dio (array([0.9563, 0.9997, 0.9986, 0.9986], dtype=float32), array([0, 1, 2, 2]), ['tench', 'goldfish', 'great_white_shark', 'great_white_shark'])
Los nombres['bears/44465723_b99395d4c2.jpg', 'cats/rengar2.jpg', 'cats/rengar.jpg', 'dogs/images.jpeg']
Los indices son :
{'dogs': 2, 'cats': 1, 'bears': 0}
Si quiero solo el vector(array([0.9563, 0.9997, 0.9986, 0.9986], dtype=float32), array([0, 1, 2, 2]))

So, because I have 3 clases I have 3 indexes, thats allright, then I have an array with 0 (bear), 1 cat (1) and 2 dogs

I thought that because of the OHC I will have this other array


What I did wrong?

EDIT: Sorry for my English it is not my natal tongue

Hi Rachel,

This is my first ever post so hopefully it makes sense. The course is amazing BTW!

I have used the lesson 1 with a dataset of 60 classes of fruit.

The model works amazingly well in that it achieves over 99.5% accuracy. However, I find it hard to understand the correct and incorrect results because I do not have the information about classes.

When I tried to use batches.class_indices, I get an error:

I think it is because in the dogs and cats model there is no mention of batches. How do I then find the mapping?

Thanks a lot!