Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/batch predict age and gender #1396

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion deepface/models/demography/Age.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# stdlib dependencies
from typing import List

# 3rd party dependencies
import numpy as np

Expand Down Expand Up @@ -43,6 +46,29 @@ def predict(self, img: np.ndarray) -> np.float64:
age_predictions = self.model(img, training=False).numpy()[0, :]
return find_apparent_age(age_predictions)

def predicts(self, imgs: List[np.ndarray]) -> np.ndarray:
"""
Predict apparent ages of multiple faces
Args:
imgs (List[np.ndarray]): (n, 224, 224, 3)
Returns:
apparent_ages (np.ndarray): (n,)
"""
# Convert list to numpy array
imgs_:np.ndarray = np.array(imgs)
# Remove batch dimension if exists
imgs_ = imgs_.squeeze()
# Check if the input is a single image
if len(imgs_.shape) == 3:
# Add batch dimension if not exists
imgs_ = np.expand_dims(imgs_, axis=0)
# Batch prediction
age_predictions = self.model.predict_on_batch(imgs_)
apparent_ages = np.array(
[find_apparent_age(age_prediction) for age_prediction in age_predictions]
)
return apparent_ages


def load_model(
url=WEIGHTS_URL,
Expand All @@ -65,7 +91,7 @@ def load_model(

# --------------------------

age_model = Model(inputs=model.input, outputs=base_model_output)
age_model = Model(inputs=model.inputs, outputs=base_model_output)

# --------------------------

Expand Down
23 changes: 22 additions & 1 deletion deepface/models/demography/Gender.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# stdlib dependencies
from typing import List

# 3rd party dependencies
import numpy as np

Expand Down Expand Up @@ -42,6 +45,24 @@ def predict(self, img: np.ndarray) -> np.ndarray:
# return self.model.predict(img, verbose=0)[0, :]
return self.model(img, training=False).numpy()[0, :]

def predicts(self, imgs: List[np.ndarray]) -> np.ndarray:
"""
Predict apparent ages of multiple faces
Args:
imgs (List[np.ndarray]): (n, 224, 224, 3)
Returns:
apparent_ages (np.ndarray): (n,)
"""
# Convert list to numpy array
imgs_:np.ndarray = np.array(imgs)
# Remove redundant dimensions
imgs_ = imgs_.squeeze()
# Check if the input is a single image
if len(imgs_.shape) == 3:
# Add batch dimension
imgs_ = np.expand_dims(imgs_, axis=0)
return self.model.predict_on_batch(imgs_)


def load_model(
url=WEIGHTS_URL,
Expand All @@ -64,7 +85,7 @@ def load_model(

# --------------------------

gender_model = Model(inputs=model.input, outputs=base_model_output)
gender_model = Model(inputs=model.inputs, outputs=base_model_output)

# --------------------------

Expand Down