{"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,"footnotes":""},"categories":[12],"tags":[120,173,328,353,407,447,540],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Setting up a development environment to run keras-retinanet\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Retinanet Dockerfile with GPU support - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"Setting up a development environment to run keras-retinanet\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2018-12-05T03:30:34+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2018-12-05T03:30:34+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Retinanet Dockerfile with GPU support - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Setting up a development environment to run keras-retinanet\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#blogposting\",\"name\":\"Retinanet Dockerfile with GPU support - Gary Sieling\",\"headline\":\"Retinanet Dockerfile with GPU support\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2018-12-05T03:30:34+00:00\",\"dateModified\":\"2018-12-05T03:30:34+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#webpage\"},\"articleSection\":\"How-To, computervision, docker, keras, machinelearning, opencv, python, tensorflow\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"name\":\"How-To\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"position\":2,\"name\":\"How-To\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#listItem\",\"name\":\"Retinanet Dockerfile with GPU support\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#listItem\",\"position\":3,\"name\":\"Retinanet Dockerfile with GPU support\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/how-to\\\/#listItem\",\"name\":\"How-To\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/\",\"name\":\"Retinanet Dockerfile with GPU support - Gary Sieling\",\"description\":\"Setting up a development environment to run keras-retinanet\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/retinanet-dockerfile-with-gpu-support\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2018-12-05T03:30:34+00:00\",\"dateModified\":\"2018-12-05T03:30:34+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Retinanet Dockerfile with GPU support - Gary Sieling","description":"Setting up a development environment to run keras-retinanet","canonical_url":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#blogposting","name":"Retinanet Dockerfile with GPU support - Gary Sieling","headline":"Retinanet Dockerfile with GPU support","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2018-12-05T03:30:34+00:00","dateModified":"2018-12-05T03:30:34+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#webpage"},"articleSection":"How-To, computervision, docker, keras, machinelearning, opencv, python, tensorflow"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","name":"How-To"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","position":2,"name":"How-To","item":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#listItem","name":"Retinanet Dockerfile with GPU support"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#listItem","position":3,"name":"Retinanet Dockerfile with GPU support","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/#listItem","name":"How-To"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/","name":"Retinanet Dockerfile with GPU support - Gary Sieling","description":"Setting up a development environment to run keras-retinanet","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2018-12-05T03:30:34+00:00","dateModified":"2018-12-05T03:30:34+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"Retinanet Dockerfile with GPU support - Gary Sieling","og:description":"Setting up a development environment to run keras-retinanet","og:url":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/","article:published_time":"2018-12-05T03:30:34+00:00","article:modified_time":"2018-12-05T03:30:34+00:00","twitter:card":"summary_large_image","twitter:title":"Retinanet Dockerfile with GPU support - Gary Sieling","twitter:description":"Setting up a development environment to run keras-retinanet"},"aioseo_meta_data":{"post_id":"6046","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 17:18:43","updated":"2026-07-06 02:30:40","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/how-to\/\" title=\"How-To\">How-To<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tRetinanet Dockerfile with GPU support\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"How-To","link":"https:\/\/www.garysieling.com\/blog\/category\/how-to\/"},{"label":"Retinanet Dockerfile with GPU support","link":"https:\/\/www.garysieling.com\/blog\/retinanet-dockerfile-with-gpu-support\/"}],"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}]}}