mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
Converted to unittest
This commit is contained in:
parent
6622d84a91
commit
34c2dbef17
2
.github/workflows/tests.yml
vendored
2
.github/workflows/tests.yml
vendored
@ -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
|
@ -9,177 +9,182 @@ 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):
|
||||||
|
|
||||||
|
def test_deepface(self):
|
||||||
|
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.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
|
||||||
from deepface.extendedmodels import Age, Gender, Race, Emotion
|
from deepface.extendedmodels import Age, Gender, Race, Emotion
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
|
|
||||||
print("DeepFace.detectFace test")
|
print("DeepFace.detectFace test")
|
||||||
#detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface']
|
#detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface']
|
||||||
detectors = ['opencv', 'ssd', 'mtcnn', 'retinaface']
|
detectors = ['opencv', 'ssd', 'mtcnn', 'retinaface']
|
||||||
|
|
||||||
for detector in detectors:
|
for detector in detectors:
|
||||||
img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector)
|
img = DeepFace.detectFace("dataset/img11.jpg", detector_backend = detector)
|
||||||
print(detector," test is done")
|
print(detector," test is done")
|
||||||
|
|
||||||
#import matplotlib.pyplot as plt
|
#import matplotlib.pyplot as plt
|
||||||
#plt.imshow(img)
|
#plt.imshow(img)
|
||||||
#plt.show()
|
#plt.show()
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
img_path = "dataset/img1.jpg"
|
img_path = "dataset/img1.jpg"
|
||||||
embedding = DeepFace.represent(img_path)
|
embedding = DeepFace.represent(img_path)
|
||||||
print("Function returned ", len(embedding), "dimensional vector")
|
print("Function returned ", len(embedding), "dimensional vector")
|
||||||
|
|
||||||
model_name = "VGG-Face"
|
model_name = "VGG-Face"
|
||||||
model = DeepFace.build_model(model_name)
|
model = DeepFace.build_model(model_name)
|
||||||
print(model_name," is built")
|
print(model_name," is built")
|
||||||
embedding = DeepFace.represent(img_path, model = model)
|
embedding = DeepFace.represent(img_path, model = model)
|
||||||
print("Represent function returned ", len(embedding), "dimensional vector")
|
print("Represent function returned ", len(embedding), "dimensional vector")
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
|
|
||||||
dataset = [
|
dataset = [
|
||||||
['dataset/img1.jpg', 'dataset/img2.jpg', True],
|
['dataset/img1.jpg', 'dataset/img2.jpg', True],
|
||||||
['dataset/img1.jpg', 'dataset/img6.jpg', True]
|
['dataset/img1.jpg', 'dataset/img6.jpg', True]
|
||||||
]
|
]
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Face detectors test")
|
print("Face detectors test")
|
||||||
|
|
||||||
print("retinaface detector")
|
print("retinaface detector")
|
||||||
res = DeepFace.verify(dataset, detector_backend = 'retinaface')
|
res = DeepFace.verify(dataset, detector_backend = 'retinaface')
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
print("ssd detector")
|
print("ssd detector")
|
||||||
res = DeepFace.verify(dataset, detector_backend = 'ssd')
|
res = DeepFace.verify(dataset, detector_backend = 'ssd')
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
print("opencv detector")
|
print("opencv detector")
|
||||||
res = DeepFace.verify(dataset, detector_backend = 'opencv')
|
res = DeepFace.verify(dataset, detector_backend = 'opencv')
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
print("dlib detector")
|
print("dlib detector")
|
||||||
res = DeepFace.verify(dataset, detector_backend = 'dlib')
|
res = DeepFace.verify(dataset, detector_backend = 'dlib')
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
print("mtcnn detector")
|
print("mtcnn detector")
|
||||||
res = DeepFace.verify(dataset, detector_backend = 'mtcnn')
|
res = DeepFace.verify(dataset, detector_backend = 'mtcnn')
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Single find function test")
|
print("Single find function test")
|
||||||
|
|
||||||
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
|
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
|
||||||
#, model_name = 'Dlib'
|
#, model_name = 'Dlib'
|
||||||
)
|
)
|
||||||
print(df.head())
|
print(df.head())
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Pre-built model for single find function test")
|
print("Pre-built model for single find function test")
|
||||||
|
|
||||||
#model_name = "VGG-Face"
|
#model_name = "VGG-Face"
|
||||||
#model = DeepFace.build_model(model_name)
|
#model = DeepFace.build_model(model_name)
|
||||||
#print(model_name," is built")
|
#print(model_name," is built")
|
||||||
|
|
||||||
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
|
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset"
|
||||||
, model_name = model_name, model = model
|
, model_name = model_name, model = model
|
||||||
)
|
)
|
||||||
print(df.head())
|
print(df.head())
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Bulk find function tests")
|
print("Bulk find function tests")
|
||||||
|
|
||||||
dfs = DeepFace.find(img_path = ["dataset/img1.jpg", "dataset/img2.jpg"], db_path = "dataset"
|
dfs = DeepFace.find(img_path = ["dataset/img1.jpg", "dataset/img2.jpg"], db_path = "dataset"
|
||||||
#, model_name = 'Dlib'
|
#, model_name = 'Dlib'
|
||||||
)
|
)
|
||||||
print(dfs[0].head())
|
print(dfs[0].head())
|
||||||
print(dfs[1].head())
|
print(dfs[1].head())
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Bulk verification tests")
|
print("Bulk verification tests")
|
||||||
|
|
||||||
resp_obj = DeepFace.verify(dataset)
|
resp_obj = DeepFace.verify(dataset)
|
||||||
print(resp_obj)
|
print(resp_obj)
|
||||||
print(resp_obj["pair_1"]["verified"] == True)
|
print(resp_obj["pair_1"]["verified"] == True)
|
||||||
print(resp_obj["pair_2"]["verified"] == True)
|
print(resp_obj["pair_2"]["verified"] == True)
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Bulk facial analysis tests")
|
print("Bulk facial analysis tests")
|
||||||
|
|
||||||
dataset = [
|
dataset = [
|
||||||
'dataset/img1.jpg',
|
'dataset/img1.jpg',
|
||||||
'dataset/img2.jpg',
|
'dataset/img2.jpg',
|
||||||
'dataset/img5.jpg',
|
'dataset/img5.jpg',
|
||||||
'dataset/img6.jpg'
|
'dataset/img6.jpg'
|
||||||
]
|
]
|
||||||
|
|
||||||
resp_obj = DeepFace.analyze(dataset)
|
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_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_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_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(resp_obj["instance_4"]["age"]," years old ", resp_obj["instance_4"]["dominant_emotion"], " ",resp_obj["instance_4"]["gender"])
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Facial analysis test. Passing nothing as an action")
|
print("Facial analysis test. Passing nothing as an action")
|
||||||
|
|
||||||
img = "dataset/img4.jpg"
|
img = "dataset/img4.jpg"
|
||||||
demography = DeepFace.analyze(img)
|
demography = DeepFace.analyze(img)
|
||||||
print(demography)
|
print(demography)
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Facial analysis test. Passing all to the action")
|
print("Facial analysis test. Passing all to the action")
|
||||||
demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion'])
|
demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion'])
|
||||||
|
|
||||||
print("Demography:")
|
print("Demography:")
|
||||||
print(demography)
|
print(demography)
|
||||||
|
|
||||||
#check response is a valid json
|
#check response is a valid json
|
||||||
print("Age: ", demography["age"])
|
print("Age: ", demography["age"])
|
||||||
print("Gender: ", demography["gender"])
|
print("Gender: ", demography["gender"])
|
||||||
print("Race: ", demography["dominant_race"])
|
print("Race: ", demography["dominant_race"])
|
||||||
print("Emotion: ", demography["dominant_emotion"])
|
print("Emotion: ", demography["dominant_emotion"])
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Facial analysis test 2. Remove some actions and check they are not computed")
|
print("Facial analysis test 2. Remove some actions and check they are not computed")
|
||||||
demography = DeepFace.analyze(img, ['age', 'gender'])
|
demography = DeepFace.analyze(img, ['age', 'gender'])
|
||||||
|
|
||||||
print("Age: ", demography.get("age"))
|
print("Age: ", demography.get("age"))
|
||||||
print("Gender: ", demography.get("gender"))
|
print("Gender: ", demography.get("gender"))
|
||||||
print("Race: ", demography.get("dominant_race"))
|
print("Race: ", demography.get("dominant_race"))
|
||||||
print("Emotion: ", demography.get("dominant_emotion"))
|
print("Emotion: ", demography.get("dominant_emotion"))
|
||||||
|
|
||||||
|
|
||||||
print("-----------------------------------------")
|
print("-----------------------------------------")
|
||||||
|
|
||||||
print("Face recognition tests")
|
print("Face recognition tests")
|
||||||
|
|
||||||
dataset = [
|
dataset = [
|
||||||
['dataset/img1.jpg', 'dataset/img2.jpg', True],
|
['dataset/img1.jpg', 'dataset/img2.jpg', True],
|
||||||
['dataset/img5.jpg', 'dataset/img6.jpg', True],
|
['dataset/img5.jpg', 'dataset/img6.jpg', True],
|
||||||
['dataset/img6.jpg', 'dataset/img7.jpg', True],
|
['dataset/img6.jpg', 'dataset/img7.jpg', True],
|
||||||
@ -191,15 +196,15 @@ dataset = [
|
|||||||
['dataset/img2.jpg', 'dataset/img3.jpg', False],
|
['dataset/img2.jpg', 'dataset/img3.jpg', False],
|
||||||
['dataset/img6.jpg', 'dataset/img8.jpg', False],
|
['dataset/img6.jpg', 'dataset/img8.jpg', False],
|
||||||
['dataset/img6.jpg', 'dataset/img9.jpg', False],
|
['dataset/img6.jpg', 'dataset/img9.jpg', False],
|
||||||
]
|
]
|
||||||
|
|
||||||
#models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace']
|
#models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace']
|
||||||
models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace'] #those are robust models
|
models = ['VGG-Face', 'Facenet', 'Facenet512', 'ArcFace', 'SFace'] #those are robust models
|
||||||
metrics = ['cosine', 'euclidean', 'euclidean_l2']
|
metrics = ['cosine', 'euclidean', 'euclidean_l2']
|
||||||
|
|
||||||
passed_tests = 0; test_cases = 0
|
passed_tests = 0; test_cases = 0
|
||||||
|
|
||||||
for model in models:
|
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:
|
||||||
@ -233,51 +238,51 @@ 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:
|
if accuracy >= min_score:
|
||||||
print("Unit tests are completed successfully. Score: ",accuracy,"%")
|
print("Unit tests are completed successfully. Score: ",accuracy,"%")
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unit test score does not satisfy the minimum required accuracy. Minimum expected score is ", min_score,"% but this got ",accuracy,"%")
|
raise ValueError("Unit test score does not satisfy the minimum required accuracy. Minimum expected score is ", min_score,"% but this got ",accuracy,"%")
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
|
|
||||||
print("Analyze function with passing pre-trained model")
|
print("Analyze function with passing pre-trained model")
|
||||||
|
|
||||||
emotion_model = DeepFace.build_model("Emotion")
|
emotion_model = DeepFace.build_model("Emotion")
|
||||||
age_model = DeepFace.build_model("Age")
|
age_model = DeepFace.build_model("Age")
|
||||||
gender_model = DeepFace.build_model("Gender")
|
gender_model = DeepFace.build_model("Gender")
|
||||||
race_model = DeepFace.build_model("Race")
|
race_model = DeepFace.build_model("Race")
|
||||||
|
|
||||||
facial_attribute_models = {}
|
facial_attribute_models = {}
|
||||||
facial_attribute_models["emotion"] = emotion_model
|
facial_attribute_models["emotion"] = emotion_model
|
||||||
facial_attribute_models["age"] = age_model
|
facial_attribute_models["age"] = age_model
|
||||||
facial_attribute_models["gender"] = gender_model
|
facial_attribute_models["gender"] = gender_model
|
||||||
facial_attribute_models["race"] = race_model
|
facial_attribute_models["race"] = race_model
|
||||||
|
|
||||||
resp_obj = DeepFace.analyze("dataset/img1.jpg", models=facial_attribute_models)
|
resp_obj = DeepFace.analyze("dataset/img1.jpg", models=facial_attribute_models)
|
||||||
print(resp_obj)
|
print(resp_obj)
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
print("Ensemble for find function")
|
print("Ensemble for find function")
|
||||||
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset", model_name = "Ensemble")
|
df = DeepFace.find(img_path = "dataset/img1.jpg", db_path = "dataset", model_name = "Ensemble")
|
||||||
print(df.head())
|
print(df.head())
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
print("Ensemble for verify function")
|
print("Ensemble for verify function")
|
||||||
resp_obj = DeepFace.verify(dataset, model_name = "Ensemble")
|
resp_obj = DeepFace.verify(dataset, model_name = "Ensemble")
|
||||||
|
|
||||||
@ -287,10 +292,10 @@ if False:
|
|||||||
score = item["score"]
|
score = item["score"]
|
||||||
print(verified)
|
print(verified)
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
|
|
||||||
print("Pre-trained ensemble method - find")
|
print("Pre-trained ensemble method - find")
|
||||||
|
|
||||||
@ -302,43 +307,47 @@ if False:
|
|||||||
|
|
||||||
print(df)
|
print(df)
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
print("Pre-trained ensemble method - verify")
|
print("Pre-trained ensemble method - verify")
|
||||||
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
|
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
#-----------------------------------
|
#-----------------------------------
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
print("Passing numpy array to analyze function")
|
print("Passing numpy array to analyze function")
|
||||||
|
|
||||||
img = cv2.imread("dataset/img1.jpg")
|
img = cv2.imread("dataset/img1.jpg")
|
||||||
resp_obj = DeepFace.analyze(img)
|
resp_obj = DeepFace.analyze(img)
|
||||||
print(resp_obj)
|
print(resp_obj)
|
||||||
|
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
print("Passing numpy array to verify function")
|
print("Passing numpy array to verify function")
|
||||||
|
|
||||||
img1 = cv2.imread("dataset/img1.jpg")
|
img1 = cv2.imread("dataset/img1.jpg")
|
||||||
img2 = cv2.imread("dataset/img2.jpg")
|
img2 = cv2.imread("dataset/img2.jpg")
|
||||||
|
|
||||||
res = DeepFace.verify(img1, img2)
|
res = DeepFace.verify(img1, img2)
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
print("Passing numpy array to find function")
|
print("Passing numpy array to find function")
|
||||||
|
|
||||||
img1 = cv2.imread("dataset/img1.jpg")
|
img1 = cv2.imread("dataset/img1.jpg")
|
||||||
|
|
||||||
df = DeepFace.find(img1, db_path = "dataset")
|
df = DeepFace.find(img1, db_path = "dataset")
|
||||||
|
|
||||||
print(df.head())
|
print(df.head())
|
||||||
|
|
||||||
print("--------------------------")
|
print("--------------------------")
|
||||||
|
|
||||||
|
self.assertEqual(accuracy >= min_score, False, "A problem on the deepface installation.")
|
||||||
|
|
||||||
|
unittest.main(exit=False)
|
Loading…
x
Reference in New Issue
Block a user