Skip to content

Commit

Permalink
Merge pull request #962 from serengil/feat-task-2101-interfaces
Browse files Browse the repository at this point in the history
cosmetic changes about interfaces
  • Loading branch information
serengil authored Jan 21, 2024
2 parents 70b4f1a + 359162a commit bc30c90
Show file tree
Hide file tree
Showing 29 changed files with 508 additions and 257 deletions.
16 changes: 15 additions & 1 deletion deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List
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 +45,7 @@
)

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

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
16 changes: 15 additions & 1 deletion deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List
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 +41,7 @@
# -------------------------------------

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

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
61 changes: 27 additions & 34 deletions deepface/basemodels/DlibResNet.py → deepface/basemodels/Dlib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
import os
import bz2
import gdown
Expand All @@ -11,7 +12,7 @@
# pylint: disable=too-few-public-methods


class Dlib(FacialRecognition):
class DlibClient(FacialRecognition):
"""
Dlib model class
"""
Expand All @@ -20,15 +21,33 @@ def __init__(self):
self.model = DlibResNet()
self.model_name = "Dlib"

def find_embeddings(self, img: np.ndarray) -> list:
def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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 +88,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
30 changes: 28 additions & 2 deletions deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List
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 +45,7 @@
# --------------------------------

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

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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 +76,18 @@ def __init__(self):
self.model = load_facenet512d_model()
self.model_name = "FaceNet-512d"

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
16 changes: 15 additions & 1 deletion deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List
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 +38,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 +47,18 @@ def __init__(self):
self.model = load_model()
self.model_name = "DeepFace"

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
16 changes: 15 additions & 1 deletion deepface/basemodels/OpenFace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List
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 +28,7 @@
# ---------------------------------------

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

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
34 changes: 15 additions & 19 deletions deepface/basemodels/SFace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Any
from typing import Any, List

import numpy as np
import cv2 as cv
Expand All @@ -14,7 +14,7 @@
# pylint: disable=line-too-long, too-few-public-methods


class SFace(FacialRecognition):
class SFaceClient(FacialRecognition):
"""
SFace model class
"""
Expand All @@ -23,15 +23,22 @@ def __init__(self):
self.model = load_model()
self.model_name = "SFace"

def find_embeddings(self, img: np.ndarray) -> list:
def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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
16 changes: 15 additions & 1 deletion deepface/basemodels/VGGFace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List
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 +39,7 @@
# ---------------------------------------

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

def find_embeddings(self, img: np.ndarray) -> List[float]:
"""
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 bc30c90

Please sign in to comment.