Git Product home page Git Product logo

thatgeeman / pybx Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 16.64 MB

A simple python module to generate anchor (aka default/prior) boxes for object detection tasks.

Home Page: https://thatgeeman.github.io/pybx/

License: MIT License

Python 5.34% Jupyter Notebook 94.62% CSS 0.04% Makefile 0.01%
anchor-boxes computer-vision deep-learning object-detection bounding-boxes anchor-box python multi-box multibox-detector multibox

pybx's Introduction

PyBx

Installation

pip install pybx

Usage

To calculate the anchor boxes for a single feature size and aspect ratio, given the image size:

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:

len(coords), len(labels)
(100, 100)

The anchor box labels are especially useful, since they are pretty descriptive:

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:

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.

coords[101], labels[101]
(array([29,  0, 47, 30]), 'a_10x10_0.5_1')
coords[-1], labels[-1]
(array([217, 228, 256, 251]), 'a_8x8_2.0_63')

MultiBx methods

Box coordinates (with/without labels) in any format (usually ndarray, list, json, dict) can be instantialized as a MultiBx, exposing many useful methods and attributes of MultiBx. For example to calculate the area of each box iteratively:

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
len(boxes)
492
areas = [b.area for b in boxes]

Each annotation in the MultiBx object boxes is also a BaseBx with its own set of methods and properties.

boxes[-1]
BaseBx(coords=[[217, 228, 256, 251]], label=['a_8x8_2.0_63'])
boxes[-1].coords, boxes[-1].label
([[217, 228, 256, 251]], (#1) ['a_8x8_2.0_63'])

MultiBx objects can also be “added” which stacks them vertically to create a new MultiBx object:

boxes_true = mbx(coords_json)    # annotation as json records
len(boxes_true)
2
boxes_anchor = mbx(coords_numpy) # annotation as ndarray
len(boxes_anchor)
492
boxes_true
MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])
boxes = boxes_true + boxes_anchor + boxes_true
len(boxes)
496

Use ground truth boxes for model training

from pybx.anchor import get_gt_thresh_iou, get_gt_max_iou
from pybx.vis import VisBx
image_sz
(256, 256)
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.

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.

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.

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 
)
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'])}
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'])
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:>

More exploratory stuff in the walkthrough notebook or Open In Collab

pybx's People

Contributors

thatgeeman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pybx's Issues

Walkthrough issue for PIL mode.

In the step: Ask VisBx to use random logits with logits=True

vis.VisBx(image_sz=image_sz, logits=True, feature_sz=feature_sz).show(anchors, labels)

Returns a key error: KeyError: ((1, 1, 3), '<i8') and TypeError: Cannot handle this data type: (1, 1, 3), <i8 with PIL.

Fix notebook (walkthrough)

Describe the bug

  • walkthrough link fails
  • Code import os bug

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

get_gt_max_iou reutrns MultiBx even if single box in it.

Describe the bug
get_gt_max_iou returns a MultiBx even if single box in it.

To Reproduce
Steps to reproduce the behavior:

passing dict

true_annots = [{'x_min': 130, 'y_min': 63, 'x_max': 225, 'y_max': 180, 'label': 'clock'},
 {'x_min': 13, 'y_min': 158, 'x_max': 90, 'y_max': 213, 'label': 'frame'}]

get_gt_max_iou(
    true_annots,
    coords, 
    positive_boxes=1,  
)

> ({'clock': MultiBx(coords=[[183, 67, 243, 188]], label=['clock']),
  'frame': MultiBx(coords=[12, 152, 72, 256]], label=['frame'])},
{},
{})

Expected behavior
Returns a BaseBx.

TypeError: 'BaseBx' object is not iterable

Describe the bug
draw method of vis module tries to iterate over BaseBx during visualisation

To Reproduce
Steps to reproduce the behavior:

anns = {'label': 5,
 'x_min': 87.0,
 'y_min': 196.0,
 'x_max': 1013.0,
 'y_max': 2129.0}

from pybx.ops import make_array
coords, label = make_array(anns)

b = bbx(coords, label)
vis.draw(img, b)

Missing sidebar in documentation page

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

KeyError: 'label'

Describe the bug
draw() looks for key 'label'

To Reproduce
pass annotations Dict without the key for 'label'

Region proposals

Is your feature request related to a problem? Please describe.
Rather than creating a bunch of anchor boxes based on geometry, create region proposals based on classic signal processing.

get_bx wont work with `dict` without 'label' key

Describe the bug
get_bx wont work with dict without 'label' key

To Reproduce
Steps to reproduce the behavior:

# passing dict
get_bx({'x_min': 130, 'y_min': 63, 'x_max': 225, 'y_max': 180})

Expected behavior
Returns a Bx with 'unknown' or empty label.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.