blog
blog copied to clipboard
Make Prediction
How can I make predictions after training the model ?
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
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])