Spaces:
Build error
Build error
Commit ·
166f2ba
1
Parent(s): c8a134a
clf model
Browse files
app/Hackathon_setup/clf_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1feb38cb9d95926b2097d92e6465941233c40418d7c944ba18db5c6768576599
|
| 3 |
+
size 25512
|
app/Hackathon_setup/face_recognition.py
CHANGED
|
@@ -10,10 +10,9 @@ import io
|
|
| 10 |
import os
|
| 11 |
import joblib
|
| 12 |
import pickle
|
|
|
|
| 13 |
# Add more imports if required
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
###########################################################################################################################################
|
| 18 |
# Caution: Don't change any of the filenames, function names and definitions #
|
| 19 |
# Always use the current_path + file_name for refering any files, without it we cannot access files on the server #
|
|
@@ -44,15 +43,8 @@ def detected_face(image):
|
|
| 44 |
required_image = Image.fromarray(required_image)
|
| 45 |
return required_image
|
| 46 |
|
| 47 |
-
|
| 48 |
-
#1) Images captured from mobile is passed as parameter to the below function in the API call. It returns the similarity measure between given images.
|
| 49 |
-
#2) The image is passed to the function in base64 encoding, Code for decoding the image is provided within the function.
|
| 50 |
-
#3) Define an object to your siamese network here in the function and load the weight from the trained network, set it in evaluation mode.
|
| 51 |
-
#4) Get the features for both the faces from the network and return the similarity measure, Euclidean,cosine etc can be it. But choose the Relevant measure.
|
| 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_similarity(img1, img2):
|
| 55 |
-
device = torch.device("cuda
|
| 56 |
|
| 57 |
det_img1 = detected_face(img1)
|
| 58 |
det_img2 = detected_face(img2)
|
|
@@ -73,11 +65,18 @@ def get_similarity(img1, img2):
|
|
| 73 |
##########################################################################################
|
| 74 |
|
| 75 |
# YOUR CODE HERE, load the model
|
|
|
|
|
|
|
|
|
|
| 76 |
|
|
|
|
| 77 |
# YOUR CODE HERE, return similarity measure using your model
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
return
|
| 80 |
-
|
|
|
|
| 81 |
#1) Image captured from mobile is passed as parameter to this function in the API call, It returns the face class in the string form ex: "Person1"
|
| 82 |
#2) The image is passed to the function in base64 encoding, Code to decode the image provided within the function
|
| 83 |
#3) Define an object to your network here in the function and load the weight from the trained network, set it in evaluation mode
|
|
@@ -93,4 +92,16 @@ def get_face_class(img1):
|
|
| 93 |
##YOUR CODE HERE, return face class here
|
| 94 |
##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
|
| 95 |
##Better Hint: Siamese experiment is covered in one of the labs
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import os
|
| 11 |
import joblib
|
| 12 |
import pickle
|
| 13 |
+
from joblib import load
|
| 14 |
# Add more imports if required
|
| 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 #
|
|
|
|
| 43 |
required_image = Image.fromarray(required_image)
|
| 44 |
return required_image
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def get_similarity(img1, img2):
|
| 47 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 48 |
|
| 49 |
det_img1 = detected_face(img1)
|
| 50 |
det_img2 = detected_face(img2)
|
|
|
|
| 65 |
##########################################################################################
|
| 66 |
|
| 67 |
# YOUR CODE HERE, load the model
|
| 68 |
+
feature_net = SiameseNetwork()
|
| 69 |
+
model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##
|
| 70 |
+
feature_net.load_state_dict(model['net_dict'])
|
| 71 |
|
| 72 |
+
feature_net.eval()
|
| 73 |
# YOUR CODE HERE, return similarity measure using your model
|
| 74 |
+
output1,output2 = feature_net(face1.cuda(),face2.cuda())
|
| 75 |
+
euclidean_distance = F.pairwise_distance(output1, output2)
|
| 76 |
|
| 77 |
+
return euclidean_distance
|
| 78 |
+
|
| 79 |
+
|
| 80 |
#1) Image captured from mobile is passed as parameter to this function in the API call, It returns the face class in the string form ex: "Person1"
|
| 81 |
#2) The image is passed to the function in base64 encoding, Code to decode the image provided within the function
|
| 82 |
#3) Define an object to your network here in the function and load the weight from the trained network, set it in evaluation mode
|
|
|
|
| 92 |
##YOUR CODE HERE, return face class here
|
| 93 |
##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
|
| 94 |
##Better Hint: Siamese experiment is covered in one of the labs
|
| 95 |
+
face1 = trnscm(det_img1).unsqueeze(0)
|
| 96 |
+
|
| 97 |
+
feature_net = SiameseNetwork()
|
| 98 |
+
model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##
|
| 99 |
+
feature_net.load_state_dict(model['net_dict'])
|
| 100 |
+
feature_net.eval()
|
| 101 |
+
|
| 102 |
+
output1,output2 = feature_net(face1.cuda(),face1.cuda())
|
| 103 |
+
output1 = output1.cpu().numpy()
|
| 104 |
+
|
| 105 |
+
clf_model = load('clf_model.joblib')
|
| 106 |
+
label = clf_model.predict(output1)
|
| 107 |
+
return label
|
app/Hackathon_setup/face_recognition_model.py
CHANGED
|
@@ -67,55 +67,7 @@ class SiameseNetwork(nn.Module):
|
|
| 67 |
|
| 68 |
# YOUR CODE HERE for pytorch classifier
|
| 69 |
|
| 70 |
-
|
| 71 |
-
eye_haar = current_path + '/haarcascade_eye.xml'
|
| 72 |
-
face_haar = current_path + '/haarcascade_frontalface_default.xml'
|
| 73 |
-
face_cascade = cv2.CascadeClassifier(face_haar)
|
| 74 |
-
eye_cascade = cv2.CascadeClassifier(eye_haar)
|
| 75 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 76 |
-
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 77 |
-
face_areas=[]
|
| 78 |
-
images = []
|
| 79 |
-
required_image=0
|
| 80 |
-
for i, (x,y,w,h) in enumerate(faces):
|
| 81 |
-
face_cropped = gray[y:y+h, x:x+w]
|
| 82 |
-
face_areas.append(w*h)
|
| 83 |
-
images.append(face_cropped)
|
| 84 |
-
required_image = images[np.argmax(face_areas)]
|
| 85 |
-
required_image = Image.fromarray(required_image)
|
| 86 |
-
return required_image
|
| 87 |
-
|
| 88 |
-
def get_similarity(img1, img2):
|
| 89 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 90 |
-
|
| 91 |
-
det_img1 = detected_face(img1)
|
| 92 |
-
det_img2 = detected_face(img2)
|
| 93 |
-
if(det_img1 == 0 or det_img2 == 0):
|
| 94 |
-
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 95 |
-
det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))
|
| 96 |
-
face1 = trnscm(det_img1).unsqueeze(0)
|
| 97 |
-
face2 = trnscm(det_img2).unsqueeze(0)
|
| 98 |
-
##########################################################################################
|
| 99 |
-
##Example for loading a model using weight state dictionary: ##
|
| 100 |
-
## feature_net = light_cnn() #Example Network ##
|
| 101 |
-
## model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##
|
| 102 |
-
## feature_net.load_state_dict(model['net_dict']) ##
|
| 103 |
-
## ##
|
| 104 |
-
##current_path + '/<network_definition>' is path of the saved model if present in ##
|
| 105 |
-
##the same path as this file, we recommend to put in the same directory ##
|
| 106 |
-
##########################################################################################
|
| 107 |
-
##########################################################################################
|
| 108 |
-
|
| 109 |
-
# YOUR CODE HERE, load the model
|
| 110 |
-
feature_net = SiameseNetwork()
|
| 111 |
-
model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##
|
| 112 |
-
feature_net.load_state_dict(model['net_dict'])
|
| 113 |
-
|
| 114 |
-
# YOUR CODE HERE, return similarity measure using your model
|
| 115 |
-
output1,output2 = feature_net(face1.cuda(),face2.cuda())
|
| 116 |
-
euclidean_distance = F.pairwise_distance(output1, output2)
|
| 117 |
-
|
| 118 |
-
return euclidean_distance
|
| 119 |
|
| 120 |
|
| 121 |
# Definition of classes as dictionary
|
|
|
|
| 67 |
|
| 68 |
# YOUR CODE HERE for pytorch classifier
|
| 69 |
|
| 70 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
# Definition of classes as dictionary
|