Created
October 17, 2019 07:03
-
-
Save zhangxiaoli73/7ee1c09605868c15338e407a9946c782 to your computer and use it in GitHub Desktop.
for myj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compute rle iou | |
def rleIOU(detection: Array[Tensor[Float]], groud: Array[Tensor[Float]], | |
height: Array[Int], width: Array[Int], | |
dtNumber: Int, gtNumber: Int, | |
iscrowd: Array[Boolean]): Array[Float] = { | |
val gtBbox = Tensor[Float](gtNumber, 4) | |
val dtBbox = Tensor[Float](dtNumber, 4) | |
for (i <- 1 to gtNumber) { | |
val box = gtBbox.select(1, i) | |
rleToOneBbox(groud(i - 1), box, height(i - 1), width(i -1)) | |
} | |
for (i <- 1 to dtNumber) { | |
val box = dtBbox.select(1, i) | |
rleToOneBbox(detection(i - 1), box, height(i - 1), width(i - 1)) | |
} | |
val iou = bboxIOU(gtBbox, dtBbox, gtNumber, dtNumber) | |
for (g <- 0 to (gtNumber - 1)) { | |
for (d <- 0 to (dtNumber - 1)) { | |
val n = g * dtNumber + d | |
if (iou(n) > 0) { | |
val crowd = iscrowd(g) | |
val dCnts = detection(d) | |
val gCnts = groud(g) | |
var a = 1 | |
var b = 1 | |
var ca = dCnts.valueAt(a) | |
val ka = dCnts.nElement() | |
var va : Boolean = false | |
var vb : Boolean = false | |
var cb = gCnts.valueAt(b) | |
val kb = gCnts.nElement() | |
var i = 0.0f | |
var u = 0.0f | |
var ct = 1.0f | |
while( ct > 0 ) { | |
val c = math.min(ca, cb) | |
if(va || vb) { | |
u = u + c | |
if (va && vb) i += c | |
} | |
ct = 0 | |
ca = ca - c | |
if(ca <= 0 && a < ka) { | |
a += 1 | |
ca = dCnts.valueAt(a) | |
va = !va | |
} | |
ct += ca | |
cb = cb - c | |
if( cb <= 0 && b < kb) { | |
b += 1 | |
cb = gCnts.valueAt(b) | |
vb = !vb | |
} | |
ct += cb | |
} | |
iou(g * gtNumber + d) = i / u | |
} | |
} | |
} | |
iou | |
} | |
// compute bbox iou, which not same with bigdl | |
def bboxIOU(gt: Tensor[Float], dt: Tensor[Float], | |
gtNumber: Int, dtNumber: Int): Array[Float] = { | |
val res = new ArrayBuffer[Float] | |
for( g <- 1 to gtNumber) { | |
val gt1 = gt.select(1, g) | |
val ga = gt1.valueAt(3) * gt1.valueAt(4) | |
for( d <- 1 to dtNumber) { | |
val dt1 = dt.select(1, d) | |
val da = dt1.valueAt(3) * dt1.valueAt(4) | |
val w = math.min(dt1.valueAt(3) + dt1.valueAt(1), gt1.valueAt(3) + gt1.valueAt(1)) - math.max(dt1.valueAt(1), gt1.valueAt(1)) | |
val h = math.min(dt1.valueAt(4) + dt1.valueAt(2), gt1.valueAt(4) + gt1.valueAt(2)) - math.max(dt1.valueAt(2), gt1.valueAt(2)) | |
var ii = w * h | |
val o = ii / (da + ga - ii) | |
res.append(o) | |
} | |
} | |
res.toArray | |
} | |
// convert one rle to one bbox | |
def rleToOneBbox(rle: Tensor[Float], bbox: Tensor[Float], | |
height: Int, width: Int): Unit = { | |
val m = rle.nElement() / 2 * 2 | |
val h = height.toFloat | |
var xp = 0.0f | |
var cc = 0 | |
var xs = width.toFloat | |
var ys = height.toFloat | |
var ye = 0.0f | |
var xe = 0.0f | |
if(m == 0) { | |
bbox.fill(0.0f) | |
} else { | |
for (j <- 0 to (m - 1)) { | |
cc += rle.valueAt(j + 1).toInt | |
val t = cc - j % 2 | |
val y = t % h | |
val x = (t - y) / h | |
if(j%2 == 0) { | |
xp = x | |
} else if (xp < x) { | |
ys = 0 | |
ye = h -1 | |
} | |
xs = math.min(xs, x) | |
xe = math.max(xe, x) | |
ys = math.min(ys, y) | |
ye = math.max(ye, y) | |
} | |
bbox.setValue(1, xs) | |
bbox.setValue(3, xe- xs) | |
bbox.setValue(2, ys) | |
bbox.setValue(4, ye - ys + 1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compute rle iou | |
def rleIOU(detection: Array[Tensor[Float]], groud: Array[Tensor[Float]], | |
height: Array[Int], width: Array[Int], | |
dtNumber: Int, gtNumber: Int, | |
iscrowd: Array[Boolean]): Array[Float] = { | |
val gtBbox = Tensor[Float](gtNumber, 4) | |
val dtBbox = Tensor[Float](dtNumber, 4) | |
for (i <- 1 to gtNumber) { | |
val box = gtBbox.select(1, i) | |
rleToOneBbox(groud(i - 1), box, height(i - 1), width(i -1)) | |
} | |
for (i <- 1 to dtNumber) { | |
val box = dtBbox.select(1, i) | |
rleToOneBbox(detection(i - 1), box, height(i - 1), width(i - 1)) | |
} | |
val iou = bboxIOU(gtBbox, dtBbox, gtNumber, dtNumber) | |
for (g <- 0 to (gtNumber - 1)) { | |
for (d <- 0 to (dtNumber - 1)) { | |
val n = g * dtNumber + d | |
if (iou(n) > 0) { | |
val crowd = iscrowd(g) | |
val dCnts = detection(d) | |
val gCnts = groud(g) | |
var a = 1 | |
var b = 1 | |
var ca = dCnts.valueAt(a) | |
val ka = dCnts.nElement() | |
var va : Boolean = false | |
var vb : Boolean = false | |
var cb = gCnts.valueAt(b) | |
val kb = gCnts.nElement() | |
var i = 0.0f | |
var u = 0.0f | |
var ct = 1.0f | |
while( ct > 0 ) { | |
val c = math.min(ca, cb) | |
if(va || vb) { | |
u = u + c | |
if (va && vb) i += c | |
} | |
ct = 0 | |
ca = ca - c | |
if(ca <= 0 && a < ka) { | |
a += 1 | |
ca = dCnts.valueAt(a) | |
va = !va | |
} | |
ct += ca | |
cb = cb - c | |
if( cb <= 0 && b < kb) { | |
b += 1 | |
cb = gCnts.valueAt(b) | |
vb = !vb | |
} | |
ct += cb | |
} | |
iou(g * gtNumber + d) = i / u | |
} | |
} | |
} | |
iou | |
} | |
// compute bbox iou, which not same with bigdl | |
def bboxIOU(gt: Tensor[Float], dt: Tensor[Float], | |
gtNumber: Int, dtNumber: Int): Array[Float] = { | |
val res = new ArrayBuffer[Float] | |
for( g <- 1 to gtNumber) { | |
val gt1 = gt.select(1, g) | |
val ga = gt1.valueAt(3) * gt1.valueAt(4) | |
for( d <- 1 to dtNumber) { | |
val dt1 = dt.select(1, d) | |
val da = dt1.valueAt(3) * dt1.valueAt(4) | |
val w = math.min(dt1.valueAt(3) + dt1.valueAt(1), gt1.valueAt(3) + gt1.valueAt(1)) - math.max(dt1.valueAt(1), gt1.valueAt(1)) | |
val h = math.min(dt1.valueAt(4) + dt1.valueAt(2), gt1.valueAt(4) + gt1.valueAt(2)) - math.max(dt1.valueAt(2), gt1.valueAt(2)) | |
var ii = w * h | |
val o = ii / (da + ga - ii) | |
res.append(o) | |
} | |
} | |
res.toArray | |
} | |
// convert one rle to one bbox | |
def rleToOneBbox(rle: Tensor[Float], bbox: Tensor[Float], | |
height: Int, width: Int): Unit = { | |
val m = rle.nElement() / 2 * 2 | |
val h = height.toFloat | |
var xp = 0.0f | |
var cc = 0 | |
var xs = width.toFloat | |
var ys = height.toFloat | |
var ye = 0.0f | |
var xe = 0.0f | |
if(m == 0) { | |
bbox.fill(0.0f) | |
} else { | |
for (j <- 0 to (m - 1)) { | |
cc += rle.valueAt(j + 1).toInt | |
val t = cc - j % 2 | |
val y = t % h | |
val x = (t - y) / h | |
if(j%2 == 0) { | |
xp = x | |
} else if (xp < x) { | |
ys = 0 | |
ye = h -1 | |
} | |
xs = math.min(xs, x) | |
xe = math.max(xe, x) | |
ys = math.min(ys, y) | |
ye = math.max(ye, y) | |
} | |
bbox.setValue(1, xs) | |
bbox.setValue(3, xe- xs) | |
bbox.setValue(2, ys) | |
bbox.setValue(4, ye - ys + 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment