PyBx
================
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
### Installation
``` shell
pip install pybx
```
### Usage
To calculate the anchor boxes for a single feature size and aspect
ratio, given the image size:
``` python
from pybx import anchor, ops
image_sz = (256, 256)
feature_sz = (10, 10)
asp_ratio = 1/2.
coords, labels = anchor.bx(image_sz, feature_sz, asp_ratio)
```
100 anchor boxes of `asp_ratio` 0.5 is generated along with [unique
labels](../data/README.md):
``` python
len(coords), len(labels)
```
(100, 100)
The anchor box labels are especially useful, since they are pretty
descriptive:
``` python
coords[-1], labels[-1]
```
([234, 225, 252, 256], 'a_10x10_0.5_99')
To calculate anchor boxes for **multiple** feature sizes and aspect
ratios, we use `anchor.bxs` instead:
``` python
feature_szs = [(10, 10), (8, 8)]
asp_ratios = [1., 1/2., 2.]
coords, labels = anchor.bxs(image_sz, feature_szs, asp_ratios)
```
All anchor boxes are returned as `ndarrays` of shape `(N,4)` where N is
the number of boxes.
The box labels are even more important now, since they help you uniquely
identify to which feature map size or aspect ratios they belong to.
``` python
coords[101], labels[101]
```
(array([29, 0, 47, 30]), 'a_10x10_0.5_1')
``` python
coords[-1], labels[-1]
```
(array([217, 228, 256, 251]), 'a_8x8_2.0_63')
#### [`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx) methods
Box coordinates (with/without labels) in any format (usually `ndarray`,
`list`, `json`, `dict`) can be instantialized as a
[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx),
exposing many useful methods and attributes of
[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx). For
example to calculate the area of each box iteratively:
``` python
from pybx.basics import *
# passing anchor boxes and labels from anchor.bxs()
print(coords.shape)
boxes = mbx(coords, labels)
type(boxes)
```
(492, 4)
pybx.basics.MultiBx
``` python
len(boxes)
```
492
``` python
areas = [b.area for b in boxes]
```
Each annotation in the
[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)
object `boxes` is also a
[`BaseBx`](https://thatgeeman.github.io/pybx/basics.html#basebx) with
its own set of methods and properties.
``` python
boxes[-1]
```
BaseBx(coords=[[217, 228, 256, 251]], label=['a_8x8_2.0_63'])
``` python
boxes[-1].coords, boxes[-1].label
```
([[217, 228, 256, 251]], (#1) ['a_8x8_2.0_63'])
[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)
objects can also be “added” which stacks them vertically to create a new
[`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx)
object:
``` python
boxes_true = mbx(coords_json) # annotation as json records
len(boxes_true)
```
2
``` python
boxes_anchor = mbx(coords_numpy) # annotation as ndarray
len(boxes_anchor)
```
492
``` python
boxes_true
```
MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])
``` python
boxes = boxes_true + boxes_anchor + boxes_true
```
``` python
len(boxes)
```
496
# Use ground truth boxes for model training
``` python
from pybx.anchor import get_gt_thresh_iou, get_gt_max_iou
from pybx.vis import VisBx
```
``` python
image_sz
```
(256, 256)
``` python
boxes_true
```
MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])
Calculate candidate anchor boxes for many aspect ratios and scales.
``` python
feature_szs = [(10, 10), (3, 3), (2, 2)]
asp_ratios = [0.3, 1/2., 2.]
anchors, labels = anchor.bxs(image_sz, feature_szs, asp_ratios)
```
Wrap using pybx methods. This step is not necessary but convenient.
``` python
boxes_anchor = get_bx(anchors, labels)
len(boxes_anchor)
```
341
The following function returns two positive ground truth anchors with
largest IOU for each class in the label bounding boxes passed.
``` python
gt_anchors, gt_ious, gt_masks = get_gt_max_iou(
true_annots=boxes_true,
anchor_boxes=boxes_anchor, # if plain numpy, pass anchor_boxes and anchor_labels
update_labels=False, # whether to replace ground truth labels with true labels
positive_boxes=1, # can request extra boxes
)
```
``` python
gt_anchors
```
{'clock': BaseBx(coords=[[156, 0, 227, 180]], label=['a_2x2_0.3_1']),
'frame': BaseBx(coords=[[12, 152, 72, 256]], label=['a_3x3_0.5_6'])}
``` python
all_gt_anchors = gt_anchors['clock'] + gt_anchors['frame']
all_gt_anchors
```
/mnt/data/projects/pybx/pybx/basics.py:464: BxViolation: Change of object type imminent if trying to add <class 'pybx.basics.BaseBx'>+<class 'pybx.basics.BaseBx'>. Use <class 'pybx.basics.BaseBx'>+<class 'pybx.basics.BaseBx'> instead or basics.stack_bxs().
f"Change of object type imminent if trying to add "
MultiBx(coords=[[156, 0, 227, 180], [12, 152, 72, 256]], label=['a_2x2_0.3_1', 'a_3x3_0.5_6'])
``` python
v = VisBx(pth='../data/', img_fn='image.jpg', image_sz=image_sz)
v.show(all_gt_anchors, color={'a_2x2_0.3_1':'red', 'a_3x3_0.5_6': 'red'})
```
<AxesSubplot:>
![](index_files/figure-commonmark/cell-26-output-2.png)
More exploratory stuff in the [walkthrough
notebook](https://github.com/thatgeeman/pybx/blob/master/examples/pybx_walkthrough_0.4.ipynb)
or [![Open In
Collab](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)
Raw data
{
"_id": null,
"home_page": "https://github.com/thatgeeman/pybx",
"name": "pybx",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "python computer-vision deep-learning fast-rcnn object-detection bounding-boxes rcnn multibox single-shot-multibox-detector single-shot-detection anchor-box rcnn-model multi-box single-shot-detector anchor-boxes multibox-detector",
"author": "Geevarghese George",
"author_email": "thatgeeman@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0a/3d/ca0f0a1a3c1ccbb09e06797da4593c971ad179aed916c98ef72a6753b33a/pybx-0.4.1.tar.gz",
"platform": null,
"description": "PyBx\n================\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\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 \u201cadded\u201d 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 <class 'pybx.basics.BaseBx'>+<class 'pybx.basics.BaseBx'>. Use <class 'pybx.basics.BaseBx'>+<class 'pybx.basics.BaseBx'> 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 <AxesSubplot:>\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",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A simple python module to generate anchor boxes for object detection tasks.",
"version": "0.4.1",
"project_urls": {
"Homepage": "https://github.com/thatgeeman/pybx"
},
"split_keywords": [
"python",
"computer-vision",
"deep-learning",
"fast-rcnn",
"object-detection",
"bounding-boxes",
"rcnn",
"multibox",
"single-shot-multibox-detector",
"single-shot-detection",
"anchor-box",
"rcnn-model",
"multi-box",
"single-shot-detector",
"anchor-boxes",
"multibox-detector"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "65d0ed9f6c0b21859fea2a22a6ccb026f2e10fc4f68560ccda7755ffdd78d352",
"md5": "94f080933808cc4ea1855519f656f258",
"sha256": "a02becc82a34fea236b8289e4cd55a28f3b4cbb3c84919d59e6a2adac01f2144"
},
"downloads": -1,
"filename": "pybx-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "94f080933808cc4ea1855519f656f258",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 30098,
"upload_time": "2023-08-20T09:33:07",
"upload_time_iso_8601": "2023-08-20T09:33:07.040619Z",
"url": "https://files.pythonhosted.org/packages/65/d0/ed9f6c0b21859fea2a22a6ccb026f2e10fc4f68560ccda7755ffdd78d352/pybx-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0a3dca0f0a1a3c1ccbb09e06797da4593c971ad179aed916c98ef72a6753b33a",
"md5": "9aff85aff27e2f09ce3405e13b9a6ebe",
"sha256": "74ccec67989b7511f970a8841c2ef79def3cc215b94fb227e13bc20e2f492b23"
},
"downloads": -1,
"filename": "pybx-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "9aff85aff27e2f09ce3405e13b9a6ebe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 27767,
"upload_time": "2023-08-20T09:33:08",
"upload_time_iso_8601": "2023-08-20T09:33:08.706024Z",
"url": "https://files.pythonhosted.org/packages/0a/3d/ca0f0a1a3c1ccbb09e06797da4593c971ad179aed916c98ef72a6753b33a/pybx-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-20 09:33:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thatgeeman",
"github_project": "pybx",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pybx"
}