Skip to content

Commit

Permalink
cosmetic changes about interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Jan 21, 2024
1 parent 70b4f1a commit 0d6aa10
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 120 deletions.
15 changes: 14 additions & 1 deletion deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand Down Expand Up @@ -43,7 +44,7 @@
)

# pylint: disable=too-few-public-methods
class ArcFace(FacialRecognition):
class ArcFaceClient(FacialRecognition):
"""
ArcFace model class
"""
Expand All @@ -52,6 +53,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "ArcFace"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with ArcFace model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5",
Expand Down
15 changes: 14 additions & 1 deletion deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand Down Expand Up @@ -39,7 +40,7 @@
# -------------------------------------

# pylint: disable=too-few-public-methods
class DeepId(FacialRecognition):
class DeepIdClient(FacialRecognition):
"""
DeepId model class
"""
Expand All @@ -48,6 +49,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "DeepId"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with DeepId model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5",
Expand Down
58 changes: 25 additions & 33 deletions deepface/basemodels/DlibResNet.py → deepface/basemodels/Dlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# pylint: disable=too-few-public-methods


class Dlib(FacialRecognition):
class DlibClient(FacialRecognition):
"""
Dlib model class
"""
Expand All @@ -22,13 +22,31 @@ def __init__(self):

def find_embeddings(self, img: np.ndarray) -> list:
"""
Custom find embeddings function of Dlib different than FacialRecognition's one
find embeddings with Dlib model - different than regular models
Args:
img (np.ndarray)
Retunrs:
embeddings (list)
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
return self.model.predict(img)[0].tolist()
# return self.model.predict(img)[0].tolist()

# extract_faces returns 4 dimensional images
if len(img.shape) == 4:
img = img[0]

# bgr to rgb
img = img[:, :, ::-1] # bgr to rgb

# img is in scale of [0, 1] but expected [0, 255]
if img.max() <= 1:
img = img * 255

img = img.astype(np.uint8)

img_representation = self.model.model.compute_face_descriptor(img)
img_representation = np.array(img_representation)
img_representation = np.expand_dims(img_representation, axis=0)
return img_representation[0].tolist()


class DlibResNet:
Expand Down Expand Up @@ -69,38 +87,12 @@ def __init__(self):

# ---------------------

model = dlib.face_recognition_model_v1(weight_file)
self.__model = model
self.model = dlib.face_recognition_model_v1(weight_file)

# ---------------------

# return None # classes must return None

def predict(self, img_aligned: np.ndarray) -> np.ndarray:

# functions.detectFace returns 4 dimensional images
if len(img_aligned.shape) == 4:
img_aligned = img_aligned[0]

# functions.detectFace returns bgr images
img_aligned = img_aligned[:, :, ::-1] # bgr to rgb

# deepface.detectFace returns an array in scale of [0, 1]
# but dlib expects in scale of [0, 255]
if img_aligned.max() <= 1:
img_aligned = img_aligned * 255

img_aligned = img_aligned.astype(np.uint8)

model = self.__model

img_representation = model.compute_face_descriptor(img_aligned)

img_representation = np.array(img_representation)
img_representation = np.expand_dims(img_representation, axis=0)

return img_representation


class DlibMetaData:
def __init__(self):
Expand Down
29 changes: 27 additions & 2 deletions deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand Down Expand Up @@ -43,7 +44,7 @@
# --------------------------------

# pylint: disable=too-few-public-methods
class FaceNet128d(FacialRecognition):
class FaceNet128dClient(FacialRecognition):
"""
FaceNet-128d model class
"""
Expand All @@ -52,8 +53,20 @@ def __init__(self):
self.model = load_facenet128d_model()
self.model_name = "FaceNet-128d"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with FaceNet-128d model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()

class FaceNet512d(FacialRecognition):

class FaceNet512dClient(FacialRecognition):
"""
FaceNet-1512d model class
"""
Expand All @@ -62,6 +75,18 @@ def __init__(self):
self.model = load_facenet512d_model()
self.model_name = "FaceNet-512d"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with FaceNet-512d model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def scaling(x, scale):
return x * scale
Expand Down
15 changes: 14 additions & 1 deletion deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import zipfile
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand Down Expand Up @@ -36,7 +37,7 @@

# -------------------------------------
# pylint: disable=line-too-long, too-few-public-methods
class DeepFace(FacialRecognition):
class DeepFaceClient(FacialRecognition):
"""
Fb's DeepFace model class
"""
Expand All @@ -45,6 +46,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "DeepFace"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with OpenFace model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def load_model(
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",
Expand Down
15 changes: 14 additions & 1 deletion deepface/basemodels/OpenFace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import gdown
import tensorflow as tf
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand All @@ -26,7 +27,7 @@
# ---------------------------------------

# pylint: disable=too-few-public-methods
class OpenFace(FacialRecognition):
class OpenFaceClient(FacialRecognition):
"""
OpenFace model class
"""
Expand All @@ -35,6 +36,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "OpenFace"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with OpenFace model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/openface_weights.h5",
Expand Down
30 changes: 13 additions & 17 deletions deepface/basemodels/SFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# pylint: disable=line-too-long, too-few-public-methods


class SFace(FacialRecognition):
class SFaceClient(FacialRecognition):
"""
SFace model class
"""
Expand All @@ -25,13 +25,20 @@ def __init__(self):

def find_embeddings(self, img: np.ndarray) -> list:
"""
Custom find embeddings function of SFace different than FacialRecognition's one
find embeddings with SFace model - different than regular models
Args:
img (np.ndarray)
Retunrs:
embeddings (list)
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
return self.model.predict(img)[0].tolist()
# return self.model.predict(img)[0].tolist()

# revert the image to original format and preprocess using the model
input_blob = (img[0] * 255).astype(np.uint8)

embeddings = self.model.model.feature(input_blob)

return embeddings[0].tolist()


def load_model(
Expand Down Expand Up @@ -74,17 +81,6 @@ def __init__(self, model_path):

self.layers = [_Layer()]

def predict(self, image: np.ndarray) -> np.ndarray:
# Preprocess
input_blob = (image[0] * 255).astype(
np.uint8
) # revert the image to original format and preprocess using the model

# Forward
embeddings = self.model.feature(input_blob)

return embeddings


class _Layer:
input_shape = (None, 112, 112, 3)
Expand Down
15 changes: 14 additions & 1 deletion deepface/basemodels/VGGFace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition
Expand Down Expand Up @@ -37,7 +38,7 @@
# ---------------------------------------

# pylint: disable=too-few-public-methods
class VggFace(FacialRecognition):
class VggFaceClient(FacialRecognition):
"""
VGG-Face model class
"""
Expand All @@ -46,6 +47,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "VGG-Face"

def find_embeddings(self, img: np.ndarray) -> list:
"""
find embeddings with VGG-Face model
Args:
img (np.ndarray): pre-loaded image in BGR
Returns
embeddings (list): multi-dimensional vector
"""
# model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist()
return self.model(img, training=False).numpy()[0].tolist()


def base_model() -> Sequential:
"""
Expand Down
Loading

0 comments on commit 0d6aa10

Please sign in to comment.