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:
Sefik Ilkin Serengil 2024-03-28 19:59:09 +00:00
parent 0f89272532
commit 6cc36922d4
12 changed files with 24 additions and 90 deletions

View File

@ -56,18 +56,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",

View File

@ -52,18 +52,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",

View File

@ -23,9 +23,10 @@ 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
Overwritten because it is different than regular models
Args:
img (np.ndarray): pre-loaded image in BGR
Returns

View File

@ -56,18 +56,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 +68,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

View File

@ -56,18 +56,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",

View File

@ -72,11 +72,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()

View File

@ -39,18 +39,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",

View File

@ -25,9 +25,10 @@ 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
Overwritten because it is different than regular models
Args:
img (np.ndarray): pre-loaded image in BGR
Returns

View File

@ -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()

View File

@ -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()

View File

@ -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

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 = 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)