vrvundyala commited on
Commit
1d2dca4
·
1 Parent(s): 07fd56c

face expressions

Browse files
.idea/Siamese_network_hackathon.iml CHANGED
@@ -3,7 +3,7 @@
3
  <component name="NewModuleRootManager" inherit-compiler-output="true">
4
  <exclude-output />
5
  <content url="file://$MODULE_DIR$" />
6
- <orderEntry type="inheritedJdk" />
7
  <orderEntry type="sourceFolder" forTests="false" />
8
  </component>
9
  </module>
 
3
  <component name="NewModuleRootManager" inherit-compiler-output="true">
4
  <exclude-output />
5
  <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
7
  <orderEntry type="sourceFolder" forTests="false" />
8
  </component>
9
  </module>
.idea/misc.xml CHANGED
@@ -1,5 +1,8 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
 
 
 
3
  <component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
4
  <output url="file://$PROJECT_DIR$/out" />
5
  </component>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
+ <component name="Black">
4
+ <option name="sdkName" value="Python 3.12" />
5
+ </component>
6
  <component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
7
  <output url="file://$PROJECT_DIR$/out" />
8
  </component>
app/Hackathon_setup/exp_recognition.py CHANGED
@@ -8,6 +8,12 @@ from PIL import Image
8
  import base64
9
  import io
10
  import os
 
 
 
 
 
 
11
  ## Add more imports if required
12
 
13
  #############################################################################################################################
@@ -50,6 +56,27 @@ def detected_face(image):
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):
52
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  ##########################################################################################
55
  ##Example for loading a model using weight state dictionary: ##
@@ -62,10 +89,22 @@ def get_expression(img):
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 "YET TO BE CODED"
 
 
 
 
 
 
 
8
  import base64
9
  import io
10
  import os
11
+ import torch
12
+ import torch.nn as nn
13
+ from torchvision import models
14
+ import torchvision.transforms as transforms
15
+ import torch.nn.functional as F
16
+
17
  ## Add more imports if required
18
 
19
  #############################################################################################################################
 
56
  ##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
57
  def get_expression(img):
58
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
59
+
60
+ # Recreate the same model architecture
61
+ num_classes = 7 # 👈 change this to match your training setup
62
+
63
+ model = models.resnet18(weights=None) # no pretrained weights now
64
+ model.fc = nn.Linear(model.fc.in_features, num_classes)
65
+
66
+ model = model.to(device)
67
+
68
+ # Create the optimizer (same as training)
69
+ optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
70
+
71
+ # Load the checkpoint
72
+ checkpoint = torch.load('resnet_expression_recognition.pth', map_location=device)
73
+
74
+ # Restore weights and optimizer
75
+ model.load_state_dict(checkpoint['model_state_dict'])
76
+ optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
77
+
78
+ # Put the model in evaluation mode
79
+ model.eval()
80
 
81
  ##########################################################################################
82
  ##Example for loading a model using weight state dictionary: ##
 
89
  ##########################################################################################
90
  ##########################################################################################
91
 
92
+ transform = transforms.Compose([
93
+ transforms.Grayscale(num_output_channels=1),
94
+ transforms.Resize(256),
95
+ transforms.CenterCrop(224),
96
+ transforms.ToTensor(),
97
+ transforms.Normalize(mean=[0.5], std=[0.5])
98
+ ])
99
+
100
  face = detected_face(img)
101
  if face==0:
102
  face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
 
 
103
 
104
+ face = transform(face).unsqueeze(0).to(device)
105
+ # YOUR CODE HERE, return expression using your model
106
+ with torch.no_grad():
107
+ outputs = model(face)
108
+ probs = F.softmax(outputs, dim=1)
109
+ predicted_class = probs.argmax(dim=1).item()
110
+ return predicted_class
app/Hackathon_setup/expression_model.t7 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a7893e2bcdf6825103b73328d0d17933ca8d5c7f0201b973cee63dd97ff6248
3
+ size 135777625