{"id":59,"date":"2017-12-27T00:10:12","date_gmt":"2017-12-26T21:10:12","guid":{"rendered":"http:\/\/mbrdancer.ru\/?p=59"},"modified":"2018-03-22T13:26:53","modified_gmt":"2018-03-22T10:26:53","slug":"nvidia-ngc-tensorflow-image-py27-to-py35-with-opencv-opencv3-for-python","status":"publish","type":"post","link":"http:\/\/mbrdancer.ru\/?p=59","title":{"rendered":"NVIDIA NGC tensorflow image py27 to py35 with OpenCV (opencv3 for python)"},"content":{"rendered":"<p>assume we are working with Ubuntu 16.04 xenial and Docker 17.09.1-ce:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ lsb_release -a\r\nNo LSB modules are available.\r\nDistributor ID:\tUbuntu\r\nDescription:\tUbuntu 16.04.3 LTS\r\nRelease:\t16.04\r\nCodename:\txenial\r\n\r\n$ docker --version\r\nDocker version 17.09.1-ce, build 19e2cf6\r\n<\/pre>\n<p>1. go to ngc.nvidia.com and get NGC tensorflow image link. Then make docker login to NGC registry:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ndocker login nvcr.io\r\n\r\nUsername: $oauthtoken\r\nPassword: &amp;lt;Your Key&amp;gt;\r\n<\/pre>\n<p>We are prompted here to type username which is &#171;$oauthtoken&#187; and password which is an API key. API key is generated at the NGC user account, in the &#171;Configuration&#187; section.<br \/>\nNow we get NGC tensorflow image. Go to &#171;Registry&#187; section, choose nvidia\/tensorflow image section, scroll down and copy the needed (latest) tag pull command. Currently its 17.12 tag and the command is the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker pull nvcr.io\/nvidia\/tensorflow:17.12\r\n<\/pre>\n<p>Now we have the image in the local images registry:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker images\r\nREPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE\r\nhello-world                 latest              f2a91732366c        5 weeks ago         1.85kB\r\nnvcr.io\/nvidia\/tensorflow   17.12               19afd620fc8e        5 weeks ago         2.88GB\r\nubuntu                      latest              20c44cd7596f        5 weeks ago         123MB\r\n<\/pre>\n<p>Now we will make our own image based on NVIDIA one.<br \/>\n1. get <code>miniconda<\/code> installer:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nwget https:\/\/repo.continuum.io\/miniconda\/Miniconda3-4.2.12-Linux-x86_64.sh\r\n<\/pre>\n<p>I am downloading the specific 4.2 version as a last one with <code>python=3.5<\/code> used by default.<\/p>\n<p>2. now create <code>Dockerfile<\/code> for the first stage:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM nvcr.io\/nvidia\/tensorflow:17.12\r\nMAINTAINER your_name_here &amp;lt;your_email_here&amp;gt;\r\nENV LANG=C.UTF-8 LC_ALL=C.UTF-8\r\nENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\/usr\/lib\/x86_64-linux-gnu\r\n\r\nRUN apt-get update --fix-missing &amp;&amp; apt-get install -y wget bzip2 ca-certificates \\\r\n    libglib2.0-0 libxext6 libsm6 libxrender1 \\\r\n    git mercurial subversion\r\n\r\nCOPY .\/Miniconda3-4.2.12-Linux-x86_64.sh \/root\/miniconda.sh\r\n\r\nRUN echo 'export PATH=\/opt\/conda\/bin:$PATH' &amp;gt; \/etc\/profile.d\/conda.sh &amp;&amp; \\\r\n    \/bin\/bash \/root\/miniconda.sh -b -p \/opt\/conda &amp;&amp; \\\r\n    rm \/root\/miniconda.sh\r\n\r\nENV PATH=\/opt\/conda\/bin:$PATH\r\n\r\nRUN apt-get install -y libgtk2.0 &amp;&amp; \\\r\n    rm -rf \/var\/lib\/apt\/lists\/*\r\n\r\nRUN conda update --all -y &amp;&amp; \\\r\n    conda install numpy=1.11.0 pandas matplotlib scikit-learn seaborn scipy tqdm jupyter ipython -y &amp;&amp; \\\r\n    conda install -c conda-forge ipywidgets netcdf4 basemap graphviz -y &amp;&amp; \\\r\n    conda install -c jaikumarm hyperopt\r\n\r\nRUN conda config --add channels conda-forge &amp;&amp; \\\r\n    conda config --add channels intel &amp;&amp; \\\r\n    conda config --add channels menpo &amp;&amp; \\\r\n    conda install -c menpo opencv3=3.1.0 -y\r\n\r\nRUN conda clean -y -all &amp;&amp; \\\r\n    rm -rf \/opt\/conda\/pkgs\/*\r\n\r\nCOPY .\/jupyter_notebook_config.py \/root\/.jupyter\/\r\n\r\nRUN conda install h5py -y\r\n\r\nVOLUME [ &quot;\/app&quot;, &quot;\/data&quot; ]\r\nEXPOSE 8888\r\nWORKDIR \/app\r\nENTRYPOINT [ &quot;\/usr\/local\/bin\/nvidia_entrypoint.sh&quot; ]\r\nCMD [&quot;jupyter&quot;, &quot;notebook&quot;, &quot;--ip='*'&quot;, &quot;--port=8888&quot;, &quot;--no-browser&quot;, &quot;--allow-root&quot;, &quot;--debug&quot;]\r\n<\/pre>\n<p><code>jupyter_notebook_config.py<\/code> is a file that will configure jupyter notebook parameters on start. Right now its content is the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nc.NotebookApp.ip = '*'\r\nc.NotebookApp.open_browser = False\r\n\r\n## Hashed password to use for web authentication.\r\n#  \r\n#  To generate, type in a python\/IPython shell:\r\n#  \r\n#    from notebook.auth import passwd; passwd()\r\n#  \r\n#  The string should be of the form type:salt:hashed-password.\r\nc.NotebookApp.password = 'sha1:your_password_hash_here'\r\n\r\nc.NotebookApp.port = 8888\r\n<\/pre>\n<p>Note here that we are installing <code>numpy=1.11.0<\/code>. It is tensorflow 1.4 requirement.<br \/>\nAlso note here that we are installing <code>opencv3=3.1.0<\/code>. It is the version that will not conflict with <code>matplotlib<\/code>, <code>numpy=1.11.0<\/code> and others in terms of dependencies. The repository for <code>opencv3<\/code> is <code>menpo<\/code>: it is the one containing opencv3 with <code>contrib<\/code> modules.<\/p>\n<p>3. From this step we will compose docker image of the stage 1 (i`ve named it &#171;<code>nvcr.io\/mk\/py35_nv_tf:int01<\/code>&#171;).<br \/>\nRemember that we have to do it in the directory where <code>Dockerfile<\/code>, <code>jupyter_notebook_config.py<\/code> and <code>Miniconda3-4.2.12-Linux-x86_64.sh<\/code> are located.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker build -t nvcr.io\/mk\/py35_nv_tf:int01 .\r\n<\/pre>\n<p>we may check now if the docker container can be run here:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ nvidia-docker run -it --rm nvcr.io\/mk\/py35_nv_tf:int01 \/bin\/bash\r\n<\/pre>\n<p>4. Now we will launch docker container using this image and build TF inside it. We are using this approach because of NVIDIA docker runtime usage that distribute some necessary libraries in <code>nvidia-docker run\/exec ...<\/code> case only.<br \/>\nParticularly <code>libcuda.so.1<\/code> of the proper version:<br \/>\n(inside container)<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# ll \/usr\/lib\/x86_64-linux-gnu | grep libcuda.so\r\nlrwxrwxrwx  1 root root        17 Dec 25 21:41 libcuda.so -&gt; libcuda.so.387.34\r\nlrwxrwxrwx  1 root root        17 Dec 25 21:41 libcuda.so.1 -&gt; libcuda.so.387.34\r\n-rw-r--r--  1 root root  11008248 Nov 21 10:18 libcuda.so.387.34\r\n<\/pre>\n<p>So we build TF inside the container, and then commit all changes made to the image of the stage 2.<br \/>\nlaunch container:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ nvidia-docker run -it --rm nvcr.io\/mk\/py35_nv_tf:int01 \/bin\/bash\r\n<\/pre>\n<p>and inside this container:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# cd \/opt\/tensorflow\/\r\n# export TF_UNOFFICIAL_SETTING=1\r\n# yes &quot;&quot; | .\/configure\r\n# bazel build -c opt --config=cuda tensorflow\/tools\/pip_package:build_pip_package\r\n<\/pre>\n<p>this will take a long &#8212; about 20min.<br \/>\n(still inside container)<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# bazel-bin\/tensorflow\/tools\/pip_package\/build_pip_package \/tmp\/tf_pip\r\n# pip install --upgrade \/tmp\/tf_pip\/tensorflow-*.whl\r\n# rm -rf \/tmp\/tf_pip\/tensorflow-*.whl\r\n# bazel clean --expunge\r\n\r\n# pip install keras==2.0.8\r\n<\/pre>\n<p>and now one more step: i am going to make Keras download VGG16 head:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# ipython\r\n<\/pre>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport keras\r\nfrom keras.applications import VGG16\r\nconv_base1 = VGG16(weights='imagenet', include_top=False, input_shape=(100, 100, 3))\r\n<\/pre>\n<p>here Keras will download the file <code>vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5<\/code> to directory <code>\/root\/.keras\/models\/<\/code><\/p>\n<p>After this we are going to detach from the container using the <code>&lt;Ctrl&gt;+&lt;Q&gt; -&gt; &lt;Ctrl&gt;+&lt;P&gt;<\/code> keys sequence.<\/p>\n<p>And now commit all changes from the container to the new image:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker commit 87bcd85ec902 nvcr.io\/mk\/py35_nv_tf\r\n<\/pre>\n<p>87bcd85ec902 here is the ID of docker container where we have built TF.<br \/>\nafter this we may stop and remove this container:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker stop 87bcd85ec902\r\n$ docker rm 87bcd85ec902\r\n<\/pre>\n<p>5. some polish. Create the new Dockerfile containing the following:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM nvcr.io\/mk\/py35_nv_tf:int02\r\n\r\nMAINTAINER YOUR NAME HERE &lt;your@email.here&gt;\r\n\r\nVOLUME [ &quot;\/app&quot;, &quot;\/data&quot; ]\r\nEXPOSE 8888\r\n\r\nWORKDIR \/app\r\n\r\nENTRYPOINT [ &quot;\/usr\/local\/bin\/nvidia_entrypoint.sh&quot; ]\r\n\r\nCMD [&quot;jupyter&quot;, &quot;notebook&quot;, &quot;--ip='*'&quot;, &quot;--port=8888&quot;, &quot;--no-browser&quot;, &quot;--allow-root&quot;, &quot;--debug&quot;]\r\n<\/pre>\n<p>rename <code>nvcr.io\/mk\/py35_nv_tf:latest<\/code> to <code>nvcr.io\/mk\/py35_nv_tf:int02<\/code> and build the new image<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ docker tag nvcr.io\/mk\/py35_nv_tf:latest nvcr.io\/mk\/py35_nv_tf:int02\r\n$ docker build -t nvcr.io\/mk\/py35_nv_tf .\r\n<\/pre>\n<p>after these 5 steps we are able to use Python 3.5 with NVIDIA-optimized Tensorflow 1.4 build for Python 3.5. And there is an opencv3 in the image as well.<br \/>\nThe container may be run with the following (jupyter notebook server will start automatically):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ nvidia-docker run --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 -it --rm --mount type=bind,source=$HOME\/py35docker_app\/,target=\/app -p 8888:8888 nvcr.io\/mk\/py35_nv_tf\r\n<\/pre>\n<p>or the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ nvidia-docker run --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 -it --rm --mount type=bind,source=$HOME\/py35docker_app\/,target=\/app -p 8888:8888 nvcr.io\/mk\/py35_nv_tf \/bin\/bash\r\n<\/pre>\n<p>in this case we will get command line of the container.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>assume we are working with Ubuntu 16.04 xenial and Docker 17.09.1-ce: 1. go to ngc.nvidia.com and get NGC tensorflow image link. Then make docker login to NGC registry: We are prompted here to type username which is &#171;$oauthtoken&#187; and password which is an API key. API key is generated at the NGC user account, in&hellip; <a class=\"more-link\" href=\"http:\/\/mbrdancer.ru\/?p=59\">\u0427\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043b\u0435\u0435 <span class=\"screen-reader-text\">NVIDIA NGC tensorflow image py27 to py35 with OpenCV (opencv3 for python)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4],"tags":[],"_links":{"self":[{"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/posts\/59"}],"collection":[{"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":30,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":92,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/92"}],"wp:attachment":[{"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mbrdancer.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}