File size: 2,422 Bytes
677c57e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# built-in dependencies
import os
# 3rd party dependencies
import gdown
import numpy as np
# project dependencies
from deepface.basemodels import VGGFace
from deepface.commons import package_utils, folder_utils
from deepface.models.Demography import Demography
from deepface.commons import logger as log
logger = log.get_singletonish_logger()
# --------------------------
# pylint: disable=line-too-long
# --------------------------
# dependency configurations
tf_version = package_utils.get_tf_major_version()
if tf_version == 1:
from keras.models import Model, Sequential
from keras.layers import Convolution2D, Flatten, Activation
else:
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Convolution2D, Flatten, Activation
# --------------------------
# Labels for the ethnic phenotypes that can be detected by the model.
labels = ["asian", "indian", "black", "white", "middle eastern", "latino hispanic"]
# pylint: disable=too-few-public-methods
class RaceClient(Demography):
"""
Race model class
"""
def __init__(self):
self.model = load_model()
self.model_name = "Race"
def predict(self, img: np.ndarray) -> np.ndarray:
return self.model.predict(img, verbose=0)[0, :]
def load_model(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/race_model_single_batch.h5",
) -> Model:
"""
Construct race model, download its weights and load
"""
model = VGGFace.base_model()
# --------------------------
classes = 6
base_model_output = Sequential()
base_model_output = Convolution2D(classes, (1, 1), name="predictions")(model.layers[-4].output)
base_model_output = Flatten()(base_model_output)
base_model_output = Activation("softmax")(base_model_output)
# --------------------------
race_model = Model(inputs=model.input, outputs=base_model_output)
# --------------------------
# load weights
home = folder_utils.get_deepface_home()
if os.path.isfile(home + "/.deepface/weights/race_model_single_batch.h5") != True:
logger.info("race_model_single_batch.h5 will be downloaded...")
output = home + "/.deepface/weights/race_model_single_batch.h5"
gdown.download(url, output, quiet=False)
race_model.load_weights(home + "/.deepface/weights/race_model_single_batch.h5")
return race_model
|