ysr commited on
Commit
e6ec6e1
·
1 Parent(s): f745c57
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +28 -0
  3. emoticlassifier-64acc-1_250loss.pth +3 -0
  4. model.py +72 -0
  5. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from model import EmotiClassifier
4
+
5
+ predictor = EmotiClassifier()
6
+
7
+ labels = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']
8
+
9
+ predictor.load_state_dict(torch.load('emoticlassifier-64acc-1_250loss.pth'))
10
+
11
+ def classify(image):
12
+
13
+ torch_image = torch.Tensor(image)
14
+ torch_image = torch_image.view(1, 1, torch_image.shape[0], torch_image.shape[1])
15
+
16
+
17
+ pred = predictor(torch_image)
18
+
19
+ label = torch.argmax(pred)
20
+
21
+ pred_class = label.item()
22
+
23
+ return labels[pred_class]
24
+
25
+ webcam = gr.Image(source='webcam', streaming=True, shape=(48, 48), image_mode='L')
26
+
27
+ interface = gr.Interface(fn=classify, inputs=webcam, outputs='text', live=True)
28
+ interface.launch();
emoticlassifier-64acc-1_250loss.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4074b9fff06fd41c2bfd66a7db94fd9e88238b4e1c8b26b92f56069b87f5fa1f
3
+ size 1743353
model.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class EmotiClassifier(nn.Module):
5
+ def __init__(self):
6
+ super().__init__()
7
+
8
+
9
+ self.l1 = nn.Sequential(
10
+
11
+ nn.Conv2d(1, 32, 3),
12
+ nn.ReLU(),
13
+ nn.BatchNorm2d(32),
14
+ nn.MaxPool2d(2),
15
+ nn.Dropout(0.2),
16
+
17
+ nn.Conv2d(32,64, 3),
18
+ nn.ReLU(),
19
+ nn.BatchNorm2d(64),
20
+ nn.MaxPool2d(2),
21
+ nn.Dropout(0.2),
22
+
23
+
24
+
25
+ nn.Conv2d(64,128, 3),
26
+ nn.ReLU(),
27
+ nn.BatchNorm2d(128),
28
+ nn.MaxPool2d(2),
29
+ nn.Dropout(0.2),
30
+
31
+ nn.Conv2d(128,256, 3),
32
+ nn.ReLU(),
33
+ nn.BatchNorm2d(256),
34
+ nn.MaxPool2d(2),
35
+ nn.Dropout(0.2),
36
+ )
37
+
38
+
39
+ self.fc = nn.Sequential(
40
+ nn.Linear(256, 128),
41
+ nn.ReLU(),
42
+ nn.Linear(128, 64),
43
+ nn.ReLU(),
44
+ nn.Linear(64, 32),
45
+ nn.ReLU(),
46
+ nn.Linear(32, 7),
47
+ )
48
+
49
+
50
+ self.loss = nn.CrossEntropyLoss();
51
+
52
+
53
+
54
+ def forward(self, x):
55
+ out = self.l1(x);
56
+ out = out.view(-1, 256);
57
+ out = self.fc(out);
58
+
59
+ return out
60
+
61
+ def predict(self, x):
62
+ self.eval();
63
+ with torch.no_grad():
64
+ out = self.forward(x);
65
+
66
+ return out;
67
+
68
+
69
+
70
+
71
+
72
+
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ torch