I'm using FastAI on Ubuntu 20.04.
>>> import fastai
>>> print(fastai.__version__)
2.7.13
I have trained my own image classifier CNN with 6 different classes. I load the learner for inference like this:
learner = load_learner("fast_ai_trained_models/face.pkl", cpu=False)
learner.dls.to(device='cuda')
learner.model.to(device='cuda')
I have successfully classified images ( with confidence values ) one by one with this code:
prediction = learner.predict(img_pil)
confidence = str(max(prediction[2]).item())[:5]
confidence = float(confidence)
Now I want to use learner.get_preds() to send many images at a time to the GPU for classification.
I call get_preds with the following code and get the expected number of results ( for each submitted image to infer ) in 'decoded' with the expected class index. However, I cannot figure out how to get the confidence value from the model for each decoded index. I have studied the docs and googled quite expensively. How do I calculate or retrieve the confidence for batch inference using get_preds()? Thanks.
classes = learner.dls.vocab
dl = learner.dls.test_dl(get_image_files(tmp_dir), bs=256)
preds, probs, decoded = learner.get_preds(dl=dl, with_decoded=True)
pred_to_consider = preds[0]
logger.info(f"First pred: {pred_to_consider} ...")
index_of_classification = np.argmax(pred_to_consider, axis=0)
logger.info(f"First class pred: {classes[index_of_classification]} ...")
logger.info(f"Decoded values are: {decoded} ...")
logger.info(f"Length of decoded values are: {len(decoded)} ...")
Output:
[2024-01-28 19:31:50,030] __main__ {batch.py:254} INFO - First pred: tensor([2.3549e-06, 2.6856e-07, 7.9529e-05, 7.0881e-05, 9.9972e-01, 4.2844e-06,
1.2226e-04]) ...
[2024-01-28 19:31:50,030] __main__ {batch.py:255} INFO - All classes: ['A', 'B', 'C', 'D', 'NEGATIVE', 'E', 'F'] ...
[2024-01-28 19:31:50,031] __main__ {batch.py:257} INFO - First class pred: NEGATIVE ...
[2024-01-28 19:31:50,032] __main__ {batch.py:258} INFO - Decoded values are: tensor([4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4,
4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4]) ...
[2024-01-28 19:31:50,033] __main__ {batch.py:259} INFO - Length of decoded values are: 181
...