Predictions not matching when using pipeline vs direct model
Hi
@AdamCodd
:
I have been trying the model and I found the results probabilities don't match when we use two methods listed in model_card file. Is there some other way the probability calculated from raw logits ?
This is the reproducible example :
from transformers import ViTImageProcessor, AutoModelForImageClassification, pipeline
from PIL import Image
import requests
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
# Direct Model
processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
print(f"AutoModelForImageClassification Probs: {torch.nn.functional.softmax(logits)}")
# Predicted class: sfw
# Pipeline Model
# Load image classification pipeline
pipeline_model = pipeline(task="image-classification", model="AdamCodd/vit-base-nsfw-detector", device=device)
print(f"Pipeline Probs: {pipeline_model(image)}")
OUTPUTS :
Device set to use cuda
AutoModelForImageClassification Probs: tensor([[0.9933, 0.0067]], grad_fn=<SoftmaxBackward0>)
Pipeline Probs: [{'label': 'sfw', 'score': 0.8909250497817993}, {'label': 'nsfw', 'score': 0.05239786580204964}]
Edit : The 2nd one also don't addup to 1, they are raw scores and not probs ?
Hello
@abhishek072
,
That's because the pipeline is applying a sigmoid function by default on the output (you can read more about it here: https://huggingface.co/docs/transformers/main_classes/pipelines#transformers.ImageClassificationPipeline). So if you want to get the probabilities, just add function_to_apply="softmax" argument to the pipeline.
pipeline_model = pipeline(task="image-classification", model="AdamCodd/vit-base-nsfw-detector", device=device, function_to_apply="softmax")
Pipeline Probs: [{'label': 'sfw', 'score': 0.9932757616043091}, {'label': 'nsfw', 'score': 0.006724205333739519}]
Thankyou
@AdamCodd
for this !!
Also do you think using softmax with 0.5 threshold as you said earlier would be better instead of sigmoid and taking higher one ? I'm more interested in getting probability of nsfw class for each input.