Merge pull request #1156 from serengil/feat-task-2803-resize-moved-to-representation

Feat task 2803 moving find embedding methods to super if not require additional logic
This commit is contained in:
Sefik Ilkin Serengil 2024-03-31 09:35:35 +01:00 committed by GitHub
commit 357a397f1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 27 additions and 103 deletions

View File

@ -1,7 +1,5 @@
from typing import List
import os import os
import gdown import gdown
import numpy as np
from deepface.commons import package_utils, folder_utils from deepface.commons import package_utils, folder_utils
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition from deepface.models.FacialRecognition import FacialRecognition
@ -56,18 +54,6 @@ class ArcFaceClient(FacialRecognition):
self.input_shape = (112, 112) self.input_shape = (112, 112)
self.output_shape = 512 self.output_shape = 512
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( def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5", url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5",

View File

@ -1,7 +1,5 @@
from typing import List
import os import os
import gdown import gdown
import numpy as np
from deepface.commons import package_utils, folder_utils from deepface.commons import package_utils, folder_utils
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition from deepface.models.FacialRecognition import FacialRecognition
@ -52,18 +50,6 @@ class DeepIdClient(FacialRecognition):
self.input_shape = (47, 55) self.input_shape = (47, 55)
self.output_shape = 160 self.output_shape = 160
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( def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5", url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5",

View File

@ -23,9 +23,11 @@ class DlibClient(FacialRecognition):
self.input_shape = (150, 150) self.input_shape = (150, 150)
self.output_shape = 128 self.output_shape = 128
def find_embeddings(self, img: np.ndarray) -> List[float]: def forward(self, img: np.ndarray) -> List[float]:
""" """
find embeddings with Dlib model - different than regular models Find embeddings with Dlib model.
This model necessitates the override of the forward method
because it is not a keras model.
Args: Args:
img (np.ndarray): pre-loaded image in BGR img (np.ndarray): pre-loaded image in BGR
Returns Returns

View File

@ -1,7 +1,5 @@
from typing import List
import os import os
import gdown import gdown
import numpy as np
from deepface.commons import package_utils, folder_utils from deepface.commons import package_utils, folder_utils
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition from deepface.models.FacialRecognition import FacialRecognition
@ -56,18 +54,6 @@ class FaceNet128dClient(FacialRecognition):
self.input_shape = (160, 160) self.input_shape = (160, 160)
self.output_shape = 128 self.output_shape = 128
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 FaceNet512dClient(FacialRecognition): class FaceNet512dClient(FacialRecognition):
""" """
@ -80,18 +66,6 @@ class FaceNet512dClient(FacialRecognition):
self.input_shape = (160, 160) self.input_shape = (160, 160)
self.output_shape = 512 self.output_shape = 512
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): def scaling(x, scale):
return x * scale return x * scale

View File

@ -1,8 +1,6 @@
from typing import List
import os import os
import zipfile import zipfile
import gdown import gdown
import numpy as np
from deepface.commons import package_utils, folder_utils from deepface.commons import package_utils, folder_utils
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition from deepface.models.FacialRecognition import FacialRecognition
@ -56,18 +54,6 @@ class DeepFaceClient(FacialRecognition):
self.input_shape = (152, 152) self.input_shape = (152, 152)
self.output_shape = 4096 self.output_shape = 4096
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( def load_model(
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip", url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",

View File

@ -1,10 +1,8 @@
# built-in dependencies # built-in dependencies
import os import os
from typing import List
# 3rd party dependencies # 3rd party dependencies
import gdown import gdown
import numpy as np
import tensorflow as tf import tensorflow as tf
# project dependencies # project dependencies
@ -72,11 +70,6 @@ class GhostFaceNetClient(FacialRecognition):
self.output_shape = 512 self.output_shape = 512
self.model = load_model() self.model = load_model()
def find_embeddings(self, img: np.ndarray) -> List[float]:
# 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(): def load_model():
model = GhostFaceNetV1() model = GhostFaceNetV1()

View File

@ -1,8 +1,6 @@
from typing import List
import os import os
import gdown import gdown
import tensorflow as tf import tensorflow as tf
import numpy as np
from deepface.commons import package_utils, folder_utils from deepface.commons import package_utils, folder_utils
from deepface.commons.logger import Logger from deepface.commons.logger import Logger
from deepface.models.FacialRecognition import FacialRecognition from deepface.models.FacialRecognition import FacialRecognition
@ -39,18 +37,6 @@ class OpenFaceClient(FacialRecognition):
self.input_shape = (96, 96) self.input_shape = (96, 96)
self.output_shape = 128 self.output_shape = 128
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( def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/openface_weights.h5", url="https://github.com/serengil/deepface_models/releases/download/v1.0/openface_weights.h5",

View File

@ -25,9 +25,11 @@ class SFaceClient(FacialRecognition):
self.input_shape = (112, 112) self.input_shape = (112, 112)
self.output_shape = 128 self.output_shape = 128
def find_embeddings(self, img: np.ndarray) -> List[float]: def forward(self, img: np.ndarray) -> List[float]:
""" """
find embeddings with SFace model - different than regular models Find embeddings with SFace model
This model necessitates the override of the forward method
because it is not a keras model.
Args: Args:
img (np.ndarray): pre-loaded image in BGR img (np.ndarray): pre-loaded image in BGR
Returns Returns

View File

@ -47,9 +47,12 @@ class VggFaceClient(FacialRecognition):
self.input_shape = (224, 224) self.input_shape = (224, 224)
self.output_shape = 4096 self.output_shape = 4096
def find_embeddings(self, img: np.ndarray) -> List[float]: def forward(self, img: np.ndarray) -> List[float]:
""" """
find embeddings with VGG-Face model Generates embeddings using the VGG-Face model.
This method incorporates an additional normalization layer,
necessitating the override of the forward method.
Args: Args:
img (np.ndarray): pre-loaded image in BGR img (np.ndarray): pre-loaded image in BGR
Returns Returns
@ -57,6 +60,7 @@ class VggFaceClient(FacialRecognition):
""" """
# model.predict causes memory issue when it is called in a for loop # model.predict causes memory issue when it is called in a for loop
# embedding = model.predict(img, verbose=0)[0].tolist() # embedding = model.predict(img, verbose=0)[0].tolist()
# having normalization layer in descriptor troubles for some gpu users (e.g. issue 957, 966) # having normalization layer in descriptor troubles for some gpu users (e.g. issue 957, 966)
# instead we are now calculating it with traditional way not with keras backend # instead we are now calculating it with traditional way not with keras backend
embedding = self.model(img, training=False).numpy()[0].tolist() embedding = self.model(img, training=False).numpy()[0].tolist()

View File

@ -1,4 +1,4 @@
from abc import ABC, abstractmethod from abc import ABC
from typing import Any, Union, List, Tuple from typing import Any, Union, List, Tuple
import numpy as np import numpy as np
from deepface.commons import package_utils from deepface.commons import package_utils
@ -18,7 +18,12 @@ class FacialRecognition(ABC):
input_shape: Tuple[int, int] input_shape: Tuple[int, int]
output_shape: int output_shape: int
def forward(self, img: np.ndarray) -> List[float]:
@abstractmethod if not isinstance(self.model, Model):
def find_embeddings(self, img: np.ndarray) -> List[float]: raise ValueError(
pass "You must overwrite forward method if it is not a keras model,"
f"but {self.model_name} not overwritten!"
)
# 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()

View File

@ -104,7 +104,7 @@ def represent(
# custom normalization # custom normalization
img = preprocessing.normalize_input(img=img, normalization=normalization) img = preprocessing.normalize_input(img=img, normalization=normalization)
embedding = model.find_embeddings(img) embedding = model.forward(img)
resp_obj = {} resp_obj = {}
resp_obj["embedding"] = embedding resp_obj["embedding"] = embedding

View File

@ -23,11 +23,11 @@ logger.info(f"target_size: {target_size}")
img1 = DeepFace.extract_faces(img_path="dataset/img1.jpg", target_size=target_size)[0]["face"] img1 = DeepFace.extract_faces(img_path="dataset/img1.jpg", target_size=target_size)[0]["face"]
img1 = np.expand_dims(img1, axis=0) # to (1, 224, 224, 3) img1 = np.expand_dims(img1, axis=0) # to (1, 224, 224, 3)
img1_representation = model.find_embeddings(img1) img1_representation = model.forward(img1)
img2 = DeepFace.extract_faces(img_path="dataset/img3.jpg", target_size=target_size)[0]["face"] img2 = DeepFace.extract_faces(img_path="dataset/img3.jpg", target_size=target_size)[0]["face"]
img2 = np.expand_dims(img2, axis=0) img2 = np.expand_dims(img2, axis=0)
img2_representation = model.find_embeddings(img2) img2_representation = model.forward(img2)
img1_representation = np.array(img1_representation) img1_representation = np.array(img1_representation)
img2_representation = np.array(img2_representation) img2_representation = np.array(img2_representation)