金子邦彦研究室人工知能Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア)chen0040/keras-face のインストールと動作確認(顔認識)(TensorFlow 1.15.5,Python 3.7 を使用)(Windows 上)

chen0040/keras-face のインストールと動作確認(顔認識)(TensorFlow 1.15.5,Python 3.7 を使用)(Windows 上)

次で配布されているソフトウェア

https://github.com/chen0040/keras-face

これは,顔検証と顔識別のソフトウェア.DeepFace, VGG16 + Siamese を使用.

手順の要点: 前準備として,NVIDIA CUDA 10.0, NVIDIA cuDNN 7.6.5, Python 3.7, TensorFlow 1.15.5 等をインストール.

ソフトウェア等の利用条件等は,利用者で確認すること.

謝辞:ソフトウェアの作者に感謝します

前準備

Git のインストール(Windows 上)

Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.

サイト内の関連ページ

Windows での Git のインストール: 別ページ »で説明している.

関連する外部ページ

Git の公式ページ: https://git-scm.com/

Python 3.7 64 ビット版のインストール,pip と setuptools の更新,Python 開発環境のインストール(Windows 上)

① Python 3.7 64 ビット版のインストール(Windows 上)

Python のインストールでの注意点

Python 3.7 のインストール手順の詳細(別ページ)

Windows での Python 3.7 のインストール: 別ページ »で説明している.

Python の公式ページ

https://www.python.org/

インストール手順の概要

  1. Python の URL を開く

    URL: https://www.python.org

  2. Windows 版の Python 3.7 をダウンロード

    ページの上の方にある「Downloads」をクリック,「Downloads」の下にメニューが出るので,その中の「Windows」をクリック.

    そして,Python 3.7.x (x は数字)を探す.

    そして,Windows の 64ビット版のインストーラをダウンロードしたいので,「Windows x86-64 executable installer」を選ぶ

    [image]
  3. インストール時の設定
    1. いまダウンロードした .exe ファイルを右クリック, 右クリックメニューで「管理者として実行」を選ぶ.

      [image]
    2. Python ランチャーをインストールするために,「Install launcher for all users (recommended)」をチェック.

      ※ すでに Python ランチャーをインストール済みのときは, 「Install launcher for all users (recommended)」がチェックできないようになっている場合がある.そのときは,チェックせずに進む.

    3. Add Python 3.7 to PATH」をチェック.

      [image]
    4. Customize installation」をクリック.

      [image]
    5. オプションの機能 (Optional Features)は,既定(デフォルト)のままでよい. 「Next」をクリック

      [image]
    6. Install for all users」を選ぶ.

      Python のインストールディレクトリは,「C:\Program Files\Python37」のように自動設定されることを確認.

      Install」をクリック

      [image]
    7. Disable path length limit」が表示される場合がある.クリックして,パス長の制限を解除する

      [image]
    8. インストールが終了したら,「Close」をクリック
  4. インストールのあと,Windows のスタートメニューに「Python 3.7」が増えていることを確認.
  5. システムの環境変数 Path の確認のため,新しくコマンドプロンプトを開き,次のコマンドを実行する.

    pypipパスが通っていることの確認である.

    where py
    where pip
    

    where py では「C:\Windows\py.exe」 が表示され, where pip では「C:\Program Files\Python37\Scripts\pip.exe」 が表示されることを確認.

    [image]

② pip と setuptools の更新(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    Windowspip を実行するときは,コマンドプロンプト管理者として開き,それを使って pip を実行することにする.

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. 次のコマンドを実行する

    Python の使用は「py -3.7」で行う.

    py -3.7 -m pip install -U pip setuptools
    

    [image]

③ Python 開発環境として,Python の隔離された環境に,Python コンソール(Jupyter Qt Console), Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract をインストール

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. venv を用いて,Python の仮想環境を作成

    次のコマンドを実行する

    py -3.7 -m pip install -U pip setuptools
    py -3.7 -m venv C:\venv\py37
    
  3. Python の仮想環境の使用開始と,Python 開発環境のインストール

    次のコマンドを実行する

    Python の使用は「C:\venv\py37\Scripts\activate.bat」の後,「python」で行う.

    C:\venv\py37\Scripts\activate.bat
    python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter
    

Visual Studio Community 2017,NVIDIA ドライバ,NVIDIA CUDA ツールキット 10.0,NVIDIA cuDNN 7.6.5 のインストール(Windows 上)

Windows での Visual Studio Community 2017,NVIDIA ドライバNVIDIA CUDA ツールキット 10.0NVIDIA cuDNN 7.6.5 のインストール: 別ページ »で説明している.

TensorFlow 1.15.5, Keras 2.3.1 のインストール

コマンドプロンプト管理者として実行し,次のコマンドを実行

C:\venv\py37\Scripts\activate.bat
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow-intel tensorflow-text tensorflow-estimator tf-models-official tf_slim tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer scipy pandas matplotlib
# TensorFlow 1.15.5 のため numpy, protobuf の古いバージョンを使用.エラーが出にくいと考えられる numpy 1.16.2, protobuf 3.19.4 を使用
python -m pip install -U numpy==1.16.2 protobuf==3.19.4 tensorflow-gpu==1.15.5 keras==2.3.1 scipy==1.5.4

chen0040/keras-face のインストールと動作確認(顔認識)(TensorFlow 1.15.5,Python 3.7 を使用)(Windows 上)

Windows での手順を下に示す.Ubuntu でも同様の手順になる.

  1. Windows で,コマンドプロンプト管理者として実行
  2. chen0040/keras-face のダウンロード

    mkdir %HOMEPATH%
    cd %HOMEPATH%
    rmdir /s /q keras-face
    

    [image]

    git clone https://github.com/chen0040/keras-face
    cd keras-face
    

    [image]
  3. 前提パッケージのインストール

    C:\venv\py37\Scripts\activate.bat
    python -m pip install -U numpy==1.16.2 matplotlib
    
  4. ファイルの移動

    %HOMEPATH%\keras-face\demo にあるファイルを丸ごと、

    %HOMEPATH%\keras-face移動

    その結果、次のようになる

    [image]
  5. data\images の下に、顔写真のファイルがあるので確認する

    [image]
  6. ファイルの編集

    エラーの回避のため.keras_face\library\face_net.py を次のように書き換える

    「np.set_printoptions(threshold=np.nan)」を削除

    [image]
  7. ディレクトリの移動

    cd %HOMEPATH%
    cd keras-face
    

    [image]
  8. デモプログラム DeepFace を実行

    このプログラムは camera_0.jpg が誰なのかを顔認識する。

    [image]

    GitHub の chen0040/keras-face の Web ページに記載されている次のプログラムを使用

    次のPython プログラムを実行する

    Python プログラムの実行: 別ページ »で説明

    Python のまとめ: 別ページ »にまとめ

    from keras_face.library.face_net import FaceNet
    
    def main():
        model_dir_path = './models'
        image_dir_path = "./data/images"
        #
        fnet = FaceNet()
        fnet.load_model(model_dir_path)
        #
        database = {}
        database["danielle"] = fnet.img_to_encoding(image_dir_path + "/danielle.png")
        database["younes"] = fnet.img_to_encoding(image_dir_path + "/younes.jpg")
        database["tian"] = fnet.img_to_encoding(image_dir_path + "/tian.jpg")
        database["andrew"] = fnet.img_to_encoding(image_dir_path + "/andrew.jpg")
        database["kian"] = fnet.img_to_encoding(image_dir_path + "/kian.jpg")
        database["dan"] = fnet.img_to_encoding(image_dir_path + "/dan.jpg")
        database["sebastiano"] = fnet.img_to_encoding(image_dir_path + "/sebastiano.jpg")
        database["bertrand"] = fnet.img_to_encoding(image_dir_path + "/bertrand.jpg")
        database["kevin"] = fnet.img_to_encoding(image_dir_path + "/kevin.jpg")
        database["felix"] = fnet.img_to_encoding(image_dir_path + "/felix.jpg")
        database["benoit"] = fnet.img_to_encoding(image_dir_path + "/benoit.jpg")
        database["arnaud"] = fnet.img_to_encoding(image_dir_path + "/arnaud.jpg")
        #
        # verifies whether a particular camera image is a person in the image database
        dist, is_valid = fnet.verify(image_dir_path + "/camera_0.jpg", "younes", database)
        print('camera_0.jpg is' + (' ' if is_valid else ' not ') + 'yournes')
        dist, is_valid = fnet.verify(image_dir_path + "/camera_2.jpg", "kian", database)
        print('camera_0.jpg is' + (' ' if is_valid else ' not ') + 'yournes')
        #    
        # whether a particular camera image is which person in the image database (or not at all)
        dist, identity = fnet.who_is_it(image_dir_path + "/camera_0.jpg", database)
        if identity is None:
            print('camera_0.jpg is not found in database')
        else:
            print('camera_0.jpg is ' + str(identity))
    
    if __name__ == '__main__':
        main()
    

    [image]
  9. 実行結果を確認する

    顔認識の結果が表示される

    [image]
  10. デモプログラム VGG16 + Siamese の学習 を実行

    このプログラムは、VGG16 + Siamese の学習を行うプログラムである。

    ※ 1つ前のプログラムは、DeepFace 法(つまり別もの)

    データのダウンロードと、学習を行うので時間がかかる

    次のPython プログラムを実行する

    py -3.7 を使用.

    from keras_face.library.siamese import SiameseFaceNet
    
    def main():
        fnet = SiameseFaceNet()
        fnet.vgg16_include_top = True # default is False
        #
        model_dir_path = './models'
        image_dir_path = "./data/images"
        #
        database = dict()
        database["danielle"] = [fnet.img_to_encoding(image_dir_path + "/danielle.png")]
        database["younes"] = [fnet.img_to_encoding(image_dir_path + "/younes.jpg")]
        database["tian"] = [fnet.img_to_encoding(image_dir_path + "/tian.jpg")]
        database["andrew"] = [fnet.img_to_encoding(image_dir_path + "/andrew.jpg")]
        database["kian"] = [fnet.img_to_encoding(image_dir_path + "/kian.jpg")]
        database["dan"] = [fnet.img_to_encoding(image_dir_path + "/dan.jpg")]
        database["sebastiano"] = [fnet.img_to_encoding(image_dir_path + "/sebastiano.jpg")]
        database["bertrand"] = [fnet.img_to_encoding(image_dir_path + "/bertrand.jpg")]
        database["kevin"] = [fnet.img_to_encoding(image_dir_path + "/kevin.jpg")]
        database["felix"] = [fnet.img_to_encoding(image_dir_path + "/felix.jpg")]
        database["benoit"] = [fnet.img_to_encoding(image_dir_path + "/benoit.jpg")]
        database["arnaud"] = [fnet.img_to_encoding(image_dir_path + "/arnaud.jpg")]
        #
        fnet.fit(database=database, model_dir_path=model_dir_path)
    
    if __name__ == '__main__':
        main()
    

    [image]
  11. 実行結果を確認する

    [image]
  12. デモプログラム VGG16 + Siamese による顔認識 を実行

    このプログラムは、VGG16 + Siamese の学習の結果を使うもの

    このプログラム camera_0.jpg が誰なのかを顔認識する。

    [image]

    次のPython プログラムを実行する

    from keras_face.library.siamese import SiameseFaceNet
    from functools import partial
    import numpy as np
    np.load = partial(np.load, allow_pickle=True) 
    def main():
        fnet = SiameseFaceNet()
        #
        model_dir_path = './models'
        image_dir_path = "./data/images"
        fnet.load_model(model_dir_path)
        #
        database = dict()
        database["danielle"] = [fnet.img_to_encoding(image_dir_path + "/danielle.png")]
        database["younes"] = [fnet.img_to_encoding(image_dir_path + "/younes.jpg")]
        database["tian"] = [fnet.img_to_encoding(image_dir_path + "/tian.jpg")]
        database["andrew"] = [fnet.img_to_encoding(image_dir_path + "/andrew.jpg")]
        database["kian"] = [fnet.img_to_encoding(image_dir_path + "/kian.jpg")]
        database["dan"] = [fnet.img_to_encoding(image_dir_path + "/dan.jpg")]
        database["sebastiano"] = [fnet.img_to_encoding(image_dir_path + "/sebastiano.jpg")]
        database["bertrand"] = [fnet.img_to_encoding(image_dir_path + "/bertrand.jpg")]
        database["kevin"] = [fnet.img_to_encoding(image_dir_path + "/kevin.jpg")]
        database["felix"] = [fnet.img_to_encoding(image_dir_path + "/felix.jpg")]
        database["benoit"] = [fnet.img_to_encoding(image_dir_path + "/benoit.jpg")]
        database["arnaud"] = [fnet.img_to_encoding(image_dir_path + "/arnaud.jpg")]
        #
        fnet.verify(image_dir_path + "/camera_0.jpg", "younes", database)
        fnet.verify(image_dir_path + "/camera_2.jpg", "kian", database)
        fnet.who_is_it(image_dir_path + "/camera_0.jpg", database)
    
    if __name__ == '__main__':
        main()
    

    [image]
  13. 実行結果を確認する

    顔認識の結果が表示される

    [image]