georgescutelnicu commited on
Commit
dff563f
·
1 Parent(s): 56c845b

Upload 17 files

Browse files
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb2_model
7
+ from timeit import default_timer as timer
8
+
9
+ # Setup class names
10
+ with open("class_names.txt", 'r') as f:
11
+ classes = [name.strip() for name in f]
12
+
13
+ # Model and transforms
14
+ model, transform = create_effnetb2_model(
15
+ num_classes=len(classes)
16
+ )
17
+
18
+ model.load_state_dict(
19
+ torch.load(
20
+ f="model_v3.pth",
21
+ map_location=torch.device("cpu")
22
+ )
23
+ )
24
+
25
+ # Predict function
26
+ def predict(img):
27
+
28
+ start_time = timer()
29
+
30
+ # Transform the target image and add a batch dimension
31
+ img = transform(img).unsqueeze(0)
32
+
33
+ model.eval()
34
+ with torch.inference_mode():
35
+
36
+ predictions = torch.softmax(model(img), dim=1)
37
+
38
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio)
39
+ pred_labels_and_probs = {classes[i]: float(predictions[0][i]) for i in range(len(classes))}
40
+
41
+ pred_time = round(timer() - start_time, 4)
42
+
43
+ return pred_labels_and_probs, pred_time
44
+
45
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
46
+
47
+ # Gradio interface
48
+ title = "Weather image recognition ⛅❄☔"
49
+ description = "Classifies the weather from an image, able to recognize 12 types of weather."
50
+
51
+ demo = gr.Interface(fn=predict,
52
+ inputs=gr.Image(type="pil"),
53
+ outputs=[gr.Label(num_top_classes=1, label="Predictions"),
54
+ gr.Number(label="Prediction time (s)")],
55
+ examples=example_list,
56
+ title=title,
57
+ description=description)
58
+
59
+
60
+ demo.launch(debug=False,
61
+ 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
examples/cloudy.jpg ADDED
examples/dew.jpg ADDED
examples/fog.jpg ADDED
examples/frost.jpg ADDED
examples/hail.jpg ADDED
examples/lightning.jpg ADDED
examples/rain.jpg ADDED
examples/rainbow.jpeg ADDED
examples/shine.jpg ADDED
examples/snow.jpg ADDED
examples/sunrise.jpg ADDED
examples/tornado.jpg ADDED
model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes: int):
7
+
8
+ """Creates an EfficientNetB2 model."""
9
+
10
+ # Create model and transforms
11
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
12
+ transforms = weights.transforms()
13
+ model = torchvision.models.efficientnet_b2(weights=weights)
14
+
15
+ # Freeze layers
16
+ for param in model.parameters():
17
+ param.requires_grad = False
18
+
19
+ # Change classifier
20
+ model.classifier = nn.Sequential(
21
+ nn.Dropout(p=0.3, inplace=True),
22
+ nn.Linear(in_features=1408, out_features=num_classes)
23
+ )
24
+
25
+ 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
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.1.4