Spaces:
Build error
Build error
Commit
Β·
1f5c0f1
1
Parent(s):
1002e61
push first instance
Browse files- app.py +56 -0
- examples/im175.png +0 -0
- examples/im867.png +0 -0
- examples/im90.png +0 -0
- model.py +36 -0
- models/efficientnet_b0.pth +3 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
from model import create_effnetb0_model
|
6 |
+
from timeit import default_timer as timer
|
7 |
+
from typing import Tuple, Dict
|
8 |
+
from torchvision import transforms
|
9 |
+
|
10 |
+
# Setup class names
|
11 |
+
class_names = ["Happy", "Sad", "Disgusted","Suprised","Fearful","Angry","Neutral"]
|
12 |
+
|
13 |
+
### 2. Model and transforms preparation ###
|
14 |
+
|
15 |
+
# Create EffNetB2 model
|
16 |
+
effnetb0, effnetb0_transforms = create_effnetb0_model(
|
17 |
+
num_classes=7, # len(class_names) would also work
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load saved weights
|
21 |
+
effnetb0.load_state_dict(
|
22 |
+
torch.load(
|
23 |
+
f="models/efficientnet_b0.pth",
|
24 |
+
map_location=torch.device("cpu"), # load to CPU
|
25 |
+
)
|
26 |
+
)
|
27 |
+
|
28 |
+
### 3. Predict function ###
|
29 |
+
|
30 |
+
# Create predict function
|
31 |
+
|
32 |
+
def predict(inp):
|
33 |
+
inp = transforms.ToTensor()(inp).unsqueeze(0)
|
34 |
+
with torch.no_grad():
|
35 |
+
prediction = torch.nn.functional.softmax(effnetb0(inp)[0], dim=0)
|
36 |
+
confidences = {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
|
37 |
+
return confidences
|
38 |
+
|
39 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
40 |
+
|
41 |
+
### 4. Gradio app ###
|
42 |
+
|
43 |
+
# Create title, description and article strings
|
44 |
+
title = "Emotion Detection App πππ°ππ€’π²π‘"
|
45 |
+
description = "An EfficientNetB0 computer vision model to classify images of emotions: Happy, Neutral, Sad, fearful, Angry, Suprised, Disgusted."
|
46 |
+
article = "Reference: [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
47 |
+
|
48 |
+
import gradio as gr
|
49 |
+
|
50 |
+
gr.Interface(fn=predict,
|
51 |
+
inputs=gr.Image(type="pil"),
|
52 |
+
outputs=gr.Label(num_top_classes=7),
|
53 |
+
examples=example_list,
|
54 |
+
title=title,
|
55 |
+
description=description,
|
56 |
+
article=article).launch()
|
examples/im175.png
ADDED
![]() |
examples/im867.png
ADDED
![]() |
examples/im90.png
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
import torchvision.transforms
|
6 |
+
|
7 |
+
def create_effnetb0_model(num_classes:int=7,
|
8 |
+
seed:int=42):
|
9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
num_classes (int, optional): number of classes in the classifier head.
|
13 |
+
Defaults to 3.
|
14 |
+
seed (int, optional): random seed value. Defaults to 42.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
model (torch.nn.Module): EffNetB0 feature extractor model.
|
18 |
+
transforms (torchvision.transforms): EffNetB0 image transforms.
|
19 |
+
"""
|
20 |
+
# Create EffNetB0 pretrained weights, transforms and model
|
21 |
+
weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
|
22 |
+
transforms = weights.transforms()
|
23 |
+
model = torchvision.models.efficientnet_b0(weights=weights)
|
24 |
+
|
25 |
+
# Freeze all layers in base model
|
26 |
+
for param in model.parameters():
|
27 |
+
param.requires_grad = False
|
28 |
+
|
29 |
+
# Change classifier head with random seed for reproducibility
|
30 |
+
torch.manual_seed(seed)
|
31 |
+
model.classifier = nn.Sequential(
|
32 |
+
nn.Dropout(p=0.3, inplace=True),
|
33 |
+
nn.Linear(in_features=1280, out_features=num_classes),
|
34 |
+
)
|
35 |
+
|
36 |
+
return model, transforms
|
models/efficientnet_b0.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1892c1219503678b193e8f0f0f8d2beff96bd995c749418ccb9a7f8e76dee102
|
3 |
+
size 16361111
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<<<<<<< HEAD
|
2 |
+
torch==1.13.0
|
3 |
+
torchvision==0.14.0
|
4 |
+
gradio==3.1.4
|
5 |
+
=======
|
6 |
+
version https://git-lfs.github.com/spec/v1
|
7 |
+
oid sha256:9ebc76c7c29ea13254e28d4372eaa9191f593f4bca0c04cbb56416b3c5dfadd7
|
8 |
+
size 48
|
9 |
+
>>>>>>> 5d0db446 (added app and model)
|