suvradip2000 commited on
Commit
b5e5bf0
·
1 Parent(s): 6b761e5
app/Hackathon_setup/exp_recognition.py CHANGED
@@ -8,11 +8,8 @@ from PIL import Image
8
  import base64
9
  import io
10
  import os
11
- import torchvision.models as models
12
  ## Add more imports if required
13
 
14
- classes = {0: 'ANGER', 1: 'DISGUST', 2: 'FEAR', 3: 'HAPPINESS', 4: 'NEUTRAL', 5: 'SADNESS', 6: 'SURPRISE'}
15
-
16
  #############################################################################################################################
17
  # Caution: Don't change any of the filenames, function names and definitions #
18
  # Always use the current_path + file_name for refering any files, without it we cannot access files on the server #
@@ -51,40 +48,40 @@ def detected_face(image):
51
  #4) Perform necessary transformations to the input(detected face using the above function), this should return the Expression in string form ex: "Anger"
52
  #5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function
53
  ##Caution: Don't change the definition or function name; for loading the model use the current_path for path example is given in comments to the function
54
- def get_expression(img):
55
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
56
-
 
 
 
57
  ##########################################################################################
58
  ##Example for loading a model using weight state dictionary: ##
59
- ## face_det_net = facExpRec() #Example Network ##
60
- ## model = torch.load(current_path + '/exp_recognition_net.t7', map_location=device) ##
61
- ## face_det_net.load_state_dict(model['net_dict']) ##
 
 
62
  ## ##
63
  ##current_path + '/<network_definition>' is path of the saved model if present in ##
64
  ##the same path as this file, we recommend to put in the same directory ##
65
  ##########################################################################################
66
  ##########################################################################################
67
- model_path = current_path + '/expression_model.t7'
68
- loaded_model = models.resnet18(pretrained=False)
69
- num_ftrs = loaded_model.fc.in_features
70
- loaded_model.fc = nn.Linear(num_ftrs, 7)
71
-
72
- loaded_model.load_state_dict(torch.load(model_path, map_location='cpu')['net_dict'])
73
- loaded_model.eval()
74
-
75
  face = detected_face(img)
76
  if face==0:
77
- face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
 
 
78
 
79
- face1 = trnscm(face).unsqueeze(0)
80
- # YOUR CODE HERE, return expression using your model
81
-
82
  with torch.no_grad():
83
- output = loaded_model(face1)
84
- output = output.cpu()
85
- _, predicted = torch.max(output, 1)
86
-
87
-
88
- predicted_label = classes[predicted.item()]
 
 
89
 
90
- return predicted_label
 
8
  import base64
9
  import io
10
  import os
 
11
  ## Add more imports if required
12
 
 
 
13
  #############################################################################################################################
14
  # Caution: Don't change any of the filenames, function names and definitions #
15
  # Always use the current_path + file_name for refering any files, without it we cannot access files on the server #
 
48
  #4) Perform necessary transformations to the input(detected face using the above function), this should return the Expression in string form ex: "Anger"
49
  #5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function
50
  ##Caution: Don't change the definition or function name; for loading the model use the current_path for path example is given in comments to the function
51
+ def get_expression(img_str):
52
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
53
+ imgdata = base64.b64decode(img_str)
54
+ img = Image.open(io.BytesIO(imgdata))
55
+ img = np.array(img.getdata()).reshape(img.size[1], img.size[0], 3).astype(np.uint8)
56
+
57
  ##########################################################################################
58
  ##Example for loading a model using weight state dictionary: ##
59
+ face_det_net = Model().to(device) #Example Network ##
60
+ model = torch.load(current_path + '/expression_model.t7', map_location=device) ##
61
+ face_det_net.load_state_dict(model['net_dict'])
62
+ #face_det_net.to(device)
63
+ #face_det_net.eval() ##
64
  ## ##
65
  ##current_path + '/<network_definition>' is path of the saved model if present in ##
66
  ##the same path as this file, we recommend to put in the same directory ##
67
  ##########################################################################################
68
  ##########################################################################################
69
+
 
 
 
 
 
 
 
70
  face = detected_face(img)
71
  if face==0:
72
+ return "No Face Found"
73
+ #Apply transformation to the image to convert to a tensor that can be passed to your model
74
+ trnscm = transforms.Compose([rgb2gray(face), transforms.Resize((100,100)), transforms.ToTensor()])
75
 
76
+ #Call the forward function of your model to get the prediction on the transformed image
 
 
77
  with torch.no_grad():
78
+ features = face_det_net.forward(trnscm)
79
+ #Take the argmax to get the index of the predicted class
80
+ predicted_class = np.argmax(features)
81
+
82
+ #classes = ['Anger', 'person2', 'person3', 'person4', 'person5', 'person6', 'person7']
83
+ #From the class index predicted, return the corresponding expression string
84
+ string = classes[predicted_class]
85
+ # YOUR CODE HERE, return expression using your model
86
 
87
+ return string
app/Hackathon_setup/exp_recognition_model.py CHANGED
@@ -2,6 +2,7 @@ import torch
2
  import torchvision
3
  import torch.nn as nn
4
  from torchvision import transforms
 
5
  ## Add more imports if required
6
 
7
  ####################################################################################################################
@@ -13,18 +14,50 @@ from torchvision import transforms
13
  classes = {0: 'ANGER', 1: 'DISGUST', 2: 'FEAR', 3: 'HAPPINESS', 4: 'NEUTRAL', 5: 'SADNESS', 6: 'SURPRISE'}
14
 
15
  # Example Network
16
- class facExpRec(torch.nn.Module):
17
  def __init__(self):
18
- pass # remove 'pass' once you have written your code
19
- #YOUR CODE HERE
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def forward(self, x):
22
- pass # remove 'pass' once you have written your code
23
- #YOUR CODE HERE
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Sample Helper function
26
- # def rgb2gray(image):
27
- # return image.convert('L')
28
 
29
  # Sample Transformation function
30
  #YOUR CODE HERE for changing the Transformation values.
 
2
  import torchvision
3
  import torch.nn as nn
4
  from torchvision import transforms
5
+ import torch.nn.functional as F
6
  ## Add more imports if required
7
 
8
  ####################################################################################################################
 
14
  classes = {0: 'ANGER', 1: 'DISGUST', 2: 'FEAR', 3: 'HAPPINESS', 4: 'NEUTRAL', 5: 'SADNESS', 6: 'SURPRISE'}
15
 
16
  # Example Network
17
+ class Model(nn.Module):
18
  def __init__(self):
19
+ super(Model, self).__init__()
 
20
 
21
+
22
+ self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3)
23
+
24
+
25
+ self.conv2 = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3)
26
+
27
+ self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
28
+
29
+ # Define the Fully connected layers
30
+ # The output of the second convolution layer will be input to the first fully connected layer
31
+ self.fc1 = nn.Linear(128*10*10, 256)
32
+ # 256 input features, 128 output features
33
+ self.fc2 = nn.Linear(256, 128)
34
+ # 128 input features, 64 output features
35
+ self.fc3 = nn.Linear(128, 64)
36
+ # 64 input features, 7 output features for our 7 defined classes
37
+ self.fc4 = nn.Linear(64, 7)
38
+
39
+ # Max pooling
40
+ self.pool = nn.MaxPool2d(kernel_size=2) # Max pooling layer with filter size 2x2
41
+
42
  def forward(self, x):
43
+
44
+ x = self.pool(F.relu(self.conv1(x)))
45
+ x = self.pool(F.relu(self.conv2(x)))
46
+ x = self.pool(F.relu(self.conv3(x)))
47
+ # Flatten the image
48
+ x = x.view(-1, 128*10*10) # Output shape of convolutional layer is 16*5*5
49
+
50
+ # Linear layers with RELU activation
51
+ x = F.relu(self.fc1(x))
52
+ x = F.relu(self.fc2(x))
53
+ x = F.relu(self.fc3(x))
54
+ x = self.fc4(x)
55
+ x = F.log_softmax(x, dim=1)
56
+ return x
57
 
58
  # Sample Helper function
59
+ def rgb2gray(image):
60
+ return image.convert('L')
61
 
62
  # Sample Transformation function
63
  #YOUR CODE HERE for changing the Transformation values.
app/Hackathon_setup/expression_model.t7 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:a06b848d5b3aec70945a05fcebd7a113ece8615efe43b681f56d2b81125b777b
3
- size 94410006
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f1fb4914225fd633bc6391ee3808bce815165c9fe8822e8ffe5f5e172adfb942
3
+ size 13612942