To get predictions in order pass in ordered=True as a parameter to get_preds()
I am getting the prediction for validation set when I am using
preds, y = learn.get_preds(DatasetType.Test
Hi there,
I did pass ordered=True to get_preds( ), but it gives me “Unexpected keyword - ordered” error
I am using the below for getting predictions on my test set. However, I am getting different results for same data points if take the entire Test Dataset vs I take just the first 1000 examples. Can anyone explain the reason for such behavior or I am missing something?
FYI- I am using FastAI v1
def get_preds_as_nparray(ds_type) -> np.ndarray:
"""
the get_preds method does not yield the elements in order by default
we borrow the code from the RNNLearner to resort the elements into their correct order
"""
preds = learner.get_preds(ds_type)[0].detach().cpu().numpy()
sampler = [i for i in databunch.dl(ds_type).sampler]
reverse_sampler = np.argsort(sampler)
return preds[reverse_sampler, :]
test_preds = get_preds_as_nparray(DatasetType.Test)
Hello, were you able to solve this problem?
Hi, i’m quite new to fastai but i’ve found a simple workaround for predicting on new data, it may be useful for someone.
NB : I’m using ‘Imagenet style’ dataset, (e.g.: ‘data/test/class_to_pred/file.jpg’), so maybe you’ll need to change some parts to fit to your data structure.
import numpy as np
from fastai.data.transforms import get_image_files
from sklearn.metrics import classification_report, confusion_matrix
def get_preds_on_test(learner, test_path, show_results=False):
fnames = get_image_files(test_path)
dl = learner.dls.test_dl(fnames)
classes = learner.dls.vocab
# extracting true target value from path
trues = [os.path.basename(os.path.dirname(fname)) for fname in fnames]
# make predictions and extract the class name of the highest probability
preds = [classes[i] for i in np.argmax(learner.get_preds(dl=dl)[0], 1)]
if show_results:
print(classification_report(trues, preds))
print(confusion_matrix(trues, preds))
return trues, preds
trues, preds = get_preds_on_test(learner, 'path/to/your/test/folder', True)