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:
NISH1001 2020-05-20 15:55:08 +05:45
parent 892cbae56a
commit 2c0a507994
7 changed files with 3448 additions and 1447 deletions

View File

@ -1,5 +1,6 @@
from keras.preprocessing import image
import warnings
warnings.filterwarnings("ignore")
import time
import os
@ -12,16 +13,23 @@ 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):
def verify(
img1_path,
img2_path="",
model_name="VGG-Face",
distance_metric="cosine",
model=None,
enforce_detection=True,
):
tic = time.time()
@ -32,45 +40,45 @@ def verify(img1_path, img2_path=''
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.")
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.")
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.")
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.")
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
else: # model != None
print("Already built model is passed")
#------------------------------
#face recognition models have different size of inputs
# ------------------------------
# face recognition models have different size of inputs
input_shape = model.layers[0].input_shape[1:3]
#------------------------------
# ------------------------------
#tuned thresholds for model and metric pair
# tuned thresholds for model and metric pair
threshold = functions.findThreshold(model_name, distance_metric)
#------------------------------
pbar = tqdm(range(0,len(img_list)), desc='Verification')
# ------------------------------
pbar = tqdm(range(0, len(img_list)), desc="Verification")
resp_objects = []
#for instance in img_list:
# for instance in img_list:
for index in pbar:
instance = img_list[index]
@ -79,66 +87,77 @@ def verify(img1_path, img2_path=''
img1_path = instance[0]
img2_path = instance[1]
#----------------------
#crop and align faces
# ----------------------
# crop and align faces
img1 = functions.detectFace(img1_path, input_shape, enforce_detection = enforce_detection)
img2 = functions.detectFace(img2_path, input_shape, enforce_detection = enforce_detection)
img1 = functions.detectFace(
img1_path, input_shape, enforce_detection=enforce_detection
)
img2 = functions.detectFace(
img2_path, input_shape, enforce_detection=enforce_detection
)
#----------------------
#find embeddings
# ----------------------
# find embeddings
img1_representation = model.predict(img1)[0,:]
img2_representation = model.predict(img2)[0,:]
img1_representation = model.predict(img1)[0, :]
img2_representation = model.predict(img2)[0, :]
#----------------------
#find distances between embeddings
# ----------------------
# 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))
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
# ----------------------
# decision
if distance <= threshold:
identified = "true"
else:
identified = "false"
#----------------------
#response object
# ----------------------
# 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 += '"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
resp_obj = json.loads(resp_obj) # string to json
if bulkProcess == True:
resp_objects.append(resp_obj)
else:
#K.clear_session()
# 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")
# print("identification lasts ",toc-tic," seconds")
if bulkProcess == True:
resp_obj = "{"
@ -149,14 +168,14 @@ def verify(img1_path, img2_path=''
if i > 0:
resp_obj += ", "
resp_obj += "\"pair_"+str(i+1)+"\": "+resp_item
resp_obj += '"pair_' + str(i + 1) + '": ' + resp_item
resp_obj += "}"
resp_obj = json.loads(resp_obj)
return resp_obj
#return resp_objects
# return resp_objects
def analyze(img_path, actions = [], models = {}, enforce_detection = True):
def analyze(img_path, actions=[], models={}, enforce_detection=True):
if type(img_path) == list:
img_paths = img_path.copy()
@ -165,62 +184,62 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True):
img_paths = [img_path]
bulkProcess = False
#---------------------------------
# ---------------------------------
#if a specific target is not passed, then find them all
# if a specific target is not passed, then find them all
if len(actions) == 0:
actions= ['emotion', 'age', 'gender', 'race']
actions = ["emotion", "age", "gender", "race"]
print("Actions to do: ", actions)
#---------------------------------
# ---------------------------------
if 'emotion' in actions:
if 'emotion' in models:
if "emotion" in actions:
if "emotion" in models:
print("already built emotion model is passed")
emotion_model = models['emotion']
emotion_model = models["emotion"]
else:
emotion_model = Emotion.loadModel()
if 'age' in actions:
if 'age' in models:
if "age" in actions:
if "age" in models:
print("already built age model is passed")
age_model = models['age']
age_model = models["age"]
else:
age_model = Age.loadModel()
if 'gender' in actions:
if 'gender' in models:
if "gender" in actions:
if "gender" in models:
print("already built gender model is passed")
gender_model = models['gender']
gender_model = models["gender"]
else:
gender_model = Gender.loadModel()
if 'race' in actions:
if 'race' in models:
if "race" in actions:
if "race" in models:
print("already built race model is passed")
race_model = models['race']
race_model = models["race"]
else:
race_model = Race.loadModel()
#---------------------------------
# ---------------------------------
resp_objects = []
global_pbar = tqdm(range(0,len(img_paths)), desc='Analyzing')
global_pbar = tqdm(range(0, len(img_paths)), desc="Analyzing")
#for img_path in img_paths:
# for img_path in img_paths:
for j in global_pbar:
img_path = img_paths[j]
resp_obj = "{"
#TO-DO: do this in parallel
# TO-DO: do this in parallel
pbar = tqdm(range(0,len(actions)), desc='Finding actions')
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 action in actions:
for index in pbar:
action = actions[index]
pbar.set_description("Action: %s" % (action))
@ -228,71 +247,114 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True):
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)
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,:]
emotion_predictions = emotion_model.predict(img)[0, :]
sum_of_predictions = emotion_predictions.sum()
emotion_obj = "\"emotion\": {"
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
emotion_prediction = (
100 * emotion_predictions[i] / sum_of_predictions
)
if i > 0: emotion_obj += ", "
if i > 0:
emotion_obj += ", "
emotion_obj += "\"%s\": %s" % (emotion_label, emotion_prediction)
emotion_obj += '"%s": %s' % (emotion_label, emotion_prediction)
emotion_obj += "}"
emotion_obj += ", \"dominant_emotion\": \"%s\"" % (emotion_labels[np.argmax(emotion_predictions)])
emotion_obj += ', "dominant_emotion": "%s"' % (
emotion_labels[np.argmax(emotion_predictions)]
)
resp_obj += emotion_obj
elif action == 'age':
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,:]
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)
resp_obj += '"age": %s' % (apparent_age)
elif action == 'gender':
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")
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,:]
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)
resp_obj += '"gender": "%s"' % (gender)
elif action == 'race':
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']
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\": {"
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 += ", "
if i > 0:
race_obj += ", "
race_obj += "\"%s\": %s" % (race_label, race_prediction)
race_obj += '"%s": %s' % (race_label, race_prediction)
race_obj += "}"
race_obj += ", \"dominant_race\": \"%s\"" % (race_labels[np.argmax(race_predictions)])
race_obj += ', "dominant_race": "%s"' % (
race_labels[np.argmax(race_predictions)]
)
resp_obj += race_obj
@ -316,26 +378,32 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True):
if i > 0:
resp_obj += ", "
resp_obj += "\"instance_"+str(i+1)+"\": "+resp_item
resp_obj += '"instance_' + str(i + 1) + '": ' + resp_item
resp_obj += "}"
resp_obj = json.loads(resp_obj)
return resp_obj
#return resp_objects
# 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):
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()
functions.initializeFolder()
#---------------------------
# ---------------------------

File diff suppressed because it is too large Load Diff

View File

@ -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():
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'))
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
#---------------------------------
home = str(Path.home())
if os.path.isfile(home+'/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5') != True:
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'
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'
zip_path = os.path.join(
home, ".deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
)
gdown.download(url, zip_path, quiet=False)
gdown.download(url, output, 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/"))
#unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
with zipfile.ZipFile(output, 'r') as zip_ref:
zip_ref.extractall(home+'/.deepface/weights/')
print(f"Loading model from {model_path}")
base_model = get_base_model()
base_model.load_weights(model_path)
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)
# 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

View File

@ -13,238 +13,397 @@ from keras.layers.normalization import BatchNormalization
from keras.models import load_model
from keras import backend as K
#---------------------------------------
# ---------------------------------------
def loadModel():
def get_base_model():
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 = 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 = 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 = 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)
# 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 = 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_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_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 = 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_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_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 = 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_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_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 = concatenate([inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1], axis=3)
inception_3a = concatenate(
[inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1],
axis=3,
)
# 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 = 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_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_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 = 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_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_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 = 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_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_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 = concatenate([inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1], axis=3)
inception_3b = concatenate(
[inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1],
axis=3,
)
# 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 = 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_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_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 = 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_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_pool = MaxPooling2D(pool_size=3, strides=2)(inception_3b)
inception_3c_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_3c_pool)
inception_3c = concatenate([inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3)
inception_3c = concatenate(
[inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3
)
#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
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_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_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_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 = 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_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_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 = 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_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_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 = concatenate([inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1], axis=3)
inception_4a = concatenate(
[inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1],
axis=3,
)
#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)
# 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_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_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 = 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_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_pool = MaxPooling2D(pool_size=3, strides=2)(inception_4a)
inception_4e_pool = ZeroPadding2D(padding=((0, 1), (0, 1)))(inception_4e_pool)
inception_4e = concatenate([inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3)
inception_4e = concatenate(
[inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3
)
#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)
# 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_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 = 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_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_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 = 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_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_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 = concatenate([inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3)
inception_5a = concatenate(
[inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3
)
#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
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 = MaxPooling2D(pool_size=3, strides=2)(inception_5a)
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 = 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 = ZeroPadding2D(padding=(1, 1))(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_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 = concatenate([inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3)
inception_5b = concatenate(
[inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3
)
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)
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
model = Model(inputs=[myInput], outputs=norm_layer)
return Model(inputs=[myInput], outputs=norm_layer)
#-----------------------------------
home = str(Path.home())
def loadModel(model_path=""):
# -----------------------------------
if model_path:
assert Path(model_path).exists()
assert model_path.endswith(".h5")
if os.path.isfile(home+'/.deepface/weights/openface_weights.h5') != True:
print("openface_weights.h5 will be downloaded...")
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'
output = home+'/.deepface/weights/openface_weights.h5'
gdown.download(url, output, quiet=False)
url = "https://drive.google.com/uc?id=1LSe1YCV1x-BfNnfb7DFZTNpv_Q9jITxn"
gdown.download(url, model_path, quiet=False)
#-----------------------------------
# -----------------------------------
model.load_weights(home+'/.deepface/weights/openface_weights.h5')
print(f"Loading model from {model_path}")
model = get_base_model()
model.load_weights(model_path)
#-----------------------------------
# -----------------------------------
return model

View File

@ -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():
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), 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)))
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(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(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(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(Convolution2D(4096, (7, 7), activation="relu"))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
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'))
model.add(Activation("softmax"))
return model
def loadModel():
model = baseModel()
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)
home = str(Path.home())
# -----------------------------------
if os.path.isfile(home+'/.deepface/weights/vgg_face_weights.h5') != True:
print("vgg_face_weights.h5 will be downloaded...")
print(f"Loading model from {model_path}")
model = get_base_model()
model.load_weights(model_path)
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)
# TO-DO: why?
vgg_face_descriptor = Model(
inputs=model.layers[0].input, outputs=model.layers[-2].output
)
return vgg_face_descriptor

View File

@ -17,23 +17,30 @@ import subprocess
import tensorflow as tf
import keras
def loadBase64Img(uri):
encoded_data = uri.split(',')[1]
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]
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
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
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
@ -41,19 +48,20 @@ def findFileHash(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"):
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")
if not os.path.exists(home + "/.deepface/weights"):
os.mkdir(home + "/.deepface/weights")
print("Directory ", home, "/.deepface/weights created")
#----------------------------------
# ----------------------------------
"""
#avoid interrupted file download
@ -79,46 +87,48 @@ 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':
if model_name == "VGG-Face":
if distance_metric == "cosine":
threshold = 0.40
elif distance_metric == 'euclidean':
elif distance_metric == "euclidean":
threshold = 0.55
elif distance_metric == 'euclidean_l2':
elif distance_metric == "euclidean_l2":
threshold = 0.75
elif model_name == 'OpenFace':
if distance_metric == 'cosine':
elif model_name == "OpenFace":
if distance_metric == "cosine":
threshold = 0.10
elif distance_metric == 'euclidean':
elif distance_metric == "euclidean":
threshold = 0.55
elif distance_metric == 'euclidean_l2':
elif distance_metric == "euclidean_l2":
threshold = 0.55
elif model_name == 'Facenet':
if distance_metric == 'cosine':
elif model_name == "Facenet":
if distance_metric == "cosine":
threshold = 0.40
elif distance_metric == 'euclidean':
elif distance_metric == "euclidean":
threshold = 10
elif distance_metric == 'euclidean_l2':
elif distance_metric == "euclidean_l2":
threshold = 0.80
elif model_name == 'DeepFace':
if distance_metric == 'cosine':
elif model_name == "DeepFace":
if distance_metric == "cosine":
threshold = 0.23
elif distance_metric == 'euclidean':
elif distance_metric == "euclidean":
threshold = 64
elif distance_metric == 'euclidean_l2':
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]
@ -127,19 +137,24 @@ def get_opencv_path():
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"
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.")
raise ValueError(
"Confirm that opencv is installed on your environment! Expected path ",
face_detector_path,
" violated.",
)
return path+"/data/"
return path + "/data/"
def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection = True):
def detectFace(img, target_size=(224, 224), grayscale=False, enforce_detection=True):
img_path = ""
#-----------------------
# -----------------------
exact_image = False
if type(img).__module__ == np.__name__:
@ -149,16 +164,20 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
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"
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.")
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)
@ -166,33 +185,33 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
if base64_img == True:
img = loadBase64Img(img)
elif exact_image != True: #image path passed as input
elif exact_image != True: # image path passed as input
if os.path.isfile(img) != True:
raise ValueError("Confirm that ",img," exists")
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))
# 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)]
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
# ---------------------------
# face alignment
eyes = eye_detector.detectMultiScale(detected_face_gray)
if len(eyes) >= 2:
#find the largest 2 eye
# find the largest 2 eye
base_eyes = eyes[:, 2]
items = []
@ -200,14 +219,17 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
item = (base_eyes[i], i)
items.append(item)
df = pd.DataFrame(items, columns = ["length", "idx"]).sort_values(by=['length'], ascending=False)
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
# -----------------------
# decide left and right eye
eye_1 = eyes[0]; eye_2 = eyes[1]
eye_1 = eyes[0]
eye_2 = eyes[1]
if eye_1[0] < eye_2[0]:
left_eye = eye_1
@ -216,41 +238,49 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
left_eye = eye_2
right_eye = eye_1
#-----------------------
#find center of eyes
# -----------------------
# 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]
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]
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
# -----------------------
# 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
direction = -1 # rotate same direction to clock
else:
point_3rd = (left_eye_x, right_eye_y)
direction = 1 #rotate inverse direction of clock
direction = 1 # rotate inverse direction of clock
#-----------------------
#find length of triangle edges
# -----------------------
# 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
# -----------------------
# 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
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
# -----------------------
# rotate base image
if direction == -1:
angle = 90 - angle
@ -258,27 +288,27 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
img = Image.fromarray(img_raw)
img = np.array(img.rotate(direction * angle))
#you recover the base image and face detection disappeared. apply again.
# 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)]
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 end
# ---------------------------
#face alignment block needs colorful images. that's why, converting to gray scale logic moved to here.
# 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)
img_pixels = np.expand_dims(img_pixels, axis=0)
#normalize input in [0, 1]
# normalize input in [0, 1]
img_pixels /= 255
return img_pixels
@ -292,21 +322,28 @@ def detectFace(img, target_size=(224, 224), grayscale = False, enforce_detection
img = cv2.resize(img, target_size)
img_pixels = image.img_to_array(img)
img_pixels = np.expand_dims(img_pixels, axis = 0)
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.")
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
# find allocated memories
gpu_indexes = []
memory_usage_percentages = []; available_memories = []; total_memories = []; utilizations = []
power_usages = []; power_capacities = []
memory_usage_percentages = []
available_memories = []
total_memories = []
utilizations = []
power_usages = []
power_capacities = []
try:
result = subprocess.check_output(['nvidia-smi'])
result = subprocess.check_output(["nvidia-smi"])
dashboard = result.decode("utf-8").split("=|")
@ -314,17 +351,19 @@ def allocateMemory():
gpu_idx = 0
for line in dashboard:
if ("MiB" in line):
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_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("/")
memory_info = line.split("|")[2].replace("MiB", "").split("/")
utilization_info = int(line.split("|")[3].split("%")[0])
allocated = int(memory_info[0])
@ -333,7 +372,9 @@ def allocateMemory():
total_memories.append(total_memory)
available_memories.append(available_memory)
memory_usage_percentages.append(round(100*int(allocated)/int(total_memory), 4))
memory_usage_percentages.append(
round(100 * int(allocated) / int(total_memory), 4)
)
utilizations.append(utilization_info)
gpu_indexes.append(gpu_idx)
@ -343,11 +384,11 @@ def allocateMemory():
except Exception as err:
gpu_count = 0
#print(str(err))
# print(str(err))
#------------------------------
# ------------------------------
df = pd.DataFrame(gpu_indexes, columns = ["gpu_index"])
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
@ -355,33 +396,37 @@ def allocateMemory():
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)
df = df.sort_values(by=["available_memories_in_mb"], ascending=False).reset_index(
drop=True
)
#------------------------------
# ------------------------------
required_memory = 10000 #All deepface models require 9016 MiB
required_memory = 10000 # All deepface models require 9016 MiB
if df.shape[0] > 0: #has gpu
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
# ------------------------------
# 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,")")
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.")
# 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")
#------------------------------
# ------------------------------