unit tests

This commit is contained in:
Sefik Ilkin Serengil 2022-05-19 11:11:17 +01:00
parent ef5fa518e5
commit eeaf1253da
2 changed files with 172 additions and 274 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
__pycache__/* __pycache__/*
basemodels/__pycache__/* basemodels/__pycache__/*
configurations/__pycache__/* configurations/__pycache__/*
tests/__pycache__/*
build/ build/
dist/ dist/
Pipfile Pipfile

View File

@ -1,183 +1,42 @@
import warnings import warnings
warnings.filterwarnings("ignore")
import os import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf
import cv2
from deepface import DeepFace
print("-----------------------------------------")
warnings.filterwarnings("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from deepface import DeepFace tf_major_version = int(tf.__version__.split(".")[0])
from deepface.commons import functions
import json
import time
#----------------------------------------- if tf_major_version == 2:
import tensorflow as tf
tf_version = int(tf.__version__.split(".")[0])
if tf_version == 2:
import logging import logging
tf.get_logger().setLevel(logging.ERROR) tf.get_logger().setLevel(logging.ERROR)
print("Running unit tests for TF ", tf.__version__) print("Running unit tests for TF ", tf.__version__)
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
from deepface.extendedmodels import Age, Gender, Race, Emotion
print("-----------------------------------------")
#-----------------------------------------
print("DeepFace.detectFace test")
#detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface']
detectors = ['opencv', 'ssd', 'mtcnn', 'retinaface']
for detector in detectors:
img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector)
print(detector," test is done")
#import matplotlib.pyplot as plt
#plt.imshow(img)
#plt.show()
#-----------------------------------------
print("-----------------------------------------") print("-----------------------------------------")
img_path = "dataset/img1.jpg" test_threshold = 97
embedding = DeepFace.represent(img_path) num_cases = 0
print("Function returned ", len(embedding), "dimensional vector") succeed_cases = 0
model_name = "VGG-Face" def evaluate(condition):
model = DeepFace.build_model(model_name)
print(model_name," is built")
embedding = DeepFace.represent(img_path, model = model)
print("Represent function returned ", len(embedding), "dimensional vector")
#----------------------------------------- global num_cases, succeed_cases
dataset = [ if condition is True:
['dataset/img1.jpg', 'dataset/img2.jpg', True], succeed_cases += 1
['dataset/img1.jpg', 'dataset/img6.jpg', True]
]
print("-----------------------------------------") num_cases += 1
print("Face detectors test") # ------------------------------------------------
print("retinaface detector") detectors = ['opencv', 'mtcnn', 'retinaface']
res = DeepFace.verify(dataset, detector_backend = 'retinaface') models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace']
print(res) metrics = ['cosine', 'euclidean', 'euclidean_l2']
print("ssd detector")
res = DeepFace.verify(dataset, detector_backend = 'ssd')
print(res)
print("opencv detector")
res = DeepFace.verify(dataset, detector_backend = 'opencv')
print(res)
if False:
print("dlib detector")
res = DeepFace.verify(dataset, detector_backend = 'dlib')
print(res)
print("mtcnn detector")
res = DeepFace.verify(dataset, detector_backend = 'mtcnn')
print(res)
print("-----------------------------------------")
print("Single find function test")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
#, model_name = 'Dlib'
)
print(df.head())
print("-----------------------------------------")
print("Pre-built model for single find function test")
#model_name = "VGG-Face"
#model = DeepFace.build_model(model_name)
#print(model_name," is built")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
, model_name = model_name, model = model
)
print(df.head())
print("-----------------------------------------")
print("Bulk find function tests")
dfs = DeepFace.find(img_path = ["dataset/img1.jpg", "dataset/img2.jpg"], db_path = "dataset"
#, model_name = 'Dlib'
)
print(dfs[0].head())
print(dfs[1].head())
print("-----------------------------------------")
print("Bulk verification tests")
resp_obj = DeepFace.verify(dataset)
print(resp_obj)
print(resp_obj["pair_1"]["verified"] == True)
print(resp_obj["pair_2"]["verified"] == True)
print("-----------------------------------------")
print("Bulk facial analysis tests")
dataset = [
'dataset/img1.jpg',
'dataset/img2.jpg',
'dataset/img5.jpg',
'dataset/img6.jpg'
]
resp_obj = DeepFace.analyze(dataset)
print(resp_obj["instance_1"]["age"]," years old ", resp_obj["instance_1"]["dominant_emotion"], " ",resp_obj["instance_1"]["gender"])
print(resp_obj["instance_2"]["age"]," years old ", resp_obj["instance_2"]["dominant_emotion"], " ",resp_obj["instance_2"]["gender"])
print(resp_obj["instance_3"]["age"]," years old ", resp_obj["instance_3"]["dominant_emotion"], " ",resp_obj["instance_3"]["gender"])
print(resp_obj["instance_4"]["age"]," years old ", resp_obj["instance_4"]["dominant_emotion"], " ",resp_obj["instance_4"]["gender"])
print("-----------------------------------------")
print("Facial analysis test. Passing nothing as an action")
img = "dataset/img4.jpg"
demography = DeepFace.analyze(img)
print(demography)
print("-----------------------------------------")
print("Facial analysis test. Passing all to the action")
demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion'])
print("Demography:")
print(demography)
#check response is a valid json
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Race: ", demography["dominant_race"])
print("Emotion: ", demography["dominant_emotion"])
print("-----------------------------------------")
print("Facial analysis test 2. Remove some actions and check they are not computed")
demography = DeepFace.analyze(img, ['age', 'gender'])
print("Age: ", demography.get("age"))
print("Gender: ", demography.get("gender"))
print("Race: ", demography.get("dominant_race"))
print("Emotion: ", demography.get("dominant_emotion"))
print("-----------------------------------------")
print("Face recognition tests")
dataset = [ dataset = [
['dataset/img1.jpg', 'dataset/img2.jpg', True], ['dataset/img1.jpg', 'dataset/img2.jpg', True],
@ -193,13 +52,94 @@ dataset = [
['dataset/img6.jpg', 'dataset/img9.jpg', False], ['dataset/img6.jpg', 'dataset/img9.jpg', False],
] ]
#models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace'] print("-----------------------------------------")
models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace'] #those are robust models
metrics = ['cosine', 'euclidean', 'euclidean_l2']
passed_tests = 0; test_cases = 0 def test_cases():
for model in models: print("DeepFace.detectFace test")
for detector in detectors:
img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector)
evaluate( img.shape[0] > 0 and img.shape[1] > 0 )
print(detector," test is done")
print("-----------------------------------------")
img_path = "dataset/img1.jpg"
embedding = DeepFace.represent(img_path)
print("Function returned ", len(embedding), "dimensional vector")
evaluate( len(embedding) > 0 )
print("-----------------------------------------")
print("Face detectors test")
for detector in detectors:
print(detector + " detector")
res = DeepFace.verify(dataset[0][0], dataset[0][1], detector_backend = detector)
print(res)
assert res["verified"] == dataset[0][2]
print("-----------------------------------------")
print("Single find function test")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset")
print(df.head())
evaluate( df.shape[0] > 0 )
print("-----------------------------------------")
print("Facial analysis test. Passing nothing as an action")
img = "dataset/img4.jpg"
demography = DeepFace.analyze(img)
print(demography)
evaluate( demography["age"] > 20 and demography["age"] < 40 )
evaluate( demography["gender"] == "Woman" )
print("-----------------------------------------")
print("Facial analysis test. Passing all to the action")
demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion'])
print("Demography:")
print(demography)
#check response is a valid json
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Race: ", demography["dominant_race"])
print("Emotion: ", demography["dominant_emotion"])
evaluate( demography.get("age") is not None )
evaluate( demography.get("gender") is not None )
evaluate( demography.get("dominant_race") is not None )
evaluate( demography.get("dominant_emotion") is not None )
print("-----------------------------------------")
print("Facial analysis test 2. Remove some actions and check they are not computed")
demography = DeepFace.analyze(img, ['age', 'gender'])
print("Age: ", demography.get("age"))
print("Gender: ", demography.get("gender"))
print("Race: ", demography.get("dominant_race"))
print("Emotion: ", demography.get("dominant_emotion"))
evaluate( demography.get("age") is not None )
evaluate( demography.get("gender") is not None )
evaluate( demography.get("dominant_race") is None )
evaluate( demography.get("dominant_emotion") is None )
print("-----------------------------------------")
print("Face recognition tests")
passed_tests = 0; test_cases = 0
for model in models:
#prebuilt_model = DeepFace.build_model(model) #prebuilt_model = DeepFace.build_model(model)
#print(model," is built") #print(model," is built")
for metric in metrics: for metric in metrics:
@ -217,6 +157,8 @@ for model in models:
distance = round(resp_obj["distance"], 2) distance = round(resp_obj["distance"], 2)
threshold = resp_obj["threshold"] threshold = resp_obj["threshold"]
evaluate( prediction == result )
test_result_label = "failed" test_result_label = "failed"
if prediction == result: if prediction == result:
passed_tests = passed_tests + 1 passed_tests = passed_tests + 1
@ -233,112 +175,67 @@ for model in models:
print("--------------------------") print("--------------------------")
#----------------------------------------- #-----------------------------------------
print("Passed unit tests: ",passed_tests," / ",test_cases) print("Passed unit tests: ",passed_tests," / ",test_cases)
min_score = 70 min_score = 70
accuracy = 100 * passed_tests / test_cases accuracy = 100 * passed_tests / test_cases
accuracy = round(accuracy, 2) accuracy = round(accuracy, 2)
if accuracy >= min_score: print("--------------------------")
print("Unit tests are completed successfully. Score: ",accuracy,"%")
else:
raise ValueError("Unit test score does not satisfy the minimum required accuracy. Minimum expected score is ", min_score,"% but this got ",accuracy,"%")
#----------------------------------- #-----------------------------------
#----------------------------------- print("--------------------------")
print("Analyze function with passing pre-trained model") print("Passing numpy array to analyze function")
emotion_model = DeepFace.build_model("Emotion") img = cv2.imread("dataset/img1.jpg")
age_model = DeepFace.build_model("Age") resp_obj = DeepFace.analyze(img)
gender_model = DeepFace.build_model("Gender") print(resp_obj)
race_model = DeepFace.build_model("Race")
facial_attribute_models = {} evaluate( resp_obj["age"] > 20 and resp_obj["age"] < 40 )
facial_attribute_models["emotion"] = emotion_model evaluate( resp_obj["gender"] == "Woman" )
facial_attribute_models["age"] = age_model
facial_attribute_models["gender"] = gender_model
facial_attribute_models["race"] = race_model
resp_obj = DeepFace.analyze("dataset/img1.jpg", models=facial_attribute_models) print("--------------------------")
print(resp_obj)
#----------------------------------- print("Passing numpy array to verify function")
print("--------------------------")
if False: img1 = cv2.imread("dataset/img1.jpg")
print("Ensemble for find function") img2 = cv2.imread("dataset/img2.jpg")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset", model_name = "Ensemble")
print(df.head())
#----------------------------------- res = DeepFace.verify(img1, img2)
print("--------------------------")
if False:
print("Ensemble for verify function")
resp_obj = DeepFace.verify(dataset, model_name = "Ensemble")
for i in range(0, len(dataset)):
item = resp_obj['pair_%s' % (i+1)]
verified = item["verified"]
score = item["score"]
print(verified)
#-----------------------------------
print("--------------------------")
if False:
print("Pre-trained ensemble method - find")
from deepface import DeepFace
from deepface.basemodels import Boosting
model = Boosting.loadModel()
df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model = model, enforce_detection=False)
print(df)
#-----------------------------------
print("--------------------------")
if False:
print("Pre-trained ensemble method - verify")
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
print(res) print(res)
#----------------------------------- evaluate( res["verified"] == True )
print("--------------------------")
import cv2 print("--------------------------")
print("Passing numpy array to analyze function") print("Passing numpy array to find function")
img = cv2.imread("dataset/img1.jpg") img1 = cv2.imread("dataset/img1.jpg")
resp_obj = DeepFace.analyze(img)
print(resp_obj)
print("--------------------------") df = DeepFace.find(img1, db_path = "dataset")
print("Passing numpy array to verify function") print(df.head())
img1 = cv2.imread("dataset/img1.jpg") evaluate( df.shape[0] > 0 )
img2 = cv2.imread("dataset/img2.jpg")
res = DeepFace.verify(img1, img2) print("--------------------------")
print(res)
print("--------------------------") test_cases()
print("Passing numpy array to find function") print("num of test cases run: " + str(num_cases))
print("succeeded test cases: " + str(succeed_cases))
img1 = cv2.imread("dataset/img1.jpg") test_score = (100 * succeed_cases) / num_cases
df = DeepFace.find(img1, db_path = "dataset") print("test coverage: " + str(test_score))
print(df.head()) if test_score > test_threshold:
print("min required test coverage is satisfied")
else:
print("min required test coverage is NOT satisfied")
print("--------------------------") assert test_score > test_threshold