0
$\begingroup$

I have trained my CNN model on CIFAR 10 and I got val_accuracy of 87% which is not a low value but when it comes to detection of pictures my model detected most of the pictures wrong. anyone knows why this is happening and how to solve this problem.

New contributor
Osama Hamada is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
4
  • $\begingroup$ I got a similar issue last time I trained a CNN, in my case i don't know exactly what happened, but it seems the classes got mixed up when training and I had to rearrange the order of the outputs. for example network always returned class 1 when it should have returned class 2, etc. So I had to create a dictionnaries to link each class to the correct output. $\endgroup$ – Ubikuity May 4 at 9:43
  • $\begingroup$ Thank you for the comment but I am a beginner and this is a university assignment so if you can provide me with the code that you have used it we will be so helpful. $\endgroup$ – Osama Hamada May 4 at 9:45
  • $\begingroup$ I will post the way I solved it in an answer, just remember that this was the solution for me, but might not the solution for your network. Just try sending images from a same class and see it it gives the same class everytime to see your network has the same problem as mine. $\endgroup$ – Ubikuity May 4 at 9:50
  • $\begingroup$ I have been given images to detect, I have downloaded an image of a house and send it to my model but my model detect it as a cat. I downloaded 3 more images of houses and my model detect them right this time so I am confused. Now I have 4 house images my model detected one as a cat and the other 3 as houses. $\endgroup$ – Osama Hamada May 4 at 10:03
0
$\begingroup$

Here is how i fixed the classes not corresponding to each other, code is done with Pytorch, most of my function are custom ones but I wrote the equivalent in comments :

def main():
    modelPath = '../Results/BestModel/model.ntw'
    model = Om.openModel(modelPath)  # torch.load(modelPath)

    dataPath = 'food-101/images'  # DataSet path
    listClasses = os.listdir(dataPath)  # List of classes, here each folder in dataPath is a class

    dicClasses = {}

    for i, imClass in enumerate(listClasses):
        if i % 5 == 0:
            print(str(i) + ' / ' + str(len(listClasses)))
        res = []
        lIm = os.listdir(os.path.join(dataPath, imClass))
        random.shuffle(lIm)
        for imName in lIm[:20]:  # Take 20 images of each class
            im = Om.loadImage(os.path.join(dataPath, imClass, imName))  # Loading image with correct transforms applied
            res.append(model(im).cpu().numpy())  # Store the result of the prediction
        resmed = np.mean(res, 0)[0]  # Sum all predictions
        r = indexesMax(resmed, 1)  # function is the same as torch.argmax(resmed).
        dicClasses[imClass] = r

    print(dicClasses)
$\endgroup$

Your Answer

Osama Hamada is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.