Hello! I am currently using fastaiv1 for single label image classification. I have the model in a fastapi endpoint and would like to return the confidence score along with the label.

Thanks a ton for this! It worked exactly how I needed it! Just another question. I was reading the learner docs, how did you know you could add the “label,ndx,probs” for this?

I actually have trouble parsing this section. It seems more to be documenting the internal representation than beginner usage. I’ve submitted a pull request for a small change that might assist. https://app.reviewnb.com/fastai/fastai/pull/3842/

Its good to preceed questions with a summary of your own research - what you tried that didn’t work. You’ll gain more from doing a little leg work first.
Try this search: How to return a dictionary in python

Hi, I was working on this problem with text classfiication and the ULMFiT langauge models and thought I’d share how I was doing this with get_preds and a test dataset that you want to generate batch predictions on:

# load learner from your pickle file
learn_inf = load_learner('models/model_v_1_0_0.pkl')
# load the text inputs from your dataframe to predict on into a test dataloader
dl = learn.dls.test_dl(df['text'])
# get_preds ordered=True as default param
preds, _ = learn.get_preds(dl=dl)
# get probs (values) and arg maxes (indices) of each row
preds_prob_max = preds.max(dim=1)
# get predicted decoded labels
predicted_labels = [learn.dls.vocab[1][p] for p in preds_prob_max.indices]
# get predicted probabilities
predicted_probs = preds_prob_max.values.numpy()
# zip decoded label predictions and probabilities into list of tuples
preds_probs_zip = list(zip(predicted_labels, predicted_probs))
# convert to dataframe
preds_probs_df = pd.DataFrame(preds_probs_zip, columns =['pred_label', 'probability'])
preds_probs_df

Now can work with preds_probs_df to add to concat to your inputs.