mirror of
https://github.com/serengil/deepface.git
synced 2025-06-06 19:45:21 +00:00
OOP adopted for find embeddings
- standard FR models are now using find embeddings method from its super FacialRecognition - find embeddings renamed to forward.
This commit is contained in:
parent
0f89272532
commit
6cc36922d4
@ -56,18 +56,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",
|
||||||
|
@ -52,18 +52,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",
|
||||||
|
@ -23,9 +23,10 @@ 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
|
||||||
|
Overwritten because it is different than regular models
|
||||||
Args:
|
Args:
|
||||||
img (np.ndarray): pre-loaded image in BGR
|
img (np.ndarray): pre-loaded image in BGR
|
||||||
Returns
|
Returns
|
||||||
|
@ -56,18 +56,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 +68,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
|
||||||
|
@ -56,18 +56,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",
|
||||||
|
@ -72,11 +72,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()
|
||||||
|
@ -39,18 +39,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",
|
||||||
|
@ -25,9 +25,10 @@ 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
|
||||||
|
Overwritten because it is different than regular models
|
||||||
Args:
|
Args:
|
||||||
img (np.ndarray): pre-loaded image in BGR
|
img (np.ndarray): pre-loaded image in BGR
|
||||||
Returns
|
Returns
|
||||||
|
@ -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()
|
||||||
|
@ -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()
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user