Skip to content

Commit

Permalink
Merge pull request #17 from MiXaiLL76/dev
Browse files Browse the repository at this point in the history
update v1.4.2
  • Loading branch information
MiXaiLL76 committed Jan 30, 2024
2 parents 5057ad0 + ec01b08 commit d8c6a93
Show file tree
Hide file tree
Showing 28 changed files with 399 additions and 323 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203, E501
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: docker-sdist docker-3.7 docker-3.8 docker-3.9 docker-3.10 docker-3.11
all: format docker-sdist docker-3.6 docker-3.7 docker-3.8 docker-3.9 docker-3.10 docker-3.11
ls -lah dist

sdist:
Expand All @@ -14,6 +14,9 @@ wheel:
docker-sdist:
bash -e docker/auto_build.sh "cp38-cp38" sdist

docker-3.6:
bash -e docker/auto_build.sh "cp36-cp36m" wheel

docker-3.7:
bash -e docker/auto_build.sh "cp37-cp37m" wheel

Expand All @@ -37,6 +40,11 @@ pull-prod:
twine check dist/*
twine upload dist/*

format:
python3 -m black --config pyproject.toml .
python3 -m isort .
flake8

clean:
rm -rf build
rm -rf *.egg-info
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cur = Curves(cocoGt, cocoDt, iou_tresh=0.5, iouType='segm')
cur.plot_pre_rec()
````

# Usage pnly GT preview
## Usage pnly GT preview

```py
cocoGt = COCO(....)
Expand All @@ -70,6 +70,12 @@ cocoGt = COCO(....)

## history

### v1.4.2
- [x] append Auto-formatters
- [x] append py36 support
- [x] append pandas to requirements for plotly[express]
- [x] update mask api with pycootools

### v1.4.1

- [x] append Plotly fig return
Expand Down
60 changes: 48 additions & 12 deletions csrc/mask/common/maskApi.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,26 @@ void rleEncode( RLE *R, const byte *M, siz h, siz w, siz n ) {
free(cnts);
}

void rleDecode( const RLE *R, byte *M, siz n ) {
// Decodes n different RLEs that have the same width and height. Write results to M.
// Returns whether the decoding succeeds or not.
byte rleDecode( const RLE *R, byte *M, siz n ) {
// Safeguards for memory boundary
siz s=R[0].h*R[0].w*n;
siz c=0;
siz i, j, k; for( i=0; i<n; i++ ) {
byte v=0; for( j=0; j<R[i].m; j++ ) {
for( k=0; k<R[i].cnts[j]; k++ ) *(M++)=v; v=!v; }}
for( k=0; k<R[i].cnts[j]; k++ ) {
if ( c >= s ) {
// Memory boundary would be crossed, wrong RLE
return 0;
}
c++;
*(M++)=v;
}
v=!v;
}
}
return 1;
}

void rleMerge( const RLE *R, RLE *M, siz n, int intersect ) {
Expand Down Expand Up @@ -132,14 +148,29 @@ void bbNms( BB dt, siz n, uint *keep, double thr ) {

void rleToBbox( const RLE *R, BB bb, siz n ) {
siz i; for( i=0; i<n; i++ ) {
uint h, w, x, y, xs, ys, xe, ye, xp, cc, t; siz j, m;
uint h, w, xs, ys, xe, ye, cc; siz j, m;
h=(uint)R[i].h; w=(uint)R[i].w; m=R[i].m;
m=((siz)(m/2))*2; xs=w; ys=h; xe=ye=0; cc=0;
if(m==0) { bb[4*i+0]=bb[4*i+1]=bb[4*i+2]=bb[4*i+3]=0; continue; }
for( j=0; j<m; j++ ) {
cc+=R[i].cnts[j]; t=cc-j%2; y=t%h; x=(t-y)/h;
if(j%2==0) xp=x; else if(xp<x) { ys=0; ye=h-1; }
xs=umin(xs,x); xe=umax(xe,x); ys=umin(ys,y); ye=umax(ye,y);
uint start = cc; // start of current segment
cc+=R[i].cnts[j]; // start of next segment
if (j % 2 == 0) continue; // skip background segment
if (R[i].cnts[j] == 0) continue; // skip zero-length foreground segment
uint y_start = start % h, x_start = (start - y_start) / h;
uint y_end = (cc - 1) % h, x_end = (cc - 1 - y_end) / h;

// x_start <= x_end must be true
xs = umin(xs, x_start);
xe = umax(xe, x_end);

if (x_start < x_end) {
ys = 0; ye = h - 1; // foreground segment goes across columns
} else {
// if x_start == x_end, then y_start <= y_end must be true
ys = umin(ys, y_start);
ye = umax(ye, y_end);
}
}
bb[4*i+0]=xs; bb[4*i+2]=xe-xs+1;
bb[4*i+1]=ys; bb[4*i+3]=ye-ys+1;
Expand All @@ -163,8 +194,10 @@ void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w ) {
/* upsample and get discrete points densely along entire boundary */
siz j, m=0; double scale=5; int *x, *y, *u, *v; uint *a, *b;
x=malloc(sizeof(int)*(k+1)); y=malloc(sizeof(int)*(k+1));
for(j=0; j<k; j++) x[j]=(int)(scale*xy[j*2+0]+.5); x[k]=x[0];
for(j=0; j<k; j++) y[j]=(int)(scale*xy[j*2+1]+.5); y[k]=y[0];
for(j=0; j<k; j++) x[j]=(int)(scale*xy[j*2+0]+.5);
x[k]=x[0];
for(j=0; j<k; j++) y[j]=(int)(scale*xy[j*2+1]+.5);
y[k]=y[0];
for(j=0; j<k; j++) m+=umax(abs(x[j]-x[j+1]),abs(y[j]-y[j+1]))+1;
u=malloc(sizeof(int)*m); v=malloc(sizeof(int)*m); m=0;
for( j=0; j<k; j++ ) {
Expand Down Expand Up @@ -209,23 +242,26 @@ char* rleToString( const RLE *R ) {
x=(long) R->cnts[i]; if(i>2) x-=(long) R->cnts[i-2]; more=1;
while( more ) {
char c=x & 0x1f; x >>= 5; more=(c & 0x10) ? x!=-1 : x!=0;
if(more) c |= 0x20; c+=48; s[p++]=c;
if(more) c |= 0x20;
c+=48; s[p++]=c;
}
}
s[p]=0; return s;
}

void rleFrString( RLE *R, char *s, siz h, siz w ) {
siz m=0, p=0, k; long x; int more; uint *cnts;
while( s[m] ) m++; cnts=malloc(sizeof(uint)*m); m=0;
while( s[m] ) m++;
cnts=malloc(sizeof(uint)*m); m=0;
while( s[p] ) {
x=0; k=0; more=1;
while( more ) {
char c=s[p]-48; x |= (c & 0x1f) << 5*k;
more = c & 0x20; p++; k++;
if(!more && (c & 0x10)) x |= -1 << 5*k;
}
if(m>2) x+=(long) cnts[m-2]; cnts[m++]=(uint) x;
if(m>2) x+=(long) cnts[m-2];
cnts[m++]=(uint) x;
}
rleInit(R,h,w,m,cnts); free(cnts);
}
}
4 changes: 2 additions & 2 deletions csrc/mask/common/maskApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void rlesFree( RLE **R, siz n );
void rleEncode( RLE *R, const byte *mask, siz h, siz w, siz n );

/* Decode binary masks encoded via RLE. */
void rleDecode( const RLE *R, byte *mask, siz n );
byte rleDecode( const RLE *R, byte *mask, siz n );

/* Compute union or intersection of encoded masks. */
void rleMerge( const RLE *R, RLE *M, siz n, int intersect );
Expand Down Expand Up @@ -57,4 +57,4 @@ void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w );
char* rleToString( const RLE *R );

/* Convert from compressed string representation of encoded mask. */
void rleFrString( RLE *R, char *s, siz h, siz w );
void rleFrString( RLE *R, char *s, siz h, siz w );
16 changes: 10 additions & 6 deletions csrc/mask/pycocotools/_mask.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# distutils: language = c
# cython: language_level=2

#**************************************************************************
# Microsoft COCO Toolbox. version 2.0
Expand All @@ -11,13 +10,15 @@
__author__ = 'tsungyi'

import sys

PYTHON_VERSION = sys.version_info[0]

# import both Python-level and C-level symbols of Numpy
# the API uses Numpy to interface C and Python
import numpy as np

cimport numpy as np
from libc.stdlib cimport malloc, free
from libc.stdlib cimport free, malloc

# intialized Numpy. must do.
np.import_array()
Expand All @@ -40,7 +41,7 @@ cdef extern from "maskApi.h":
uint* cnts,
void rlesInit( RLE **R, siz n )
void rleEncode( RLE *R, const byte *M, siz h, siz w, siz n )
void rleDecode( const RLE *R, byte *mask, siz n )
byte rleDecode( const RLE *R, byte *mask, siz n )
void rleMerge( const RLE *R, RLE *M, siz n, int intersect )
void rleArea( const RLE *R, siz n, uint *a )
void rleIou( RLE *dt, RLE *gt, siz m, siz n, byte *iscrowd, double *o )
Expand Down Expand Up @@ -146,7 +147,8 @@ def decode(rleObjs):
cdef RLEs Rs = _frString(rleObjs)
h, w, n = Rs._R[0].h, Rs._R[0].w, Rs._n
masks = Masks(h, w, n)
rleDecode(<RLE*>Rs._R, masks._mask, n);
if rleDecode(<RLE*>Rs._R, masks._mask, n) != 1:
raise ValueError("Invalid RLE mask representation")
return np.array(masks)

def merge(rleObjs, intersect=0):
Expand Down Expand Up @@ -210,11 +212,13 @@ def iou( dt, gt, pyiscrowd ):
# convert iscrowd to numpy array
cdef np.ndarray[np.uint8_t, ndim=1] iscrowd = np.array(pyiscrowd, dtype=np.uint8)
# simple type checking
cdef siz m, n
cdef siz m, n, crowd_length
dt = _preproc(dt)
gt = _preproc(gt)
m = _len(dt)
n = _len(gt)
crowd_length = len(pyiscrowd)
assert crowd_length == n, "iou(iscrowd=) must have the same length as gt"
if m == 0 or n == 0:
return []
if not type(dt) == type(gt):
Expand Down Expand Up @@ -305,4 +309,4 @@ def frPyObjects(pyobj, h, w):
objs = frUncompressedRLE([pyobj], h, w)[0]
else:
raise Exception('input type is not supported.')
return objs
return objs
10 changes: 8 additions & 2 deletions docker/auto_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ docker build -f ./docker/Dockerfile \

docker_name="faster_coco_eval_${PYTHON3_VERSION//-/_}_${MAKE_CONFIG}"

docker run --name ${docker_name} -v $(pwd):/app/src faster_coco_eval:${PYTHON3_VERSION}_${MAKE_CONFIG}
docker rm ${docker_name}
exited=$(docker ps -a -q -f status=exited)
if [ -n "$exited" ]; then
docker rm $exited
fi

docker run --name ${docker_name} -v $(pwd):/app/src "faster_coco_eval:${PYTHON3_VERSION}_${MAKE_CONFIG}"

docker rm ${docker_name}
2 changes: 2 additions & 0 deletions docker/build_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mkdir -p /app/src/dist
WHL="${build_dir}/dist/$(ls -lt ${build_dir}/dist/ | tail -1 | awk '{print $9}')"

echo "WHL: ${WHL}"
python3 -m pip install ${WHL}
python3 tests/basic.py

if [ "${WHL: -4}" == ".whl" ]; then
auditwheel repair ${WHL} --plat "$PLAT" -w /app/src/dist
Expand Down
Loading

0 comments on commit d8c6a93

Please sign in to comment.