Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bbox consistent order #451

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void main(String[] args) throws NotFoundException, IOException {
Collections.singletonList(new Pair<>(null, "ai.konduit.serving.endpoint.AssetServingEndpoint"))
);
mappings.put("ai.konduit.serving.pipeline.api.data.BoundingBox",
Arrays.asList(new Pair<>(null, "ai.konduit.serving.pipeline.impl.data.box.BBoxCHW"),
Arrays.asList(new Pair<>(null, "ai.konduit.serving.pipeline.impl.data.box.BBoxCWH"),
new Pair<>(null, "ai.konduit.serving.pipeline.impl.data.box.BBoxXY"))
);
mappings.put("ai.konduit.serving.pipeline.api.pipeline.Pipeline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package ai.konduit.serving.data.image.step.point.convert;

import ai.konduit.serving.annotation.runner.CanRun;
import ai.konduit.serving.data.image.convert.ImageToNDArray;
import ai.konduit.serving.data.image.convert.ImageToNDArrayConfig;
import ai.konduit.serving.data.image.util.ImageUtils;
import ai.konduit.serving.pipeline.api.context.Context;
import ai.konduit.serving.pipeline.api.data.*;
Expand Down Expand Up @@ -104,7 +102,7 @@ public Data exec(Context ctx, Data data) {
BoundingBox b = ImageUtils.accountForCrop(data.getBoundingBox(s), w, h, step.imageToNDArrayConfig());
BoundingBox absolute = b;
if(b.cx() < 1.0 && b.cy() < 1.0){
absolute = BoundingBox.create(b.cx()*w, b.cy()*h, b.height()*h, b.width()*w, b.label(), b.probability());
absolute = BoundingBox.create(b.cx()*w, b.cy()*h, b.width()*w, b.height()*h, b.label(), b.probability());
}
out.put(s, absolute);
} else if(data.listType(s) == ValueType.POINT){
Expand All @@ -123,7 +121,7 @@ public Data exec(Context ctx, Data data) {
BoundingBox b = ImageUtils.accountForCrop(bb, w, h, step.imageToNDArrayConfig());
BoundingBox absolute = b;
if(b.cx() < 1.0 && b.cy() < 1.0){
absolute = BoundingBox.create(b.cx()*w, b.cy()*h, b.height()*h, b.width()*w, b.label(), b.probability());
absolute = BoundingBox.create(b.cx()*w, b.cy()*h, b.width()*w, b.height()*h, b.label(), b.probability());
}
lOut.add(absolute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void testRelativeToAbsolute(){
Data d = Data.empty();
d.put("pt", Point.create(0.3, 0.4));
d.putListPoint("ptList", Arrays.asList(Point.create(0.3, 0.4)));
d.put("box", BoundingBox.create(0.5, 0.6, 0.2, 0.3, "myLabel", 0.99));
d.putListBoundingBox("boxList", Arrays.asList(BoundingBox.create(0.5, 0.6, 0.2, 0.3, "myLabel", 0.99)));
d.put("box", BoundingBox.create(0.5, 0.6, 0.3, 0.2, "myLabel", 0.99));
d.putListBoundingBox("boxList", Arrays.asList(BoundingBox.create(0.5, 0.6, 0.3, 0.2, "myLabel", 0.99)));

Pipeline p = SequencePipeline.builder()
.add(new RelativeToAbsoluteStep().imageH(200).imageW(300))
Expand All @@ -54,7 +54,7 @@ public void testRelativeToAbsolute(){


Point exp = Point.create(0.3*300, 0.4*200);
BoundingBox bb = BoundingBox.create(0.5*300, 0.6*200, 0.2*200, 0.3*300, "myLabel", 0.99);
BoundingBox bb = BoundingBox.create(0.5*300, 0.6*200, 0.3*300, 0.2*200, "myLabel", 0.99);

assertEquals(exp, out.getPoint("pt"));
assertEquals(bb, out.getBoundingBox("box"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
******************************************************************************/
package ai.konduit.serving.pipeline.api.data;

import ai.konduit.serving.pipeline.impl.data.box.BBoxCHW;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCWH;
import ai.konduit.serving.pipeline.impl.data.box.BBoxXY;
import ai.konduit.serving.pipeline.impl.pipeline.serde.BoundingBoxDeserializer;
import ai.konduit.serving.pipeline.impl.pipeline.serde.BoundingBoxSerializer;
Expand Down Expand Up @@ -53,8 +53,8 @@ public interface BoundingBox {
/**
* As per {@link #createXY(double, double, double, double, String, Double)} without a label or probability
*/
static BoundingBox create(double cx, double cy, double h, double w) {
return create(cx, cy, h, w, "", 0.0);
static BoundingBox create(double cx, double cy, double w, double h) {
return create(cx, cy, w, h, null, null);
}

/**
Expand All @@ -69,8 +69,8 @@ static BoundingBox create(double cx, double cy, double h, double w) {
* @param probability PRobability for the bounding box, in range [0.0, 1.0]. May be null
* @return BoundingBox instance
*/
static BoundingBox create(double cx, double cy, double h, double w, String label, Double probability) {
return new BBoxCHW(cx, cy, h, w, label, probability);
static BoundingBox create(double cx, double cy, double w, double h, String label, Double probability) {
return new BBoxCWH(cx, cy, w, h, label, probability);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Data
@Accessors(fluent = true)
@Schema(description = "A bounding box based on center X, center Y, height and width format.")
public class BBoxCHW implements BoundingBox {
public class BBoxCWH implements BoundingBox {

@Schema(description = "Center X coordinate.")
private final double cx;
Expand All @@ -45,11 +45,11 @@ public class BBoxCHW implements BoundingBox {
@Schema(description = "Class probability.")
private final Double probability;

public BBoxCHW(double cx, double cy, double h, double w){
this(cx, cy, h, w, null, null);
public BBoxCWH(double cx, double cy, double w, double h){
this(cx, cy, w, h, null, null);
}

public BBoxCHW(double cx, double cy, double h, double w, String label, Double probability){
public BBoxCWH(double cx, double cy, double w, double h, String label, Double probability){
this.cx = cx;
this.cy = cy;
this.h = h;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import ai.konduit.serving.pipeline.api.data.*;
import ai.konduit.serving.pipeline.impl.data.JData;
import ai.konduit.serving.pipeline.impl.data.Value;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCHW;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCWH;
import ai.konduit.serving.pipeline.impl.data.box.BBoxXY;
import ai.konduit.serving.pipeline.impl.data.image.Png;
import ai.konduit.serving.pipeline.impl.data.ndarray.SerializedNDArray;
Expand Down Expand Up @@ -155,7 +155,7 @@ private static BoundingBox deserializeBoundingBox(DataProtoMessage.BoundingBox p
String lbl = pbBox.getLabel().isEmpty() ? null : pbBox.getLabel();
Double prob = Double.isNaN(pbBox.getProbability()) ? null : pbBox.getProbability();
if (pbBox.getType() == DataProtoMessage.BoundingBox.BoxType.CHW) {
boundingBox = new BBoxCHW(pbBox.getCx(), pbBox.getCy(), pbBox.getH(), pbBox.getW(), lbl, prob);
boundingBox = new BBoxCWH(pbBox.getCx(), pbBox.getCy(), pbBox.getW(), pbBox.getH(), lbl, prob);
} else if (pbBox.getType() == DataProtoMessage.BoundingBox.BoxType.XY) {
boundingBox = new BBoxXY(pbBox.getX0(), pbBox.getX1(), pbBox.getY0(), pbBox.getY1(), lbl, prob);
} else {
Expand Down Expand Up @@ -299,7 +299,7 @@ else if (value.type() == ValueType.DATA) {
else if (value.type() == ValueType.BOUNDING_BOX) {
BoundingBox boundingBox = (BoundingBox) nextItem.getValue().get();
DataProtoMessage.BoundingBox pbBox = null;
if (boundingBox instanceof BBoxCHW) {
if (boundingBox instanceof BBoxCWH) {
pbBox = DataProtoMessage.BoundingBox.newBuilder().
setCx(boundingBox.cx()).setCy(boundingBox.cy()).
setH(boundingBox.height()).
Expand Down Expand Up @@ -424,7 +424,7 @@ else if (lv.elementType() == ValueType.BOUNDING_BOX) {
List<DataProtoMessage.BoundingBox> pbBoxes = new ArrayList<>();
for (val boundingBox : bboxes) {
DataProtoMessage.BoundingBox pbBox = null;
if (boundingBox instanceof BBoxCHW) {
if (boundingBox instanceof BBoxCWH) {
pbBox = DataProtoMessage.BoundingBox.newBuilder().
setCx(boundingBox.cx()).setCy(boundingBox.cy()).
setH(boundingBox.height()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static BoundingBox deserializeBB(JsonNode n2){
double cy = n2.get(Data.RESERVED_KEY_BB_CY).doubleValue();
double h = n2.get(Data.RESERVED_KEY_BB_H).doubleValue();
double w = n2.get(Data.RESERVED_KEY_BB_W).doubleValue();
return BoundingBox.create(cx, cy, h, w, label, prob);
return BoundingBox.create(cx, cy, w, h, label, prob);
} else {
double x1 = n2.get(Data.RESERVED_KEY_BB_X1).doubleValue();
double x2 = n2.get(Data.RESERVED_KEY_BB_X2).doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ai.konduit.serving.pipeline.impl.serde;

import ai.konduit.serving.pipeline.api.data.*;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCHW;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCWH;
import ai.konduit.serving.pipeline.impl.data.image.Png;
import ai.konduit.serving.pipeline.impl.data.ndarray.SerializedNDArray;
import ai.konduit.serving.pipeline.impl.format.JavaNDArrayFormats;
Expand Down Expand Up @@ -227,8 +227,8 @@ private void writeNDArray(JsonGenerator jg, NDArray n) throws IOException {
public static void writeBB(JsonGenerator jg, BoundingBox bb) throws IOException {
//We'll keep it in the original format, if possible - but encode it as a X/Y format otherwise
jg.writeStartObject();
if(bb instanceof BBoxCHW){
BBoxCHW b = (BBoxCHW)bb;
if(bb instanceof BBoxCWH){
BBoxCWH b = (BBoxCWH)bb;
jg.writeFieldName(Data.RESERVED_KEY_BB_CX);
jg.writeNumber(b.cx());
jg.writeFieldName(Data.RESERVED_KEY_BB_CY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Data exec(Context ctx, Data data) {
lbl = classLabels.get(pIdx);
}

out.add(BoundingBox.create(px/w, py/h, ph/h, pw/w, lbl, (double)prob));
out.add(BoundingBox.create(px/w, py/h, pw/w,ph/h, lbl, (double)prob));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void testList(){
break;
case BOUNDING_BOX:
List<BoundingBox> bbList = Arrays.asList(BoundingBox.createXY(0.2, 0.4, 0.7, 0.9, "myLabel", 0.8),
BoundingBox.create(0.4, 0.5, 0.3, 0.1, "otherlabel", 0.99));
BoundingBox.create(0.4, 0.5, 0.1, 0.3, "otherlabel", 0.99));
d = Data.singletonList("key", bbList, ValueType.BOUNDING_BOX);
break;
case POINT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
package ai.konduit.serving.pipeline.impl.data;

import ai.konduit.serving.pipeline.api.data.*;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCHW;
import ai.konduit.serving.pipeline.impl.data.box.BBoxCWH;
import ai.konduit.serving.pipeline.impl.data.box.BBoxXY;
import ai.konduit.serving.pipeline.impl.data.image.Png;
import ai.konduit.serving.pipeline.impl.data.ndarray.SerializedNDArray;
import org.apache.commons.compress.utils.Lists;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -508,7 +507,7 @@ public void testMetaDataSerde() throws IOException {

@Test
public void testBoundingBoxesCHWSerde() throws IOException {
BoundingBox boundingBox = new BBoxCHW(1.0, 2.0, 1.0, 2.0, "head", 7.0);
BoundingBox boundingBox = new BBoxCWH(1.0, 2.0, 2.0, 1.0, "head", 7.0);
Data boxData = Data.singleton(KEY, boundingBox);
assertEquals(boundingBox, boxData.get(KEY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public void testYoloStep(){
//System.out.println(out.toJson());

List<BoundingBox> expList = new ArrayList<>();
expList.add(BoundingBox.create(0.7f / 4, (1.0f + 0.8f) / 4, 1.5f / 4, 2.0f / 4, "a", 0.75));
expList.add(BoundingBox.create((3 + 0.2f) / 4, (2 + 0.3f) / 4, 0.75f / 4, 3.5f / 4, "c", 0.5));
expList.add(BoundingBox.create(0.7f / 4, (1.0f + 0.8f) / 4, 2.0f / 4, 1.5f / 4,"a", 0.75));
expList.add(BoundingBox.create((3 + 0.2f) / 4, (2 + 0.3f) / 4, 3.5f / 4, 0.75f / 4,"c", 0.5));

List<BoundingBox> actList = out.getListBoundingBox(YoloToBoundingBoxStep.DEFAULT_OUTPUT_NAME);
assertEquals(expList, actList);
Expand Down