Spaces:
Sleeping
Sleeping
samuel2424
commited on
Commit
·
9342e63
1
Parent(s):
17e4bee
Adding app file
Browse files- app.py +61 -3
- class_names.txt +12 -0
- model.py +24 -0
- model_v3.pth +3 -0
- model_v4.pth +3 -0
- requirements.txt +4 -0
app.py
CHANGED
@@ -5,7 +5,65 @@ Created on Sun Dec 10 21:09:55 2023
|
|
5 |
|
6 |
@author: samuel
|
7 |
"""
|
8 |
-
import
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
@author: samuel
|
7 |
"""
|
8 |
+
import gradio as gr
|
9 |
+
import os
|
10 |
+
import torch
|
11 |
|
12 |
+
from model import create_effnetb2_model
|
13 |
+
from timeit import default_timer as timer
|
14 |
+
|
15 |
+
# Setup class names
|
16 |
+
with open("class_names.txt", 'r') as f:
|
17 |
+
classes = [name.strip() for name in f]
|
18 |
+
|
19 |
+
# Model and transforms
|
20 |
+
model, transform = create_effnetb2_model(
|
21 |
+
num_classes=len(classes)
|
22 |
+
)
|
23 |
+
|
24 |
+
model.load_state_dict(
|
25 |
+
torch.load(
|
26 |
+
f="model_v3.pth",
|
27 |
+
map_location=torch.device("cpu")
|
28 |
+
)
|
29 |
+
)
|
30 |
+
|
31 |
+
# Predict function
|
32 |
+
def predict(img):
|
33 |
+
|
34 |
+
start_time = timer()
|
35 |
+
|
36 |
+
# Transform the target image and add a batch dimension
|
37 |
+
img = transform(img).unsqueeze(0)
|
38 |
+
|
39 |
+
model.eval()
|
40 |
+
with torch.inference_mode():
|
41 |
+
|
42 |
+
predictions = torch.softmax(model(img), dim=1)
|
43 |
+
|
44 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio)
|
45 |
+
pred_labels_and_probs = {classes[i]: float(predictions[0][i]) for i in range(len(classes))}
|
46 |
+
|
47 |
+
pred_time = round(timer() - start_time, 4)
|
48 |
+
|
49 |
+
return pred_labels_and_probs, pred_time
|
50 |
+
|
51 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
52 |
+
|
53 |
+
# Gradio interface
|
54 |
+
title = "Weather image classification ⛅❄☔"
|
55 |
+
description = "Classifies the weather from an image, able to recognize 12 types of weather."
|
56 |
+
article = "See the code on [GitHub](https://github.com/georgescutelnicu/Weather-Image-Classification)."
|
57 |
+
|
58 |
+
demo = gr.Interface(fn=predict,
|
59 |
+
inputs=gr.Image(type="pil"),
|
60 |
+
outputs=[gr.Label(num_top_classes=1, label="Predictions"),
|
61 |
+
gr.Number(label="Prediction time (s)")],
|
62 |
+
examples=example_list,
|
63 |
+
title=title,
|
64 |
+
description=description,
|
65 |
+
article=article)
|
66 |
+
|
67 |
+
|
68 |
+
demo.launch(debug=False,
|
69 |
+
share=False)
|
class_names.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
cloudy
|
2 |
+
dew
|
3 |
+
fogsmog
|
4 |
+
frost
|
5 |
+
hail
|
6 |
+
lightning
|
7 |
+
rain
|
8 |
+
rainbow
|
9 |
+
shine
|
10 |
+
snow
|
11 |
+
sunrise
|
12 |
+
tornado
|
model.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torch import nn
|
4 |
+
|
5 |
+
def create_effnetb2_model(num_classes: int):
|
6 |
+
|
7 |
+
"""Creates an EfficientNetB2 model."""
|
8 |
+
|
9 |
+
# Create model and transforms
|
10 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
11 |
+
transforms = weights.transforms()
|
12 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
13 |
+
|
14 |
+
# Freeze layers
|
15 |
+
for param in model.parameters():
|
16 |
+
param.requires_grad = False
|
17 |
+
|
18 |
+
# Change classifier
|
19 |
+
model.classifier = nn.Sequential(
|
20 |
+
nn.Dropout(p=0.3, inplace=True),
|
21 |
+
nn.Linear(in_features=1408, out_features=num_classes)
|
22 |
+
)
|
23 |
+
|
24 |
+
return model, transforms
|
model_v3.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9dc5444d4c7cb46fc682a72c83f5cc3316240497e2b5e19f86186c0c866d6791
|
3 |
+
size 31323721
|
model_v4.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:78b4a47818ed9df88b2099e837789fbac7c1c94026a7cd43eab1f7812cf57859
|
3 |
+
size 31323721
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|
4 |
+
httpx==0.24.1
|