-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: master
Are you sure you want to change the base?
Changes from all commits
c42df04
a4b1b5d
b55cb31
29c818d
27e8fc9
38c0652
b9418eb
d992428
e96ede3
e1417a0
f8be282
9f3875e
9c079e9
bba4322
4cf43be
05dbc80
c312684
ffbba7f
edcef02
472f146
b69dcfc
0f65a87
bb820a7
e182285
69267cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# stdlib dependencies | ||
|
||
from typing import List, Union | ||
|
||
# 3rd party dependencies | ||
import numpy as np | ||
|
||
|
@@ -37,12 +41,28 @@ def __init__(self): | |
self.model = load_model() | ||
self.model_name = "Age" | ||
|
||
def predict(self, img: np.ndarray) -> np.float64: | ||
# 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) | ||
|
||
def predict(self, img: Union[np.ndarray, List[np.ndarray]]) -> np.ndarray: | ||
""" | ||
Predict apparent age(s) for single or multiple faces | ||
Args: | ||
img: Single image as np.ndarray (224, 224, 3) or | ||
List of images as List[np.ndarray] or | ||
Batch of images as np.ndarray (n, 224, 224, 3) | ||
Returns: | ||
np.ndarray (n,) | ||
""" | ||
# Preprocessing input image or image list. | ||
imgs = self._preprocess_batch_or_single_input(img) | ||
|
||
# Batch prediction | ||
age_predictions = self.model.predict_on_batch(imgs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
in your design, if this is called in a for loop, still it will cause memory problem. IMO, if it is single image, we should call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for sharing your perspective on this matter. We found the issue you mentioned is also mentioned in this page: tensorflow/tensorflow#44711. We believe it’s being resolved. Furthermore, if we can utilize the batch prediction method provided in this PR, we may be able to avoid repeatedly calling the We recommend that you consider retaining our batch prediction method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey, even though this is sorted in newer tf versions, many users using old tf versions raise tickets about this problem. so, we should consider the people using older tf version. that is why, i recommend to use |
||
|
||
# Calculate apparent ages | ||
apparent_ages = np.array( | ||
[find_apparent_age(age_prediction) for age_prediction in age_predictions] | ||
) | ||
|
||
return apparent_ages | ||
|
||
def load_model( | ||
url=WEIGHTS_URL, | ||
|
@@ -65,7 +85,7 @@ def load_model( | |
|
||
# -------------------------- | ||
|
||
age_model = Model(inputs=model.input, outputs=base_model_output) | ||
age_model = Model(inputs=model.inputs, outputs=base_model_output) | ||
|
||
# -------------------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you use it like this, you don't have to have if and else blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think @h-alice 's concern is likely about the
img
variable being compromised. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@serengil We appreciate your advice.
We will implement your recommended approach later.