Commit ·
e4fc860
1
Parent(s): ea45513
face expressions
Browse files
app/Hackathon_setup/exp_recognition.py
CHANGED
|
@@ -54,49 +54,104 @@ def detected_face(image):
|
|
| 54 |
#4) Perform necessary transformations to the input(detected face using the above function), this should return the Expression in string form ex: "Anger"
|
| 55 |
#5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function
|
| 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 |
-
#
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
model.fc = nn.Sequential(
|
| 67 |
nn.Linear(model.fc.in_features, 256),
|
| 68 |
-
nn.ReLU(
|
|
|
|
| 69 |
nn.Linear(256, num_classes)
|
| 70 |
)
|
| 71 |
|
| 72 |
model = model.to(device)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
|
| 76 |
-
|
| 77 |
-
# Load the checkpoint
|
| 78 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 79 |
ckpt_path = os.path.join(BASE_DIR, "expression_model.t7")
|
| 80 |
checkpoint = torch.load(ckpt_path, map_location=device)
|
| 81 |
|
| 82 |
-
# Restore weights
|
| 83 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 84 |
-
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 85 |
-
|
| 86 |
-
# Put the model in evaluation mode
|
| 87 |
model.eval()
|
| 88 |
-
|
| 89 |
-
##########################################################################################
|
| 90 |
-
##Example for loading a model using weight state dictionary: ##
|
| 91 |
-
## face_det_net = facExpRec() #Example Network ##
|
| 92 |
-
## model = torch.load(current_path + '/exp_recognition_net.t7', map_location=device) ##
|
| 93 |
-
## face_det_net.load_state_dict(model['net_dict']) ##
|
| 94 |
-
## ##
|
| 95 |
-
##current_path + '/<network_definition>' is path of the saved model if present in ##
|
| 96 |
-
##the same path as this file, we recommend to put in the same directory ##
|
| 97 |
-
##########################################################################################
|
| 98 |
-
##########################################################################################
|
| 99 |
|
|
|
|
| 100 |
transform = transforms.Compose([
|
| 101 |
transforms.Grayscale(num_output_channels=1),
|
| 102 |
transforms.Resize(256),
|
|
@@ -106,13 +161,14 @@ def get_expression(img):
|
|
| 106 |
])
|
| 107 |
|
| 108 |
face = detected_face(img)
|
| 109 |
-
if face==0:
|
| 110 |
face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
| 111 |
|
| 112 |
face = transform(face).unsqueeze(0).to(device)
|
| 113 |
-
|
| 114 |
with torch.no_grad():
|
| 115 |
outputs = model(face)
|
| 116 |
probs = F.softmax(outputs, dim=1)
|
| 117 |
predicted_class = probs.argmax(dim=1).item()
|
|
|
|
| 118 |
return predicted_class
|
|
|
|
| 54 |
#4) Perform necessary transformations to the input(detected face using the above function), this should return the Expression in string form ex: "Anger"
|
| 55 |
#5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function
|
| 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)
|
| 64 |
+
# model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
| 65 |
+
# # no pretrained weights now
|
| 66 |
+
# model.fc = nn.Sequential(
|
| 67 |
+
# nn.Linear(model.fc.in_features, 256),
|
| 68 |
+
# nn.ReLU(inplace=True),
|
| 69 |
+
# nn.Linear(256, num_classes)
|
| 70 |
+
# )
|
| 71 |
+
#
|
| 72 |
+
# model = model.to(device)
|
| 73 |
+
#
|
| 74 |
+
# # Create the optimizer (same as training)
|
| 75 |
+
# optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
|
| 76 |
+
#
|
| 77 |
+
# # Load the checkpoint
|
| 78 |
+
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 79 |
+
# ckpt_path = os.path.join(BASE_DIR, "expression_model.t7")
|
| 80 |
+
# checkpoint = torch.load(ckpt_path, map_location=device)
|
| 81 |
+
#
|
| 82 |
+
# # Restore weights and optimizer
|
| 83 |
+
# model.load_state_dict(checkpoint['model_state_dict'])
|
| 84 |
+
# optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 85 |
+
#
|
| 86 |
+
# # Put the model in evaluation mode
|
| 87 |
+
# model.eval()
|
| 88 |
+
#
|
| 89 |
+
# ##########################################################################################
|
| 90 |
+
# ##Example for loading a model using weight state dictionary: ##
|
| 91 |
+
# ## face_det_net = facExpRec() #Example Network ##
|
| 92 |
+
# ## model = torch.load(current_path + '/exp_recognition_net.t7', map_location=device) ##
|
| 93 |
+
# ## face_det_net.load_state_dict(model['net_dict']) ##
|
| 94 |
+
# ## ##
|
| 95 |
+
# ##current_path + '/<network_definition>' is path of the saved model if present in ##
|
| 96 |
+
# ##the same path as this file, we recommend to put in the same directory ##
|
| 97 |
+
# ##########################################################################################
|
| 98 |
+
# ##########################################################################################
|
| 99 |
+
#
|
| 100 |
+
# transform = transforms.Compose([
|
| 101 |
+
# transforms.Grayscale(num_output_channels=1),
|
| 102 |
+
# transforms.Resize(256),
|
| 103 |
+
# transforms.CenterCrop(224),
|
| 104 |
+
# transforms.ToTensor(),
|
| 105 |
+
# transforms.Normalize(mean=[0.5], std=[0.5])
|
| 106 |
+
# ])
|
| 107 |
+
#
|
| 108 |
+
# face = detected_face(img)
|
| 109 |
+
# if face==0:
|
| 110 |
+
# face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
| 111 |
+
#
|
| 112 |
+
# face = transform(face).unsqueeze(0).to(device)
|
| 113 |
+
# # YOUR CODE HERE, return expression using your model
|
| 114 |
+
# with torch.no_grad():
|
| 115 |
+
# outputs = model(face)
|
| 116 |
+
# probs = F.softmax(outputs, dim=1)
|
| 117 |
+
# predicted_class = probs.argmax(dim=1).item()
|
| 118 |
+
# return predicted_class
|
| 119 |
+
|
| 120 |
+
|
| 121 |
def get_expression(img):
|
| 122 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 123 |
|
| 124 |
+
num_classes = 7 # update as per your dataset
|
| 125 |
+
|
| 126 |
+
# Recreate exact same architecture as training
|
| 127 |
+
model = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
|
| 128 |
|
| 129 |
+
# Convert first conv layer to accept 1 channel (grayscale)
|
| 130 |
+
pretrained_conv = model.conv1.weight
|
| 131 |
model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
| 132 |
+
with torch.no_grad():
|
| 133 |
+
model.conv1.weight = nn.Parameter(pretrained_conv.mean(dim=1, keepdim=True))
|
| 134 |
+
|
| 135 |
+
# Fully connected head (same as training)
|
| 136 |
model.fc = nn.Sequential(
|
| 137 |
nn.Linear(model.fc.in_features, 256),
|
| 138 |
+
nn.ReLU(),
|
| 139 |
+
nn.Dropout(0.5),
|
| 140 |
nn.Linear(256, num_classes)
|
| 141 |
)
|
| 142 |
|
| 143 |
model = model.to(device)
|
| 144 |
|
| 145 |
+
# Load checkpoint
|
|
|
|
|
|
|
|
|
|
| 146 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 147 |
ckpt_path = os.path.join(BASE_DIR, "expression_model.t7")
|
| 148 |
checkpoint = torch.load(ckpt_path, map_location=device)
|
| 149 |
|
| 150 |
+
# Restore weights (no need for optimizer if inference-only)
|
| 151 |
model.load_state_dict(checkpoint['model_state_dict'])
|
|
|
|
|
|
|
|
|
|
| 152 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Preprocessing pipeline
|
| 155 |
transform = transforms.Compose([
|
| 156 |
transforms.Grayscale(num_output_channels=1),
|
| 157 |
transforms.Resize(256),
|
|
|
|
| 161 |
])
|
| 162 |
|
| 163 |
face = detected_face(img)
|
| 164 |
+
if face == 0:
|
| 165 |
face = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
|
| 166 |
|
| 167 |
face = transform(face).unsqueeze(0).to(device)
|
| 168 |
+
|
| 169 |
with torch.no_grad():
|
| 170 |
outputs = model(face)
|
| 171 |
probs = F.softmax(outputs, dim=1)
|
| 172 |
predicted_class = probs.argmax(dim=1).item()
|
| 173 |
+
|
| 174 |
return predicted_class
|