{"id":511,"date":"2019-09-13T21:01:50","date_gmt":"2019-09-13T18:01:50","guid":{"rendered":"https:\/\/grechka.family\/dmitry\/blog\/?p=511"},"modified":"2019-09-13T21:01:52","modified_gmt":"2019-09-13T18:01:52","slug":"yolo-v3-anchors-for-traffic-sign-detection","status":"publish","type":"post","link":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/","title":{"rendered":"YOLO v3 anchors for traffic sign detection"},"content":{"rendered":"\n<p>The creators of YOLO v3 advice to regenerate &#8220;anchors&#8221; if you retrain YOLO for your own dataset. Here I describe what are anchors, how to generate them and also give specific anchor values for traffic sign detection problem.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"219\" src=\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg\" alt=\"bicycles prohibited traffic sign\" class=\"wp-image-513\" srcset=\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg 640w, https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445-300x103.jpg 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p>(If you need the anchors for training of traffic sign detection on 16:9 aspect ratio images, scroll down to the end of the post)<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">What is YOLO anchors?<\/h3>\n\n\n\n<p style=\"text-align:left\"><a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/\">YOLO <\/a>infers bounding box around the detected object not as an arbitrary rectangle, but as an offset from one of the preconfigured bounding boxes. These bounding box presets are called anchors or anchor boxes. They are chosen to match real object bounding boxes that are common in the training set. Thus they give a clue about typical object shapes and sizes that the network should look for. Each anchor is represented as width and height. Therefore anchor fixes not only aspect ratio of a bounding box but also an exact size of it. Thus, it is crucial to tune them right. In <a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/\">YOLO v3<\/a>   there are 9 different anchors, which are equally divided into three groups. Each group of anchors operate on separate scale of the image.  See details in <a href=\"https:\/\/pjreddie.com\/media\/files\/papers\/YOLOv3.pdf\">original paper<\/a>.<\/p>\n\n\n\n<p>Imagine that we analyse a car camcorder video and do traffic sign detection. We are unlikely to encounter a frame where the object to detect (traffic sign) takes more than a half of a view sight. Therefore we do not need an anchor with a size about a half of the whole image or larger.<\/p>\n\n\n\n<p>However if we do general purpose image annotation, we must be ready to detect a close up photo of an object and the usage of large size anchor can be helpful.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to get the anchors?<\/h3>\n\n\n\n<p>To compute the anchors it is advised to do K-means clustering of the bounding boxes in the target data set. I&#8217;ll show here how to do it in R for the <a href=\"https:\/\/graphics.cs.msu.ru\/en\/research\/projects\/rtsd\">Russian Traffic Sign Dataset (RTSD)<\/a>, which is a set of wide screen (1280 x 720) images.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">annotation_image_width &lt;- 1280\nannotation_image_height &lt;- 720\n\nyolo_image_width &lt;- 416 # multiple of 32\nyolo_image_height &lt;- 416\n\ndf &lt;- read.csv('data\/rtsd\/full-gt.csv')\n\n# scaling bound box sizes to the Yolo v3 image size\nscaled_w &lt;- df$width \/ annotation_image_width * yolo_image_width\nscaled_h &lt;- df$height \/ annotation_image_height * yolo_image_height\n\ndf2 &lt;- data.frame(width = scaled_w,height = scaled_h)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>     width            height       \n Min.   : 5.200   Min.   :  9.244  \n 1st Qu.: 7.475   1st Qu.: 13.289  \n Median :10.400   Median : 17.911  \n Mean   :12.867   Mean   : 22.273  \n 3rd Qu.:15.275   3rd Qu.: 26.578  \n Max.   :99.775   Max.   :166.400 <\/code><\/pre>\n\n\n\n<p>After we have a data frame with bounding box widths and heights  we can do K-mean clustering<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># do clustering in reproducible way\nset.seed(20)\nrtsdCluster &lt;- kmeans(df2, 9, nstart = 20)\nclusters &lt;- as.data.frame(rtsdCluster$centers)\n\n# rounding to get exact pixel value\nclusters$width &lt;- round(clusters$width)\nclusters$height &lt;- round(clusters$height)\n\n# calculation of bounding box area for sorting\nclusters$area &lt;- clusters$width * clusters$height\n\n# and finally sorting\nclusters &lt;- clusters[order(clusters$area),]\nclusters<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  width height area\n9     7     12   84\n1    10     17  170\n6    13     24  312\n4    26     20  520\n3    17     32  544\n5    23     41  943\n2    31     54 1674\n7    41     72 2952\n8    58    100 5800<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">YOLO Anchors for traffic sign detection<\/h3>\n\n\n\n<p>Original COCO dataset YOLO v3 anchors are:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326<\/pre>\n\n\n\n<p> ([width,height] pairs scaled to the YOLO internal image size) <\/p>\n\n\n\n<p>They are suitable for general purpose all scales object detection.<\/p>\n\n\n\n<p>In contrast, for the <strong>traffic sign detection<\/strong> in <strong>car camcorder videos<\/strong> of <strong>aspect ratio 16:9<\/strong> and <strong>YOLO image size of 416&#215;416<\/strong>,the anchors are:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">7,12,  10,17,  13,24,  26,20,  17,32,  23,41,  31,54,  41,72,  58,100<\/pre>\n\n\n\n<p>You can see that the size of the anchors is considerably smaller. That&#8217;s because we face only traffic signs that are in middle or long range from the camera.<\/p>\n\n\n\n<p>For more info about <a href=\"https:\/\/towardsdatascience.com\/yolo-v3-object-detection-53fb7d3bfe6b\">YOLO v3<\/a> in general see this post or <a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/\">original homepage<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The creators of YOLO v3 advice to regenerate &#8220;anchors&#8221; if you retrain YOLO for your own dataset. Here I describe what are anchors, how to generate them and also give specific anchor values for traffic sign detection problem. (If you need the anchors for training of traffic sign detection on 16:9 aspect ratio images, scroll &hellip; <a href=\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;YOLO v3 anchors for traffic sign detection&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,5],"tags":[55,61,50,52,62,60],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-study-research","tag-cnn","tag-computer-vision","tag-machine-learning","tag-neural-network","tag-object-detection","tag-yolo"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka<\/title>\n<meta name=\"description\" content=\"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka\" \/>\n<meta property=\"og:description\" content=\"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\" \/>\n<meta property=\"og:site_name\" content=\"Dmitry A. Grechka\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-13T18:01:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-13T18:01:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg\" \/>\n<meta name=\"author\" content=\"dmitry\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dmitry\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\",\"url\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\",\"name\":\"YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka\",\"isPartOf\":{\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg\",\"datePublished\":\"2019-09-13T18:01:50+00:00\",\"dateModified\":\"2019-09-13T18:01:52+00:00\",\"author\":{\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/63485104fdec6dbe258ea67c2e053a6f\"},\"description\":\"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.\",\"breadcrumb\":{\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage\",\"url\":\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg\",\"contentUrl\":\"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg\",\"width\":640,\"height\":219,\"caption\":\"bicycles prohibited traffic sign\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/grechka.family\/dmitry\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"YOLO v3 anchors for traffic sign detection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/#website\",\"url\":\"https:\/\/grechka.family\/dmitry\/blog\/\",\"name\":\"Dmitry A. Grechka\",\"description\":\"Personal blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/grechka.family\/dmitry\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/63485104fdec6dbe258ea67c2e053a6f\",\"name\":\"dmitry\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ce55dc1fed08e9a15667f56e3285826aa634c717d9c0e34809d717f699bb7b0b?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ce55dc1fed08e9a15667f56e3285826aa634c717d9c0e34809d717f699bb7b0b?s=96&d=identicon&r=g\",\"caption\":\"dmitry\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka","description":"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/","og_locale":"en_GB","og_type":"article","og_title":"YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka","og_description":"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.","og_url":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/","og_site_name":"Dmitry A. Grechka","article_published_time":"2019-09-13T18:01:50+00:00","article_modified_time":"2019-09-13T18:01:52+00:00","og_image":[{"url":"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg","type":"","width":"","height":""}],"author":"dmitry","twitter_misc":{"Written by":"dmitry","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/","url":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/","name":"YOLO v3 anchors for traffic sign detection - Dmitry A. Grechka","isPartOf":{"@id":"https:\/\/grechka.family\/dmitry\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage"},"image":{"@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage"},"thumbnailUrl":"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg","datePublished":"2019-09-13T18:01:50+00:00","dateModified":"2019-09-13T18:01:52+00:00","author":{"@id":"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/63485104fdec6dbe258ea67c2e053a6f"},"description":"Here I describe what is anchors, how to generate them and also give specific YOLO anchors for traffic sign detection problem.","breadcrumb":{"@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#primaryimage","url":"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg","contentUrl":"https:\/\/grechka.family\/dmitry\/blog\/wp-content\/uploads\/2019\/09\/4k-wallpaper-blur-bokeh-2174445.jpg","width":640,"height":219,"caption":"bicycles prohibited traffic sign"},{"@type":"BreadcrumbList","@id":"https:\/\/grechka.family\/dmitry\/blog\/2019\/09\/yolo-v3-anchors-for-traffic-sign-detection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/grechka.family\/dmitry\/blog\/"},{"@type":"ListItem","position":2,"name":"YOLO v3 anchors for traffic sign detection"}]},{"@type":"WebSite","@id":"https:\/\/grechka.family\/dmitry\/blog\/#website","url":"https:\/\/grechka.family\/dmitry\/blog\/","name":"Dmitry A. Grechka","description":"Personal blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/grechka.family\/dmitry\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/63485104fdec6dbe258ea67c2e053a6f","name":"dmitry","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/grechka.family\/dmitry\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ce55dc1fed08e9a15667f56e3285826aa634c717d9c0e34809d717f699bb7b0b?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ce55dc1fed08e9a15667f56e3285826aa634c717d9c0e34809d717f699bb7b0b?s=96&d=identicon&r=g","caption":"dmitry"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/comments?post=511"}],"version-history":[{"count":17,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/posts\/511\/revisions\/530"}],"wp:attachment":[{"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grechka.family\/dmitry\/blog\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}