mirror of
https://github.com/serengil/deepface.git
synced 2025-06-08 20:45:22 +00:00
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:
commit
357a397f1b
@ -1,7 +1,5 @@
|
||||
from typing import List
|
||||
import os
|
||||
import gdown
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils, folder_utils
|
||||
from deepface.commons.logger import Logger
|
||||
from deepface.models.FacialRecognition import FacialRecognition
|
||||
@ -56,18 +54,6 @@ class ArcFaceClient(FacialRecognition):
|
||||
self.input_shape = (112, 112)
|
||||
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(
|
||||
url="https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5",
|
||||
|
@ -1,7 +1,5 @@
|
||||
from typing import List
|
||||
import os
|
||||
import gdown
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils, folder_utils
|
||||
from deepface.commons.logger import Logger
|
||||
from deepface.models.FacialRecognition import FacialRecognition
|
||||
@ -52,18 +50,6 @@ class DeepIdClient(FacialRecognition):
|
||||
self.input_shape = (47, 55)
|
||||
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(
|
||||
url="https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5",
|
||||
|
@ -23,9 +23,11 @@ class DlibClient(FacialRecognition):
|
||||
self.input_shape = (150, 150)
|
||||
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:
|
||||
img (np.ndarray): pre-loaded image in BGR
|
||||
Returns
|
||||
|
@ -1,7 +1,5 @@
|
||||
from typing import List
|
||||
import os
|
||||
import gdown
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils, folder_utils
|
||||
from deepface.commons.logger import Logger
|
||||
from deepface.models.FacialRecognition import FacialRecognition
|
||||
@ -56,18 +54,6 @@ class FaceNet128dClient(FacialRecognition):
|
||||
self.input_shape = (160, 160)
|
||||
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):
|
||||
"""
|
||||
@ -80,18 +66,6 @@ class FaceNet512dClient(FacialRecognition):
|
||||
self.input_shape = (160, 160)
|
||||
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):
|
||||
return x * scale
|
||||
|
@ -1,8 +1,6 @@
|
||||
from typing import List
|
||||
import os
|
||||
import zipfile
|
||||
import gdown
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils, folder_utils
|
||||
from deepface.commons.logger import Logger
|
||||
from deepface.models.FacialRecognition import FacialRecognition
|
||||
@ -56,18 +54,6 @@ class DeepFaceClient(FacialRecognition):
|
||||
self.input_shape = (152, 152)
|
||||
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(
|
||||
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",
|
||||
|
@ -1,10 +1,8 @@
|
||||
# built-in dependencies
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
# 3rd party dependencies
|
||||
import gdown
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
# project dependencies
|
||||
@ -72,11 +70,6 @@ class GhostFaceNetClient(FacialRecognition):
|
||||
self.output_shape = 512
|
||||
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():
|
||||
model = GhostFaceNetV1()
|
||||
|
@ -1,8 +1,6 @@
|
||||
from typing import List
|
||||
import os
|
||||
import gdown
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils, folder_utils
|
||||
from deepface.commons.logger import Logger
|
||||
from deepface.models.FacialRecognition import FacialRecognition
|
||||
@ -39,18 +37,6 @@ class OpenFaceClient(FacialRecognition):
|
||||
self.input_shape = (96, 96)
|
||||
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(
|
||||
url="https://github.com/serengil/deepface_models/releases/download/v1.0/openface_weights.h5",
|
||||
|
@ -25,9 +25,11 @@ class SFaceClient(FacialRecognition):
|
||||
self.input_shape = (112, 112)
|
||||
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:
|
||||
img (np.ndarray): pre-loaded image in BGR
|
||||
Returns
|
||||
|
@ -47,9 +47,12 @@ class VggFaceClient(FacialRecognition):
|
||||
self.input_shape = (224, 224)
|
||||
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:
|
||||
img (np.ndarray): pre-loaded image in BGR
|
||||
Returns
|
||||
@ -57,6 +60,7 @@ class VggFaceClient(FacialRecognition):
|
||||
"""
|
||||
# model.predict causes memory issue when it is called in a for loop
|
||||
# embedding = model.predict(img, verbose=0)[0].tolist()
|
||||
|
||||
# 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
|
||||
embedding = self.model(img, training=False).numpy()[0].tolist()
|
||||
|
@ -1,4 +1,4 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from abc import ABC
|
||||
from typing import Any, Union, List, Tuple
|
||||
import numpy as np
|
||||
from deepface.commons import package_utils
|
||||
@ -18,7 +18,12 @@ class FacialRecognition(ABC):
|
||||
input_shape: Tuple[int, int]
|
||||
output_shape: int
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def find_embeddings(self, img: np.ndarray) -> List[float]:
|
||||
pass
|
||||
def forward(self, img: np.ndarray) -> List[float]:
|
||||
if not isinstance(self.model, Model):
|
||||
raise ValueError(
|
||||
"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()
|
||||
|
@ -104,7 +104,7 @@ def represent(
|
||||
# custom normalization
|
||||
img = preprocessing.normalize_input(img=img, normalization=normalization)
|
||||
|
||||
embedding = model.find_embeddings(img)
|
||||
embedding = model.forward(img)
|
||||
|
||||
resp_obj = {}
|
||||
resp_obj["embedding"] = embedding
|
||||
|
@ -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 = 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 = np.expand_dims(img2, axis=0)
|
||||
img2_representation = model.find_embeddings(img2)
|
||||
img2_representation = model.forward(img2)
|
||||
|
||||
img1_representation = np.array(img1_representation)
|
||||
img2_representation = np.array(img2_representation)
|
||||
|
Loading…
x
Reference in New Issue
Block a user