Bazyl
commited on
Commit
·
7dfb637
1
Parent(s):
6149674
first commit
Browse files- app.py +40 -0
- examples/00009.png +0 -0
- examples/00027.png +0 -0
- examples/00052.png +0 -0
- examples/00072.png +0 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from timeit import default_timer as timer
|
| 7 |
+
from typing import Tuple, Dict
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 11 |
+
start_time = timer()
|
| 12 |
+
processor = ViTImageProcessor.from_pretrained('bazyl/gtsrb-model')
|
| 13 |
+
model = ViTForImageClassification.from_pretrained('bazyl/gtsrb-model')
|
| 14 |
+
inputs = processor(images=img, return_tensors="pt")
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
logits = outputs.logits
|
| 17 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 18 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx])
|
| 19 |
+
|
| 20 |
+
title = "GTSRB - German Traffic Sign Recognition by Bazyl Horsey"
|
| 21 |
+
description = "CNN created for the GTSRB Dataset, achieved 99.93% test accuracy"
|
| 22 |
+
|
| 23 |
+
# Create examples list from "examples/" directory
|
| 24 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Label(num_top_classes=5, label="Predictions"),
|
| 32 |
+
gr.Number(label="Prediction time (s)"),
|
| 33 |
+
],
|
| 34 |
+
examples=example_list,
|
| 35 |
+
title=title,
|
| 36 |
+
description=description,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Launch the app!
|
| 40 |
+
demo.launch()
|
examples/00009.png
ADDED
|
examples/00027.png
ADDED
|
examples/00052.png
ADDED
|
examples/00072.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.28.3
|
| 2 |
+
Pillow==9.5.0
|
| 3 |
+
Requests==2.30.0
|
| 4 |
+
transformers==4.28.1
|