Image Classifier learn.predict(img)

Hi all,

Thanks to muellerzr and sgugger, we resolved this issue. Here is the catch, the code that threw an error used F.cross_entropy loss learn = cnn_learner(dls, resnet18, pretrained=False, loss_func=F.cross_entropy, metrics=accuracy)

When it magically worked for you learn = cnn_learner(dls, resnet34, metrics=error_rate), you skipped providing the loss function and fastai picked the CrossEntropyLossFlat function for you. The reason this works is that every loss function has an extension where both input and targets are flattened. In technical terms, the loss function requires an activation and a decode method for the predict method to work.

Why is that important is beyond me for now but I’ll try to answer that soon. Hope that helped.

3 Likes