Saving get_preds results to Excel with file names

I am using a saved model to run inference for 1200+ image files. I want to save the predictions from get_preds() function to an Excel file in this format - File Name, Prediction, Class.

The objective is to submit the excel file to our governance group so that they can calculate the ROC.

My inference code looks like this.

learn = load_learner(path, test=ImageList.from_folder(testPath_img/‘tbhelp’/‘tbhelp_png’))
preds,y = learn.get_preds(ds_type=DatasetType.Test)

Any help is highly appreciated.

Hi. I am not sure what help you are asking for specifically. The simplest file format that Excel can read is CSV. You have many options for writing a CSV file: in Python, the csv package, numpy.savetxt, and pandas.DataFrame.to_csv.

Here is a code fragment I wrote for submitting probability predictions to Kaggle. Maybe it can be adapted to your problem.

testprobs,labels = learn.get_preds(ds_type=DatasetType.Test)
testdf = pd.DataFrame(dict(id=data.test_ds.x._relative_item_paths(), label=0))
testdf['label'] = testprobs.numpy()
testdf['id'] = testdf['id'].apply(lambda fp: Path(fp).stem)
testdf.to_csv(SUBM/'submissionFile.csv', index=False, float_format='%.9f')

HTH,
Malcolm

Malcolm,

Thank you so much for your feedback.

The missing piece for me was the lambda statement. I was able to solve my problem by first creating a ImageDataBunch object instead of passing ImageList to the load_learner method and then used your testdf[‘label’] line!!