Create visiontransformer
Browse files- visiontransformer +16 -0
visiontransformer
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
|
5 |
+
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
6 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
7 |
+
|
8 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
|
9 |
+
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
|
10 |
+
|
11 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
12 |
+
outputs = model(**inputs)
|
13 |
+
logits = outputs.logits
|
14 |
+
# model predicts one of the 1000 ImageNet classes
|
15 |
+
predicted_class_idx = logits.argmax(-1).item()
|
16 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx
|