{"id":14959016,"url":"https://github.com/joydeepmedhi/anchor-boxes-with-kmeans","last_synced_at":"2025-05-02T12:31:44.218Z","repository":{"id":141856096,"uuid":"145549759","full_name":"joydeepmedhi/Anchor-Boxes-with-KMeans","owner":"joydeepmedhi","description":"How to initialize Anchors in Faster RCNN for custom dataset?","archived":false,"fork":false,"pushed_at":"2020-08-18T15:39:01.000Z","size":6046,"stargazers_count":91,"open_issues_count":2,"forks_count":31,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-07T02:05:48.915Z","etag":null,"topics":["anchor","anchor-box","aspect-ratio","bounding-boxes","clusters","computer-vision","custom-dataset","detection","distance-metric","faster-rcnn","hyperparameters","iou","kmeans","object-detection","python","tensorflow-models"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joydeepmedhi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-08-21T10:41:00.000Z","updated_at":"2024-07-31T04:42:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"6e5ec3f0-6c1d-453a-8c8f-73793f1fc617","html_url":"https://github.com/joydeepmedhi/Anchor-Boxes-with-KMeans","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joydeepmedhi%2FAnchor-Boxes-with-KMeans","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joydeepmedhi%2FAnchor-Boxes-with-KMeans/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joydeepmedhi%2FAnchor-Boxes-with-KMeans/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joydeepmedhi%2FAnchor-Boxes-with-KMeans/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joydeepmedhi","download_url":"https://codeload.github.com/joydeepmedhi/Anchor-Boxes-with-KMeans/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252038183,"owners_count":21684643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anchor","anchor-box","aspect-ratio","bounding-boxes","clusters","computer-vision","custom-dataset","detection","distance-metric","faster-rcnn","hyperparameters","iou","kmeans","object-detection","python","tensorflow-models"],"created_at":"2024-09-24T13:18:42.125Z","updated_at":"2025-05-02T12:31:39.434Z","avatar_url":"https://github.com/joydeepmedhi.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Initial Anchor Boxes Estimation using KMeans Clusterring for Faster-RCNN\n\n[![CodeFactor](https://www.codefactor.io/repository/github/joydeepmedhi/anchor-boxes-with-kmeans/badge)](https://www.codefactor.io/repository/github/joydeepmedhi/anchor-boxes-with-kmeans)\n\n## Introduction\nFaster-RCNN is one of the **state-of-the-art** object detection algorithms around.\n\nIf you are not familiar with Faster-RCNN, Please go through [this blog](https://tryolabs.com/blog/2018/01/18/faster-r-cnn-down-the-rabbit-hole-of-modern-object-detection/).\n\nHere is the link to the original paper [ Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks](https://arxiv.org/abs/1506.01497).\n\nWhen we train Faster RCNN for custom datasets, we often get confused over how to choose hyperparameters for the Network. Anchor boxes (one of the hyperparameters) are very important to detect objects with different scales and aspect ratios. We will get improved detection results if we get the anchors right.\n\nThe training \u0026 hypereparameters are in accordance with **Tensorflow Object Detection [API](https://github.com/tensorflow/models/tree/master/research/object_detection)**.\n\n### Faster-RCNN config file\n```\nfaster_rcnn{\n    # other hyperparameters\n\n    first_stage_anchor_generator {\n      grid_anchor_generator {\n        height: 256\n        width: 256\n        height_stride: 16\n        width_stride: 16\n        scales: 0.9\n        scales: 1.14\n        scales: 1.53\n        aspect_ratios: .8\n        aspect_ratios: 1.15\n        aspect_ratios: 2.77\n      }\n    }    \n}\n```\n#### height \u0026 width\nThis is the size of base anchor size. (i.e. for scale 1 and aspect ratio 1, base anchor is 256 x 256)\n\n#### height_stride \u0026 width_stride\nThis is basically the stride of anchor centers. Generally, we want to visit each point of the feature map (final convolutional layer) and create a set of anchors. Hence, It is the *subsampling ratio* of the network. In case of **VGG16** this ratio is 16. Different network archetectures have different subsampling ratios. User may select this stride as per the base-model or use case.\n\n#### scales \u0026 aspect_ratios\n\nAspect Ratio of an anchor box is basically *width/height*. Scales are bigger as the anchor box are from base box (i.e. 512 x 512 box is twice as big as 256 x 256).\n```\nif aspect_ratio = ar\n   base_anchor = 256 x 256\n   \"width_b x height_b\" is the dimension of an anchor box\n\nwidth_b = scale * sqrt(ar) * base_anchor[0]\nheight_b = scale * base_anchor[1] / sqrt(ar)\n```\n---\n## Analysis of bounding boxes (Training data)\n\n1. Convert the *XML* files to a *csv* file.\n\n    ``` xml_to_csv.py``` (modify this file as per your *XML* format)\n2. Open     ```EDA_of_bbox.ipynb ```    **jupyter notebook** for analysis.\n\n    Here, we convert the image dimension with *_compute_new_static_size()* function. Then we normalize bounding box height and width according to new image dimension. \n\nThen we find optimal clusters and cluster centers using **K-Means**. This is inspired from [YOLO](https://pjreddie.com/darknet/yolo/).\n\nDistribution of Bounding Boxes!\n\n![bbox](images/readme/bbox.png)\n\n### Experiments\n\n#### 1\n\nCluster bbox (width, height) on **eucledian** distance metric\n\n ![clusters](images/readme/cluster.png)\n\n  \n  Blue Line - Base Model (cards dataset)\n\n  Red Line - Cluster Model (cards dataset)\n  \n  ![precision_eu](images/readme/presicion_eucle.png)\n\n  ![f1_eu](images/readme/F1_eucl.png)\n\n#### 2\nCluster bbox (width, height) on **iou** metric (This is prefered as eucledian distance metric will give priority to bigger boxes and minimize their loss)\n  \n  Blue Line - Base Model (cards dataset)\n\n  Pink Line - IOU Cluster Model (cards dataset)\n    \n![Precision_iou](images/readme/precision.png)\n![F1_iou](images/readme/F1.png)\n\n#### 3\n  Cluster **AR** and **Scales** of bbox Separately with **eucledian** distance metric.\n \n    Blue Line - Base Model (cards dataset)\n\n    Green Line - Cluster Model (cards dataset)\n  \n![Precision_a_s](images/readme/precision_a_s.png)\n![F1_a_s](images/readme/F1_a_s.png)\n\n\n*************** **More to be added** *****************\n\n---\n## References\n1. [KMeans in YOLO](https://lars76.github.io/object-detection/k-means-anchor-boxes/)\n2. [Cards Dataset (Reference)](https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10)\n3. [Advantage \u0026 Disadvantage of KMeans](http://playwidtech.blogspot.com/2013/02/k-means-clustering-advantages-and.html)\n4. [Different Clusterring Algorithms](https://towardsdatascience.com/the-5-clustering-algorithms-data-scientists-need-to-know-a36d136ef68)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoydeepmedhi%2Fanchor-boxes-with-kmeans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoydeepmedhi%2Fanchor-boxes-with-kmeans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoydeepmedhi%2Fanchor-boxes-with-kmeans/lists"}