Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facial recognition, detector and demography models are now using inte… #960

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 2 additions & 66 deletions deepface/DeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import numpy as np
import pandas as pd
import tensorflow as tf
from deprecated import deprecated

# package dependencies
from deepface.commons import functions
Expand All @@ -23,8 +22,6 @@
realtime,
)

# pylint: disable=no-else-raise, simplifiable-if-expression

logger = Logger(module="DeepFace")

# -----------------------------------
Expand All @@ -40,6 +37,8 @@
from keras.models import Model
# -----------------------------------

functions.initialize_folder()


def build_model(model_name: str) -> Union[Model, Any]:
"""
Expand Down Expand Up @@ -413,69 +412,6 @@ def extract_faces(
)


# ---------------------------
# deprecated functions


@deprecated(version="0.0.78", reason="Use DeepFace.extract_faces instead of DeepFace.detectFace")
def detectFace(
img_path: Union[str, np.ndarray],
target_size: tuple = (224, 224),
detector_backend: str = "opencv",
enforce_detection: bool = True,
align: bool = True,
) -> Union[np.ndarray, None]:
"""
Deprecated function. Use extract_faces for same functionality.

This function applies pre-processing stages of a face recognition pipeline
including detection and alignment

Parameters:
img_path: exact image path, numpy array (BGR) or base64 encoded image.
Source image can have many face. Then, result will be the size of number
of faces appearing in that source image.

target_size (tuple): final shape of facial image. black pixels will be
added to resize the image.

detector_backend (string): face detection backends are retinaface, mtcnn,
opencv, ssd or dlib

enforce_detection (boolean): function throws exception if face cannot be
detected in the fed image. Set this to False if you do not want to get
an exception and run the function anyway.

align (boolean): alignment according to the eye positions.

grayscale (boolean): extracting faces in rgb or gray scale

Returns:
detected and aligned face as numpy array

"""
logger.warn("Function detectFace is deprecated. Use extract_faces instead.")
face_objs = extract_faces(
img_path=img_path,
target_size=target_size,
detector_backend=detector_backend,
enforce_detection=enforce_detection,
align=align,
grayscale=False,
)

extracted_face = None
if len(face_objs) > 0:
extracted_face = face_objs[0]["face"]
return extracted_face


# ---------------------------
# main

functions.initialize_folder()


def cli() -> None:
"""
command line interface function will be offered in this block
Expand Down
24 changes: 22 additions & 2 deletions deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition

logger = Logger(module="basemodels.ArcFace")

Expand Down Expand Up @@ -42,10 +43,25 @@
Dense,
)

# pylint: disable=too-few-public-methods
class ArcFace(FacialRecognition):
"""
ArcFace model class
"""

def loadModel(
def __init__(self):
self.model = load_model()
self.model_name = "ArcFace"


def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5",
) -> Model:
"""
Construct ArcFace model, download its weights and load
Returns:
model (Model)
"""
base_model = ResNet34()
inputs = base_model.inputs[0]
arcface_model = base_model.outputs[0]
Expand Down Expand Up @@ -81,7 +97,11 @@ def loadModel(


def ResNet34() -> Model:

"""
ResNet34 model
Returns:
model (Model)
"""
img_input = Input(shape=(112, 112, 3))

x = ZeroPadding2D(padding=1, name="conv1_pad")(img_input)
Expand Down
16 changes: 15 additions & 1 deletion deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition

logger = Logger(module="basemodels.DeepID")

Expand Down Expand Up @@ -38,10 +39,23 @@

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

# pylint: disable=too-few-public-methods
class DeepId(FacialRecognition):
"""
DeepId model class
"""

def loadModel(
def __init__(self):
self.model = load_model()
self.model_name = "DeepId"


def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5",
) -> Model:
"""
Construct DeepId model, download its weights and load
"""

myInput = Input(shape=(55, 47, 3))

Expand Down
21 changes: 21 additions & 0 deletions deepface/basemodels/DlibResNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition

logger = Logger(module="basemodels.DlibResNet")

# pylint: disable=too-few-public-methods


class Dlib(FacialRecognition):
"""
Dlib model class
"""

def __init__(self):
self.model = DlibResNet()
self.model_name = "Dlib"

def find_embeddings(self, img: np.ndarray) -> list:
"""
Custom find embeddings function of Dlib different than FacialRecognition's one
Args:
img (np.ndarray)
Retunrs:
embeddings (list)
"""
return self.model.predict(img)[0].tolist()


class DlibResNet:
def __init__(self):

Expand Down
6 changes: 0 additions & 6 deletions deepface/basemodels/DlibWrapper.py

This file was deleted.

69 changes: 67 additions & 2 deletions deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition

logger = Logger(module="basemodels.Facenet")

Expand Down Expand Up @@ -42,12 +43,39 @@

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

# pylint: disable=too-few-public-methods
class FaceNet128d(FacialRecognition):
"""
FaceNet-128d model class
"""

def __init__(self):
self.model = load_facenet128d_model()
self.model_name = "FaceNet-128d"


class FaceNet512d(FacialRecognition):
"""
FaceNet-1512d model class
"""

def __init__(self):
self.model = load_facenet512d_model()
self.model_name = "FaceNet-512d"


def scaling(x, scale):
return x * scale


def InceptionResNetV2(dimension=128) -> Model:
def InceptionResNetV2(dimension: int = 128) -> Model:
"""
InceptionResNetV2 model
Args:
dimension (int): number of dimensions in the embedding layer
Returns:
model (Model)
"""

inputs = Input(shape=(160, 160, 3))
x = Conv2D(32, 3, strides=2, padding="valid", use_bias=False, name="Conv2d_1a_3x3")(inputs)
Expand Down Expand Up @@ -1618,9 +1646,16 @@ def InceptionResNetV2(dimension=128) -> Model:
return model


def loadModel(
def load_facenet128d_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facenet_weights.h5",
) -> Model:
"""
Construct FaceNet-128d model, download weights and then load weights
Args:
dimension (int): construct FaceNet-128d or FaceNet-512d models
Returns:
model (Model)
"""
model = InceptionResNetV2()

# -----------------------------------
Expand All @@ -1640,3 +1675,33 @@ def loadModel(
# -----------------------------------

return model


def load_facenet512d_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facenet512_weights.h5",
) -> Model:
"""
Construct FaceNet-512d model, download its weights and load
Returns:
model (Model)
"""

model = InceptionResNetV2(dimension=512)

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

home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/facenet512_weights.h5") != True:
logger.info("facenet512_weights.h5 will be downloaded...")

output = home + "/.deepface/weights/facenet512_weights.h5"
gdown.download(url, output, quiet=False)

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

model.load_weights(home + "/.deepface/weights/facenet512_weights.h5")

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

return model
40 changes: 0 additions & 40 deletions deepface/basemodels/Facenet512.py

This file was deleted.

16 changes: 14 additions & 2 deletions deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition

logger = Logger(module="basemodels.FbDeepFace")

Expand Down Expand Up @@ -35,12 +36,23 @@


# -------------------------------------
# pylint: disable=line-too-long
# pylint: disable=line-too-long, too-few-public-methods
class DeepFace(FacialRecognition):
"""
Fb's DeepFace model class
"""

def __init__(self):
self.model = load_model()
self.model_name = "DeepFace"

def loadModel(

def load_model(
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",
) -> Model:
"""
Construct DeepFace model, download its weights and load
"""
base_model = Sequential()
base_model.add(
Convolution2D(32, (11, 11), activation="relu", name="C1", input_shape=(152, 152, 3))
Expand Down
Loading
Loading