SSD and TensorFlow on Docker (GPU)
最終更新日  2019/08/20
使用OS ver Ubuntu18.04
Proxy    あり
目次: Ubuntu18.04
前準備@host側
データを準備する
modelを落とす
tensorflow/modelsからzipをダウンロード
移動
$ cd ~/Downloads
解凍
$ unzip models-master.zip
リネーム
$ mv models-master models
とりあえずhomeへ
$ mv models ~/
protobufの手動コンパイル(ref:ここ)
移動
$ cd models/research/
protobufを落とす
$ wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip
解凍
$ unzip protobuf.zip
実行
$ ./bin/protoc object_detection/protos/*.proto --python_out=.
何も表示されないけど気にしない
COCO APIのインストール
移動
$ cd ~/
COCO APIを落とす
$ git clone https://github.com/cocodataset/cocoapi.git
移動
$ cd cocoapi/PythonAPI
makeする
$ make
ここで、エラー。はまった。
原因は
makeで叩かれるsetup.pyがpythonコマンドで叩かれていること
Python3前提で環境構築していたので、python-pipがなかったこと
当然、pipにはsetuotoolsやnumpyがないこと
よって下記を実施した
$ sudo apt install python-pip
$ pip install setuptools
$ pip install numpy
これでもエラー。_mask.cが読めないらしい。cythonを入れる
$ pip install cython
これでmakeできた
生成したpycocotoolsを移動
$ cp -r pycocotools ~/models/research/
modelをDockerとの共有ディレクトリに移動
$ sudo mv ~/models ~/work/ssd/
SSD用のイメージ作成
下記Dockerfileを作成
code: Dockerfile
FROM tensorflow/tensorflow:1.14.0-gpu-py3-jupyter
MAINTAINER genta
# git installの時にtzdataで止まる対策
# ref https://qiita.com/yagince/items/deba267f789604643bab
ENV DEBIAN_FRONTEND=noninteractive
# aptインストール
RUN apt update && apt install -y \
libsm6\
libxext6\
libxrender-dev\
cmake\
ffmpeg \
libavcodec-dev \
libavformat-dev \
libavresample-dev \
libjpeg-dev \
libpng-dev \
libswscale-dev \
libtbb-dev \
libtiff-dev \
libv4l-dev \
libssl-dev\
python-tk\
cpio \
git \
sudo && \
apt clean
# pipインストール
RUN pip3 install opencv-python opencv-contrib-python imageio scikit-image lxml pillow Cython contextlib2
# reserch modelへPathを通す
ENV PYTHONPATH=$PYTHONPATH:/tf/work/models/research:/tf/work/models/research/slim:
移動
$ cd ~/Dockerfiles
Dockerfileからイメージをビルド
$ sudo docker build -t tf-ssd-cv .
起動と動作確認
homeへ移動
$ cd ~/
作成したイメージを起動
$ docker run --runtime=nvidia -it --rm -v $(pwd)/work:/tf/work -p 8888:8888 tf-ssd-cv:latest bash
pythonのパスが合っているか動作確認
移動
$ cd work/models/research
テスト実行
$ python object_detection/builders/model_builder_test.py
OKが出れば実行環境準備できているはず
tutorial
イメージを起動
$ docker run --runtime=nvidia -it --rm -v $(pwd)/work:/tf/work -p 8888:8888 tf-ssd-cv:latest
jupyter notebookへアクセス
http://127.0.0.1:8888/
"work/models/research/object_detection" にある「object_detection_tutorial.ipynb」を開く
このページより、最後のセルに%matplotlib inlineを追加
これやんないと最後の画がうつらない
code:test
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: 1, None, None, 3
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict'detection_boxes',
output_dict'detection_classes',
output_dict'detection_scores',
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
%matplotlib inline
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
https://datatechnologylab.readthedocs.io/ja/latest/MLWorkflow/training/index.html
https://qiita.com/sanoyo/items/1a5c4e8671203d190fca
https://qiita.com/hirokikmr/items/f89d5405aa7e385dff7e
https://towardsdatascience.com/how-to-train-your-own-object-detector-with-tensorflows-object-detector-api-bec72ecfe1d9