Data Science Asked by Osama Hamada on July 7, 2021
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.
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)
Answered by Ubikuity on July 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP