Converted to unittest

This commit is contained in:
Onur Atakan ULUSOY 2022-05-11 16:44:48 +00:00
parent 6622d84a91
commit 34c2dbef17
2 changed files with 241 additions and 232 deletions

View File

@ -67,4 +67,4 @@ jobs:
- name: Test with pytest - name: Test with pytest
run: | run: |
cd tests cd tests
pytest /home/runner/work/deepface/tests/unit_tests.py pytest unit_tests.py

View File

@ -9,336 +9,345 @@ from deepface import DeepFace
from deepface.commons import functions from deepface.commons import functions
import json import json
import time import time
import unittest
#----------------------------------------- #-----------------------------------------
import tensorflow as tf import tensorflow as tf
tf_version = int(tf.__version__.split(".")[0])
if tf_version == 2: class deepface_unit_tests(unittest.TestCase):
import logging
tf.get_logger().setLevel(logging.ERROR)
print("Running unit tests for TF ", tf.__version__) def test_deepface(self):
tf_version = int(tf.__version__.split(".")[0])
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace if tf_version == 2:
from deepface.extendedmodels import Age, Gender, Race, Emotion import logging
tf.get_logger().setLevel(logging.ERROR)
print("-----------------------------------------") print("Running unit tests for TF ", tf.__version__)
#-----------------------------------------
print("DeepFace.detectFace test") from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
#detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface'] from deepface.extendedmodels import Age, Gender, Race, Emotion
detectors = ['opencv', 'ssd', 'mtcnn', 'retinaface']
for detector in detectors: print("-----------------------------------------")
img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector) #-----------------------------------------
print(detector," test is done")
#import matplotlib.pyplot as plt print("DeepFace.detectFace test")
#plt.imshow(img) #detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface']
#plt.show() detectors = ['opencv', 'ssd', 'mtcnn', 'retinaface']
#----------------------------------------- for detector in detectors:
print("-----------------------------------------") img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector)
print(detector," test is done")
img_path = "dataset/img1.jpg" #import matplotlib.pyplot as plt
embedding = DeepFace.represent(img_path) #plt.imshow(img)
print("Function returned ", len(embedding), "dimensional vector") #plt.show()
model_name = "VGG-Face" #-----------------------------------------
model = DeepFace.build_model(model_name) print("-----------------------------------------")
print(model_name," is built")
embedding = DeepFace.represent(img_path, model = model)
print("Represent function returned ", len(embedding), "dimensional vector")
#----------------------------------------- img_path = "dataset/img1.jpg"
embedding = DeepFace.represent(img_path)
print("Function returned ", len(embedding), "dimensional vector")
dataset = [ model_name = "VGG-Face"
['dataset/img1.jpg', 'dataset/img2.jpg', True], model = DeepFace.build_model(model_name)
['dataset/img1.jpg', 'dataset/img6.jpg', True] print(model_name," is built")
] embedding = DeepFace.represent(img_path, model = model)
print("Represent function returned ", len(embedding), "dimensional vector")
print("-----------------------------------------") #-----------------------------------------
print("Face detectors test") dataset = [
['dataset/img1.jpg', 'dataset/img2.jpg', True],
['dataset/img1.jpg', 'dataset/img6.jpg', True]
]
print("retinaface detector") print("-----------------------------------------")
res = DeepFace.verify(dataset, detector_backend = 'retinaface')
print(res)
print("ssd detector") print("Face detectors test")
res = DeepFace.verify(dataset, detector_backend = 'ssd')
print(res)
print("opencv detector") print("retinaface detector")
res = DeepFace.verify(dataset, detector_backend = 'opencv') res = DeepFace.verify(dataset, detector_backend = 'retinaface')
print(res) print(res)
if False: print("ssd detector")
print("dlib detector") res = DeepFace.verify(dataset, detector_backend = 'ssd')
res = DeepFace.verify(dataset, detector_backend = 'dlib') print(res)
print(res)
print("mtcnn detector") print("opencv detector")
res = DeepFace.verify(dataset, detector_backend = 'mtcnn') res = DeepFace.verify(dataset, detector_backend = 'opencv')
print(res) print(res)
print("-----------------------------------------") if False:
print("dlib detector")
res = DeepFace.verify(dataset, detector_backend = 'dlib')
print(res)
print("Single find function test") print("mtcnn detector")
res = DeepFace.verify(dataset, detector_backend = 'mtcnn')
print(res)
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset" print("-----------------------------------------")
#, model_name = 'Dlib'
)
print(df.head())
print("-----------------------------------------") print("Single find function test")
print("Pre-built model for single find function test") df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
#, model_name = 'Dlib'
)
print(df.head())
#model_name = "VGG-Face" print("-----------------------------------------")
#model = DeepFace.build_model(model_name)
#print(model_name," is built")
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset" print("Pre-built model for single find function test")
, model_name = model_name, model = model
)
print(df.head())
print("-----------------------------------------") #model_name = "VGG-Face"
#model = DeepFace.build_model(model_name)
#print(model_name," is built")
print("Bulk find function tests") df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
, model_name = model_name, model = model
)
print(df.head())
dfs = DeepFace.find(img_path = ["dataset/img1.jpg", "dataset/img2.jpg"], db_path = "dataset" print("-----------------------------------------")
#, model_name = 'Dlib'
)
print(dfs[0].head())
print(dfs[1].head())
print("-----------------------------------------") print("Bulk find function tests")
print("Bulk verification 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())
resp_obj = DeepFace.verify(dataset) print("-----------------------------------------")
print(resp_obj)
print(resp_obj["pair_1"]["verified"] == True)
print(resp_obj["pair_2"]["verified"] == True)
print("-----------------------------------------") print("Bulk verification tests")
print("Bulk facial analysis tests") resp_obj = DeepFace.verify(dataset)
print(resp_obj)
print(resp_obj["pair_1"]["verified"] == True)
print(resp_obj["pair_2"]["verified"] == True)
dataset = [ print("-----------------------------------------")
'dataset/img1.jpg',
'dataset/img2.jpg',
'dataset/img5.jpg',
'dataset/img6.jpg'
]
resp_obj = DeepFace.analyze(dataset) print("Bulk facial analysis tests")
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("-----------------------------------------") dataset = [
'dataset/img1.jpg',
'dataset/img2.jpg',
'dataset/img5.jpg',
'dataset/img6.jpg'
]
print("Facial analysis test. Passing nothing as an action") 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"])
img = "dataset/img4.jpg" print("-----------------------------------------")
demography = DeepFace.analyze(img)
print(demography)
print("-----------------------------------------") print("Facial analysis test. Passing nothing as an action")
print("Facial analysis test. Passing all to the action") img = "dataset/img4.jpg"
demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion']) demography = DeepFace.analyze(img)
print(demography)
print("Demography:") print("-----------------------------------------")
print(demography)
#check response is a valid json print("Facial analysis test. Passing all to the action")
print("Age: ", demography["age"]) demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion'])
print("Gender: ", demography["gender"])
print("Race: ", demography["dominant_race"])
print("Emotion: ", demography["dominant_emotion"])
print("-----------------------------------------") print("Demography:")
print(demography)
print("Facial analysis test 2. Remove some actions and check they are not computed") #check response is a valid json
demography = DeepFace.analyze(img, ['age', 'gender']) print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Race: ", demography["dominant_race"])
print("Emotion: ", demography["dominant_emotion"])
print("Age: ", demography.get("age")) print("-----------------------------------------")
print("Gender: ", demography.get("gender"))
print("Race: ", demography.get("dominant_race"))
print("Emotion: ", demography.get("dominant_emotion"))
print("Facial analysis test 2. Remove some actions and check they are not computed")
demography = DeepFace.analyze(img, ['age', 'gender'])
print("-----------------------------------------") print("Age: ", demography.get("age"))
print("Gender: ", demography.get("gender"))
print("Race: ", demography.get("dominant_race"))
print("Emotion: ", demography.get("dominant_emotion"))
print("Face recognition tests")
dataset = [ print("-----------------------------------------")
['dataset/img1.jpg', 'dataset/img2.jpg', True],
['dataset/img5.jpg', 'dataset/img6.jpg', True],
['dataset/img6.jpg', 'dataset/img7.jpg', True],
['dataset/img8.jpg', 'dataset/img9.jpg', True],
['dataset/img1.jpg', 'dataset/img11.jpg', True],
['dataset/img2.jpg', 'dataset/img11.jpg', True],
['dataset/img1.jpg', 'dataset/img3.jpg', False], print("Face recognition tests")
['dataset/img2.jpg', 'dataset/img3.jpg', False],
['dataset/img6.jpg', 'dataset/img8.jpg', False],
['dataset/img6.jpg', 'dataset/img9.jpg', False],
]
#models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace'] dataset = [
models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace'] #those are robust models ['dataset/img1.jpg', 'dataset/img2.jpg', True],
metrics = ['cosine', 'euclidean', 'euclidean_l2'] ['dataset/img5.jpg', 'dataset/img6.jpg', True],
['dataset/img6.jpg', 'dataset/img7.jpg', True],
['dataset/img8.jpg', 'dataset/img9.jpg', True],
['dataset/img1.jpg', 'dataset/img11.jpg', True],
['dataset/img2.jpg', 'dataset/img11.jpg', True],
passed_tests = 0; test_cases = 0 ['dataset/img1.jpg', 'dataset/img3.jpg', False],
['dataset/img2.jpg', 'dataset/img3.jpg', False],
['dataset/img6.jpg', 'dataset/img8.jpg', False],
['dataset/img6.jpg', 'dataset/img9.jpg', False],
]
for model in models: #models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace']
#prebuilt_model = DeepFace.build_model(model) models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace'] #those are robust models
#print(model," is built") metrics = ['cosine', 'euclidean', 'euclidean_l2']
for metric in metrics:
for instance in dataset:
img1 = instance[0]
img2 = instance[1]
result = instance[2]
resp_obj = DeepFace.verify(img1, img2 passed_tests = 0; test_cases = 0
, model_name = model
#, model = prebuilt_model
, distance_metric = metric)
prediction = resp_obj["verified"] for model in models:
distance = round(resp_obj["distance"], 2) #prebuilt_model = DeepFace.build_model(model)
threshold = resp_obj["threshold"] #print(model," is built")
for metric in metrics:
for instance in dataset:
img1 = instance[0]
img2 = instance[1]
result = instance[2]
test_result_label = "failed" resp_obj = DeepFace.verify(img1, img2
if prediction == result: , model_name = model
passed_tests = passed_tests + 1 #, model = prebuilt_model
test_result_label = "passed" , distance_metric = metric)
if prediction == True: prediction = resp_obj["verified"]
classified_label = "verified" distance = round(resp_obj["distance"], 2)
else: threshold = resp_obj["threshold"]
classified_label = "unverified"
test_cases = test_cases + 1 test_result_label = "failed"
if prediction == result:
passed_tests = passed_tests + 1
test_result_label = "passed"
print(img1.split("/")[-1], "-", img2.split("/")[-1], classified_label, "as same person based on", model,"and",metric,". Distance:",distance,", Threshold:", threshold,"(",test_result_label,")") if prediction == True:
classified_label = "verified"
else:
classified_label = "unverified"
print("--------------------------") test_cases = test_cases + 1
#----------------------------------------- print(img1.split("/")[-1], "-", img2.split("/")[-1], classified_label, "as same person based on", model,"and",metric,". Distance:",distance,", Threshold:", threshold,"(",test_result_label,")")
print("Passed unit tests: ",passed_tests," / ",test_cases) print("--------------------------")
min_score = 70 #-----------------------------------------
accuracy = 100 * passed_tests / test_cases print("Passed unit tests: ",passed_tests," / ",test_cases)
accuracy = round(accuracy, 2)
if accuracy >= min_score: min_score = 70
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,"%")
#----------------------------------- accuracy = 100 * passed_tests / test_cases
#----------------------------------- accuracy = round(accuracy, 2)
print("Analyze function with passing pre-trained model") if accuracy >= min_score:
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,"%")
emotion_model = DeepFace.build_model("Emotion") #-----------------------------------
age_model = DeepFace.build_model("Age") #-----------------------------------
gender_model = DeepFace.build_model("Gender")
race_model = DeepFace.build_model("Race")
facial_attribute_models = {} print("Analyze function with passing pre-trained model")
facial_attribute_models["emotion"] = emotion_model
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) emotion_model = DeepFace.build_model("Emotion")
print(resp_obj) age_model = DeepFace.build_model("Age")
gender_model = DeepFace.build_model("Gender")
race_model = DeepFace.build_model("Race")
#----------------------------------- facial_attribute_models = {}
print("--------------------------") facial_attribute_models["emotion"] = emotion_model
facial_attribute_models["age"] = age_model
facial_attribute_models["gender"] = gender_model
facial_attribute_models["race"] = race_model
if False: resp_obj = DeepFace.analyze("dataset/img1.jpg", models=facial_attribute_models)
print("Ensemble for find function") print(resp_obj)
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset", model_name = "Ensemble")
print(df.head())
#----------------------------------- #-----------------------------------
print("--------------------------") print("--------------------------")
if False: if False:
print("Ensemble for verify function") print("Ensemble for find function")
resp_obj = DeepFace.verify(dataset, model_name = "Ensemble") df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset", model_name = "Ensemble")
print(df.head())
for i in range(0, len(dataset)): #-----------------------------------
item = resp_obj['pair_%s' % (i+1)] print("--------------------------")
verified = item["verified"]
score = item["score"]
print(verified)
#----------------------------------- if False:
print("--------------------------") print("Ensemble for verify function")
resp_obj = DeepFace.verify(dataset, model_name = "Ensemble")
if False: for i in range(0, len(dataset)):
item = resp_obj['pair_%s' % (i+1)]
verified = item["verified"]
score = item["score"]
print(verified)
print("Pre-trained ensemble method - find") #-----------------------------------
print("--------------------------")
from deepface import DeepFace if False:
from deepface.basemodels import Boosting
model = Boosting.loadModel() print("Pre-trained ensemble method - find")
df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model = model, enforce_detection=False)
print(df) from deepface import DeepFace
from deepface.basemodels import Boosting
#----------------------------------- model = Boosting.loadModel()
print("--------------------------") df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model = model, enforce_detection=False)
if False: print(df)
print("Pre-trained ensemble method - verify")
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
print(res)
#----------------------------------- #-----------------------------------
print("--------------------------") print("--------------------------")
import cv2 if False:
print("Pre-trained ensemble method - verify")
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
print(res)
print("Passing numpy array to analyze function") #-----------------------------------
print("--------------------------")
img = cv2.imread("dataset/img1.jpg") import cv2
resp_obj = DeepFace.analyze(img)
print(resp_obj)
print("--------------------------") print("Passing numpy array to analyze function")
print("Passing numpy array to verify function") img = cv2.imread("dataset/img1.jpg")
resp_obj = DeepFace.analyze(img)
print(resp_obj)
img1 = cv2.imread("dataset/img1.jpg") print("--------------------------")
img2 = cv2.imread("dataset/img2.jpg")
res = DeepFace.verify(img1, img2) print("Passing numpy array to verify function")
print(res)
print("--------------------------") img1 = cv2.imread("dataset/img1.jpg")
img2 = cv2.imread("dataset/img2.jpg")
print("Passing numpy array to find function") res = DeepFace.verify(img1, img2)
print(res)
img1 = cv2.imread("dataset/img1.jpg") print("--------------------------")
df = DeepFace.find(img1, db_path = "dataset") print("Passing numpy array to find function")
print(df.head()) img1 = cv2.imread("dataset/img1.jpg")
print("--------------------------") df = DeepFace.find(img1, db_path = "dataset")
print(df.head())
print("--------------------------")
self.assertEqual(accuracy >= min_score, False, "A problem on the deepface installation.")
unittest.main(exit=False)