Sushrut98 commited on
Commit
35c6f04
·
verified ·
1 Parent(s): 5ab7df3

Upload 18 files

Browse files
TinyCNN.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ebe4d82d0b37c4f3c04f924f426505d96ff0880693d6eefe29268a2504479590
3
+ size 40675386
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+ # Setup class names
10
+ with open("class_names.txt", "r") as f: # reading them in from class_names.txt
11
+ class_names = [food_name.strip() for food_name in f.readlines()]
12
+
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create model
16
+ TinyCNN_model = TinyCNN(input_shape=3, # number of color channels (3 for RGB)
17
+ hidden_units=64,
18
+ output_shape=len(class_names))
19
+
20
+ loss_fn = nn.CrossEntropyLoss() # measure how wrong our model is
21
+ optimizer = torch.optim.Adam(params = TinyCNN_model.parameters() ,lr=0.001)
22
+
23
+ transform = transforms.Compose([
24
+ transforms.Resize((128, 128)),
25
+ transforms.Grayscale(num_output_channels=3)
26
+ ])
27
+
28
+ # Load saved weights
29
+ TinyCNN_model.load_state_dict(
30
+ torch.load(
31
+ f="TinyCNN.pth",
32
+ map_location=torch.device("cpu"), # load to CPU
33
+ )
34
+ )
35
+
36
+ ### 3. Predict function ###
37
+
38
+ # Create predict function
39
+ def predict(img) -> Tuple[Dict, float]:
40
+ """Transforms and performs a prediction on img and returns prediction and time taken.
41
+ """
42
+ # Start the timer
43
+ start_time = timer()
44
+
45
+ # Transform the target image and add a batch dimension
46
+ img = transform(img).unsqueeze(0)
47
+
48
+ # Put model into evaluation mode and turn on inference mode
49
+ TinyCNN_model.eval()
50
+ with torch.inference_mode():
51
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
52
+ pred_probs = torch.softmax(TinyCNN_model(img), dim=1)
53
+
54
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
55
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
56
+
57
+ # Calculate the prediction time
58
+ pred_time = round(timer() - start_time, 5)
59
+ emoji_list = [["emojis/" + example] for example in os.listdir("emojis")]
60
+
61
+ emoji_1 = torch.argmax(pred_probs)
62
+ emoji = class_names[emoji_1]
63
+ if emoji == 'angry':
64
+ a = example_list[0]
65
+ a = a[0]
66
+ return pred_labels_and_probs,a
67
+ elif emoji == 'disgust':
68
+ a = example_list[1]
69
+ a = a[0]
70
+ return pred_labels_and_probs,a
71
+ elif emoji == 'fear':
72
+ a = example_list[2]
73
+ a = a[0]
74
+ return pred_labels_and_probs,a
75
+ elif emoji == 'happy':
76
+ a = example_list[3]
77
+ a = a[0]
78
+ return pred_labels_and_probs,a
79
+ elif emoji == 'neutral':
80
+ a = example_list[4]
81
+ a = a[0]
82
+ return pred_labels_and_probs,a
83
+ elif emoji == 'sad':
84
+ a = example_list[5]
85
+ a = a[0]
86
+ return pred_labels_and_probs,a
87
+ elif emoji == 'surprise':
88
+ a = example_list[6]
89
+ a = a[0]
90
+ return pred_labels_and_probs,a
91
+
92
+ # Return the prediction dictionary and prediction time
93
+
94
+
95
+ ### 4. Gradio app ###
96
+
97
+ # Create title, description and article strings
98
+ title = "Expression Detection"
99
+ description = "An app to predict emotions from the list.[Angry, Disgust, Fear, Happy, Neutral, Sad, Surprise]"
100
+ article = "Created as a college project."
101
+
102
+ # Create examples list from "examples/" directory
103
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
104
+
105
+ # Create Gradio interface
106
+ demo = gr.Interface(
107
+ fn=predict,
108
+ inputs=gr.Image(type="pil"),
109
+ outputs=[
110
+ gr.Label(num_top_classes=5, label="Predictions"),
111
+ gr.Image(label="Emotion"),
112
+ ],
113
+ examples=example_list,
114
+ title=title,
115
+ description=description,
116
+ article=article,
117
+ )
118
+
119
+ # Launch the app!
120
+ demo.launch()
class_names.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ angry
2
+ disgust
3
+ fear
4
+ happy
5
+ neutral
6
+ sad
7
+ surprise
emojis/Angry.png ADDED
emojis/Disgust.png ADDED
emojis/Fear.png ADDED
emojis/Happy.png ADDED
emojis/Neutral.png ADDED
emojis/Sad.png ADDED
emojis/Surprise.png ADDED
examples/Training_1206.jpg ADDED
examples/Training_12567.jpg ADDED
examples/Training_23814.jpg ADDED
examples/Training_3908.jpg ADDED
examples/Training_659019.jpg ADDED
examples/Training_8796.jpg ADDED
examples/Training_98123.jpg ADDED
model.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+ class TinyCNN(nn.Module):
6
+
7
+
8
+ def __init__(self, input_shape: int, hidden_units: int, output_shape: int) -> None:
9
+ super().__init__()
10
+ self.conv_block_1 = nn.Sequential(
11
+ nn.Conv2d(in_channels=input_shape,
12
+ out_channels=hidden_units,
13
+ kernel_size=3,
14
+ stride=1,
15
+ padding=0),
16
+ nn.ReLU(),
17
+ nn.Conv2d(in_channels=hidden_units,
18
+ out_channels=128,
19
+ kernel_size=3,
20
+ stride=1,
21
+ padding=0),
22
+ nn.BatchNorm2d(128),
23
+ nn.ReLU(),
24
+ nn.MaxPool2d(kernel_size=2,
25
+ stride=2),
26
+ nn.Dropout(p=0.25)
27
+ )
28
+ self.conv_block_2 = nn.Sequential(
29
+ nn.Conv2d(128, 128, kernel_size=3, padding=0),
30
+ nn.ReLU(),
31
+ nn.Conv2d(128, 128, kernel_size=3, padding=0),
32
+ nn.BatchNorm2d(128),
33
+ nn.ReLU(),
34
+ nn.MaxPool2d(2),
35
+ nn.Dropout(p=0.25)
36
+ )
37
+
38
+ self.conv_block_3 = nn.Sequential(
39
+ nn.Conv2d(128, 128, kernel_size=3, padding=0),
40
+ nn.ReLU(),
41
+ nn.Conv2d(128, 512, kernel_size=3, padding=0),
42
+ nn.BatchNorm2d(512),
43
+ nn.ReLU(),
44
+ nn.MaxPool2d(2),
45
+ nn.Dropout(p=0.25)
46
+ )
47
+
48
+ self.conv_block_4 = nn.Sequential(
49
+ nn.Conv2d(512, 512, kernel_size=3, padding=0),
50
+ nn.ReLU(),
51
+ nn.Conv2d(512, 512, kernel_size=3, padding=0),
52
+ nn.BatchNorm2d(512),
53
+ nn.ReLU(),
54
+ nn.MaxPool2d(2),
55
+ nn.Dropout(p=0.25)
56
+ )
57
+
58
+ self.fc_1 = nn.Sequential(
59
+ nn.Flatten(),
60
+ nn.Linear(in_features=512*16, out_features = 512),
61
+ nn.BatchNorm1d(512),
62
+ nn.ReLU(),
63
+ nn.Dropout(p=0.25)
64
+ )
65
+
66
+ self.fc_2 = nn.Sequential(
67
+
68
+
69
+ nn.Linear(in_features=512,
70
+ out_features=256),
71
+ nn.BatchNorm1d(256),
72
+ nn.ReLU(),
73
+ nn.Dropout(p=0.25)
74
+ )
75
+
76
+ self.classifier = nn.Sequential(
77
+ nn.Linear(in_features=256,
78
+ out_features=output_shape)
79
+ )
80
+
81
+ def forward(self, x):
82
+ x = self.conv_block_1(x)
83
+ x = self.conv_block_2(x)
84
+ x = self.conv_block_3(x)
85
+ x = self.conv_block_4(x)
86
+ x = self.fc_1(x)
87
+ x = self.fc_2(x)
88
+ x = self.classifier(x)
89
+ return x