Spaces:
Running
Running
FiratIsmailoglu
commited on
Upload 3 files
Browse files- app.py +54 -0
- model.pth +3 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torchvision
|
4 |
+
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
+
from torchvision import transforms
|
7 |
+
|
8 |
+
agirliklar=torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
9 |
+
eff_don=agirliklar.transforms()
|
10 |
+
|
11 |
+
model=torchvision.models.efficientnet_b2(weights=agirliklar)
|
12 |
+
model.classifier=nn.Sequential(nn.Linear(1408,100),nn.ReLU(),nn.Linear(100,5))
|
13 |
+
model.load_state_dict(torch.load('model.pth'))
|
14 |
+
|
15 |
+
class_names=['beş', 'bir', 'dört', 'iki', 'üç']
|
16 |
+
|
17 |
+
def predict(img):
|
18 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
19 |
+
"""
|
20 |
+
# Start the timer
|
21 |
+
# img=Image.open(img)
|
22 |
+
# Transform the target image and add a batch dimension
|
23 |
+
img = eff_don(img).unsqueeze(0)
|
24 |
+
|
25 |
+
# Put model into evaluation mode and turn on inference mode
|
26 |
+
model.eval()
|
27 |
+
with torch.inference_mode():
|
28 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
29 |
+
pred_probs = torch.softmax(model(img), dim=1)
|
30 |
+
|
31 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
32 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
33 |
+
|
34 |
+
|
35 |
+
# Return the prediction dictionary and prediction time
|
36 |
+
return pred_labels_and_probs
|
37 |
+
|
38 |
+
|
39 |
+
# Create title, description and article strings
|
40 |
+
title = "El işaretleri"
|
41 |
+
description = "Birden beşe kadar olan sayilarin el işaretlerini anlar"
|
42 |
+
|
43 |
+
# Create the Gradio demo
|
44 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
45 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
46 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions")], # what are the outputs?
|
47 |
+
# gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
48 |
+
# examples=example_list,
|
49 |
+
title=title,
|
50 |
+
description=description)
|
51 |
+
|
52 |
+
# Launch the demo!
|
53 |
+
demo.launch(debug=False, # print errors locally?
|
54 |
+
share=True) # generate a publically shareable URL?
|
model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0f210cb7da722afb244a7de3e2c7f65b86af5db517155b456b6e3fc0a577ea40
|
3 |
+
size 31791154
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
gradio
|
3 |
+
torchvision
|
4 |
+
pillow
|