{"id":6046,"date":"2018-12-05T03:30:34","date_gmt":"2018-12-05T03:30:34","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=6046"},"modified":"2018-12-05T03:30:34","modified_gmt":"2018-12-05T03:30:34","slug":"retinanet-dockerfile-with-gpu-support","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/","title":{"rendered":"Retinanet Dockerfile with GPU support"},"content":{"rendered":"<p>Retinanet is an object detection model that is supposed to be suitable for tagging objects in videos. To use this, you need to install the <a href=\"https:\/\/github.com\/fizyr\/keras-retinanet\">keras-retinanet<\/a> project from github. <\/p>\n<p>The following example shows how to train this, taken from the excellent <a href=\"https:\/\/www.pyimagesearch.com\">pyimagesearch<\/a> book:<\/p>\n<pre lang=\"bash\">\nretinanet-train \\\n  --batch-size 4 \\\n  --steps 349 \\\n  --epochs 50 \\\n  --weights logos\/resnet50_coco_best_v2.1.0.h5 \\\n  --snapshot-path $DIR\/snapshots \\\n  csv logos\/retinanet_train.csv \\\n  logos\/retinanet_classes.csv\n<\/pre>\n<p>Under the hood this uses Tensorflow, which is easy to get running against a CPU (and the default install doesn&#8217;t use Intel vector instructions, so even there it&#8217;s slower than it needs to be)<\/p>\n<p>When I ran the above command it estimated 5 hours per epoch &#8211; 50 training epochs would take around ten days. Based on my laptop&#8217;s electricity consumption, this would consume around $5.25 of electricity ($0.21\/kWh).<\/p>\n<p>The &#8220;easy&#8221; answer is to use a GPU (1080 Ti), which will bring the time to 5 minutes per epoch (4.1 hours for 50 epochs).<\/p>\n<p>While it may look like this saves 9.75 days, you could easily spend that time just installing dependencies: reinstalling your OS to a version CUDA supports (I chose Ubuntu 18.04), identifying which of the six ways to install CUDA works best (the .run file, if you want Steam to keep working), building a custom Tensorflow version (if using CUDA 10, which isn&#8217;t officially supported yet), and identifying the correct version of Google&#8217;s build tool (bazel) to build Tensorflow.<\/p>\n<p>Enter Docker.<\/p>\n<p>Tensorflow provides convenient pre-built Docker containers, so you can avoid a lot of this hassle. <\/p>\n<pre lang=\"bash\">\ndocker run --rm -it --runtime=nvidia tensorflow\/tensorflow:latest-gpu-py3\n<\/pre>\n<p>To use these, you must install the <a href=\"https:\/\/github.com\/NVIDIA\/nvidia-docker\">NVIDIA Docker runtime<\/a>, so you do need to have CUDA configured, but this lets you avoid the pain of configuring the downstream libraries (Tensorflow, mxnet, etc) by delegating it to library maintainers.<\/p>\n<p>You may be wondering how this integrates with your main machine: you want tagged images and the model stored outside a container, with minimal I\/O overhead. Because we&#8217;re using the Linux version of Docker we avoid all the <a href=\"https:\/\/forums.docker.com\/t\/file-access-in-mounted-volumes-extremely-slow-cpu-bound\/8076\/107\">I\/O problems of OSX Docker versions<\/a> (while there are workarounds, you can waste a few hours the first time you hit these problems).<\/p>\n<p>To use this to run Keras \/ retinanet, we also need opencv, but the version on pip seems to work OK for this:<\/p>\n<pre lang=\"bash\">\nFROM tensorflow\/tensorflow:latest-gpu-py3\nRUN apt-get clean && \\\n    apt-get update && \\\n    apt-get upgrade -y && \\\n    apt-get install -y git\nRUN git clone https:\/\/github.com\/fizyr\/keras-retinanet\nRUN cd keras-retinanet && pip install . --user\nRUN pip install opencv-python\nRUN apt-get install -y libsm6 libxrender1\nENTRYPOINT [\"\/bin\/bash\", \"-c\", \"\/root\/.local\/bin\/retinanet-train ${*}\", \"--\"]\n<\/pre>\n<p>We&#8217;ll take any arguments and pass them on to retinanet, so you can treat this as if it was the equivalent command line app running locally.<\/p>\n<pre lang=\"bash\">\ndocker build . -t retinanet-train\n<\/pre>\n<p>I found it convenient to trick the Docker container into looking like it&#8217;s file system had the same structure as my laptop &#8211; so the volume mount is the output of &#8220;pwd -P&#8221; (a deep link into a drive partition).<\/p>\n<pre lang=\"bash\">\nDIR=$(pwd -P) docker run --rm -it \\\n  --runtime=nvidia \\\n  -v $DIR:$DIR \\\n  retinanet-train \\\n  --batch-size 4 \\\n  --steps 349 \\\n  --epochs 50 \\\n  --weights $DIR\/logos\/resnet50_coco_best_v2.1.0.h5 \\\n  --snapshot-path $DIR\/snapshots \\\n  csv $DIR\/logos\/retinanet_train.csv \\\n  $DIR\/logos\/retinanet_classes.csv\n<\/pre>\n<p>Once you do this it works like a charm &#8211; you&#8217;ll be building new models in no time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up a development environment to run keras-retinanet<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[120,173,328,353,407,447,540],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6046"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=6046"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6046\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=6046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=6046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=6046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}