【このページの目次】
手順の要点: Python 3.6, TensorFlow 1.15, Python の仮想環境(Windows では C:\venv\tf115py36)
ソフトウエア等の利用条件等は,利用者で確認すること.
サイト内の関連ページ:
謝辞:ソフトウエアの作者に感謝します
Git の URL: https://git-scm.com/
sudo apt -y install git
※ TensorFlow 1.15 に対応する Python は,3.6 や 3.7 など(3.8 は対応していない)(2020/06 時点).このページでは 3.6 を使って説明する.3.7 でも同様の手順になる.
最新版ではない TensorFlow を使う.そうした場合,運用を簡単にする(TensorFlow を間違ってアップデートしないなど)ために,venv を用いて,隔離された Python 仮想環境を作る方が便利かもしれない(必ずしも,そうする必要はない).その手順もここに示している.
Python の URL: http://www.python.org/
インストール手順の詳細は: 別ページで説明している.
Windows では,Python 3.8 や 3.6 の共存は簡単.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
Python の使用は「py -3.6」で行う.
py -3.6 -m pip install -U pip setuptools py -3.6 -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
隔離された Python 仮想環境を使用したいときは,次のような手順で, venv を用いて,隔離された Python 仮想環境を作る.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
Python の使用は「C:\venv\tf115py36\Scripts\activate.bat」の後,「python」で行う.
py -3.6 -m pip install -U pip setuptools py -3.6 -m venv --system-site-packages C:\venv\tf115py36 C:\venv\tf115py36\Scripts\activate.bat python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
次の手順により,システム Python とは別に,pyenv を用いて Pytnon 3.6 をインストールする.(システム Python の設定は変えたくないため).
Ubuntu での pyenv のインストール手順の詳細説明: 別ページで説明している.
rm -rf ~/.pyenv cd /tmp git clone https://github.com/pyenv/pyenv.git ~/.pyenv cd ~/.pyenv git pull echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bashrc echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bashrc echo ' export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bashrc echo 'fi' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc exec $SHELL -l source ~/.bashrc sudo apt -y install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
pyenv rehash pyenv install -l | grep 3.6
pyenv install 3.6.12
端末で,次のコマンドを実行.
pyenv shell 3.6.12
Python の切り替えは次のようなコマンドで行う.
デフォルトで pyenv 配下の python 3.6.12 を使いたいときは, 次のように設定する
echo 'pyenv shell 3.6.12' >> ~/.bashrc exec $SHELL -l
インストールするには, 端末で,次のコマンドを実行.
python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
隔離された Python 仮想環境を作成したいときは,次のような手順で, venv を用いて,隔離された Python 仮想環境を作る.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools python -m venv --system-site-packages ~/tf115py36 source ~/tf115py36/bin/activate python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
「Visual Studio Community 2019 vesion 16.2, Visual C++ ビルドツールのインストール(Windows 上)」で説明している.
以下,Windows に Python, git, Visual C++ ビルドツール (Build Tools) をインストール済みであるものとして説明を続ける.
※ GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
ダウンロードページ
詳細説明
※ CUDA とは,NVIDIA社が提供している GPU 用のプラットフォームである.
指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
ダウンロードページ
ダウンロードして展開(解凍)したら,パスを通しておくこと.
TensorFlow 2.4 の GPU 版での,cuDNN のバージョンは 8.0.5(現時点で,NVIDIA CUDA ツールキット 11 で動く最新版).
TensorFlow 2.3, 2.2, 2.1 の GPU 版での,cuDNN のバージョンは 7.6.そして,TensorFlow 1.13 以上 TensorFlow 2.0 までの GPU 版での,cuDNN のバージョンは7.4 .それより高いバージョンよりは使わないこと.
詳細説明
Python 3.6 を起動するコマンドを確認しておくこと.
C:\venv\tf115py36\Scripts\activate.bat
Python の使用は,隔離された Python 仮想環境の有効化の後,「python」で行う. Windows で,隔離された Python 仮想環境を使用しないときは,「python」ではなく「py -3.6」を使う.
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets tensorflow-hub keras python -m pip install -U tensorflow==1.15.5 tensorflow_datasets tensorflow-hub keras==2.3.1 matplotlib opencv-python python -m pip install git+https://github.com/tensorflow/docs python -m pip install git+https://github.com/tensorflow/examples.git
python -c "import tensorflow as tf; print( tf.__version__ )"
TensorFlow が GPU を認識できているかの確認は,端末で,次を実行して行う.
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
端末で,次のコマンドを実行.
pyenv shell 3.6.12
隔離された Python 仮想環境を使用するときは, 「pyenv shell 3.6.12」は実行しないで, 次のようなコマンドで Python 仮想環境を有効化する.
source ~/tf115py36/bin/activate
Python の使用は,「python」で行う.
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets tensorflow-hub keras python -m pip install -U tensorflow==1.15.5 tensorflow_datasets tensorflow-hub keras==2.3.1 matplotlib opencv-python python -m pip install git+https://github.com/tensorflow/docs python -m pip install git+https://github.com/tensorflow/examples.git
python -c "import tensorflow as tf; print( tf.__version__ )"
TensorFlow が GPU を認識できているかの確認は,端末で,次を実行して行う.
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
Windows での手順を下に示す.Ubuntu でも同様の手順になる.
Windows の場合での,有効化のコマンドの例.
C:\venv\tf115py36\Scripts\activate.bat
Ubuntu の場合での,有効化のコマンドの例.
source ~/tf115py36/bin/activate
※ 以下,Windows での手順を示す.Ubuntu でも同様の手順になる.
cd C:\venv\tf115py36 rmdir /s /q deepgaze
このとき,virtualenv の隔離された Python 仮想環境を有効化する(「tf115py36」のところは、Python仮想環境の名前)
C:\venv\tf115py36\.venv\Scripts\activate.bat git clone https://github.com/mpatacchiola/deepgaze cd deepgaze python setup.py build python setup.py install
Windows での手順を下に示す.Ubuntu でも同様の手順になる.
※ 以下,Windows での手順を示す.Ubuntu でも同様の手順になる.
ファイルのコピー
cd C:\venv\tf115py36 cd deepgaze\examples\ex_cnn_head_pose_estimation_images copy ex_cnn_head_pose_estimation_images.py a.py
v = cv2.VideoCapture("C:/face-image/sample2.mp4") while(v.isOpened()): r, img = v.read() if ( r == False ): break img = cv2.resize(img, (480, 480)) roll = my_head_pose_estimator.return_roll(img) # Evaluate the roll angle using a CNN pitch = my_head_pose_estimator.return_pitch(img) # Evaluate the pitch angle using a CNN yaw = my_head_pose_estimator.return_yaw(img) # Evaluate the yaw angle using a CNN print("roll %s, pitch %s, yaw %s" % ( str(roll[0,0,0]), str(pitch[0,0,0]), str(yaw[0,0,0]) ) ) cv2.imshow("", img) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()
Python プログラムを動かす.
python a.py
v = cv2.VideoCapture(0)
v = cv2.VideoCapture(0) while(v.isOpened()): r, img = v.read() if ( r == False ): break img = cv2.resize(img, (480, 480)) roll = my_head_pose_estimator.return_roll(img) # Evaluate the roll angle using a CNN pitch = my_head_pose_estimator.return_pitch(img) # Evaluate the pitch angle using a CNN yaw = my_head_pose_estimator.return_yaw(img) # Evaluate the yaw angle using a CNN print("roll %s, pitch %s, yaw %s" % ( str(roll[0,0,0]), str(pitch[0,0,0]), str(yaw[0,0,0]) ) ) cv2.imshow("", img) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()
Python プログラムを動かす.
python a.py