I've a classifier model with 8 classes. I run predictions with it on the test set and the model returns one hot encoded arrays. Now, when I argmax these predictions in order to convert them into strings, they are not being converted into the right categories. I think the encoded arrays are the right predictions but after argmax, it is getting messed up.
import cv2
def prepare(path):
imgsize=128
img_array = cv2.imread(path)
new_array = cv2.resize(img_array, (imgsize,imgsize))
return new_array.reshape(-1,imgsize,imgsize,3)
predictions = []
matrix = []
for label in os.listdir(path_test):
p = model.predict([prepare(path_test "/" label)])
cl = numpy.argmax(p)
matrix.append(p)
predictions.append(cl)
My eight classes are:
['yam', 'hak', 'ali', 'udi', 'uri', 'tam', 'ssi', 'iya']
Thus when I run a for loop on the three lists, they're not matching up:
for i in predictions:
print(classes[i], matrix[int(i)], i)
ssi [[0. 0. 0. 0. 0. 0. 1. 0.]] 6
ssi [[0. 0. 0. 0. 0. 0. 1. 0.]] 6
ali [[0. 0. 0. 0. 0. 0. 1. 0.]] 2
yam [[0. 0. 0. 0. 0. 0. 1. 0.]] 0
udi [[0. 0. 1. 0. 0. 0. 0. 0.]] 3
ali [[0. 0. 0. 0. 0. 0. 1. 0.]] 2
yam [[0. 0. 0. 0. 0. 0. 1. 0.]] 0
tam [[0. 0. 0. 0. 0. 0. 1. 0.]] 5
uri [[0. 0. 0. 0. 0. 0. 1. 0.]] 4
As you can see the same arrays are being assigned different values after argmax. For many images in the test set that appear to be index 6 are being classified anything and everything. I'm not sure if the same is happening with other classes. Can someone explain why this could be happening or I'm not interpreting it right?
CodePudding user response:
I think your printing loop is wrong because you're indexing into matrix
with the prediction class index rather than the actual corresponding row. Try this.
for cl, preds in zip(predictions, matrix):
print(classes[cl], preds, cl)