blog icon indicating copy to clipboard operation
blog copied to clipboard

Make Prediction

Open oumalek25 opened this issue 3 years ago • 2 comments

How can I make predictions after training the model ?

oumalek25 avatar Jun 05 '22 14:06 oumalek25

Follow!!!!!

I saved a model: which one is the function to call in order to do prediction? Should be something like model.prediction(x_te), but it shows this error:

AttributeError: 'ViTForImageClassification' object has no attribute 'predict'

@nateraw @mariosasko

dgrnd4 avatar Jul 14 '22 14:07 dgrnd4

from transformers import ViTFeatureExtractor, ViTForImageClassification
from PIL import Image
import requests

url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)

feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')

inputs = feature_extractor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
# model predicts one of the 1000 ImageNet classes
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])

nateraw avatar Jul 14 '22 16:07 nateraw