Last active
August 25, 2022 13:48
-
-
Save maziyarpanahi/64f0575cdccd383242c6683c0def46cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
predicted_class_idx = logits.argmax(-1).item() | |
print("Predicted class:", model.config.id2label[predicted_class_idx]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment