SSD_keras demo をubuntu16.04で実行
SSD: Single Shot MultiBox Detectorをkerasフレームワークを用いて実装できるとのことで
仮想マシン上のUbuntu16.04を用いて実装した。
まず、今回用いたソフトなどのVersionを大雑把に記載する。
- Python3.5.2
- Keras v2.0.8(pip)
- tensorflow v1.3.0(pip)
- h5py v2.7.1(pip)
- pillow v4.2.1(pip)
- OpenCV v3.1.0(自前でビルドインストール)
- ffmpeg v3.3.3-1(apt経由)
本当は上記の他にCUDAをインストールしたかったが仮想マシンOSの為断念した。
また、実装環境はPythonの固有環境を作成するvirtualenvを用いている。
※この実装は大量のパッケージをインストールするほか、システム変数も弄るので
仮想OSのようなテスト環境を用いることを強く推奨します!!!
opencv3.1.0インストール
まずは必要なパッケージ類をインストール
初めにopencvをビルドする為に必要なパッケージをインストールする。
sudo apt-get install build-essential cmake cmake-qt-gui pkg-config sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev sudo apt-get install libgtk-3-dev sudo apt-get install libatlas-base-dev gfortran sudo apt-get install python2.7-dev python3.5-dev
pythonのライブラリ管理ツールであるpipをインストール
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py #delete get-pip.py sudo rm -rf ~/get-pip.py ~/.cache/pip
Pythonの固有環境を作成するvirtualenvをインストールと
bashで利用できるように設定を書き込む。
sudo pip install virtualenv virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc #設定反映 source ~/.bashrc
すると、以下のようなメッセージが表示されるはずです
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details
そして、virtualenvをPython3で使用する為に以下のコマンドを入力
mkvirtualenv cv -p python3
以下のメッセージが表示されたら設定完了です。
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /root/.virtualenvs/cv/bin/python3
Also creating executable in /root/.virtualenvs/cv/bin/python
Installing setuptools, pip, wheel...done.
#workon cv とコマンドを打てば固有環境が構築できています。
(コンソールに(cv)と表示されている状態)
図1: virtualenv動作
以後、この状態にて実装していきます。
ffmpeg v3.3.3-1インストール
Ubuntuだとapt経由でインストールできます。
sudo add-apt-repository ppa:jonathonf/ffmpeg-3 sudo apt update && sudo apt install ffmpeg libav-tools x264 x265
opencv3.3.1ビルド&インストール
まず、opencv3.1.0をDLします。(opencv_contribも念のためDL)
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip unzip opencv.zip wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip unzip opencv_contrib.zip
そしてopencvフォルダの中にbuildフォルダを作成します。
cd opencv-3.1.0/ mkdir build cd build/
いよいよopencvをビルドインストール!virtualenvのpython3を認識させたいので
ビルドは#workon cvを忘れずに実行してから行うこと
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D WITH_CUDA=OFF -D WITH_FFMPEG=ON -D BUILD_EXAMPLES=ON .. #-j○の○の部分は自身のコアを入力する make –j8 make clean make install ldconfig
opencvにcv2ライブラリを認識させる為以下の手順を実行します。
cd /usr/local/lib/python3.5/site-packages/ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so cd ~/.virtualenvs/cv/lib/python3.5/site-packages/ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so
最後に不要となったファイルを削除
cd rm -rf opencv-3.1.0 opencv_contrib-3.1.0 opencv.zip opencv_contrib.zip
ssd_keras をダウンロード
gitレポジトリからssd_kerasのdemo環境フォルダをDL
git clone https://github.com/rykov8/ssd_keras.git
さらに教師データ(weights_SSD300.hdf5)を以下のサイトからDL
MEGA
demoを実行する為に以下の操作を行う
まず、動作に必要なpythonモジュールをインストール
pip install tensorflow keras h5py pillow
さらに、ssd_kerasフォルダの中のファイルを編集する
###/root/ssd_keras/testing_utils/videotest.py
# 87行目 vidw = vid.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) vidh = vid.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) # 以下に修正 vidw = vid.get(cv2.CAP_PROP_FRAME_WIDTH) vidh = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) #物体検出結果をコンソールでも確認したい場合 # 162行目に以下を追加 print(text)
###/root/ssd_keras/testing_utils/videotest_example.py
# 18行目 #weights_SSD300.hdf5ファイルのパスを確認 model.load_weights('/root/SSD/weights_SSD300.hdf5') #24行目 #動画のパスを入力 vid_test.run('/root/test.mp4')
keras2.0から仕様が変わったため以下の編集を行う。
###/root/ssd_keras/ssd_layers.py
#111行目?を以下のように変更 def get_output_shape_for(self, input_shape): ↓ def compute_output_shape(self, input_shape):
これで全ての準備が整いました!
/root/ssd_keras/testing_utilsにある videotest_example.pyを実行
python videotest_example.py
図2: SSD_RESULT
めちゃくちゃ大変だった・・・
次はGPUに対応できるよう挑戦する予定です。
参考サイト様:
SSD: Single Shot MultiBox Detector 高速リアルタイム物体検出デモをKerasで試す - Qiita
How to Install FFMPEG 3.1.3 in Ubuntu 16.04 via PPA | UbuntuHandbook
Install OpenCV3 on Ubuntu | Learn OpenCV
keras 2.0 concat error in ssd.train ipython · Issue #60 · rykov8/ssd_keras · GitHub