Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import MobileNetV2FeatureExtractor, MobileNetV2ForImageClassification
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# Replace with the path to the directory containing the model files
|
| 5 |
+
model_folder = "/path/to/your/model_folder"
|
| 6 |
+
|
| 7 |
+
# Load the feature extractor
|
| 8 |
+
feature_extractor = MobileNetV2FeatureExtractor.from_pretrained(model_folder)
|
| 9 |
+
|
| 10 |
+
# Load the model
|
| 11 |
+
model = MobileNetV2ForImageClassification.from_pretrained(model_folder)
|
| 12 |
+
|
| 13 |
+
# Load the image
|
| 14 |
+
image = Image.open("/path/to/your/image.jpg")
|
| 15 |
+
|
| 16 |
+
# Preprocess the image
|
| 17 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 18 |
+
|
| 19 |
+
# Make predictions
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
logits = outputs.logits
|
| 22 |
+
|
| 23 |
+
# Model predicts one of the 1000 ImageNet classes
|
| 24 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 25 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx])
|