01.08 DeepFace ~顔検索(find) STEP2~

現在使用している顔データベースは、アイドルグループからピックアップした10名の画像を、ネット検索画像をダウンロードして使用します。このため、例えば上半身と顔だけというように同じ画像を重複して使用しているケースがあります。 このため「顔検索 STEP2」は、データベース内にある全てのデータを自身のデータベース内から検索し重複検査も兼ねて行っていきます。

【実施目的】
・ データベース内の重複した写真のチェック
・ モデル・バックエンド・類似度・正規化で動作不能・エラーのチェック
・ CUDAの使用状況を確認
・ 全体的処理速度の基準の作成


【動作仕様】
・ 対象データベースの画像ファイルを最初から順に1つ取り出します。
・ 取り出した画像に対しデータベース検索します。
・ 絶対パス付検索結果がピックアップしたファイル名と一致・不一致・エラーをカウント。
・ エラー又は、不一致の場合はコンソールにファイル名を表示します。

★ 奇妙な異常事態

事前調査全検索で意味不明なエラーが出るため、最初はレベルを下げ検索対象を1つとして、全てのモデル・バックエンドを変更したサンプルプログラムを作成しました。 以降のサンプルプログラムは関数「deepface_find」より前部は同一使用の為、以降は省略します。

【DF_FaceFind_testALL_03.py】
#coding: utf-8
# ////////////////////////////////////////////////////////////////////////////
# ///【DeepFace・顔検索~エラーファイル全パターンチェック~】
# ////////////////////////////////////////////////////////////////////////////
if "__file__" in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
    sys.path.append(os.path.join(os.path.dirname(__file__), "..") + "//..//"+ "//..//")
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from DeZero.common.nlp_util import *
tensorflow_warning_cancel()
import cv2
import pandas as pd
import datetime
from deepface import DeepFace
from tqdm import tqdm

# ////////////////////////////////////////////////////////////////////////////
# /// [顔検索(find)]
# ////////////////////////////////////////////////////////////////////////////
def deepface_find(img1_path, db_path, model, metric, backend, normalization, silent):
    try:
        list_df = DeepFace.find(
            img_path = img1_path,               # img_path: Union[str, np.ndarray]
            db_path  = db_path,                 # db_path: str
            model_name = model,                 # model_name: str = "VGG-Face"
            distance_metric = metric,           # distance_metric: str = "cosine"
            enforce_detection=False,            # enforce_detection: bool = True ←②
            detector_backend = backend,         # detector_backend: str = "opencv"
            align=True,                         # align: bool = True
            expand_percentage=0,                # expand_percentage: int = 0
            threshold=None,                     # threshold: Optional[float] = None
            normalization=normalization,        # normalization: str = "base"
            silent=silent                       # silent: bool = False
        )

        # /// V1.2A 2024/03/20 検索できないケースがある
        # ////////////////////////////////////////////////////////////////////////////
        if  list_df == []:
            return "ERROR"
        else:
            df = list_df[0]
            pd.set_option('display.max_rows', 1000)
            pd.set_option('display.max_columns', 4096)
            df.head()
            dataframe = pd.DataFrame(df)

            matched = ""
            if  df.shape[0] > 0:
                matched = df.iloc[0].identity
            return matched

    except:
        return "ERROR"


if __name__ == "__main__":
    StartTime = datetime.datetime.now()
    # /// 顔認識モデル(models)
    # ////////////////////////////////////////////////////////////////////////////
    models = [
      "VGG-Face",
      "Facenet",
      "Facenet512",
      "OpenFace",
      "DeepFace",
      "DeepID",
      "ArcFace",
      "Dlib",
      "SFace",
    ]

    # /// バックエンド(backends)
    # ////////////////////////////////////////////////////////////////////////////
    backends = [
      'opencv',
      'ssd',
      'dlib',
      'mtcnn',
      'retinaface',
      'mediapipe',
      'yolov8',
      'yunet',
      'fastmtcnn',
    ]

    # /// 類似度( metrics)
    # ////////////////////////////////////////////////////////////////////////////
    # metrics = ["cosine", "euclidean", "euclidean_l2"]
    metrics = ["cosine"]

    # /// 正規化(normalizations)
    # ////////////////////////////////////////////////////////////////////////////
    # normalizations = ["base", "raw", "Facenet", "Facenet2018", "VGGFace", "VGGFace2", "ArcFace"]
    normalizations = ["base"]

    img1_path = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace\\nogi01\\index.jpg"
    db_path   = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace"
    dest_img  = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace\\nogi01\\index.jpg"

    loop_cnt = len(models) * len(backends) * len(metrics) * len(normalizations)
    progress_tqdm = tqdm(total=loop_cnt, unit="count")
    loop_cnt = len(models) * len(backends) * len(metrics) * len(normalizations)
    progress_tqdm = tqdm(total=loop_cnt, unit="count")

    for model  in models:
        for backend  in backends:
            for metric  in metrics:
                for normalization  in normalizations:
                    progress_tqdm.update(1)

                    # /// 顔検索(find)
                    # ////////////////////////////////////////////////////////////////////////////
                    matched = deepface_find(img1_path, db_path, model, metric, backend, normalization, silent=True)
                    if  matched == "ERROR":
                        print("ERROR : " , model,",",backend,",",metric,",",normalization,", ERROR")
                    else:

                        # /// 結果評価
                        # ////////////////////////////////////////////////////////////////////////////
                        if  matched == dest_img:
                            pass
                        else:
                            print("NG : ", model,",",backend,",",metric,",",normalization,",",matched)

    progress_tqdm.close()

今回起こった奇妙な現象は、処理が全体の7%(6ケース目)で停止します。
停止位置はバックエンド「YOLO8」のようです。 ちなみに、この試験はGPUを使用しないPCで試験していますが、GPU有でも同様の結果でした。

# case : no GPU簡易版
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_03.py
#   4%|██▎                                                           | 3/81 [00:03<01:44,  1.34s/count]NG :  VGG-Face , dlib , cosine , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
# 1/1 [==============================] - 0s 89ms/step                | 4/81 [00:05<01:51,  1.45s/count]
# 1/1 [==============================] - 0s 62ms/step
# 1/1 [==============================] - 0s 16ms/step
# 1/1 [==============================] - 0s 17ms/step
# 1/1 [==============================] - 0s 14ms/step
# 1/1 [==============================] - 0s 13ms/step
# 1/1 [==============================] - 0s 15ms/step
# 1/1 [==============================] - 0s 14ms/step
# 4/4 [==============================] - 0s 3ms/step
# 1/1 [==============================] - 0s 85ms/step
#   7%|████▌                                                         | 6/81 [00:13<03:53,  3.11s/count]
# INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
# NG :  VGG-Face , mediapipe , cosine , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi05\10413871.jpg

★ 全てのデータ検証

同じ試験をデータベースにある全てのデータで検証してみることにしました。

【DF_FaceFind_testALL_check01.py】
if __name__ == "__main__":
    StartTime = datetime.datetime.now()
    # /// 顔認識モデル(models)
    # ////////////////////////////////////////////////////////////////////////////
    models = [
      "VGG-Face",
      "Facenet",
      "Facenet512",
      "OpenFace",
      "DeepFace",
      "DeepID",
      "ArcFace",
      "Dlib",
      "SFace",
    ]

    # /// バックエンド(backends)
    # ////////////////////////////////////////////////////////////////////////////
    backends = [
      'opencv',
      'ssd',
      'dlib',
      'mtcnn',
      'retinaface',
      'yolov8',
      'yunet',
      'fastmtcnn',
      'mediapipe',
    ]

    # /// 類似度( metrics)
    # ////////////////////////////////////////////////////////////////////////////
    # metrics = ["cosine", "euclidean", "euclidean_l2"]
    metrics = ["cosine"]

    # /// 正規化(normalizations)
    # ////////////////////////////////////////////////////////////////////////////
    # normalizations = ["base", "raw", "Facenet", "Facenet2018", "VGGFace", "VGGFace2", "ArcFace"]
    normalizations = ["base"]

    db_path     = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace"

    # /// フォルダー内ファイル検索
    # ////////////////////////////////////////////////////////////////////////////
    file_lists = glob.glob(db_path + "\\*\\*.jpg")
    loop_cnt = len(file_lists) * len(models) * len(backends) * len(metrics) * len(normalizations)
    progress_tqdm = tqdm(total=loop_cnt, unit="count")

    for img1_path in file_lists:
        count_all = 0
        count_ok  = 0
        count_ng  = 0
        count_er  = 0

        ng_flag     = False
        break_flag  = False
        ng_img      = ""
        for model  in models:
            for backend  in backends:
                for metric  in metrics:
                    for normalization  in normalizations:
                        progress_tqdm.update(1)
                        count_all += 1
                        # /// 顔検索(find)
                        # ////////////////////////////////////////////////////////////////////////////
                        matched = deepface_find(img1_path, db_path, model, metric, backend, normalization, silent=True)
                        if  matched == "ERROR":
                            count_er += 1
                            ng_img  = matched
                            print("=> ,error,", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                            ng_flag = True
                        else:
                            # /// 同一写真がないか確認
                            # ////////////////////////////////////////////////////////////////////////////
                            if  matched == img1_path:
                                count_ok += 1
                            else:
                                count_ng += 1
                                ng_img  = matched

                                print(matched , img1_path)
                                print("=> ,no matched, ", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                                ng_flag = True
                                break_flag  = True
                        if break_flag:
                            break
                    if break_flag:
                        break
                if break_flag:
                    break
            if break_flag:
                break

    progress_tqdm.close()

今度はバックエンド「dlib」「mediapipe」等で不一致が発生し、同じように6%(106ケース目)で停止しています。停止位置は同様にモデル「VGG-Face」でバックエンド「mediapipe」があることはわかります。
どうやら条件を変えると、エラー発生個所が変化しているということになります。

# case : no GPU全検索
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_03.py
#   3%|█▍                                                         | 43/1701 [00:59<38:07,  1.38s/count]
# NG :  VGG-Face , dlib , cosine , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
#   3%|█▌                                                         | 44/1701 [01:01<40:52,  1.48s/count]
# NG :  VGG-Face , dlib , cosine , raw , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
#   3%|█▋                                                         | 50/1701 [01:09<38:11,  1.39s/count]
# NG :  VGG-Face , dlib , euclidean , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
#   3%|█▊                                                         | 51/1701 [01:10<38:23,  1.40s/count]
# NG :  VGG-Face , dlib , euclidean , raw , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
#   3%|█▉                                                         | 57/1701 [01:19<38:13,  1.39s/count]
# NG :  VGG-Face , dlib , euclidean_l2 , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
#   3%|██                                                         | 58/1701 [01:20<38:02,  1.39s/count]
# NG :  VGG-Face , dlib , euclidean_l2 , raw , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\E8Ob2I6UcAEm6MP.jpg
# 1/1 [==============================] - 0s 90ms/step             | 64/1701 [01:29<37:35,  1.38s/count]
# 1/1 [==============================] - 0s 61ms/step
# 1/1 [==============================] - 0s 20ms/step

# 1/1 [==============================] - 0s 16ms/step
# 4/4 [==============================] - 0s 3ms/step
# 1/1 [==============================] - 0s 19ms/step
#   6%|███▍                                                    | 106/1701 [03:10<1:15:04,  2.82s/count]INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
# NG :  VGG-Face , mediapipe , cosine , base , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi05\10413871.jpg

★ 再び奇妙な現象

どうやら問題は2系統あって、1つは「抽出・認識」ができないケース、もうひとつは実行しているモデル・バックエンドの順番によって不可解な現象が変化して発生するということです。
そこで問題点を絞り込めるように、専用チェックプログラムを作成しGPUの有り無し2つのPCを使用して、同時に試験を行ってみました。(下記コメント「error」は検索結果が空の状態を示します)
GPU無しのPCでは途中「INFO: Created TensorFlow Lite XNNPACK delegate for CPU.」というコメントが出力され、GPUの有無により処理時間に差ができており、およそ10倍終了時間が異なるようになり、GPU無しでは終了までにかなりの時間を要することも分かってきました。

【注意事項】
本来複数のモデルやバックエンドを変えながら行う顔認識は実施されませんので不必要な試験と言えます。但し、注意しないといけないのは複数変更しながらでなくてもエラー表示なしで中断してしまう可能性や不正な不一致や空の結果が出る可能性にあります。

そのために、根本的な原因を初めに探ろうとしているわけです。



・ GPU無し : 無条件起動
モデル「Facenet」で「error」が多発する現象が見られ、前回のモデル「VGG-Face」は通過しバックエンド「ssd」の不一致も無くなり、今度は「fastmtcnn」で空になっています。

# case : (no gpu)
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er01.py
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001_size10 (1).jpg

・ GPU有 : 無条件起動
GPUを使用するPCに変更し実行してみると、今度はバックエンド「mediapipe」「yolov8」「fastmtcnn」が空のためエラーになっています。 どうやらGPU・CPU使用で違いがあり、それ原因はどうやらモデルに問題があるのではなく、バックエンドに問題にあるのではないかと思われます。

# CASE 1 : 
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , mediapipe , cosine , raw , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , yolov8 , cosine , raw , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , fastmtcnn , cosine , raw , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg

・ GPU有 : バックエンド「mediapipe」「yolov8」「fastmtcnn」のみ試験
バックエンド「mediapipe」「yolov8」「fastmtcnn」のみにして試験してみました。
バックエンド「mediapipe」はエラーとなりGPU有無にかかわらずエラーになるため使用不能であることが確定しました。一方、バックエンド「yolov8」「fastmtcnn」はエラーではなくなりました。

# CASE 2 : 
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# VGG-Face , yolov8 , cosine , base , True
# VGG-Face , fastmtcnn , cosine , base , True
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
# VGG-Face , yolov8 , cosine , base , True
# VGG-Face , fastmtcnn , cosine , base , True

・ GPU有 : 再度全バックエンド試験
今度はバックエンド「ssd」でエラーが発生していますが、一回目は正常で二回目以降がエラーになりました。

# case 3 : 
# VGG-Face , opencv , cosine , base , True
# VGG-Face , ssd , cosine , base , True
# VGG-Face , dlib , cosine , base , True
# VGG-Face , mtcnn , cosine , base , True
# VGG-Face , retinaface , cosine , base , True
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# VGG-Face , yunet , cosine , base , True
# => ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , ssd , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg

・ GPU有 : バックエンド「YOLO8」単独試験
バックエンド「YOLO8」単独で実験したらどうなるでしょう。
このように正常に動作します。

# case 5 : 
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base

・ GPU有 : バックエンド「mediapipe」単独試験
バックエンド「mediapipe」単独で実験したらどうなるでしょう。
バックエンド「mediapipe」はどうなっても「find」メソッドはエラーになります。

# case 6 : 
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001.jpg
# => ,error, VGG-Face , mediapipe , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001_size10 (1).jpg

・ GPU有 : バックエンド「fastmtcnn」単独試験
バックエンド「fastmtcnn」単独で実験したらどうなるでしょう。
バックエンド「fastmtcnn」は前半のいくつかは正常終了しましたが、後半エラーが発生します。

# case 7 : 
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\1.jpg

・ GPU有 : バックエンド「YOLO8」「fastmtcnn」で試験
バックエンド「YOLO8」「fastmtcnn」の2つだけで試験すると、正常に終了することが分かります。

# case 8 : 
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base
# => ,True, VGG-Face , yolov8 , cosine , base
# => ,True, VGG-Face , fastmtcnn , cosine , base

・ GPU有 : バックエンド「retinaface」「YOLO8」「yunet」「fastmtcnn」で試験

# case 9 : yolov8/fastmtcnnはretinafaceが入るとエラーになる
# (py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01.py
# 0%| | 0/1488 [00:00<?, ?count/s]
# => ,True, VGG-Face , retinaface , cosine , base
# => ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,True, VGG-Face , yunet , cosine , base
# => ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg

段々と見えてきたのは、

    ・ 同じデータを使用した事例ですが違う結果となる
    ・ バックエンドの並びと組み合わせに関係している
    ・ GPU=CUDAによる原因では無さそう
    ・ ケースによって、結果が空または不一致という現象になる

これらのことから導き出される結論は、
    ・ バックエンドの並びや組み合わせが原因
    ・ 発生はバックエンド「retinaface」「YOLO8」「yunet」「fastmtcnn」らしい
    ・ バックエンド「YOLO8」「fastmtcnn」は、それ以外のモデルをサンドイッチされると
        以降の作業がキャンセルされる、または無応答になる

そこで、さらに詳細な試験を実施しました。

★ DeepFaceバックエンド怪現象の解明

この怪現象は、最初は正常でも二回目以降で発生したり、何かがきっかけとなって発生する場合があるということです。 現在までで怪現象を推理すると、CUDAには直接的原因はなさそうですが、モデル・バックエンドの組み合わせでCUDAアーキテクチャの有無が分かれます。以前Kerasを使用した場合順伝播と逆伝播間の内部パラメータ残留により、メモリー消費が増幅する問題がありましたが、同様に残留物が他のバックエンドに干渉する可能性を考えました。
このため使用モデルを2つに限定し処理時間短縮を図り、バックエンド「YOLO8」「fastmtcnn」を、それ以外のバックエンドでサンドイッチしたシンプルモデルを作成し、必ずエラーになるパターンのサンプルプログラムで試験します。

【DF_FaceFind_testALL_check01_er01.py】
if __name__ == "__main__":
    StartTime = datetime.datetime.now()
    # /// 顔認識モデル(models)
    # ////////////////////////////////////////////////////////////////////////////
    models = [
      "VGG-Face",
      "Facenet",
      # "Facenet512",
      # "OpenFace",
      # "DeepFace",
      # "DeepID",
      # "ArcFace",
      # "Dlib",
      # "SFace",
    ]

    # /// バックエンド(backends)
    # ////////////////////////////////////////////////////////////////////////////
    backends = [
      # 'opencv',
      # 'ssd',
      # 'dlib',
      # 'mtcnn',
      'retinaface',
      'yolov8',
      'yunet',
      'fastmtcnn',
      # 'mediapipe',
    ]

    # /// 類似度( metrics)
    # ////////////////////////////////////////////////////////////////////////////
    metrics = ["cosine"]

    # /// 正規化(normalizations)
    # ///   入力画像をモデルに供給する前に正規化します
    # ////////////////////////////////////////////////////////////////////////////
    normalizations = ["base"]

    db_path     = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace"

    # /// フォルダー内ファイル検索
    # ////////////////////////////////////////////////////////////////////////////
    file_lists = glob.glob(db_path + "\\*\\*.jpg")
    loop_cnt = len(file_lists) * len(models) * len(backends) * len(metrics) * len(normalizations)
    progress_tqdm = tqdm(total=loop_cnt, unit="count")

    for img1_path in file_lists:
        count_all = 0
        count_ok  = 0
        count_ng  = 0
        count_er  = 0

        ng_flag     = False
        break_flag  = False
        ng_img      = ""
        for model  in models:
            for backend  in backends:
                for metric  in metrics:
                    for normalization  in normalizations:
                        progress_tqdm.update(1)
                        count_all += 1
                        matched = deepface_find(img1_path, db_path, model, metric, backend, normalization, silent=True)
                        if  matched == "ERROR":
                            count_er += 1
                            ng_img  = matched
                            print("=> ,error,", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                            ng_flag = True
                        else:
                            if  matched == img1_path:
                                count_ok += 1
                            else:
                                count_ng += 1
                                ng_img  = matched

                                print(matched , img1_path)
                                print("=> ,no matched, ", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                                ng_flag = True

    progress_tqdm.close()


【動作結果 GPU版】
バックエンド「mediapipe」を削除しているため、前述同様「YOLO8」「fastmtcnn」のみが、最初からエラーになっていることが分かります。

# => ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, Facenet , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg


【動作結果 CPU版】
CPU版の場合では、バックエンド「fastmtcnn」のみがエラーになっています。
これによってエラーは二種類あって、
    ・  バックエンド「fastmtcnn」と「YOLO8」は、内部データ干渉
    ・ 「YOLO8」はCUDAが関連している

つまり、バックエンド「YOLO8」「fastmtcnn」は旧バージョンにはなく、今回CUDAバージョンも比較的新しい版を使用しています。この新しいバックエンドが、古い版のバックエンドとの共用を許していないということ、そしてGPU無しtensorflowではCUDAを使用していないためエラーにはならないのではないかと想像されます。

# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001_size10 (1).jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001_size10.jpg
# => ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\001_size9.jpg

★ 【問題解決】GPUの影響

少なくともバックエンド「YOLO8」はCUDAの影響を受けている可能性が高くなってきました。
CUDAを使用しているのはtensorflowであるため、実際にtensorflowがKerasを使用しているかどうかは不明ですが、とりあえずガベージコレクションおよびKerasの内部データクリアを行ってエラーを回避してみましたが、結果は同様にエラーを発生しました。

【DF_FaceFind_testALL_check01_er02.py】
import gc
import keras

        gc.collect()
        keras.backend.clear_session()
    progress_tqdm.close()

★ バックエンドグループ分け

これらを受けて、問題となるバックエンドを問題の有無を受けてグループ化してみました。

・ ケース1 : 他のバックエンドの干渉を受けない
・ ケース2 : 他のバックエンドの干渉を受ける
・ ケース3 : エラーとなる

【DF_FaceFind_testALL_check01_er03.py】
if __name__ == "__main__":
    StartTime = datetime.datetime.now()
    # /// 顔認識モデル(models)
    # ////////////////////////////////////////////////////////////////////////////
   models = [
      "VGG-Face",
      "Facenet",
      # "Facenet512",
      # "OpenFace",
      # "DeepFace",
      # "DeepID",
      # "ArcFace",
      # "Dlib",
      # "SFace",
    ]

    # /// バックエンド(backends)
    # ////////////////////////////////////////////////////////////////////////////
    backends = [
      # case 1:
      # 'opencv',
      # 'dlib',
      # 'mtcnn',
      # 'retinaface',
      # 'yunet',

      # case 2:
      'yolov8',
      'fastmtcnn',

      # case 3: error
      # 'ssd',
      # 'mediapipe',
    ]

    # /// 類似度( metrics)
    # ////////////////////////////////////////////////////////////////////////////
    # metrics = ["cosine", "euclidean", "euclidean_l2"]
    metrics = ["cosine"]

    # /// 正規化(normalizations)
    # ////////////////////////////////////////////////////////////////////////////
    # normalizations = ["base", "raw", "Facenet", "Facenet2018", "VGGFace", "VGGFace2", "ArcFace"]
    normalizations = ["base"]

    db_path     = "d:\\VisualStudio2017\\Python3.5_GPU\\test_dataset\\DeepFace"

    # /// フォルダー内ファイル検索
    # ////////////////////////////////////////////////////////////////////////////
    file_lists = glob.glob(db_path + "\\*\\*.jpg")
    loop_cnt = len(file_lists) * len(models) * len(backends) * len(metrics) * len(normalizations)
    progress_tqdm = tqdm(total=loop_cnt, unit="count")

    for backend  in backends:
        cnt = 0
        for img1_path in file_lists:
            count_all = 0
            count_ok  = 0
            count_ng  = 0
            count_er  = 0

            cnt +=1
            if  cnt > 2:
                continue

            ng_flag     = False
            break_flag  = False
            ng_img      = ""
            for model  in models:
                # for backend  in backends:
                for metric  in metrics:
                    for normalization  in normalizations:
                        progress_tqdm.update(1)
                        count_all += 1
                        # /// 顔検索(find)
                        # ////////////////////////////////////////////////////////////////////////////
                        matched = deepface_find(img1_path, db_path, model, metric, backend, normalization, silent=True)
                        if  matched == "ERROR":
                            count_er += 1
                            ng_img  = matched
                            print("=> ,error,", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                            ng_flag = True
                        else:
                            # /// 同一写真がないか確認
                            # ////////////////////////////////////////////////////////////////////////////
                            if  matched == img1_path:
                                count_ok += 1
                            else:
                                count_ng += 1
                                ng_img  = matched

                                print(matched , img1_path)
                                print("=> ,no matched, ", model,",",backend,",",metric,",",normalization,",",matched,",",img1_path)
                                ng_flag = True
    progress_tqdm.close()

★ グループ分け動作試験

現在運用しているPCOASを観てみると、

# DeepFace学習済モデル :
["VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", "Ensemble"]
model_name ="VGG-Face"

# DeepFaceメトリック :
["cosine", "euclidean", "euclidean_l2"]
distance_metric ="cosine"

# DeepFaceディテクタ :
["opencv", "ssd", "dlib", "mtcnn", "retinaface", "mediapipe"]
# detector_backend ="mtcnn"
# detector_backend ="opencv"
detector_backend ="ssd"

エラーグループにあるケース3の「ssd」を使用しており。旧バージョンDeepFaceでは動作していましたが、新しいCUDA11を使用するDeepFaceでは動作しない可能性も出てきました。

そこで、 もう一度 バックエンド複合的使用時のグループを確認するとともに、グループ別で試験します。 同時に グループを跨いで使用した場合、不一致・空の結果・エラー停止等が再現されることを再確認しましょう。

case 1:
    'opencv','dlib','mtcnn','retinaface','yunet',

case 2:
    'yolov8','fastmtcnn',

case 3:
    'ssd','mediapipe',



【グループ間干渉実験】
# [result]
case :
# 'opencv',
# 'dlib',
# 'mtcnn', 
'retinaface',
'yunet',

'yolov8', 
'fastmtcnn',

# 'ssd',
# 'mediapipe',
(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
=> ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, Facenet , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, Facenet , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg

case :
# 'opencv',
# 'dlib',
# 'mtcnn', 
# 'retinaface',
'yunet',

'yolov8', 
'fastmtcnn',

# 'ssd',
# 'mediapipe',
(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
0%| | 12/8928 [08:59<111:15:16, 44.92s/count]


case :
'opencv',
# 'dlib',
# 'mtcnn', 
# 'retinaface',
'yunet',

'yolov8', 
'fastmtcnn',

# 'ssd',
# 'mediapipe',
(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
=> ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, Facenet , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, VGG-Face , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, Facenet , yolov8 , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0000.jpg
=> ,error, VGG-Face , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, Facenet , fastmtcnn , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg


case :
'opencv',
'dlib',
'mtcnn', 
'retinaface',
# 'yunet',

# 'yolov8', 
# 'fastmtcnn',

'ssd',
# 'mediapipe',
(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
=> ,error, VGG-Face , ssd , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
=> ,error, Facenet , ssd , cosine , base , ERROR , d:\VisualStudio2017\Python3.5_GPU\test_dataset\DeepFace\nogi01\0001.jpg
1/1 [==============================] - 1s 661ms/step | 13/14880 [00:17<3:27:26, 1.19count/s]
1/1 [==============================] - 0s 178ms/step
1/1 [==============================] - 0s 116ms/step
1/1 [==============================] - 0s 112ms/step
1/1 [==============================] - 0s 113ms/step
1/1 [==============================] - 0s 114ms/step
1/1 [==============================] - 0s 114ms/step
3/3 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 187ms/step
1/1 [==============================] - 0s 17ms/step | 14/14880 [00:21<7:16:35, 1.76s/count]
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
3/3 [==============================] - 0s 2ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 17ms/step | 15/14880 [00:21<6:00:46, 1.46s/count]
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 15ms/step
3/3 [==============================] - 0s 92ms/step
1/1 [==============================] - 0s 141ms/step
1/1 [==============================] - 0s 16ms/step | 16/14880 [00:23<6:26:49, 1.56s/count]
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
3/3 [==============================] - 0s 2ms/step
1/1 [==============================] - 0s 17ms/step


グループを跨がなければ、エラーが出ないこと確認されました。

# [result]
case :
'opencv',
'dlib',
'mtcnn',
'retinaface',
'yunet',

# 'yolov8',
# 'fastmtcnn',

# 'ssd',
# 'mediapipe',
(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
1/1 [==============================] - 1s 758ms/step | 9/14880 [00:15<4:26:42, 1.08s/count]
1/1 [==============================] - 0s 182ms/step
1/1 [==============================] - 0s 113ms/step
1/1 [==============================] - 0s 111ms/step
1/1 [==============================] - 0s 112ms/step
1/1 [==============================] - 0s 110ms/step
1/1 [==============================] - 0s 111ms/step
3/3 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 187ms/step
1/1 [==============================] - 0s 17ms/step | 10/14880 [00:19<8:08:26, 1.97s/count]
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
3/3 [==============================] - 0s 2ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 18ms/step | 11/14880 [00:20<6:33:34, 1.59s/count]
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 17ms/step
3/3 [==============================] - 0s 115ms/step
1/1 [==============================] - 0s 137ms/step
1/1 [==============================] - 0s 17ms/step | 12/14880 [00:22<6:50:34, 1.66s/count]
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 17ms/step
3/3 [==============================] - 0s 2ms/step
1/1 [==============================] - 0s 17ms/step
0%| | 20/14880 [00:37<7:39:32, 1.86s/count]


case :
# 'opencv',
# 'dlib',
# 'mtcnn', 
# 'retinaface',

'yolov8',
'fastmtcnn',

# 'ssd',
# 'mediapipe',

(py39) d:\VisualStudio2017\Python3.5_GPU\Sample_TEST\顔認識2024\03_DF_FaceFind>python DF_FaceFind_testALL_check01_er03.py
0%| | 8/5952 [00:18<3:55:01, 2.37s/count]


≪清須電脳倶楽部メインページへ戻る場合はここをクリックしてください≫
Copyright(c)2018 GGE Kiyosu Cyber Club Allrights Reserved
inserted by FC2 system