mirror of
https://github.com/serengil/deepface.git
synced 2025-06-09 12:57:08 +00:00
Pass custom path argument while loading model
Add `model_path` parameter in `loadModel()` function. This adds more flexiblity while loading the models. Also, refactor code using standard `os.path.join` which will make sure to join the paths correctly.
This commit is contained in:
parent
892cbae56a
commit
2c0a507994
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,4 +13,4 @@ deepface/__pycache__/*
|
||||
deepface/commons/__pycache__/*
|
||||
deepface/basemodels/__pycache__/*
|
||||
deepface/extendedmodels/__pycache__/*
|
||||
deepface/subsidiarymodels/__pycache__/*
|
||||
deepface/subsidiarymodels/__pycache__/*
|
||||
|
@ -1,5 +1,6 @@
|
||||
from keras.preprocessing import image
|
||||
import warnings
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
import time
|
||||
import os
|
||||
@ -12,330 +13,397 @@ from keras import backend as K
|
||||
import keras
|
||||
import tensorflow as tf
|
||||
|
||||
#from basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
|
||||
#from extendedmodels import Age, Gender, Race, Emotion
|
||||
#from commons import functions, realtime, distance as dst
|
||||
# from basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
|
||||
# from extendedmodels import Age, Gender, Race, Emotion
|
||||
# from commons import functions, realtime, distance as dst
|
||||
|
||||
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
|
||||
from deepface.extendedmodels import Age, Gender, Race, Emotion
|
||||
from deepface.commons import functions, realtime, distance as dst
|
||||
|
||||
def verify(img1_path, img2_path=''
|
||||
, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True):
|
||||
|
||||
tic = time.time()
|
||||
|
||||
if type(img1_path) == list:
|
||||
bulkProcess = True
|
||||
img_list = img1_path.copy()
|
||||
else:
|
||||
bulkProcess = False
|
||||
img_list = [[img1_path, img2_path]]
|
||||
|
||||
#------------------------------
|
||||
|
||||
if model == None:
|
||||
if model_name == 'VGG-Face':
|
||||
print("Using VGG-Face model backend and", distance_metric,"distance.")
|
||||
model = VGGFace.loadModel()
|
||||
|
||||
elif model_name == 'OpenFace':
|
||||
print("Using OpenFace model backend", distance_metric,"distance.")
|
||||
model = OpenFace.loadModel()
|
||||
|
||||
elif model_name == 'Facenet':
|
||||
print("Using Facenet model backend", distance_metric,"distance.")
|
||||
model = Facenet.loadModel()
|
||||
def verify(
|
||||
img1_path,
|
||||
img2_path="",
|
||||
model_name="VGG-Face",
|
||||
distance_metric="cosine",
|
||||
model=None,
|
||||
enforce_detection=True,
|
||||
):
|
||||
|
||||
tic = time.time()
|
||||
|
||||
if type(img1_path) == list:
|
||||
bulkProcess = True
|
||||
img_list = img1_path.copy()
|
||||
else:
|
||||
bulkProcess = False
|
||||
img_list = [[img1_path, img2_path]]
|
||||
|
||||
# ------------------------------
|
||||
|
||||
if model == None:
|
||||
if model_name == "VGG-Face":
|
||||
print("Using VGG-Face model backend and", distance_metric, "distance.")
|
||||
model = VGGFace.loadModel()
|
||||
|
||||
elif model_name == "OpenFace":
|
||||
print("Using OpenFace model backend", distance_metric, "distance.")
|
||||
model = OpenFace.loadModel()
|
||||
|
||||
elif model_name == "Facenet":
|
||||
print("Using Facenet model backend", distance_metric, "distance.")
|
||||
model = Facenet.loadModel()
|
||||
|
||||
elif model_name == "DeepFace":
|
||||
print("Using FB DeepFace model backend", distance_metric, "distance.")
|
||||
model = FbDeepFace.loadModel()
|
||||
|
||||
elif model_name == 'DeepFace':
|
||||
print("Using FB DeepFace model backend", distance_metric,"distance.")
|
||||
model = FbDeepFace.loadModel()
|
||||
else:
|
||||
raise ValueError("Invalid model_name passed - ", model_name)
|
||||
else: # model != None
|
||||
print("Already built model is passed")
|
||||
|
||||
else:
|
||||
raise ValueError("Invalid model_name passed - ", model_name)
|
||||
else: #model != None
|
||||
print("Already built model is passed")
|
||||
# ------------------------------
|
||||
# face recognition models have different size of inputs
|
||||
input_shape = model.layers[0].input_shape[1:3]
|
||||
|
||||
#------------------------------
|
||||
#face recognition models have different size of inputs
|
||||
input_shape = model.layers[0].input_shape[1:3]
|
||||
# ------------------------------
|
||||
|
||||
#------------------------------
|
||||
# tuned thresholds for model and metric pair
|
||||
threshold = functions.findThreshold(model_name, distance_metric)
|
||||
|
||||
# ------------------------------
|
||||
pbar = tqdm(range(0, len(img_list)), desc="Verification")
|
||||
|
||||
#tuned thresholds for model and metric pair
|
||||
threshold = functions.findThreshold(model_name, distance_metric)
|
||||
resp_objects = []
|
||||
|
||||
#------------------------------
|
||||
pbar = tqdm(range(0,len(img_list)), desc='Verification')
|
||||
|
||||
resp_objects = []
|
||||
|
||||
#for instance in img_list:
|
||||
for index in pbar:
|
||||
|
||||
instance = img_list[index]
|
||||
|
||||
if type(instance) == list and len(instance) >= 2:
|
||||
img1_path = instance[0]
|
||||
img2_path = instance[1]
|
||||
# for instance in img_list:
|
||||
for index in pbar:
|
||||
|
||||
#----------------------
|
||||
#crop and align faces
|
||||
instance = img_list[index]
|
||||
|
||||
img1 = functions.detectFace(img1_path, input_shape, enforce_detection = enforce_detection)
|
||||
img2 = functions.detectFace(img2_path, input_shape, enforce_detection = enforce_detection)
|
||||
if type(instance) == list and len(instance) >= 2:
|
||||
img1_path = instance[0]
|
||||
img2_path = instance[1]
|
||||
|
||||
#----------------------
|
||||
#find embeddings
|
||||
|
||||
img1_representation = model.predict(img1)[0,:]
|
||||
img2_representation = model.predict(img2)[0,:]
|
||||
# ----------------------
|
||||
# crop and align faces
|
||||
|
||||
#----------------------
|
||||
#find distances between embeddings
|
||||
|
||||
if distance_metric == 'cosine':
|
||||
distance = dst.findCosineDistance(img1_representation, img2_representation)
|
||||
elif distance_metric == 'euclidean':
|
||||
distance = dst.findEuclideanDistance(img1_representation, img2_representation)
|
||||
elif distance_metric == 'euclidean_l2':
|
||||
distance = dst.findEuclideanDistance(dst.l2_normalize(img1_representation), dst.l2_normalize(img2_representation))
|
||||
else:
|
||||
raise ValueError("Invalid distance_metric passed - ", distance_metric)
|
||||
|
||||
#----------------------
|
||||
#decision
|
||||
|
||||
if distance <= threshold:
|
||||
identified = "true"
|
||||
else:
|
||||
identified = "false"
|
||||
|
||||
#----------------------
|
||||
#response object
|
||||
|
||||
resp_obj = "{"
|
||||
resp_obj += "\"verified\": "+identified
|
||||
resp_obj += ", \"distance\": "+str(distance)
|
||||
resp_obj += ", \"max_threshold_to_verify\": "+str(threshold)
|
||||
resp_obj += ", \"model\": \""+model_name+"\""
|
||||
resp_obj += ", \"similarity_metric\": \""+distance_metric+"\""
|
||||
resp_obj += "}"
|
||||
|
||||
resp_obj = json.loads(resp_obj) #string to json
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_objects.append(resp_obj)
|
||||
else:
|
||||
#K.clear_session()
|
||||
return resp_obj
|
||||
#----------------------
|
||||
|
||||
else:
|
||||
raise ValueError("Invalid arguments passed to verify function: ", instance)
|
||||
|
||||
#-------------------------
|
||||
|
||||
toc = time.time()
|
||||
|
||||
#print("identification lasts ",toc-tic," seconds")
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_obj = "{"
|
||||
img1 = functions.detectFace(
|
||||
img1_path, input_shape, enforce_detection=enforce_detection
|
||||
)
|
||||
img2 = functions.detectFace(
|
||||
img2_path, input_shape, enforce_detection=enforce_detection
|
||||
)
|
||||
|
||||
for i in range(0, len(resp_objects)):
|
||||
resp_item = json.dumps(resp_objects[i])
|
||||
# ----------------------
|
||||
# find embeddings
|
||||
|
||||
if i > 0:
|
||||
resp_obj += ", "
|
||||
img1_representation = model.predict(img1)[0, :]
|
||||
img2_representation = model.predict(img2)[0, :]
|
||||
|
||||
resp_obj += "\"pair_"+str(i+1)+"\": "+resp_item
|
||||
resp_obj += "}"
|
||||
resp_obj = json.loads(resp_obj)
|
||||
return resp_obj
|
||||
#return resp_objects
|
||||
# ----------------------
|
||||
# find distances between embeddings
|
||||
|
||||
if distance_metric == "cosine":
|
||||
distance = dst.findCosineDistance(
|
||||
img1_representation, img2_representation
|
||||
)
|
||||
elif distance_metric == "euclidean":
|
||||
distance = dst.findEuclideanDistance(
|
||||
img1_representation, img2_representation
|
||||
)
|
||||
elif distance_metric == "euclidean_l2":
|
||||
distance = dst.findEuclideanDistance(
|
||||
dst.l2_normalize(img1_representation),
|
||||
dst.l2_normalize(img2_representation),
|
||||
)
|
||||
else:
|
||||
raise ValueError("Invalid distance_metric passed - ", distance_metric)
|
||||
|
||||
# ----------------------
|
||||
# decision
|
||||
|
||||
if distance <= threshold:
|
||||
identified = "true"
|
||||
else:
|
||||
identified = "false"
|
||||
|
||||
def analyze(img_path, actions = [], models = {}, enforce_detection = True):
|
||||
# ----------------------
|
||||
# response object
|
||||
|
||||
if type(img_path) == list:
|
||||
img_paths = img_path.copy()
|
||||
bulkProcess = True
|
||||
else:
|
||||
img_paths = [img_path]
|
||||
bulkProcess = False
|
||||
resp_obj = "{"
|
||||
resp_obj += '"verified": ' + identified
|
||||
resp_obj += ', "distance": ' + str(distance)
|
||||
resp_obj += ', "max_threshold_to_verify": ' + str(threshold)
|
||||
resp_obj += ', "model": "' + model_name + '"'
|
||||
resp_obj += ', "similarity_metric": "' + distance_metric + '"'
|
||||
resp_obj += "}"
|
||||
|
||||
#---------------------------------
|
||||
resp_obj = json.loads(resp_obj) # string to json
|
||||
|
||||
#if a specific target is not passed, then find them all
|
||||
if len(actions) == 0:
|
||||
actions= ['emotion', 'age', 'gender', 'race']
|
||||
if bulkProcess == True:
|
||||
resp_objects.append(resp_obj)
|
||||
else:
|
||||
# K.clear_session()
|
||||
return resp_obj
|
||||
# ----------------------
|
||||
|
||||
print("Actions to do: ", actions)
|
||||
else:
|
||||
raise ValueError("Invalid arguments passed to verify function: ", instance)
|
||||
|
||||
#---------------------------------
|
||||
# -------------------------
|
||||
|
||||
if 'emotion' in actions:
|
||||
if 'emotion' in models:
|
||||
print("already built emotion model is passed")
|
||||
emotion_model = models['emotion']
|
||||
else:
|
||||
emotion_model = Emotion.loadModel()
|
||||
toc = time.time()
|
||||
|
||||
# print("identification lasts ",toc-tic," seconds")
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_obj = "{"
|
||||
|
||||
for i in range(0, len(resp_objects)):
|
||||
resp_item = json.dumps(resp_objects[i])
|
||||
|
||||
if i > 0:
|
||||
resp_obj += ", "
|
||||
|
||||
if 'age' in actions:
|
||||
if 'age' in models:
|
||||
print("already built age model is passed")
|
||||
age_model = models['age']
|
||||
else:
|
||||
age_model = Age.loadModel()
|
||||
|
||||
if 'gender' in actions:
|
||||
if 'gender' in models:
|
||||
print("already built gender model is passed")
|
||||
gender_model = models['gender']
|
||||
else:
|
||||
gender_model = Gender.loadModel()
|
||||
|
||||
if 'race' in actions:
|
||||
if 'race' in models:
|
||||
print("already built race model is passed")
|
||||
race_model = models['race']
|
||||
else:
|
||||
race_model = Race.loadModel()
|
||||
#---------------------------------
|
||||
|
||||
resp_objects = []
|
||||
|
||||
global_pbar = tqdm(range(0,len(img_paths)), desc='Analyzing')
|
||||
|
||||
#for img_path in img_paths:
|
||||
for j in global_pbar:
|
||||
img_path = img_paths[j]
|
||||
|
||||
resp_obj = "{"
|
||||
|
||||
#TO-DO: do this in parallel
|
||||
|
||||
pbar = tqdm(range(0,len(actions)), desc='Finding actions')
|
||||
|
||||
action_idx = 0
|
||||
img_224 = None # Set to prevent re-detection
|
||||
#for action in actions:
|
||||
for index in pbar:
|
||||
action = actions[index]
|
||||
pbar.set_description("Action: %s" % (action))
|
||||
|
||||
if action_idx > 0:
|
||||
resp_obj += ", "
|
||||
|
||||
if action == 'emotion':
|
||||
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
||||
img = functions.detectFace(img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection)
|
||||
|
||||
emotion_predictions = emotion_model.predict(img)[0,:]
|
||||
|
||||
sum_of_predictions = emotion_predictions.sum()
|
||||
|
||||
emotion_obj = "\"emotion\": {"
|
||||
for i in range(0, len(emotion_labels)):
|
||||
emotion_label = emotion_labels[i]
|
||||
emotion_prediction = 100 * emotion_predictions[i] / sum_of_predictions
|
||||
|
||||
if i > 0: emotion_obj += ", "
|
||||
|
||||
emotion_obj += "\"%s\": %s" % (emotion_label, emotion_prediction)
|
||||
|
||||
emotion_obj += "}"
|
||||
|
||||
emotion_obj += ", \"dominant_emotion\": \"%s\"" % (emotion_labels[np.argmax(emotion_predictions)])
|
||||
|
||||
resp_obj += emotion_obj
|
||||
|
||||
elif action == 'age':
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images
|
||||
#print("age prediction")
|
||||
age_predictions = age_model.predict(img_224)[0,:]
|
||||
apparent_age = Age.findApparentAge(age_predictions)
|
||||
|
||||
resp_obj += "\"age\": %s" % (apparent_age)
|
||||
|
||||
elif action == 'gender':
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images
|
||||
#print("gender prediction")
|
||||
|
||||
gender_prediction = gender_model.predict(img_224)[0,:]
|
||||
|
||||
if np.argmax(gender_prediction) == 0:
|
||||
gender = "Woman"
|
||||
elif np.argmax(gender_prediction) == 1:
|
||||
gender = "Man"
|
||||
|
||||
resp_obj += "\"gender\": \"%s\"" % (gender)
|
||||
|
||||
elif action == 'race':
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images
|
||||
race_predictions = race_model.predict(img_224)[0,:]
|
||||
race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
|
||||
|
||||
sum_of_predictions = race_predictions.sum()
|
||||
|
||||
race_obj = "\"race\": {"
|
||||
for i in range(0, len(race_labels)):
|
||||
race_label = race_labels[i]
|
||||
race_prediction = 100 * race_predictions[i] / sum_of_predictions
|
||||
|
||||
if i > 0: race_obj += ", "
|
||||
|
||||
race_obj += "\"%s\": %s" % (race_label, race_prediction)
|
||||
|
||||
race_obj += "}"
|
||||
race_obj += ", \"dominant_race\": \"%s\"" % (race_labels[np.argmax(race_predictions)])
|
||||
|
||||
resp_obj += race_obj
|
||||
|
||||
action_idx = action_idx + 1
|
||||
|
||||
resp_obj += "}"
|
||||
|
||||
resp_obj = json.loads(resp_obj)
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_objects.append(resp_obj)
|
||||
else:
|
||||
return resp_obj
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_obj = "{"
|
||||
|
||||
for i in range(0, len(resp_objects)):
|
||||
resp_item = json.dumps(resp_objects[i])
|
||||
|
||||
if i > 0:
|
||||
resp_obj += ", "
|
||||
|
||||
resp_obj += "\"instance_"+str(i+1)+"\": "+resp_item
|
||||
resp_obj += "}"
|
||||
resp_obj = json.loads(resp_obj)
|
||||
return resp_obj
|
||||
#return resp_objects
|
||||
resp_obj += '"pair_' + str(i + 1) + '": ' + resp_item
|
||||
resp_obj += "}"
|
||||
resp_obj = json.loads(resp_obj)
|
||||
return resp_obj
|
||||
# return resp_objects
|
||||
|
||||
|
||||
def analyze(img_path, actions=[], models={}, enforce_detection=True):
|
||||
|
||||
if type(img_path) == list:
|
||||
img_paths = img_path.copy()
|
||||
bulkProcess = True
|
||||
else:
|
||||
img_paths = [img_path]
|
||||
bulkProcess = False
|
||||
|
||||
# ---------------------------------
|
||||
|
||||
# if a specific target is not passed, then find them all
|
||||
if len(actions) == 0:
|
||||
actions = ["emotion", "age", "gender", "race"]
|
||||
|
||||
print("Actions to do: ", actions)
|
||||
|
||||
# ---------------------------------
|
||||
|
||||
if "emotion" in actions:
|
||||
if "emotion" in models:
|
||||
print("already built emotion model is passed")
|
||||
emotion_model = models["emotion"]
|
||||
else:
|
||||
emotion_model = Emotion.loadModel()
|
||||
|
||||
if "age" in actions:
|
||||
if "age" in models:
|
||||
print("already built age model is passed")
|
||||
age_model = models["age"]
|
||||
else:
|
||||
age_model = Age.loadModel()
|
||||
|
||||
if "gender" in actions:
|
||||
if "gender" in models:
|
||||
print("already built gender model is passed")
|
||||
gender_model = models["gender"]
|
||||
else:
|
||||
gender_model = Gender.loadModel()
|
||||
|
||||
if "race" in actions:
|
||||
if "race" in models:
|
||||
print("already built race model is passed")
|
||||
race_model = models["race"]
|
||||
else:
|
||||
race_model = Race.loadModel()
|
||||
# ---------------------------------
|
||||
|
||||
resp_objects = []
|
||||
|
||||
global_pbar = tqdm(range(0, len(img_paths)), desc="Analyzing")
|
||||
|
||||
# for img_path in img_paths:
|
||||
for j in global_pbar:
|
||||
img_path = img_paths[j]
|
||||
|
||||
resp_obj = "{"
|
||||
|
||||
# TO-DO: do this in parallel
|
||||
|
||||
pbar = tqdm(range(0, len(actions)), desc="Finding actions")
|
||||
|
||||
action_idx = 0
|
||||
img_224 = None # Set to prevent re-detection
|
||||
# for action in actions:
|
||||
for index in pbar:
|
||||
action = actions[index]
|
||||
pbar.set_description("Action: %s" % (action))
|
||||
|
||||
if action_idx > 0:
|
||||
resp_obj += ", "
|
||||
|
||||
if action == "emotion":
|
||||
emotion_labels = [
|
||||
"angry",
|
||||
"disgust",
|
||||
"fear",
|
||||
"happy",
|
||||
"sad",
|
||||
"surprise",
|
||||
"neutral",
|
||||
]
|
||||
img = functions.detectFace(
|
||||
img_path,
|
||||
target_size=(48, 48),
|
||||
grayscale=True,
|
||||
enforce_detection=enforce_detection,
|
||||
)
|
||||
|
||||
emotion_predictions = emotion_model.predict(img)[0, :]
|
||||
|
||||
sum_of_predictions = emotion_predictions.sum()
|
||||
|
||||
emotion_obj = '"emotion": {'
|
||||
for i in range(0, len(emotion_labels)):
|
||||
emotion_label = emotion_labels[i]
|
||||
emotion_prediction = (
|
||||
100 * emotion_predictions[i] / sum_of_predictions
|
||||
)
|
||||
|
||||
if i > 0:
|
||||
emotion_obj += ", "
|
||||
|
||||
emotion_obj += '"%s": %s' % (emotion_label, emotion_prediction)
|
||||
|
||||
emotion_obj += "}"
|
||||
|
||||
emotion_obj += ', "dominant_emotion": "%s"' % (
|
||||
emotion_labels[np.argmax(emotion_predictions)]
|
||||
)
|
||||
|
||||
resp_obj += emotion_obj
|
||||
|
||||
elif action == "age":
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(
|
||||
img_path,
|
||||
target_size=(224, 224),
|
||||
grayscale=False,
|
||||
enforce_detection=enforce_detection,
|
||||
) # just emotion model expects grayscale images
|
||||
# print("age prediction")
|
||||
age_predictions = age_model.predict(img_224)[0, :]
|
||||
apparent_age = Age.findApparentAge(age_predictions)
|
||||
|
||||
resp_obj += '"age": %s' % (apparent_age)
|
||||
|
||||
elif action == "gender":
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(
|
||||
img_path,
|
||||
target_size=(224, 224),
|
||||
grayscale=False,
|
||||
enforce_detection=enforce_detection,
|
||||
) # just emotion model expects grayscale images
|
||||
# print("gender prediction")
|
||||
|
||||
gender_prediction = gender_model.predict(img_224)[0, :]
|
||||
|
||||
if np.argmax(gender_prediction) == 0:
|
||||
gender = "Woman"
|
||||
elif np.argmax(gender_prediction) == 1:
|
||||
gender = "Man"
|
||||
|
||||
resp_obj += '"gender": "%s"' % (gender)
|
||||
|
||||
elif action == "race":
|
||||
if img_224 is None:
|
||||
img_224 = functions.detectFace(
|
||||
img_path,
|
||||
target_size=(224, 224),
|
||||
grayscale=False,
|
||||
enforce_detection=enforce_detection,
|
||||
) # just emotion model expects grayscale images
|
||||
race_predictions = race_model.predict(img_224)[0, :]
|
||||
race_labels = [
|
||||
"asian",
|
||||
"indian",
|
||||
"black",
|
||||
"white",
|
||||
"middle eastern",
|
||||
"latino hispanic",
|
||||
]
|
||||
|
||||
sum_of_predictions = race_predictions.sum()
|
||||
|
||||
race_obj = '"race": {'
|
||||
for i in range(0, len(race_labels)):
|
||||
race_label = race_labels[i]
|
||||
race_prediction = 100 * race_predictions[i] / sum_of_predictions
|
||||
|
||||
if i > 0:
|
||||
race_obj += ", "
|
||||
|
||||
race_obj += '"%s": %s' % (race_label, race_prediction)
|
||||
|
||||
race_obj += "}"
|
||||
race_obj += ', "dominant_race": "%s"' % (
|
||||
race_labels[np.argmax(race_predictions)]
|
||||
)
|
||||
|
||||
resp_obj += race_obj
|
||||
|
||||
action_idx = action_idx + 1
|
||||
|
||||
resp_obj += "}"
|
||||
|
||||
resp_obj = json.loads(resp_obj)
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_objects.append(resp_obj)
|
||||
else:
|
||||
return resp_obj
|
||||
|
||||
if bulkProcess == True:
|
||||
resp_obj = "{"
|
||||
|
||||
for i in range(0, len(resp_objects)):
|
||||
resp_item = json.dumps(resp_objects[i])
|
||||
|
||||
if i > 0:
|
||||
resp_obj += ", "
|
||||
|
||||
resp_obj += '"instance_' + str(i + 1) + '": ' + resp_item
|
||||
resp_obj += "}"
|
||||
resp_obj = json.loads(resp_obj)
|
||||
return resp_obj
|
||||
# return resp_objects
|
||||
|
||||
|
||||
def detectFace(img_path):
|
||||
img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3)
|
||||
return img[:, :, ::-1] #bgr to rgb
|
||||
img = functions.detectFace(img_path)[0] # detectFace returns (1, 224, 224, 3)
|
||||
return img[:, :, ::-1] # bgr to rgb
|
||||
|
||||
|
||||
def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine', enable_face_analysis = True):
|
||||
realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis)
|
||||
def stream(
|
||||
db_path="",
|
||||
model_name="VGG-Face",
|
||||
distance_metric="cosine",
|
||||
enable_face_analysis=True,
|
||||
):
|
||||
realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis)
|
||||
|
||||
|
||||
def allocateMemory():
|
||||
print("Analyzing your system...")
|
||||
functions.allocateMemory()
|
||||
print("Analyzing your system...")
|
||||
functions.allocateMemory()
|
||||
|
||||
|
||||
functions.initializeFolder()
|
||||
|
||||
#---------------------------
|
||||
|
||||
# ---------------------------
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,44 +3,71 @@ from pathlib import Path
|
||||
import gdown
|
||||
import keras
|
||||
from keras.models import Model, Sequential
|
||||
from keras.layers import Convolution2D, LocallyConnected2D, MaxPooling2D, Flatten, Dense, Dropout
|
||||
from keras.layers import (
|
||||
Convolution2D,
|
||||
LocallyConnected2D,
|
||||
MaxPooling2D,
|
||||
Flatten,
|
||||
Dense,
|
||||
Dropout,
|
||||
)
|
||||
import zipfile
|
||||
|
||||
#-------------------------------------
|
||||
# -------------------------------------
|
||||
|
||||
def loadModel():
|
||||
base_model = Sequential()
|
||||
base_model.add(Convolution2D(32, (11, 11), activation='relu', name='C1', input_shape=(152, 152, 3)))
|
||||
base_model.add(MaxPooling2D(pool_size=3, strides=2, padding='same', name='M2'))
|
||||
base_model.add(Convolution2D(16, (9, 9), activation='relu', name='C3'))
|
||||
base_model.add(LocallyConnected2D(16, (9, 9), activation='relu', name='L4'))
|
||||
base_model.add(LocallyConnected2D(16, (7, 7), strides=2, activation='relu', name='L5') )
|
||||
base_model.add(LocallyConnected2D(16, (5, 5), activation='relu', name='L6'))
|
||||
base_model.add(Flatten(name='F0'))
|
||||
base_model.add(Dense(4096, activation='relu', name='F7'))
|
||||
base_model.add(Dropout(rate=0.5, name='D0'))
|
||||
base_model.add(Dense(8631, activation='softmax', name='F8'))
|
||||
|
||||
#---------------------------------
|
||||
|
||||
home = str(Path.home())
|
||||
|
||||
if os.path.isfile(home+'/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5') != True:
|
||||
print("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")
|
||||
|
||||
url = 'https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip'
|
||||
|
||||
output = home+'/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip'
|
||||
|
||||
gdown.download(url, output, quiet=False)
|
||||
|
||||
#unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
|
||||
with zipfile.ZipFile(output, 'r') as zip_ref:
|
||||
zip_ref.extractall(home+'/.deepface/weights/')
|
||||
|
||||
base_model.load_weights(home+'/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5')
|
||||
|
||||
#drop F8 and D0. F7 is the representation layer.
|
||||
deepface_model = Model(inputs=base_model.layers[0].input, outputs=base_model.layers[-3].output)
|
||||
|
||||
return deepface_model
|
||||
|
||||
def get_base_model():
|
||||
base_model = Sequential()
|
||||
base_model.add(
|
||||
Convolution2D(
|
||||
32, (11, 11), activation="relu", name="C1", input_shape=(152, 152, 3)
|
||||
)
|
||||
)
|
||||
base_model.add(MaxPooling2D(pool_size=3, strides=2, padding="same", name="M2"))
|
||||
base_model.add(Convolution2D(16, (9, 9), activation="relu", name="C3"))
|
||||
base_model.add(LocallyConnected2D(16, (9, 9), activation="relu", name="L4"))
|
||||
base_model.add(
|
||||
LocallyConnected2D(16, (7, 7), strides=2, activation="relu", name="L5")
|
||||
)
|
||||
base_model.add(LocallyConnected2D(16, (5, 5), activation="relu", name="L6"))
|
||||
base_model.add(Flatten(name="F0"))
|
||||
base_model.add(Dense(4096, activation="relu", name="F7"))
|
||||
base_model.add(Dropout(rate=0.5, name="D0"))
|
||||
base_model.add(Dense(8631, activation="softmax", name="F8"))
|
||||
return base_model
|
||||
|
||||
|
||||
def loadModel(model_path=""):
|
||||
# ---------------------------------
|
||||
if model_path:
|
||||
assert Path(model_path).exists()
|
||||
assert model_path.endswith(".h5")
|
||||
else:
|
||||
home = Path.home().as_posix()
|
||||
model_path = os.path.join(
|
||||
home, ".deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5"
|
||||
)
|
||||
if not os.path.isfile(model_path):
|
||||
print("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")
|
||||
|
||||
url = "https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
|
||||
|
||||
zip_path = os.path.join(
|
||||
home, ".deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
|
||||
)
|
||||
gdown.download(url, zip_path, quiet=False)
|
||||
|
||||
# unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
|
||||
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
||||
zip_ref.extractall(os.path.join(home, "/.deepface/weights/"))
|
||||
|
||||
print(f"Loading model from {model_path}")
|
||||
base_model = get_base_model()
|
||||
base_model.load_weights(model_path)
|
||||
|
||||
# drop F8 and D0. F7 is the representation layer.
|
||||
deepface_model = Model(
|
||||
inputs=base_model.layers[0].input, outputs=base_model.layers[-3].output
|
||||
)
|
||||
|
||||
return deepface_model
|
||||
|
@ -13,238 +13,397 @@ from keras.layers.normalization import BatchNormalization
|
||||
from keras.models import load_model
|
||||
from keras import backend as K
|
||||
|
||||
#---------------------------------------
|
||||
# ---------------------------------------
|
||||
|
||||
def loadModel():
|
||||
myInput = Input(shape=(96, 96, 3))
|
||||
|
||||
x = ZeroPadding2D(padding=(3, 3), input_shape=(96, 96, 3))(myInput)
|
||||
x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name='bn1')(x)
|
||||
x = Activation('relu')(x)
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_1')(x)
|
||||
x = Conv2D(64, (1, 1), name='conv2')(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name='bn2')(x)
|
||||
x = Activation('relu')(x)
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = Conv2D(192, (3, 3), name='conv3')(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name='bn3')(x)
|
||||
x = Activation('relu')(x)
|
||||
x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_2')(x) #x is equal added
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
def get_base_model():
|
||||
myInput = Input(shape=(96, 96, 3))
|
||||
|
||||
# Inception3a
|
||||
inception_3a_3x3 = Conv2D(96, (1, 1), name='inception_3a_3x3_conv1')(x)
|
||||
inception_3a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_3x3_bn1')(inception_3a_3x3)
|
||||
inception_3a_3x3 = Activation('relu')(inception_3a_3x3)
|
||||
inception_3a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3a_3x3)
|
||||
inception_3a_3x3 = Conv2D(128, (3, 3), name='inception_3a_3x3_conv2')(inception_3a_3x3)
|
||||
inception_3a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_3x3_bn2')(inception_3a_3x3)
|
||||
inception_3a_3x3 = Activation('relu')(inception_3a_3x3)
|
||||
x = ZeroPadding2D(padding=(3, 3), input_shape=(96, 96, 3))(myInput)
|
||||
x = Conv2D(64, (7, 7), strides=(2, 2), name="conv1")(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name="bn1")(x)
|
||||
x = Activation("relu")(x)
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name="lrn_1")(x)
|
||||
x = Conv2D(64, (1, 1), name="conv2")(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name="bn2")(x)
|
||||
x = Activation("relu")(x)
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = Conv2D(192, (3, 3), name="conv3")(x)
|
||||
x = BatchNormalization(axis=3, epsilon=0.00001, name="bn3")(x)
|
||||
x = Activation("relu")(x)
|
||||
x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name="lrn_2")(
|
||||
x
|
||||
) # x is equal added
|
||||
x = ZeroPadding2D(padding=(1, 1))(x)
|
||||
x = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
|
||||
inception_3a_5x5 = Conv2D(16, (1, 1), name='inception_3a_5x5_conv1')(x)
|
||||
inception_3a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_5x5_bn1')(inception_3a_5x5)
|
||||
inception_3a_5x5 = Activation('relu')(inception_3a_5x5)
|
||||
inception_3a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3a_5x5)
|
||||
inception_3a_5x5 = Conv2D(32, (5, 5), name='inception_3a_5x5_conv2')(inception_3a_5x5)
|
||||
inception_3a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_5x5_bn2')(inception_3a_5x5)
|
||||
inception_3a_5x5 = Activation('relu')(inception_3a_5x5)
|
||||
# Inception3a
|
||||
inception_3a_3x3 = Conv2D(96, (1, 1), name="inception_3a_3x3_conv1")(x)
|
||||
inception_3a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_3x3_bn1"
|
||||
)(inception_3a_3x3)
|
||||
inception_3a_3x3 = Activation("relu")(inception_3a_3x3)
|
||||
inception_3a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3a_3x3)
|
||||
inception_3a_3x3 = Conv2D(128, (3, 3), name="inception_3a_3x3_conv2")(
|
||||
inception_3a_3x3
|
||||
)
|
||||
inception_3a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_3x3_bn2"
|
||||
)(inception_3a_3x3)
|
||||
inception_3a_3x3 = Activation("relu")(inception_3a_3x3)
|
||||
|
||||
inception_3a_pool = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
inception_3a_pool = Conv2D(32, (1, 1), name='inception_3a_pool_conv')(inception_3a_pool)
|
||||
inception_3a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_pool_bn')(inception_3a_pool)
|
||||
inception_3a_pool = Activation('relu')(inception_3a_pool)
|
||||
inception_3a_pool = ZeroPadding2D(padding=((3, 4), (3, 4)))(inception_3a_pool)
|
||||
inception_3a_5x5 = Conv2D(16, (1, 1), name="inception_3a_5x5_conv1")(x)
|
||||
inception_3a_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_5x5_bn1"
|
||||
)(inception_3a_5x5)
|
||||
inception_3a_5x5 = Activation("relu")(inception_3a_5x5)
|
||||
inception_3a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3a_5x5)
|
||||
inception_3a_5x5 = Conv2D(32, (5, 5), name="inception_3a_5x5_conv2")(
|
||||
inception_3a_5x5
|
||||
)
|
||||
inception_3a_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_5x5_bn2"
|
||||
)(inception_3a_5x5)
|
||||
inception_3a_5x5 = Activation("relu")(inception_3a_5x5)
|
||||
|
||||
inception_3a_1x1 = Conv2D(64, (1, 1), name='inception_3a_1x1_conv')(x)
|
||||
inception_3a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_1x1_bn')(inception_3a_1x1)
|
||||
inception_3a_1x1 = Activation('relu')(inception_3a_1x1)
|
||||
inception_3a_pool = MaxPooling2D(pool_size=3, strides=2)(x)
|
||||
inception_3a_pool = Conv2D(32, (1, 1), name="inception_3a_pool_conv")(
|
||||
inception_3a_pool
|
||||
)
|
||||
inception_3a_pool = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_pool_bn"
|
||||
)(inception_3a_pool)
|
||||
inception_3a_pool = Activation("relu")(inception_3a_pool)
|
||||
inception_3a_pool = ZeroPadding2D(padding=((3, 4), (3, 4)))(inception_3a_pool)
|
||||
|
||||
inception_3a = concatenate([inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1], axis=3)
|
||||
inception_3a_1x1 = Conv2D(64, (1, 1), name="inception_3a_1x1_conv")(x)
|
||||
inception_3a_1x1 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3a_1x1_bn"
|
||||
)(inception_3a_1x1)
|
||||
inception_3a_1x1 = Activation("relu")(inception_3a_1x1)
|
||||
|
||||
# Inception3b
|
||||
inception_3b_3x3 = Conv2D(96, (1, 1), name='inception_3b_3x3_conv1')(inception_3a)
|
||||
inception_3b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_3x3_bn1')(inception_3b_3x3)
|
||||
inception_3b_3x3 = Activation('relu')(inception_3b_3x3)
|
||||
inception_3b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3b_3x3)
|
||||
inception_3b_3x3 = Conv2D(128, (3, 3), name='inception_3b_3x3_conv2')(inception_3b_3x3)
|
||||
inception_3b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_3x3_bn2')(inception_3b_3x3)
|
||||
inception_3b_3x3 = Activation('relu')(inception_3b_3x3)
|
||||
inception_3a = concatenate(
|
||||
[inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1],
|
||||
axis=3,
|
||||
)
|
||||
|
||||
inception_3b_5x5 = Conv2D(32, (1, 1), name='inception_3b_5x5_conv1')(inception_3a)
|
||||
inception_3b_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_5x5_bn1')(inception_3b_5x5)
|
||||
inception_3b_5x5 = Activation('relu')(inception_3b_5x5)
|
||||
inception_3b_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3b_5x5)
|
||||
inception_3b_5x5 = Conv2D(64, (5, 5), name='inception_3b_5x5_conv2')(inception_3b_5x5)
|
||||
inception_3b_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_5x5_bn2')(inception_3b_5x5)
|
||||
inception_3b_5x5 = Activation('relu')(inception_3b_5x5)
|
||||
# Inception3b
|
||||
inception_3b_3x3 = Conv2D(96, (1, 1), name="inception_3b_3x3_conv1")(inception_3a)
|
||||
inception_3b_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_3x3_bn1"
|
||||
)(inception_3b_3x3)
|
||||
inception_3b_3x3 = Activation("relu")(inception_3b_3x3)
|
||||
inception_3b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3b_3x3)
|
||||
inception_3b_3x3 = Conv2D(128, (3, 3), name="inception_3b_3x3_conv2")(
|
||||
inception_3b_3x3
|
||||
)
|
||||
inception_3b_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_3x3_bn2"
|
||||
)(inception_3b_3x3)
|
||||
inception_3b_3x3 = Activation("relu")(inception_3b_3x3)
|
||||
|
||||
inception_3b_pool = Lambda(lambda x: x**2, name='power2_3b')(inception_3a)
|
||||
inception_3b_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_3b_pool)
|
||||
inception_3b_pool = Lambda(lambda x: x*9, name='mult9_3b')(inception_3b_pool)
|
||||
inception_3b_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_3b')(inception_3b_pool)
|
||||
inception_3b_pool = Conv2D(64, (1, 1), name='inception_3b_pool_conv')(inception_3b_pool)
|
||||
inception_3b_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_pool_bn')(inception_3b_pool)
|
||||
inception_3b_pool = Activation('relu')(inception_3b_pool)
|
||||
inception_3b_pool = ZeroPadding2D(padding=(4, 4))(inception_3b_pool)
|
||||
inception_3b_5x5 = Conv2D(32, (1, 1), name="inception_3b_5x5_conv1")(inception_3a)
|
||||
inception_3b_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_5x5_bn1"
|
||||
)(inception_3b_5x5)
|
||||
inception_3b_5x5 = Activation("relu")(inception_3b_5x5)
|
||||
inception_3b_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3b_5x5)
|
||||
inception_3b_5x5 = Conv2D(64, (5, 5), name="inception_3b_5x5_conv2")(
|
||||
inception_3b_5x5
|
||||
)
|
||||
inception_3b_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_5x5_bn2"
|
||||
)(inception_3b_5x5)
|
||||
inception_3b_5x5 = Activation("relu")(inception_3b_5x5)
|
||||
|
||||
inception_3b_1x1 = Conv2D(64, (1, 1), name='inception_3b_1x1_conv')(inception_3a)
|
||||
inception_3b_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_1x1_bn')(inception_3b_1x1)
|
||||
inception_3b_1x1 = Activation('relu')(inception_3b_1x1)
|
||||
inception_3b_pool = Lambda(lambda x: x ** 2, name="power2_3b")(inception_3a)
|
||||
inception_3b_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(
|
||||
inception_3b_pool
|
||||
)
|
||||
inception_3b_pool = Lambda(lambda x: x * 9, name="mult9_3b")(inception_3b_pool)
|
||||
inception_3b_pool = Lambda(lambda x: K.sqrt(x), name="sqrt_3b")(inception_3b_pool)
|
||||
inception_3b_pool = Conv2D(64, (1, 1), name="inception_3b_pool_conv")(
|
||||
inception_3b_pool
|
||||
)
|
||||
inception_3b_pool = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_pool_bn"
|
||||
)(inception_3b_pool)
|
||||
inception_3b_pool = Activation("relu")(inception_3b_pool)
|
||||
inception_3b_pool = ZeroPadding2D(padding=(4, 4))(inception_3b_pool)
|
||||
|
||||
inception_3b = concatenate([inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1], axis=3)
|
||||
inception_3b_1x1 = Conv2D(64, (1, 1), name="inception_3b_1x1_conv")(inception_3a)
|
||||
inception_3b_1x1 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3b_1x1_bn"
|
||||
)(inception_3b_1x1)
|
||||
inception_3b_1x1 = Activation("relu")(inception_3b_1x1)
|
||||
|
||||
# Inception3c
|
||||
inception_3c_3x3 = Conv2D(128, (1, 1), strides=(1, 1), name='inception_3c_3x3_conv1')(inception_3b)
|
||||
inception_3c_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_3x3_bn1')(inception_3c_3x3)
|
||||
inception_3c_3x3 = Activation('relu')(inception_3c_3x3)
|
||||
inception_3c_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3c_3x3)
|
||||
inception_3c_3x3 = Conv2D(256, (3, 3), strides=(2, 2), name='inception_3c_3x3_conv'+'2')(inception_3c_3x3)
|
||||
inception_3c_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_3x3_bn'+'2')(inception_3c_3x3)
|
||||
inception_3c_3x3 = Activation('relu')(inception_3c_3x3)
|
||||
inception_3b = concatenate(
|
||||
[inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1],
|
||||
axis=3,
|
||||
)
|
||||
|
||||
inception_3c_5x5 = Conv2D(32, (1, 1), strides=(1, 1), name='inception_3c_5x5_conv1')(inception_3b)
|
||||
inception_3c_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_5x5_bn1')(inception_3c_5x5)
|
||||
inception_3c_5x5 = Activation('relu')(inception_3c_5x5)
|
||||
inception_3c_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3c_5x5)
|
||||
inception_3c_5x5 = Conv2D(64, (5, 5), strides=(2, 2), name='inception_3c_5x5_conv'+'2')(inception_3c_5x5)
|
||||
inception_3c_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3c_5x5_bn'+'2')(inception_3c_5x5)
|
||||
inception_3c_5x5 = Activation('relu')(inception_3c_5x5)
|
||||
# Inception3c
|
||||
inception_3c_3x3 = Conv2D(
|
||||
128, (1, 1), strides=(1, 1), name="inception_3c_3x3_conv1"
|
||||
)(inception_3b)
|
||||
inception_3c_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3c_3x3_bn1"
|
||||
)(inception_3c_3x3)
|
||||
inception_3c_3x3 = Activation("relu")(inception_3c_3x3)
|
||||
inception_3c_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3c_3x3)
|
||||
inception_3c_3x3 = Conv2D(
|
||||
256, (3, 3), strides=(2, 2), name="inception_3c_3x3_conv" + "2"
|
||||
)(inception_3c_3x3)
|
||||
inception_3c_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3c_3x3_bn" + "2"
|
||||
)(inception_3c_3x3)
|
||||
inception_3c_3x3 = Activation("relu")(inception_3c_3x3)
|
||||
|
||||
inception_3c_pool = MaxPooling2D(pool_size=3, strides=2)(inception_3b)
|
||||
inception_3c_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_3c_pool)
|
||||
inception_3c_5x5 = Conv2D(
|
||||
32, (1, 1), strides=(1, 1), name="inception_3c_5x5_conv1"
|
||||
)(inception_3b)
|
||||
inception_3c_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3c_5x5_bn1"
|
||||
)(inception_3c_5x5)
|
||||
inception_3c_5x5 = Activation("relu")(inception_3c_5x5)
|
||||
inception_3c_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3c_5x5)
|
||||
inception_3c_5x5 = Conv2D(
|
||||
64, (5, 5), strides=(2, 2), name="inception_3c_5x5_conv" + "2"
|
||||
)(inception_3c_5x5)
|
||||
inception_3c_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_3c_5x5_bn" + "2"
|
||||
)(inception_3c_5x5)
|
||||
inception_3c_5x5 = Activation("relu")(inception_3c_5x5)
|
||||
|
||||
inception_3c = concatenate([inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3)
|
||||
inception_3c_pool = MaxPooling2D(pool_size=3, strides=2)(inception_3b)
|
||||
inception_3c_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_3c_pool)
|
||||
|
||||
#inception 4a
|
||||
inception_4a_3x3 = Conv2D(96, (1, 1), strides=(1, 1), name='inception_4a_3x3_conv'+'1')(inception_3c)
|
||||
inception_4a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_3x3_bn'+'1')(inception_4a_3x3)
|
||||
inception_4a_3x3 = Activation('relu')(inception_4a_3x3)
|
||||
inception_4a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4a_3x3)
|
||||
inception_4a_3x3 = Conv2D(192, (3, 3), strides=(1, 1), name='inception_4a_3x3_conv'+'2')(inception_4a_3x3)
|
||||
inception_4a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_3x3_bn'+'2')(inception_4a_3x3)
|
||||
inception_4a_3x3 = Activation('relu')(inception_4a_3x3)
|
||||
inception_3c = concatenate(
|
||||
[inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3
|
||||
)
|
||||
|
||||
inception_4a_5x5 = Conv2D(32, (1,1), strides=(1,1), name='inception_4a_5x5_conv1')(inception_3c)
|
||||
inception_4a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_5x5_bn1')(inception_4a_5x5)
|
||||
inception_4a_5x5 = Activation('relu')(inception_4a_5x5)
|
||||
inception_4a_5x5 = ZeroPadding2D(padding=(2,2))(inception_4a_5x5)
|
||||
inception_4a_5x5 = Conv2D(64, (5,5), strides=(1,1), name='inception_4a_5x5_conv'+'2')(inception_4a_5x5)
|
||||
inception_4a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_5x5_bn'+'2')(inception_4a_5x5)
|
||||
inception_4a_5x5 = Activation('relu')(inception_4a_5x5)
|
||||
# inception 4a
|
||||
inception_4a_3x3 = Conv2D(
|
||||
96, (1, 1), strides=(1, 1), name="inception_4a_3x3_conv" + "1"
|
||||
)(inception_3c)
|
||||
inception_4a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_3x3_bn" + "1"
|
||||
)(inception_4a_3x3)
|
||||
inception_4a_3x3 = Activation("relu")(inception_4a_3x3)
|
||||
inception_4a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4a_3x3)
|
||||
inception_4a_3x3 = Conv2D(
|
||||
192, (3, 3), strides=(1, 1), name="inception_4a_3x3_conv" + "2"
|
||||
)(inception_4a_3x3)
|
||||
inception_4a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_3x3_bn" + "2"
|
||||
)(inception_4a_3x3)
|
||||
inception_4a_3x3 = Activation("relu")(inception_4a_3x3)
|
||||
|
||||
inception_4a_pool = Lambda(lambda x: x**2, name='power2_4a')(inception_3c)
|
||||
inception_4a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_4a_pool)
|
||||
inception_4a_pool = Lambda(lambda x: x*9, name='mult9_4a')(inception_4a_pool)
|
||||
inception_4a_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_4a')(inception_4a_pool)
|
||||
inception_4a_5x5 = Conv2D(
|
||||
32, (1, 1), strides=(1, 1), name="inception_4a_5x5_conv1"
|
||||
)(inception_3c)
|
||||
inception_4a_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_5x5_bn1"
|
||||
)(inception_4a_5x5)
|
||||
inception_4a_5x5 = Activation("relu")(inception_4a_5x5)
|
||||
inception_4a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4a_5x5)
|
||||
inception_4a_5x5 = Conv2D(
|
||||
64, (5, 5), strides=(1, 1), name="inception_4a_5x5_conv" + "2"
|
||||
)(inception_4a_5x5)
|
||||
inception_4a_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_5x5_bn" + "2"
|
||||
)(inception_4a_5x5)
|
||||
inception_4a_5x5 = Activation("relu")(inception_4a_5x5)
|
||||
|
||||
inception_4a_pool = Conv2D(128, (1,1), strides=(1,1), name='inception_4a_pool_conv'+'')(inception_4a_pool)
|
||||
inception_4a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_pool_bn'+'')(inception_4a_pool)
|
||||
inception_4a_pool = Activation('relu')(inception_4a_pool)
|
||||
inception_4a_pool = ZeroPadding2D(padding=(2, 2))(inception_4a_pool)
|
||||
inception_4a_pool = Lambda(lambda x: x ** 2, name="power2_4a")(inception_3c)
|
||||
inception_4a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(
|
||||
inception_4a_pool
|
||||
)
|
||||
inception_4a_pool = Lambda(lambda x: x * 9, name="mult9_4a")(inception_4a_pool)
|
||||
inception_4a_pool = Lambda(lambda x: K.sqrt(x), name="sqrt_4a")(inception_4a_pool)
|
||||
|
||||
inception_4a_1x1 = Conv2D(256, (1, 1), strides=(1, 1), name='inception_4a_1x1_conv'+'')(inception_3c)
|
||||
inception_4a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4a_1x1_bn'+'')(inception_4a_1x1)
|
||||
inception_4a_1x1 = Activation('relu')(inception_4a_1x1)
|
||||
inception_4a_pool = Conv2D(
|
||||
128, (1, 1), strides=(1, 1), name="inception_4a_pool_conv" + ""
|
||||
)(inception_4a_pool)
|
||||
inception_4a_pool = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_pool_bn" + ""
|
||||
)(inception_4a_pool)
|
||||
inception_4a_pool = Activation("relu")(inception_4a_pool)
|
||||
inception_4a_pool = ZeroPadding2D(padding=(2, 2))(inception_4a_pool)
|
||||
|
||||
inception_4a = concatenate([inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1], axis=3)
|
||||
inception_4a_1x1 = Conv2D(
|
||||
256, (1, 1), strides=(1, 1), name="inception_4a_1x1_conv" + ""
|
||||
)(inception_3c)
|
||||
inception_4a_1x1 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4a_1x1_bn" + ""
|
||||
)(inception_4a_1x1)
|
||||
inception_4a_1x1 = Activation("relu")(inception_4a_1x1)
|
||||
|
||||
#inception4e
|
||||
inception_4e_3x3 = Conv2D(160, (1,1), strides=(1,1), name='inception_4e_3x3_conv'+'1')(inception_4a)
|
||||
inception_4e_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_3x3_bn'+'1')(inception_4e_3x3)
|
||||
inception_4e_3x3 = Activation('relu')(inception_4e_3x3)
|
||||
inception_4e_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4e_3x3)
|
||||
inception_4e_3x3 = Conv2D(256, (3,3), strides=(2,2), name='inception_4e_3x3_conv'+'2')(inception_4e_3x3)
|
||||
inception_4e_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_3x3_bn'+'2')(inception_4e_3x3)
|
||||
inception_4e_3x3 = Activation('relu')(inception_4e_3x3)
|
||||
inception_4a = concatenate(
|
||||
[inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1],
|
||||
axis=3,
|
||||
)
|
||||
|
||||
inception_4e_5x5 = Conv2D(64, (1,1), strides=(1,1), name='inception_4e_5x5_conv'+'1')(inception_4a)
|
||||
inception_4e_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_5x5_bn'+'1')(inception_4e_5x5)
|
||||
inception_4e_5x5 = Activation('relu')(inception_4e_5x5)
|
||||
inception_4e_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4e_5x5)
|
||||
inception_4e_5x5 = Conv2D(128, (5,5), strides=(2,2), name='inception_4e_5x5_conv'+'2')(inception_4e_5x5)
|
||||
inception_4e_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_4e_5x5_bn'+'2')(inception_4e_5x5)
|
||||
inception_4e_5x5 = Activation('relu')(inception_4e_5x5)
|
||||
# inception4e
|
||||
inception_4e_3x3 = Conv2D(
|
||||
160, (1, 1), strides=(1, 1), name="inception_4e_3x3_conv" + "1"
|
||||
)(inception_4a)
|
||||
inception_4e_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4e_3x3_bn" + "1"
|
||||
)(inception_4e_3x3)
|
||||
inception_4e_3x3 = Activation("relu")(inception_4e_3x3)
|
||||
inception_4e_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4e_3x3)
|
||||
inception_4e_3x3 = Conv2D(
|
||||
256, (3, 3), strides=(2, 2), name="inception_4e_3x3_conv" + "2"
|
||||
)(inception_4e_3x3)
|
||||
inception_4e_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4e_3x3_bn" + "2"
|
||||
)(inception_4e_3x3)
|
||||
inception_4e_3x3 = Activation("relu")(inception_4e_3x3)
|
||||
|
||||
inception_4e_pool = MaxPooling2D(pool_size=3, strides=2)(inception_4a)
|
||||
inception_4e_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_4e_pool)
|
||||
inception_4e_5x5 = Conv2D(
|
||||
64, (1, 1), strides=(1, 1), name="inception_4e_5x5_conv" + "1"
|
||||
)(inception_4a)
|
||||
inception_4e_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4e_5x5_bn" + "1"
|
||||
)(inception_4e_5x5)
|
||||
inception_4e_5x5 = Activation("relu")(inception_4e_5x5)
|
||||
inception_4e_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4e_5x5)
|
||||
inception_4e_5x5 = Conv2D(
|
||||
128, (5, 5), strides=(2, 2), name="inception_4e_5x5_conv" + "2"
|
||||
)(inception_4e_5x5)
|
||||
inception_4e_5x5 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_4e_5x5_bn" + "2"
|
||||
)(inception_4e_5x5)
|
||||
inception_4e_5x5 = Activation("relu")(inception_4e_5x5)
|
||||
|
||||
inception_4e = concatenate([inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3)
|
||||
inception_4e_pool = MaxPooling2D(pool_size=3, strides=2)(inception_4a)
|
||||
inception_4e_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_4e_pool)
|
||||
|
||||
#inception5a
|
||||
inception_5a_3x3 = Conv2D(96, (1,1), strides=(1,1), name='inception_5a_3x3_conv'+'1')(inception_4e)
|
||||
inception_5a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_3x3_bn'+'1')(inception_5a_3x3)
|
||||
inception_5a_3x3 = Activation('relu')(inception_5a_3x3)
|
||||
inception_5a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5a_3x3)
|
||||
inception_5a_3x3 = Conv2D(384, (3,3), strides=(1,1), name='inception_5a_3x3_conv'+'2')(inception_5a_3x3)
|
||||
inception_5a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_3x3_bn'+'2')(inception_5a_3x3)
|
||||
inception_5a_3x3 = Activation('relu')(inception_5a_3x3)
|
||||
inception_4e = concatenate(
|
||||
[inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3
|
||||
)
|
||||
|
||||
inception_5a_pool = Lambda(lambda x: x**2, name='power2_5a')(inception_4e)
|
||||
inception_5a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_5a_pool)
|
||||
inception_5a_pool = Lambda(lambda x: x*9, name='mult9_5a')(inception_5a_pool)
|
||||
inception_5a_pool = Lambda(lambda x: K.sqrt(x), name='sqrt_5a')(inception_5a_pool)
|
||||
# inception5a
|
||||
inception_5a_3x3 = Conv2D(
|
||||
96, (1, 1), strides=(1, 1), name="inception_5a_3x3_conv" + "1"
|
||||
)(inception_4e)
|
||||
inception_5a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5a_3x3_bn" + "1"
|
||||
)(inception_5a_3x3)
|
||||
inception_5a_3x3 = Activation("relu")(inception_5a_3x3)
|
||||
inception_5a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5a_3x3)
|
||||
inception_5a_3x3 = Conv2D(
|
||||
384, (3, 3), strides=(1, 1), name="inception_5a_3x3_conv" + "2"
|
||||
)(inception_5a_3x3)
|
||||
inception_5a_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5a_3x3_bn" + "2"
|
||||
)(inception_5a_3x3)
|
||||
inception_5a_3x3 = Activation("relu")(inception_5a_3x3)
|
||||
|
||||
inception_5a_pool = Conv2D(96, (1,1), strides=(1,1), name='inception_5a_pool_conv'+'')(inception_5a_pool)
|
||||
inception_5a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_pool_bn'+'')(inception_5a_pool)
|
||||
inception_5a_pool = Activation('relu')(inception_5a_pool)
|
||||
inception_5a_pool = ZeroPadding2D(padding=(1,1))(inception_5a_pool)
|
||||
inception_5a_pool = Lambda(lambda x: x ** 2, name="power2_5a")(inception_4e)
|
||||
inception_5a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(
|
||||
inception_5a_pool
|
||||
)
|
||||
inception_5a_pool = Lambda(lambda x: x * 9, name="mult9_5a")(inception_5a_pool)
|
||||
inception_5a_pool = Lambda(lambda x: K.sqrt(x), name="sqrt_5a")(inception_5a_pool)
|
||||
|
||||
inception_5a_1x1 = Conv2D(256, (1,1), strides=(1,1), name='inception_5a_1x1_conv'+'')(inception_4e)
|
||||
inception_5a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5a_1x1_bn'+'')(inception_5a_1x1)
|
||||
inception_5a_1x1 = Activation('relu')(inception_5a_1x1)
|
||||
inception_5a_pool = Conv2D(
|
||||
96, (1, 1), strides=(1, 1), name="inception_5a_pool_conv" + ""
|
||||
)(inception_5a_pool)
|
||||
inception_5a_pool = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5a_pool_bn" + ""
|
||||
)(inception_5a_pool)
|
||||
inception_5a_pool = Activation("relu")(inception_5a_pool)
|
||||
inception_5a_pool = ZeroPadding2D(padding=(1, 1))(inception_5a_pool)
|
||||
|
||||
inception_5a = concatenate([inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3)
|
||||
inception_5a_1x1 = Conv2D(
|
||||
256, (1, 1), strides=(1, 1), name="inception_5a_1x1_conv" + ""
|
||||
)(inception_4e)
|
||||
inception_5a_1x1 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5a_1x1_bn" + ""
|
||||
)(inception_5a_1x1)
|
||||
inception_5a_1x1 = Activation("relu")(inception_5a_1x1)
|
||||
|
||||
#inception_5b
|
||||
inception_5b_3x3 = Conv2D(96, (1,1), strides=(1,1), name='inception_5b_3x3_conv'+'1')(inception_5a)
|
||||
inception_5b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_3x3_bn'+'1')(inception_5b_3x3)
|
||||
inception_5b_3x3 = Activation('relu')(inception_5b_3x3)
|
||||
inception_5b_3x3 = ZeroPadding2D(padding=(1,1))(inception_5b_3x3)
|
||||
inception_5b_3x3 = Conv2D(384, (3,3), strides=(1,1), name='inception_5b_3x3_conv'+'2')(inception_5b_3x3)
|
||||
inception_5b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_3x3_bn'+'2')(inception_5b_3x3)
|
||||
inception_5b_3x3 = Activation('relu')(inception_5b_3x3)
|
||||
inception_5a = concatenate(
|
||||
[inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3
|
||||
)
|
||||
|
||||
inception_5b_pool = MaxPooling2D(pool_size=3, strides=2)(inception_5a)
|
||||
# inception_5b
|
||||
inception_5b_3x3 = Conv2D(
|
||||
96, (1, 1), strides=(1, 1), name="inception_5b_3x3_conv" + "1"
|
||||
)(inception_5a)
|
||||
inception_5b_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5b_3x3_bn" + "1"
|
||||
)(inception_5b_3x3)
|
||||
inception_5b_3x3 = Activation("relu")(inception_5b_3x3)
|
||||
inception_5b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5b_3x3)
|
||||
inception_5b_3x3 = Conv2D(
|
||||
384, (3, 3), strides=(1, 1), name="inception_5b_3x3_conv" + "2"
|
||||
)(inception_5b_3x3)
|
||||
inception_5b_3x3 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5b_3x3_bn" + "2"
|
||||
)(inception_5b_3x3)
|
||||
inception_5b_3x3 = Activation("relu")(inception_5b_3x3)
|
||||
|
||||
inception_5b_pool = Conv2D(96, (1,1), strides=(1,1), name='inception_5b_pool_conv'+'')(inception_5b_pool)
|
||||
inception_5b_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_pool_bn'+'')(inception_5b_pool)
|
||||
inception_5b_pool = Activation('relu')(inception_5b_pool)
|
||||
inception_5b_pool = MaxPooling2D(pool_size=3, strides=2)(inception_5a)
|
||||
|
||||
inception_5b_pool = ZeroPadding2D(padding=(1, 1))(inception_5b_pool)
|
||||
inception_5b_pool = Conv2D(
|
||||
96, (1, 1), strides=(1, 1), name="inception_5b_pool_conv" + ""
|
||||
)(inception_5b_pool)
|
||||
inception_5b_pool = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5b_pool_bn" + ""
|
||||
)(inception_5b_pool)
|
||||
inception_5b_pool = Activation("relu")(inception_5b_pool)
|
||||
|
||||
inception_5b_1x1 = Conv2D(256, (1,1), strides=(1,1), name='inception_5b_1x1_conv'+'')(inception_5a)
|
||||
inception_5b_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_5b_1x1_bn'+'')(inception_5b_1x1)
|
||||
inception_5b_1x1 = Activation('relu')(inception_5b_1x1)
|
||||
inception_5b_pool = ZeroPadding2D(padding=(1, 1))(inception_5b_pool)
|
||||
|
||||
inception_5b = concatenate([inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3)
|
||||
inception_5b_1x1 = Conv2D(
|
||||
256, (1, 1), strides=(1, 1), name="inception_5b_1x1_conv" + ""
|
||||
)(inception_5a)
|
||||
inception_5b_1x1 = BatchNormalization(
|
||||
axis=3, epsilon=0.00001, name="inception_5b_1x1_bn" + ""
|
||||
)(inception_5b_1x1)
|
||||
inception_5b_1x1 = Activation("relu")(inception_5b_1x1)
|
||||
|
||||
av_pool = AveragePooling2D(pool_size=(3, 3), strides=(1, 1))(inception_5b)
|
||||
reshape_layer = Flatten()(av_pool)
|
||||
dense_layer = Dense(128, name='dense_layer')(reshape_layer)
|
||||
norm_layer = Lambda(lambda x: K.l2_normalize(x, axis=1), name='norm_layer')(dense_layer)
|
||||
inception_5b = concatenate(
|
||||
[inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3
|
||||
)
|
||||
|
||||
# Final Model
|
||||
model = Model(inputs=[myInput], outputs=norm_layer)
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
home = str(Path.home())
|
||||
|
||||
if os.path.isfile(home+'/.deepface/weights/openface_weights.h5') != True:
|
||||
print("openface_weights.h5 will be downloaded...")
|
||||
|
||||
url = 'https://drive.google.com/uc?id=1LSe1YCV1x-BfNnfb7DFZTNpv_Q9jITxn'
|
||||
output = home+'/.deepface/weights/openface_weights.h5'
|
||||
gdown.download(url, output, quiet=False)
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
model.load_weights(home+'/.deepface/weights/openface_weights.h5')
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
return model
|
||||
av_pool = AveragePooling2D(pool_size=(3, 3), strides=(1, 1))(inception_5b)
|
||||
reshape_layer = Flatten()(av_pool)
|
||||
dense_layer = Dense(128, name="dense_layer")(reshape_layer)
|
||||
norm_layer = Lambda(lambda x: K.l2_normalize(x, axis=1), name="norm_layer")(
|
||||
dense_layer
|
||||
)
|
||||
|
||||
# Final Model
|
||||
return Model(inputs=[myInput], outputs=norm_layer)
|
||||
|
||||
|
||||
def loadModel(model_path=""):
|
||||
# -----------------------------------
|
||||
if model_path:
|
||||
assert Path(model_path).exists()
|
||||
assert model_path.endswith(".h5")
|
||||
|
||||
else:
|
||||
home = Path.home().as_posix()
|
||||
model_path = home + "/.deepface/weights/openface_weights.h5"
|
||||
if not os.path.isfile(model_path):
|
||||
print(f"openface_weights.h5 will be downloaded to {model_path}")
|
||||
|
||||
url = "https://drive.google.com/uc?id=1LSe1YCV1x-BfNnfb7DFZTNpv_Q9jITxn"
|
||||
gdown.download(url, model_path, quiet=False)
|
||||
|
||||
# -----------------------------------
|
||||
|
||||
print(f"Loading model from {model_path}")
|
||||
model = get_base_model()
|
||||
model.load_weights(model_path)
|
||||
|
||||
# -----------------------------------
|
||||
|
||||
return model
|
||||
|
@ -1,81 +1,99 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from keras.models import Model, Sequential
|
||||
from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout, Activation
|
||||
from keras.layers import (
|
||||
Input,
|
||||
Convolution2D,
|
||||
ZeroPadding2D,
|
||||
MaxPooling2D,
|
||||
Flatten,
|
||||
Dense,
|
||||
Dropout,
|
||||
Activation,
|
||||
)
|
||||
import gdown
|
||||
|
||||
#---------------------------------------
|
||||
# ---------------------------------------
|
||||
|
||||
def baseModel():
|
||||
model = Sequential()
|
||||
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
|
||||
model.add(Convolution2D(64, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(64, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D((2,2), strides=(2,2)))
|
||||
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(128, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(128, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D((2,2), strides=(2,2)))
|
||||
def get_base_model():
|
||||
model = Sequential()
|
||||
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
|
||||
model.add(Convolution2D(64, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(64, (3, 3), activation="relu"))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D((2,2), strides=(2,2)))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(128, (3, 3), activation="relu"))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D((2,2), strides=(2,2)))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(256, (3, 3), activation="relu"))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(ZeroPadding2D((1,1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation='relu'))
|
||||
model.add(MaxPooling2D((2,2), strides=(2,2)))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
model.add(Convolution2D(4096, (7, 7), activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Convolution2D(4096, (1, 1), activation='relu'))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Convolution2D(2622, (1, 1)))
|
||||
model.add(Flatten())
|
||||
model.add(Activation('softmax'))
|
||||
|
||||
return model
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(ZeroPadding2D((1, 1)))
|
||||
model.add(Convolution2D(512, (3, 3), activation="relu"))
|
||||
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
|
||||
|
||||
def loadModel():
|
||||
|
||||
model = baseModel()
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
home = str(Path.home())
|
||||
|
||||
if os.path.isfile(home+'/.deepface/weights/vgg_face_weights.h5') != True:
|
||||
print("vgg_face_weights.h5 will be downloaded...")
|
||||
|
||||
url = 'https://drive.google.com/uc?id=1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo'
|
||||
output = home+'/.deepface/weights/vgg_face_weights.h5'
|
||||
gdown.download(url, output, quiet=False)
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
model.load_weights(home+'/.deepface/weights/vgg_face_weights.h5')
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
#TO-DO: why?
|
||||
vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
|
||||
|
||||
return vgg_face_descriptor
|
||||
model.add(Convolution2D(4096, (7, 7), activation="relu"))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Convolution2D(4096, (1, 1), activation="relu"))
|
||||
model.add(Dropout(0.5))
|
||||
model.add(Convolution2D(2622, (1, 1)))
|
||||
model.add(Flatten())
|
||||
model.add(Activation("softmax"))
|
||||
|
||||
return model
|
||||
|
||||
|
||||
def loadModel(model_path=""):
|
||||
"""
|
||||
Args:
|
||||
model_path: str
|
||||
If provided, this path will be used to load the model from.
|
||||
"""
|
||||
if model_path:
|
||||
assert Path(model_path).exists()
|
||||
assert model_path.endswith(".h5")
|
||||
else:
|
||||
home = Path.home().as_posix()
|
||||
model_path = os.path.join(home, ".deepface/weights/vgg_face_weights.h5")
|
||||
if not os.path.isfile(model_path):
|
||||
print(f"vgg_face_weights.h5 will be downloaded to {model_path}")
|
||||
|
||||
url = "https://drive.google.com/uc?id=1CPSeum3HpopfomUEK1gybeuIVoeJT_Eo"
|
||||
gdown.download(url, model_path, quiet=False)
|
||||
|
||||
# -----------------------------------
|
||||
|
||||
print(f"Loading model from {model_path}")
|
||||
model = get_base_model()
|
||||
model.load_weights(model_path)
|
||||
|
||||
# -----------------------------------
|
||||
|
||||
# TO-DO: why?
|
||||
vgg_face_descriptor = Model(
|
||||
inputs=model.layers[0].input, outputs=model.layers[-2].output
|
||||
)
|
||||
|
||||
return vgg_face_descriptor
|
||||
|
@ -17,46 +17,54 @@ import subprocess
|
||||
import tensorflow as tf
|
||||
import keras
|
||||
|
||||
|
||||
def loadBase64Img(uri):
|
||||
encoded_data = uri.split(',')[1]
|
||||
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
|
||||
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||
return img
|
||||
encoded_data = uri.split(",")[1]
|
||||
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
|
||||
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||
return img
|
||||
|
||||
|
||||
def distance(a, b):
|
||||
x1 = a[0]; y1 = a[1]
|
||||
x2 = b[0]; y2 = b[1]
|
||||
|
||||
return math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)))
|
||||
x1 = a[0]
|
||||
y1 = a[1]
|
||||
x2 = b[0]
|
||||
y2 = b[1]
|
||||
|
||||
return math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)))
|
||||
|
||||
|
||||
def findFileHash(file):
|
||||
BLOCK_SIZE = 65536 # The size of each read from the file
|
||||
BLOCK_SIZE = 65536 # The size of each read from the file
|
||||
|
||||
file_hash = (
|
||||
hashlib.sha256()
|
||||
) # Create the hash object, can use something other than `.sha256()` if you wish
|
||||
with open(file, "rb") as f: # Open the file to read it's bytes
|
||||
fb = f.read(BLOCK_SIZE) # Read from the file. Take in the amount declared above
|
||||
while len(fb) > 0: # While there is still data being read from the file
|
||||
file_hash.update(fb) # Update the hash
|
||||
fb = f.read(BLOCK_SIZE) # Read the next block from the file
|
||||
|
||||
return file_hash.hexdigest()
|
||||
|
||||
file_hash = hashlib.sha256() # Create the hash object, can use something other than `.sha256()` if you wish
|
||||
with open(file, 'rb') as f: # Open the file to read it's bytes
|
||||
fb = f.read(BLOCK_SIZE) # Read from the file. Take in the amount declared above
|
||||
while len(fb) > 0: # While there is still data being read from the file
|
||||
file_hash.update(fb) # Update the hash
|
||||
fb = f.read(BLOCK_SIZE) # Read the next block from the file
|
||||
|
||||
return file_hash.hexdigest()
|
||||
|
||||
def initializeFolder():
|
||||
|
||||
home = str(Path.home())
|
||||
|
||||
if not os.path.exists(home+"/.deepface"):
|
||||
os.mkdir(home+"/.deepface")
|
||||
print("Directory ",home,"/.deepface created")
|
||||
|
||||
if not os.path.exists(home+"/.deepface/weights"):
|
||||
os.mkdir(home+"/.deepface/weights")
|
||||
print("Directory ",home,"/.deepface/weights created")
|
||||
|
||||
#----------------------------------
|
||||
"""
|
||||
|
||||
home = str(Path.home())
|
||||
|
||||
if not os.path.exists(home + "/.deepface"):
|
||||
os.mkdir(home + "/.deepface")
|
||||
print("Directory ", home, "/.deepface created")
|
||||
|
||||
if not os.path.exists(home + "/.deepface/weights"):
|
||||
os.mkdir(home + "/.deepface/weights")
|
||||
print("Directory ", home, "/.deepface/weights created")
|
||||
|
||||
# ----------------------------------
|
||||
"""
|
||||
#avoid interrupted file download
|
||||
|
||||
|
||||
weight_hashes = [
|
||||
['age_model_weights.h5', '0aeff75734bfe794113756d2bfd0ac823d51e9422c8961125b570871d3c2b114']
|
||||
, ['facenet_weights.h5', '90659cc97bfda5999120f95d8e122f4d262cca11715a21e59ba024bcce816d5c']
|
||||
@ -66,12 +74,12 @@ def initializeFolder():
|
||||
, ['race_model_single_batch.h5', 'eb22b28b1f6dfce65b64040af4e86003a5edccb169a1a338470dde270b6f5e54']
|
||||
, ['vgg_face_weights.h5', '759266b9614d0fd5d65b97bf716818b746cc77ab5944c7bffc937c6ba9455d8c']
|
||||
]
|
||||
|
||||
|
||||
for i in weight_hashes:
|
||||
|
||||
|
||||
weight_file = home+"/.deepface/weights/"+i[0]
|
||||
expected_hash = i[1]
|
||||
|
||||
|
||||
#check file exits
|
||||
if os.path.isfile(weight_file) == True:
|
||||
current_hash = findFileHash(weight_file)
|
||||
@ -79,309 +87,346 @@ def initializeFolder():
|
||||
print("hash violated for ", i[0],". It's going to be removed.")
|
||||
os.remove(weight_file)
|
||||
"""
|
||||
#----------------------------------
|
||||
# ----------------------------------
|
||||
|
||||
|
||||
def findThreshold(model_name, distance_metric):
|
||||
|
||||
threshold = 0.40
|
||||
|
||||
if model_name == 'VGG-Face':
|
||||
if distance_metric == 'cosine':
|
||||
threshold = 0.40
|
||||
elif distance_metric == 'euclidean':
|
||||
threshold = 0.55
|
||||
elif distance_metric == 'euclidean_l2':
|
||||
threshold = 0.75
|
||||
|
||||
elif model_name == 'OpenFace':
|
||||
if distance_metric == 'cosine':
|
||||
threshold = 0.10
|
||||
elif distance_metric == 'euclidean':
|
||||
threshold = 0.55
|
||||
elif distance_metric == 'euclidean_l2':
|
||||
threshold = 0.55
|
||||
|
||||
elif model_name == 'Facenet':
|
||||
if distance_metric == 'cosine':
|
||||
threshold = 0.40
|
||||
elif distance_metric == 'euclidean':
|
||||
threshold = 10
|
||||
elif distance_metric == 'euclidean_l2':
|
||||
threshold = 0.80
|
||||
|
||||
elif model_name == 'DeepFace':
|
||||
if distance_metric == 'cosine':
|
||||
threshold = 0.23
|
||||
elif distance_metric == 'euclidean':
|
||||
threshold = 64
|
||||
elif distance_metric == 'euclidean_l2':
|
||||
threshold = 0.64
|
||||
|
||||
return threshold
|
||||
|
||||
threshold = 0.40
|
||||
|
||||
if model_name == "VGG-Face":
|
||||
if distance_metric == "cosine":
|
||||
threshold = 0.40
|
||||
elif distance_metric == "euclidean":
|
||||
threshold = 0.55
|
||||
elif distance_metric == "euclidean_l2":
|
||||
threshold = 0.75
|
||||
|
||||
elif model_name == "OpenFace":
|
||||
if distance_metric == "cosine":
|
||||
threshold = 0.10
|
||||
elif distance_metric == "euclidean":
|
||||
threshold = 0.55
|
||||
elif distance_metric == "euclidean_l2":
|
||||
threshold = 0.55
|
||||
|
||||
elif model_name == "Facenet":
|
||||
if distance_metric == "cosine":
|
||||
threshold = 0.40
|
||||
elif distance_metric == "euclidean":
|
||||
threshold = 10
|
||||
elif distance_metric == "euclidean_l2":
|
||||
threshold = 0.80
|
||||
|
||||
elif model_name == "DeepFace":
|
||||
if distance_metric == "cosine":
|
||||
threshold = 0.23
|
||||
elif distance_metric == "euclidean":
|
||||
threshold = 64
|
||||
elif distance_metric == "euclidean_l2":
|
||||
threshold = 0.64
|
||||
|
||||
return threshold
|
||||
|
||||
|
||||
def get_opencv_path():
|
||||
opencv_home = cv2.__file__
|
||||
folders = opencv_home.split(os.path.sep)[0:-1]
|
||||
|
||||
path = folders[0]
|
||||
for folder in folders[1:]:
|
||||
path = path + "/" + folder
|
||||
opencv_home = cv2.__file__
|
||||
folders = opencv_home.split(os.path.sep)[0:-1]
|
||||
|
||||
face_detector_path = path+"/data/haarcascade_frontalface_default.xml"
|
||||
eye_detector_path = path+"/data/haarcascade_eye.xml"
|
||||
|
||||
if os.path.isfile(face_detector_path) != True:
|
||||
raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.")
|
||||
|
||||
return path+"/data/"
|
||||
path = folders[0]
|
||||
for folder in folders[1:]:
|
||||
path = path + "/" + folder
|
||||
|
||||
face_detector_path = path + "/data/haarcascade_frontalface_default.xml"
|
||||
eye_detector_path = path + "/data/haarcascade_eye.xml"
|
||||
|
||||
if os.path.isfile(face_detector_path) != True:
|
||||
raise ValueError(
|
||||
"Confirm that opencv is installed on your environment! Expected path ",
|
||||
face_detector_path,
|
||||
" violated.",
|
||||
)
|
||||
|
||||
return path + "/data/"
|
||||
|
||||
|
||||
def detectFace(img, target_size=(224, 224), grayscale=False, enforce_detection=True):
|
||||
|
||||
img_path = ""
|
||||
|
||||
# -----------------------
|
||||
|
||||
exact_image = False
|
||||
if type(img).__module__ == np.__name__:
|
||||
exact_image = True
|
||||
|
||||
base64_img = False
|
||||
if len(img) > 11 and img[0:11] == "data:image/":
|
||||
base64_img = True
|
||||
|
||||
# -----------------------
|
||||
|
||||
opencv_path = get_opencv_path()
|
||||
face_detector_path = opencv_path + "haarcascade_frontalface_default.xml"
|
||||
eye_detector_path = opencv_path + "haarcascade_eye.xml"
|
||||
|
||||
if os.path.isfile(face_detector_path) != True:
|
||||
raise ValueError(
|
||||
"Confirm that opencv is installed on your environment! Expected path ",
|
||||
face_detector_path,
|
||||
" violated.",
|
||||
)
|
||||
|
||||
# --------------------------------
|
||||
|
||||
face_detector = cv2.CascadeClassifier(face_detector_path)
|
||||
eye_detector = cv2.CascadeClassifier(eye_detector_path)
|
||||
|
||||
if base64_img == True:
|
||||
img = loadBase64Img(img)
|
||||
|
||||
elif exact_image != True: # image path passed as input
|
||||
|
||||
if os.path.isfile(img) != True:
|
||||
raise ValueError("Confirm that ", img, " exists")
|
||||
|
||||
img = cv2.imread(img)
|
||||
|
||||
img_raw = img.copy()
|
||||
|
||||
# --------------------------------
|
||||
|
||||
faces = face_detector.detectMultiScale(img, 1.3, 5)
|
||||
|
||||
# print("found faces in ",image_path," is ",len(faces))
|
||||
|
||||
if len(faces) > 0:
|
||||
x, y, w, h = faces[0]
|
||||
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
|
||||
detected_face_gray = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# ---------------------------
|
||||
# face alignment
|
||||
|
||||
eyes = eye_detector.detectMultiScale(detected_face_gray)
|
||||
|
||||
if len(eyes) >= 2:
|
||||
# find the largest 2 eye
|
||||
base_eyes = eyes[:, 2]
|
||||
|
||||
items = []
|
||||
for i in range(0, len(base_eyes)):
|
||||
item = (base_eyes[i], i)
|
||||
items.append(item)
|
||||
|
||||
df = pd.DataFrame(items, columns=["length", "idx"]).sort_values(
|
||||
by=["length"], ascending=False
|
||||
)
|
||||
|
||||
eyes = eyes[df.idx.values[0:2]]
|
||||
|
||||
# -----------------------
|
||||
# decide left and right eye
|
||||
|
||||
eye_1 = eyes[0]
|
||||
eye_2 = eyes[1]
|
||||
|
||||
if eye_1[0] < eye_2[0]:
|
||||
left_eye = eye_1
|
||||
right_eye = eye_2
|
||||
else:
|
||||
left_eye = eye_2
|
||||
right_eye = eye_1
|
||||
|
||||
# -----------------------
|
||||
# find center of eyes
|
||||
|
||||
left_eye_center = (
|
||||
int(left_eye[0] + (left_eye[2] / 2)),
|
||||
int(left_eye[1] + (left_eye[3] / 2)),
|
||||
)
|
||||
left_eye_x = left_eye_center[0]
|
||||
left_eye_y = left_eye_center[1]
|
||||
|
||||
right_eye_center = (
|
||||
int(right_eye[0] + (right_eye[2] / 2)),
|
||||
int(right_eye[1] + (right_eye[3] / 2)),
|
||||
)
|
||||
right_eye_x = right_eye_center[0]
|
||||
right_eye_y = right_eye_center[1]
|
||||
|
||||
# -----------------------
|
||||
# find rotation direction
|
||||
|
||||
if left_eye_y > right_eye_y:
|
||||
point_3rd = (right_eye_x, left_eye_y)
|
||||
direction = -1 # rotate same direction to clock
|
||||
else:
|
||||
point_3rd = (left_eye_x, right_eye_y)
|
||||
direction = 1 # rotate inverse direction of clock
|
||||
|
||||
# -----------------------
|
||||
# find length of triangle edges
|
||||
|
||||
a = distance(left_eye_center, point_3rd)
|
||||
b = distance(right_eye_center, point_3rd)
|
||||
c = distance(right_eye_center, left_eye_center)
|
||||
|
||||
# -----------------------
|
||||
# apply cosine rule
|
||||
|
||||
cos_a = (b * b + c * c - a * a) / (2 * b * c)
|
||||
angle = np.arccos(cos_a) # angle in radian
|
||||
angle = (angle * 180) / math.pi # radian to degree
|
||||
|
||||
# -----------------------
|
||||
# rotate base image
|
||||
|
||||
if direction == -1:
|
||||
angle = 90 - angle
|
||||
|
||||
img = Image.fromarray(img_raw)
|
||||
img = np.array(img.rotate(direction * angle))
|
||||
|
||||
# you recover the base image and face detection disappeared. apply again.
|
||||
faces = face_detector.detectMultiScale(img, 1.3, 5)
|
||||
if len(faces) > 0:
|
||||
x, y, w, h = faces[0]
|
||||
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
|
||||
|
||||
# -----------------------
|
||||
|
||||
# face alignment block end
|
||||
# ---------------------------
|
||||
|
||||
# face alignment block needs colorful images. that's why, converting to gray scale logic moved to here.
|
||||
if grayscale == True:
|
||||
detected_face = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
detected_face = cv2.resize(detected_face, target_size)
|
||||
|
||||
img_pixels = image.img_to_array(detected_face)
|
||||
img_pixels = np.expand_dims(img_pixels, axis=0)
|
||||
|
||||
# normalize input in [0, 1]
|
||||
img_pixels /= 255
|
||||
|
||||
return img_pixels
|
||||
|
||||
else:
|
||||
|
||||
if (exact_image == True) or (enforce_detection != True):
|
||||
|
||||
if grayscale == True:
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
img = cv2.resize(img, target_size)
|
||||
img_pixels = image.img_to_array(img)
|
||||
img_pixels = np.expand_dims(img_pixels, axis=0)
|
||||
img_pixels /= 255
|
||||
return img_pixels
|
||||
else:
|
||||
raise ValueError(
|
||||
"Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False."
|
||||
)
|
||||
|
||||
def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection = True):
|
||||
|
||||
img_path = ""
|
||||
|
||||
#-----------------------
|
||||
|
||||
exact_image = False
|
||||
if type(img).__module__ == np.__name__:
|
||||
exact_image = True
|
||||
|
||||
base64_img = False
|
||||
if len(img) > 11 and img[0:11] == "data:image/":
|
||||
base64_img = True
|
||||
|
||||
#-----------------------
|
||||
|
||||
opencv_path = get_opencv_path()
|
||||
face_detector_path = opencv_path+"haarcascade_frontalface_default.xml"
|
||||
eye_detector_path = opencv_path+"haarcascade_eye.xml"
|
||||
|
||||
if os.path.isfile(face_detector_path) != True:
|
||||
raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.")
|
||||
|
||||
#--------------------------------
|
||||
|
||||
face_detector = cv2.CascadeClassifier(face_detector_path)
|
||||
eye_detector = cv2.CascadeClassifier(eye_detector_path)
|
||||
|
||||
if base64_img == True:
|
||||
img = loadBase64Img(img)
|
||||
|
||||
elif exact_image != True: #image path passed as input
|
||||
|
||||
if os.path.isfile(img) != True:
|
||||
raise ValueError("Confirm that ",img," exists")
|
||||
|
||||
img = cv2.imread(img)
|
||||
|
||||
img_raw = img.copy()
|
||||
|
||||
#--------------------------------
|
||||
|
||||
faces = face_detector.detectMultiScale(img, 1.3, 5)
|
||||
|
||||
#print("found faces in ",image_path," is ",len(faces))
|
||||
|
||||
if len(faces) > 0:
|
||||
x,y,w,h = faces[0]
|
||||
detected_face = img[int(y):int(y+h), int(x):int(x+w)]
|
||||
detected_face_gray = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
#---------------------------
|
||||
#face alignment
|
||||
|
||||
eyes = eye_detector.detectMultiScale(detected_face_gray)
|
||||
|
||||
if len(eyes) >= 2:
|
||||
#find the largest 2 eye
|
||||
base_eyes = eyes[:, 2]
|
||||
|
||||
items = []
|
||||
for i in range(0, len(base_eyes)):
|
||||
item = (base_eyes[i], i)
|
||||
items.append(item)
|
||||
|
||||
df = pd.DataFrame(items, columns = ["length", "idx"]).sort_values(by=['length'], ascending=False)
|
||||
|
||||
eyes = eyes[df.idx.values[0:2]]
|
||||
|
||||
#-----------------------
|
||||
#decide left and right eye
|
||||
|
||||
eye_1 = eyes[0]; eye_2 = eyes[1]
|
||||
|
||||
if eye_1[0] < eye_2[0]:
|
||||
left_eye = eye_1
|
||||
right_eye = eye_2
|
||||
else:
|
||||
left_eye = eye_2
|
||||
right_eye = eye_1
|
||||
|
||||
#-----------------------
|
||||
#find center of eyes
|
||||
|
||||
left_eye_center = (int(left_eye[0] + (left_eye[2] / 2)), int(left_eye[1] + (left_eye[3] / 2)))
|
||||
left_eye_x = left_eye_center[0]; left_eye_y = left_eye_center[1]
|
||||
|
||||
right_eye_center = (int(right_eye[0] + (right_eye[2]/2)), int(right_eye[1] + (right_eye[3]/2)))
|
||||
right_eye_x = right_eye_center[0]; right_eye_y = right_eye_center[1]
|
||||
|
||||
#-----------------------
|
||||
#find rotation direction
|
||||
|
||||
if left_eye_y > right_eye_y:
|
||||
point_3rd = (right_eye_x, left_eye_y)
|
||||
direction = -1 #rotate same direction to clock
|
||||
else:
|
||||
point_3rd = (left_eye_x, right_eye_y)
|
||||
direction = 1 #rotate inverse direction of clock
|
||||
|
||||
#-----------------------
|
||||
#find length of triangle edges
|
||||
|
||||
a = distance(left_eye_center, point_3rd)
|
||||
b = distance(right_eye_center, point_3rd)
|
||||
c = distance(right_eye_center, left_eye_center)
|
||||
|
||||
#-----------------------
|
||||
#apply cosine rule
|
||||
|
||||
cos_a = (b*b + c*c - a*a)/(2*b*c)
|
||||
angle = np.arccos(cos_a) #angle in radian
|
||||
angle = (angle * 180) / math.pi #radian to degree
|
||||
|
||||
#-----------------------
|
||||
#rotate base image
|
||||
|
||||
if direction == -1:
|
||||
angle = 90 - angle
|
||||
|
||||
img = Image.fromarray(img_raw)
|
||||
img = np.array(img.rotate(direction * angle))
|
||||
|
||||
#you recover the base image and face detection disappeared. apply again.
|
||||
faces = face_detector.detectMultiScale(img, 1.3, 5)
|
||||
if len(faces) > 0:
|
||||
x,y,w,h = faces[0]
|
||||
detected_face = img[int(y):int(y+h), int(x):int(x+w)]
|
||||
|
||||
#-----------------------
|
||||
|
||||
#face alignment block end
|
||||
#---------------------------
|
||||
|
||||
#face alignment block needs colorful images. that's why, converting to gray scale logic moved to here.
|
||||
if grayscale == True:
|
||||
detected_face = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
detected_face = cv2.resize(detected_face, target_size)
|
||||
|
||||
img_pixels = image.img_to_array(detected_face)
|
||||
img_pixels = np.expand_dims(img_pixels, axis = 0)
|
||||
|
||||
#normalize input in [0, 1]
|
||||
img_pixels /= 255
|
||||
|
||||
return img_pixels
|
||||
|
||||
else:
|
||||
|
||||
if (exact_image == True) or (enforce_detection != True):
|
||||
|
||||
if grayscale == True:
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
img = cv2.resize(img, target_size)
|
||||
img_pixels = image.img_to_array(img)
|
||||
img_pixels = np.expand_dims(img_pixels, axis = 0)
|
||||
img_pixels /= 255
|
||||
return img_pixels
|
||||
else:
|
||||
raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
||||
|
||||
def allocateMemory():
|
||||
|
||||
#find allocated memories
|
||||
gpu_indexes = []
|
||||
memory_usage_percentages = []; available_memories = []; total_memories = []; utilizations = []
|
||||
power_usages = []; power_capacities = []
|
||||
|
||||
try:
|
||||
result = subprocess.check_output(['nvidia-smi'])
|
||||
|
||||
dashboard = result.decode("utf-8").split("=|")
|
||||
# find allocated memories
|
||||
gpu_indexes = []
|
||||
memory_usage_percentages = []
|
||||
available_memories = []
|
||||
total_memories = []
|
||||
utilizations = []
|
||||
power_usages = []
|
||||
power_capacities = []
|
||||
|
||||
dashboard = dashboard[1].split("\n")
|
||||
|
||||
gpu_idx = 0
|
||||
for line in dashboard:
|
||||
if ("MiB" in line):
|
||||
power_info = line.split("|")[1]
|
||||
power_capacity = int(power_info.split("/")[-1].replace("W", ""))
|
||||
power_usage = int((power_info.split("/")[-2]).strip().split(" ")[-1].replace("W", ""))
|
||||
|
||||
power_usages.append(power_usage)
|
||||
power_capacities.append(power_capacity)
|
||||
|
||||
#----------------------------
|
||||
|
||||
memory_info = line.split("|")[2].replace("MiB","").split("/")
|
||||
utilization_info = int(line.split("|")[3].split("%")[0])
|
||||
|
||||
allocated = int(memory_info[0])
|
||||
total_memory = int(memory_info[1])
|
||||
available_memory = total_memory - allocated
|
||||
|
||||
total_memories.append(total_memory)
|
||||
available_memories.append(available_memory)
|
||||
memory_usage_percentages.append(round(100*int(allocated)/int(total_memory), 4))
|
||||
utilizations.append(utilization_info)
|
||||
gpu_indexes.append(gpu_idx)
|
||||
|
||||
gpu_idx = gpu_idx + 1
|
||||
|
||||
gpu_count = gpu_idx * 1
|
||||
|
||||
except Exception as err:
|
||||
gpu_count = 0
|
||||
#print(str(err))
|
||||
|
||||
#------------------------------
|
||||
|
||||
df = pd.DataFrame(gpu_indexes, columns = ["gpu_index"])
|
||||
df["total_memories_in_mb"] = total_memories
|
||||
df["available_memories_in_mb"] = available_memories
|
||||
df["memory_usage_percentage"] = memory_usage_percentages
|
||||
df["utilizations"] = utilizations
|
||||
df["power_usages_in_watts"] = power_usages
|
||||
df["power_capacities_in_watts"] = power_capacities
|
||||
|
||||
df = df.sort_values(by = ["available_memories_in_mb"], ascending = False).reset_index(drop = True)
|
||||
|
||||
#------------------------------
|
||||
|
||||
required_memory = 10000 #All deepface models require 9016 MiB
|
||||
|
||||
if df.shape[0] > 0: #has gpu
|
||||
if df.iloc[0].available_memories_in_mb > required_memory:
|
||||
my_gpu = str(int(df.iloc[0].gpu_index))
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = my_gpu
|
||||
|
||||
#------------------------------
|
||||
#tf allocates all memory by default
|
||||
#this block avoids greedy approach
|
||||
|
||||
config = tf.ConfigProto()
|
||||
config.gpu_options.allow_growth = True
|
||||
session = tf.Session(config=config)
|
||||
keras.backend.set_session(session)
|
||||
|
||||
print("DeepFace will run on GPU (gpu_", my_gpu,")")
|
||||
else:
|
||||
#this case has gpu but no enough memory to allocate
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "" #run it on cpu
|
||||
print("Even though the system has GPUs, there is no enough space in memory to allocate.")
|
||||
print("DeepFace will run on CPU")
|
||||
else:
|
||||
print("DeepFace will run on CPU")
|
||||
|
||||
#------------------------------
|
||||
try:
|
||||
result = subprocess.check_output(["nvidia-smi"])
|
||||
|
||||
dashboard = result.decode("utf-8").split("=|")
|
||||
|
||||
dashboard = dashboard[1].split("\n")
|
||||
|
||||
gpu_idx = 0
|
||||
for line in dashboard:
|
||||
if "MiB" in line:
|
||||
power_info = line.split("|")[1]
|
||||
power_capacity = int(power_info.split("/")[-1].replace("W", ""))
|
||||
power_usage = int(
|
||||
(power_info.split("/")[-2]).strip().split(" ")[-1].replace("W", "")
|
||||
)
|
||||
|
||||
power_usages.append(power_usage)
|
||||
power_capacities.append(power_capacity)
|
||||
|
||||
# ----------------------------
|
||||
|
||||
memory_info = line.split("|")[2].replace("MiB", "").split("/")
|
||||
utilization_info = int(line.split("|")[3].split("%")[0])
|
||||
|
||||
allocated = int(memory_info[0])
|
||||
total_memory = int(memory_info[1])
|
||||
available_memory = total_memory - allocated
|
||||
|
||||
total_memories.append(total_memory)
|
||||
available_memories.append(available_memory)
|
||||
memory_usage_percentages.append(
|
||||
round(100 * int(allocated) / int(total_memory), 4)
|
||||
)
|
||||
utilizations.append(utilization_info)
|
||||
gpu_indexes.append(gpu_idx)
|
||||
|
||||
gpu_idx = gpu_idx + 1
|
||||
|
||||
gpu_count = gpu_idx * 1
|
||||
|
||||
except Exception as err:
|
||||
gpu_count = 0
|
||||
# print(str(err))
|
||||
|
||||
# ------------------------------
|
||||
|
||||
df = pd.DataFrame(gpu_indexes, columns=["gpu_index"])
|
||||
df["total_memories_in_mb"] = total_memories
|
||||
df["available_memories_in_mb"] = available_memories
|
||||
df["memory_usage_percentage"] = memory_usage_percentages
|
||||
df["utilizations"] = utilizations
|
||||
df["power_usages_in_watts"] = power_usages
|
||||
df["power_capacities_in_watts"] = power_capacities
|
||||
|
||||
df = df.sort_values(by=["available_memories_in_mb"], ascending=False).reset_index(
|
||||
drop=True
|
||||
)
|
||||
|
||||
# ------------------------------
|
||||
|
||||
required_memory = 10000 # All deepface models require 9016 MiB
|
||||
|
||||
if df.shape[0] > 0: # has gpu
|
||||
if df.iloc[0].available_memories_in_mb > required_memory:
|
||||
my_gpu = str(int(df.iloc[0].gpu_index))
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = my_gpu
|
||||
|
||||
# ------------------------------
|
||||
# tf allocates all memory by default
|
||||
# this block avoids greedy approach
|
||||
|
||||
config = tf.ConfigProto()
|
||||
config.gpu_options.allow_growth = True
|
||||
session = tf.Session(config=config)
|
||||
keras.backend.set_session(session)
|
||||
|
||||
print("DeepFace will run on GPU (gpu_", my_gpu, ")")
|
||||
else:
|
||||
# this case has gpu but no enough memory to allocate
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "" # run it on cpu
|
||||
print(
|
||||
"Even though the system has GPUs, there is no enough space in memory to allocate."
|
||||
)
|
||||
print("DeepFace will run on CPU")
|
||||
else:
|
||||
print("DeepFace will run on CPU")
|
||||
|
||||
# ------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user