From 51717db2ba03dac3524d803339791267b5217449 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Sun, 4 Aug 2024 12:47:36 +0100 Subject: [PATCH] batch run support added for facial attribute models --- deepface/extendedmodels/Age.py | 4 +++- deepface/extendedmodels/Emotion.py | 5 ++++- deepface/extendedmodels/Gender.py | 4 +++- deepface/extendedmodels/Race.py | 4 +++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/deepface/extendedmodels/Age.py b/deepface/extendedmodels/Age.py index 8d0f0cf65..e9363a1f2 100644 --- a/deepface/extendedmodels/Age.py +++ b/deepface/extendedmodels/Age.py @@ -33,7 +33,9 @@ def __init__(self): self.model_name = "Age" def predict(self, img: np.ndarray) -> np.float64: - age_predictions = self.model.predict(img, verbose=0)[0, :] + # model.predict causes memory issue when it is called in a for loop + # age_predictions = self.model.predict(img, verbose=0)[0, :] + age_predictions = self.model(img, training=False).numpy()[0, :] return find_apparent_age(age_predictions) diff --git a/deepface/extendedmodels/Emotion.py b/deepface/extendedmodels/Emotion.py index 7338131af..a6cf288a5 100644 --- a/deepface/extendedmodels/Emotion.py +++ b/deepface/extendedmodels/Emotion.py @@ -52,7 +52,10 @@ def predict(self, img: np.ndarray) -> np.ndarray: img_gray = cv2.resize(img_gray, (48, 48)) img_gray = np.expand_dims(img_gray, axis=0) - emotion_predictions = self.model.predict(img_gray, verbose=0)[0, :] + # model.predict causes memory issue when it is called in a for loop + # emotion_predictions = self.model.predict(img_gray, verbose=0)[0, :] + emotion_predictions = self.model(img_gray, training=False).numpy()[0, :] + return emotion_predictions diff --git a/deepface/extendedmodels/Gender.py b/deepface/extendedmodels/Gender.py index cb766fec1..fff8e2dd4 100644 --- a/deepface/extendedmodels/Gender.py +++ b/deepface/extendedmodels/Gender.py @@ -41,7 +41,9 @@ def __init__(self): self.model_name = "Gender" def predict(self, img: np.ndarray) -> np.ndarray: - return self.model.predict(img, verbose=0)[0, :] + # model.predict causes memory issue when it is called in a for loop + # return self.model.predict(img, verbose=0)[0, :] + return self.model(img, training=False).numpy()[0, :] def load_model( diff --git a/deepface/extendedmodels/Race.py b/deepface/extendedmodels/Race.py index 6f9e643e5..5e3b3e201 100644 --- a/deepface/extendedmodels/Race.py +++ b/deepface/extendedmodels/Race.py @@ -40,7 +40,9 @@ def __init__(self): self.model_name = "Race" def predict(self, img: np.ndarray) -> np.ndarray: - return self.model.predict(img, verbose=0)[0, :] + # model.predict causes memory issue when it is called in a for loop + # return self.model.predict(img, verbose=0)[0, :] + return self.model(img, training=False).numpy()[0, :] def load_model(