{"id":23113874,"url":"https://github.com/thatgeeman/pybx","last_synced_at":"2025-08-16T20:31:34.655Z","repository":{"id":42048535,"uuid":"447566124","full_name":"thatgeeman/pybx","owner":"thatgeeman","description":"A simple python module to generate anchor (aka default/prior) boxes for object detection tasks. ","archived":false,"fork":false,"pushed_at":"2024-03-17T22:43:37.000Z","size":17038,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-04T04:34:38.322Z","etag":null,"topics":["anchor-box","anchor-boxes","bounding-boxes","computer-vision","deep-learning","fast-rcnn","fastrcnn","ground-truth","multi-box","multibox","multibox-detector","object-detection","positive-ground-truth-bounding-boxes","python","rcnn","rcnn-model","single-shot-detection","single-shot-detector","single-shot-multibox-detector"],"latest_commit_sha":null,"homepage":"https://thatgeeman.github.io/pybx/","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thatgeeman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-01-13T11:00:24.000Z","updated_at":"2024-08-21T19:30:12.000Z","dependencies_parsed_at":"2024-03-17T21:27:53.117Z","dependency_job_id":"452393eb-7f34-42c2-b6e6-dad68e5b51e4","html_url":"https://github.com/thatgeeman/pybx","commit_stats":{"total_commits":105,"total_committers":3,"mean_commits":35.0,"dds":0.2571428571428571,"last_synced_commit":"f9b3cf5676fe78f869b75f066bc7fc1125519efe"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thatgeeman%2Fpybx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thatgeeman%2Fpybx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thatgeeman%2Fpybx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thatgeeman%2Fpybx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thatgeeman","download_url":"https://codeload.github.com/thatgeeman/pybx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230056065,"owners_count":18165880,"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-box","anchor-boxes","bounding-boxes","computer-vision","deep-learning","fast-rcnn","fastrcnn","ground-truth","multi-box","multibox","multibox-detector","object-detection","positive-ground-truth-bounding-boxes","python","rcnn","rcnn-model","single-shot-detection","single-shot-detector","single-shot-multibox-detector"],"created_at":"2024-12-17T03:16:08.488Z","updated_at":"2024-12-17T03:16:09.200Z","avatar_url":"https://github.com/thatgeeman.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"PyBx\n================\n\n\u003c!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! --\u003e\n\n### Installation\n\n``` shell\npip install pybx\n```\n\n### Usage\n\nTo calculate the anchor boxes for a single feature size and aspect\nratio, given the image size:\n\n``` python\nfrom pybx import anchor, ops\n\nimage_sz = (256, 256)\nfeature_sz = (10, 10)\nasp_ratio = 1/2.\n\ncoords, labels = anchor.bx(image_sz, feature_sz, asp_ratio)\n```\n\n100 anchor boxes of `asp_ratio` 0.5 is generated along with [unique\nlabels](../data/README.md):\n\n``` python\nlen(coords), len(labels)\n```\n\n    (100, 100)\n\nThe anchor box labels are especially useful, since they are pretty\ndescriptive:\n\n``` python\ncoords[-1], labels[-1]\n```\n\n    ([234, 225, 252, 256], 'a_10x10_0.5_99')\n\nTo calculate anchor boxes for **multiple** feature sizes and aspect\nratios, we use `anchor.bxs` instead:\n\n``` python\nfeature_szs = [(10, 10), (8, 8)]\nasp_ratios = [1., 1/2., 2.]\n\ncoords, labels = anchor.bxs(image_sz, feature_szs, asp_ratios)\n```\n\nAll anchor boxes are returned as `ndarrays` of shape `(N,4)` where N is\nthe number of boxes.\n\nThe box labels are even more important now, since they help you uniquely\nidentify to which feature map size or aspect ratios they belong to.\n\n``` python\ncoords[101], labels[101]\n```\n\n    (array([29,  0, 47, 30]), 'a_10x10_0.5_1')\n\n``` python\ncoords[-1], labels[-1]\n```\n\n    (array([217, 228, 256, 251]), 'a_8x8_2.0_63')\n\n#### [`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx) methods\n\nBox coordinates (with/without labels) in any format (usually `ndarray`,\n`list`, `json`, `dict`) can be instantialized as a\n[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx),\nexposing many useful methods and attributes of\n[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx). For\nexample to calculate the area of each box iteratively:\n\n``` python\nfrom pybx.basics import * \n# passing anchor boxes and labels from anchor.bxs()\nprint(coords.shape)\n\nboxes = mbx(coords, labels)\ntype(boxes)\n```\n\n    (492, 4)\n\n    pybx.basics.MultiBx\n\n``` python\nlen(boxes)\n```\n\n    492\n\n``` python\nareas = [b.area for b in boxes]\n```\n\nEach annotation in the\n[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)\nobject `boxes` is also a\n[`BaseBx`](https://thatgeeman.github.io/pybx/basics.html#basebx) with\nits own set of methods and properties.\n\n``` python\nboxes[-1]\n```\n\n    BaseBx(coords=[[217, 228, 256, 251]], label=['a_8x8_2.0_63'])\n\n``` python\nboxes[-1].coords, boxes[-1].label\n```\n\n    ([[217, 228, 256, 251]], (#1) ['a_8x8_2.0_63'])\n\n[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)\nobjects can also be “added” which stacks them vertically to create a new\n[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)\nobject:\n\n``` python\nboxes_true = mbx(coords_json)    # annotation as json records\nlen(boxes_true)\n```\n\n    2\n\n``` python\nboxes_anchor = mbx(coords_numpy) # annotation as ndarray\nlen(boxes_anchor)\n```\n\n    492\n\n``` python\nboxes_true\n```\n\n    MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])\n\n``` python\nboxes = boxes_true + boxes_anchor + boxes_true\n```\n\n``` python\nlen(boxes)\n```\n\n    496\n\n# Use ground truth boxes for model training\n\n``` python\nfrom pybx.anchor import get_gt_thresh_iou, get_gt_max_iou\nfrom pybx.vis import VisBx\n```\n\n``` python\nimage_sz\n```\n\n    (256, 256)\n\n``` python\nboxes_true\n```\n\n    MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])\n\nCalculate candidate anchor boxes for many aspect ratios and scales.\n\n``` python\nfeature_szs = [(10, 10), (3, 3), (2, 2)]\nasp_ratios = [0.3, 1/2., 2.]\n\nanchors, labels = anchor.bxs(image_sz, feature_szs, asp_ratios)\n```\n\nWrap using pybx methods. This step is not necessary but convenient.\n\n``` python\nboxes_anchor = get_bx(anchors, labels) \nlen(boxes_anchor)\n```\n\n    341\n\nThe following function returns two positive ground truth anchors with\nlargest IOU for each class in the label bounding boxes passed.\n\n``` python\ngt_anchors, gt_ious, gt_masks = get_gt_max_iou( \n    true_annots=boxes_true, \n    anchor_boxes=boxes_anchor,  # if plain numpy, pass anchor_boxes and anchor_labels \n    update_labels=False,  # whether to replace ground truth labels with true labels\n    positive_boxes=1,  # can request extra boxes \n)\n```\n\n``` python\ngt_anchors\n```\n\n    {'clock': BaseBx(coords=[[156, 0, 227, 180]], label=['a_2x2_0.3_1']),\n     'frame': BaseBx(coords=[[12, 152, 72, 256]], label=['a_3x3_0.5_6'])}\n\n``` python\nall_gt_anchors = gt_anchors['clock'] + gt_anchors['frame']\nall_gt_anchors\n```\n\n    /mnt/data/projects/pybx/pybx/basics.py:464: BxViolation: Change of object type imminent if trying to add \u003cclass 'pybx.basics.BaseBx'\u003e+\u003cclass 'pybx.basics.BaseBx'\u003e. Use \u003cclass 'pybx.basics.BaseBx'\u003e+\u003cclass 'pybx.basics.BaseBx'\u003e instead or basics.stack_bxs().\n      f\"Change of object type imminent if trying to add \"\n\n    MultiBx(coords=[[156, 0, 227, 180], [12, 152, 72, 256]], label=['a_2x2_0.3_1', 'a_3x3_0.5_6'])\n\n``` python\nv = VisBx(pth='../data/', img_fn='image.jpg', image_sz=image_sz)\nv.show(all_gt_anchors, color={'a_2x2_0.3_1':'red', 'a_3x3_0.5_6': 'red'})\n```\n\n    \u003cAxesSubplot:\u003e\n\n![](index_files/figure-commonmark/cell-26-output-2.png)\n\nMore exploratory stuff in the [walkthrough\nnotebook](https://github.com/thatgeeman/pybx/blob/master/examples/pybx_walkthrough_0.4.ipynb)\nor [![Open In\nCollab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/thatgeeman/pybx/blob/master/examples/pybx_walkthrough_0.4.ipynb)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthatgeeman%2Fpybx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthatgeeman%2Fpybx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthatgeeman%2Fpybx/lists"}