Predict_array returns constant value [0.0, 1.0]

Hi all! Thanks for this awesome course and the great people who take the time to answer questions on the threads.

I have trained a model with two classes and multi class images. I am pretty happy with the training result. However, now when I try to predict an image using predict_array based on a saved model, I get constant response that looks like this: [[0. 1.]]. I get the same response on different images and even on a completely black image.

Here’s the code I’ve used for training. Please note that I have not used any augmentation as I am using 100 000 images.

f_model = resnet101
n = len(list(open(CSVPATH)))-1
val_idxs = get_cv_idxs(n)

def get_data(sz):
    tfms = tfms_from_model(f_model, sz)
    return ImageClassifierData.from_csv(PATH, 'train', CSVPATH, tfms=tfms,
                    val_idxs=val_idxs)

sz = 128
data = get_data(sz)

# Since we are training earlier layers in the model, precompute needs to be False
learn = ConvLearner.pretrained(f_model, data, precompute=False)

lr = 0.0045
lrs = np.array([lr/9,lr/3,lr])
learn.unfreeze()
learn.fit(lrs, 3, cycle_len=1, cycle_mult=2)
learn.save(f'{MODELPATH}{sz}')

The last Epoch looks like this:
6 0.069832 0.090509 0.967358

After saving the model, this is the code I use to predict a single image:

predict = ConvLearner.pretrained(f_model, data, precompute=False)
predict.load(f'{MODELPATH}{sz}')
predict.model.eval()
trn_tfms, val_tfms = tfms_from_model(f_model, sz)

fileName = PREDICTPATH+"im1.jpg"
im = np.array(Image.open(fileName).resize((sz, sz)))
im_transformed = val_tfms(im)
plt.imshow(im)

pr = predict.predict_array(im_transformed[None])
print(pr)

Any help is appreciated. Thanks!

Check out this thread: Cannot make Individual prediction work

Basically, you should use open_image (from dataset.py) to open a single image as np array. It’ll handle scaling it the same way data being loaded into the learner was scaled. np.resize may cause you an issue too, since that will only juggle things around in the array, it won’t change dimensions of the image through bilinear interpolation or any of that stuff. Surprised you aren’t getting a shape error from resizing to 2d array. Are these grayscale images?

Thanks @gmlander using open_image worked!

The images are not grayscale.