Spaces:
Sleeping
Sleeping
Pallavi Bhoj
commited on
Commit
·
a54e4a9
1
Parent(s):
c7bae90
Update exp_recognition_model.t7
Browse files
app/Hackathon_setup/exp_recognition.py
CHANGED
@@ -61,11 +61,21 @@ def get_expression(img):
|
|
61 |
##the same path as this file, we recommend to put in the same directory ##
|
62 |
##########################################################################################
|
63 |
##########################################################################################
|
64 |
-
|
|
|
|
|
65 |
face = detected_face(img)
|
66 |
if face==0:
|
67 |
face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# YOUR CODE HERE, return expression using your model
|
70 |
|
71 |
-
return
|
|
|
|
61 |
##the same path as this file, we recommend to put in the same directory ##
|
62 |
##########################################################################################
|
63 |
##########################################################################################
|
64 |
+
face_det_net = facExpRec()
|
65 |
+
model = torch.load(current_path + '/exp_recognition_net.t7', map_location=device)
|
66 |
+
face_det_net.load_state_dict(model['net_dict'])
|
67 |
face = detected_face(img)
|
68 |
if face==0:
|
69 |
face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
70 |
+
|
71 |
+
with torch.no_grad():
|
72 |
+
face = trnscm(face)
|
73 |
+
output = face_det_net(face)
|
74 |
+
_, predicted = torch.max(output, 1)
|
75 |
+
|
76 |
+
predicted_expression = classes[predicted.item()]
|
77 |
+
|
78 |
# YOUR CODE HERE, return expression using your model
|
79 |
|
80 |
+
return predicted_expression
|
81 |
+
|
app/Hackathon_setup/exp_recognition_model.py
CHANGED
@@ -15,11 +15,45 @@ classes = {0: 'ANGER', 1: 'DISGUST', 2: 'FEAR', 3: 'HAPPINESS', 4: 'NEUTRAL', 5:
|
|
15 |
# Example Network
|
16 |
class facExpRec(torch.nn.Module):
|
17 |
def __init__(self):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
#YOUR CODE HERE
|
24 |
|
25 |
# Sample Helper function
|
|
|
15 |
# Example Network
|
16 |
class facExpRec(torch.nn.Module):
|
17 |
def __init__(self):
|
18 |
+
super(facExpRec, self)._init_()
|
19 |
+
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3)
|
20 |
+
|
21 |
+
self.conv2 = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3)
|
22 |
+
|
23 |
+
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
|
24 |
+
|
25 |
+
# Define the Fully connected layers
|
26 |
+
# The output of the second convolution layer will be input to the first fully connected layer
|
27 |
+
self.fc1 = nn.Linear(128 * 10 * 10, 256)
|
28 |
+
# 256 input features, 128 output features
|
29 |
+
self.fc2 = nn.Linear(256, 128)
|
30 |
+
# 128 input features, 64 output features
|
31 |
+
self.fc3 = nn.Linear(128, 64)
|
32 |
+
# 64 input features, 7 output features for our 7 defined classes
|
33 |
+
self.fc4 = nn.Linear(64, 7)
|
34 |
+
|
35 |
+
# Max pooling
|
36 |
+
self.pool = nn.MaxPool2d(kernel_size=2) # Max pooling layer with filter size 2x2
|
37 |
+
|
38 |
+
|
39 |
+
def forward(self, x):
|
40 |
+
x = self.pool(F.relu(self.conv1(x)))
|
41 |
+
x = self.pool(F.relu(self.conv2(x)))
|
42 |
+
x = self.pool(F.relu(self.conv3(x)))
|
43 |
+
# Flatten the image
|
44 |
+
x = x.view(-1, 128 * 10 * 10) # Output shape of convolutional layer is 16*5*5
|
45 |
+
|
46 |
+
# Linear layers with RELU activation
|
47 |
+
x = F.relu(self.fc1(x))
|
48 |
+
x = F.relu(self.fc2(x))
|
49 |
+
x = F.relu(self.fc3(x))
|
50 |
+
x = self.fc4(x)
|
51 |
+
x = F.log_softmax(x, dim=1)
|
52 |
+
return x
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
# remove 'pass' once you have written your code
|
57 |
#YOUR CODE HERE
|
58 |
|
59 |
# Sample Helper function
|
app/Hackathon_setup/exp_recognition_net.t7
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c4bdcc922b6a7eb0117e3c5da04e4ea074bb87b1d59f60ff99aeb9c33380a6b7
|
3 |
+
size 13612996
|