Skip to content

Commit

Permalink
ground truth filters based on IOU (#18)
Browse files Browse the repository at this point in the history
* The coords are `int` now.

* Added `get_gt_max_thresh` to generate ground truth boxes and extra ground truth boxes with thresholding IOUs

* Added `get_gt_max_iou` to generate ground truth boxes and extra ground truth boxes from largest IOUs

* update unittests and workflow

* run export and build

* update README.md

* Create python-publish.yml
  • Loading branch information
thatgeeman authored Aug 15, 2023
1 parent 024e037 commit 7899bf3
Show file tree
Hide file tree
Showing 80 changed files with 7,592 additions and 9,575 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade build
- name: Get version
run: |
export PYBX_VERSION=$(cat settings.ini| grep "version =" | cut -d' ' -f3)
echo "setting PYBX_VERSION = $PYBX_VERSION"
echo "$(ls dist/*$PYBX_VERSION*)$"
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.7"
python-version: "3.8"
- name: Install pip
run: |
python -m pip install --upgrade pip
Expand All @@ -40,6 +40,7 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
- name: Test with unittests
run: |
pytest .
# pytest .
python -m unittest tests/test_*.py
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ dmypy.json

# Pyre type checker
.pyre/

# kdev
*.kdev4
*.kdev4/
changes

2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ repos:
rev: 2.3.7
hooks:
- id: nbdev_clean
- id: nbdev_export
- id: nbdev_export
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ black = "*"
pybx = {editable = true, path = "."}

[requires]
python_version = "3.7"
python_version = "3.8"
1,232 changes: 666 additions & 566 deletions Pipfile.lock

Large diffs are not rendered by default.

101 changes: 88 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
Use ground truth boxes for model training
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

[![PyPI
version](https://badge.fury.io/py/pybx.svg)](https://badge.fury.io/py/pybx)
[![Open In
Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/thatgeeman/pybx/blob/master/nbs/pybx_walkthrough.ipynb)
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)

A simple python package to generate anchor boxes for multi-box object
detection models.

Calculated anchor boxes are in `pascal_voc` format by default.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

### Installation

``` shell
Expand All @@ -25,7 +27,7 @@ ratio, given the image size:
``` python
from pybx import anchor, ops

image_sz = (300, 300)
image_sz = (256, 256)
feature_sz = (10, 10)
asp_ratio = 1/2.

Expand All @@ -48,8 +50,7 @@ descriptive:
coords[-1], labels[-1]
```

([274.3933982822018, 263.7867965644036, 295.6066017177982, 300.0],
'a_10x10_0.5_99')
([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:
Expand All @@ -71,14 +72,13 @@ identify to which feature map size or aspect ratios they belong to.
coords[101], labels[101]
```

(array([34.39339828, 0. , 55.60660172, 36.21320344]), 'a_10x10_0.5_1')
(array([29, 0, 47, 30]), 'a_10x10_0.5_1')

``` python
coords[-1], labels[-1]
```

(array([254.73349571, 267.99174785, 300. , 294.50825215]),
'a_8x8_2.0_63')
(array([217, 228, 256, 251]), 'a_8x8_2.0_63')

#### [`MultiBx`](https://thatgeeman.github.io/pybx/basics.html#multibx) methods

Expand Down Expand Up @@ -122,14 +122,13 @@ its own set of methods and properties.
boxes[-1]
```

BaseBx(coords=[[254.73349570550448, 267.9917478527522, 300.0, 294.5082521472478]], label=['a_8x8_2.0_63'])
BaseBx(coords=[[217, 228, 256, 251]], label=['a_8x8_2.0_63'])

``` python
boxes[-1].coords, boxes[-1].label
```

((#1) [[254.73349570550448, 267.9917478527522, 300.0, 294.5082521472478]],
(#1) ['a_8x8_2.0_63'])
([[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
Expand All @@ -154,7 +153,7 @@ len(boxes_anchor)
boxes_true
```

MultiBx(coords=[[150, 70, 270, 220], [10, 180, 115, 260]], label=['clock', 'frame'])
MultiBx(coords=[[130, 63, 225, 180], [13, 158, 90, 213]], label=['clock', 'frame'])

``` python
boxes = boxes_true + boxes_anchor + boxes_true
Expand All @@ -165,3 +164,79 @@ len(boxes)
```

496

``` 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](../examples/pybx_walkthrough_0.4.ipynb)!
Loading

0 comments on commit 7899bf3

Please sign in to comment.