-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
48 lines (38 loc) · 1.53 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Import the required library
from transformers import BeitFeatureExtractor, BeitForImageClassification
from PIL import Image
import requests
import argparse
# Command-line arguments handling
parser = argparse.ArgumentParser()
parser.add_argument('--url') # Argument for image URL
parser.add_argument('--image') # Argument for local image path
args = parser.parse_args()
# Check if URL or local image path is provided
if args.url:
# Open image from URL and convert to RGB format
image = Image.open(requests.get(args.url, stream=True).raw).convert('RGB')
elif args.image:
# Open local image and convert to RGB format
image = Image.open(args.image).convert('RGB')
else:
print('Please choose a URL or local image!')
parser.print_usage()
exit()
# Load pre-trained BEiT feature extractor
feature_extractor = BeitFeatureExtractor.from_pretrained('yuuki0/test')
# Load pre-trained BEiT model for image classification
model = BeitForImageClassification.from_pretrained('yuuki0/test')
# Extract features from the image
inputs = feature_extractor(images=image, return_tensors="pt")
# Pass the image features through the model
outputs = model(**inputs)
# Get the logits from the model output
logits = outputs.logits
# Calculate probabilities from logits
probs = logits.softmax(-1)
# Get the index of the predicted class
predicted_class_idx = probs.argmax().item()
# Print the predicted class and its probability
print("Predicted class:", model.config.id2label[predicted_class_idx])
print("Probability:", probs[0][predicted_class_idx].item())