I have a model I trained in fastai. It achieves 95% accuracy, but my predictions are always the same class. I’ve never had this problem using rgb images, so I am assuming something is wrong with the fact that the data is grayscale.
For reference, the model was trained on gray scale images that were already in shape (24, 24, 3)
. When I run inference on test images, they are already (24,24,3)
. I don’t know if this has any effect, but when I look at the print out from my data
object, I see images in shape (3,24,24)
, see print out below. I’ve tried getting my test images into this shape (swapping the axis) but they are no longer accepted as proper input. What do I do?
Here is my training code:
data = ImageDataBunch.from_folder(path, train='train', valid='valid', size=24, bs=64, num_workers=8)
learn = cnn_learner(data, models.resnet34, metrics = [accuracy])
learn.fit_one_cycle(4)
learn.unfreeze()
learn.lr_find()
learn.recorder.plot(suggestion=True)
learn.fit_one_cycle(2, max_lr=slice(3e-7, 3e-6))
learn.export('model.pkl')
Now for inference:
l = load_learner('', 'model.pkl')
r = random.randint(0,100)
t = glob.glob('fastai_format/test/pos/*')[r]
img = cv2.imread(t)
print(img.shape)
This gives (24,24,3)
And for the actual prediction:
pil_im = PImage.fromarray(img)
x = pil2tensor(pil_im ,np.float32)
l.predict(Image(x))
This gives me:
(Category tensor(1), tensor(1), tensor([0., 1.]))
I get class 1 every time. Its a 2 class problem. I even get class 1 everytime if I run inference on training data. The model yields 95% so this is confusing.
What am I doing wrong?
Extra info: Here is the print out for data
:
Train: LabelList (9603 items)
x: ImageList
Image (3, 24, 24),Image (3, 24, 24),Image (3, 24, 24), ...
...
I am using fastai version 1.0.61.