Repository: deepjavalibrary/djl
Pull request #2691 (2023-07-03 23:12:05)

[ci] Fixes nightly publish ci bug
## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here



Patch:
diff --git a/.github/workflows/nightly_publish.yml b/.github/workflows/nightly_publish.yml
index 7253210c1..0a51c3d53 100644
--- a/.github/workflows/nightly_publish.yml
+++ b/.github/workflows/nightly_publish.yml
@@ -163,7 +163,7 @@ jobs:
   publish:
     if: github.repository == 'deepjavalibrary/djl'
     runs-on: ubuntu-latest
-    needs: [ build, test-pytorch, test-tensorflow, test-aarch64, test-cuda-102, test-cuda-118 ]
+    needs: [ build, test-pytorch, test-tensorflow, test-aarch64, test-cuda-118 ]
     steps:
       - uses: actions/checkout@v3
       - name: Set up JDK 11
@@ -244,7 +244,7 @@ jobs:
   stop-runners:
     if: ${{ github.repository == 'deepjavalibrary/djl' && always() }}
     runs-on: [ self-hosted, scheduler ]
-    needs: [ create-runners, test-aarch64, test-cuda-102, test-cuda-118 ]
+    needs: [ create-runners, test-aarch64, test-cuda-118 ]
     steps:
       - name: Stop all instances
         run: |
diff --git a/api/src/main/java/ai/djl/modality/cv/output/BoundingBox.java b/api/src/main/java/ai/djl/modality/cv/output/BoundingBox.java
index e07627e75..3e582f463 100644
--- a/api/src/main/java/ai/djl/modality/cv/output/BoundingBox.java
+++ b/api/src/main/java/ai/djl/modality/cv/output/BoundingBox.java
@@ -41,7 +41,7 @@ public interface BoundingBox extends Serializable {
     Point getPoint();
 
     /**
-     * Gets the Intersection over Union (IoU) value between bounding boxes.
+     * Returns the Intersection over Union (IoU) value between bounding boxes.
      *
      * <p>Also known as <a href="https://en.wikipedia.org/wiki/Jaccard_index">Jaccard index</a>
      *
diff --git a/api/src/main/java/ai/djl/modality/cv/output/Rectangle.java b/api/src/main/java/ai/djl/modality/cv/output/Rectangle.java
index caad442f2..92afc6032 100644
--- a/api/src/main/java/ai/djl/modality/cv/output/Rectangle.java
+++ b/api/src/main/java/ai/djl/modality/cv/output/Rectangle.java
@@ -86,15 +86,26 @@ public class Rectangle implements BoundingBox {
     /** {@inheritDoc} */
     @Override
     public double getIoU(BoundingBox box) {
-        Rectangle rec = (Rectangle) box;
-        // caculate intesection lrtb
-        double left = Math.max(getX(), rec.getX());
-        double top = Math.max(getY(), rec.getY());
-        double right = Math.min(getX() + getWidth(), rec.getX() + rec.getWidth());
-        double bottom = Math.min(getY() + getHeight(), rec.getY() + rec.getHeight());
-        double intersection = (right - left) * (bottom - top);
-        return intersection
-                / (getWidth() * getHeight() + rec.getWidth() * rec.getHeight() - intersection);
+        Rectangle rect = box.getBounds();
+
+        // computing area of each rectangles
+        double s1 = (width + 1) * (height + 1);
+        double s2 = (rect.getWidth() + 1) * (rect.getHeight() + 1);
+        double sumArea = s1 + s2;
+
+        // find each edge of intersect rectangle
+        double left = Math.max(getX(), rect.getX());
+        double top = Math.max(getY(), rect.getY());
+        double right = Math.min(getX() + getWidth(), rect.getX() + rect.getWidth());
+        double bottom = Math.min(getY() + getHeight(), rect.getY() + rect.getHeight());
+
+        // judge if there is a intersect
+        if (left > right || top > bottom) {
+            return 0.0;
+        }
+
+        double intersect = (right - left + 1) * (bottom - top + 1);
+        return intersect / (sumArea - intersect);
     }
 
     /**


Test patch:
diff --git a/api/src/test/java/ai/djl/modality/cv/output/RectangleTest.java b/api/src/test/java/ai/djl/modality/cv/output/RectangleTest.java
new file mode 100644
index 000000000..b4fc3bafa
--- /dev/null
+++ b/api/src/test/java/ai/djl/modality/cv/output/RectangleTest.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
+ * with the License. A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+ * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
+ * and limitations under the License.
+ */
+package ai.djl.modality.cv.output;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class RectangleTest {
+
+    @Test
+    public void testIoU() {
+        BoundingBox box = new Rectangle(1, 3, 4, 5);
+        Rectangle rect = new Rectangle(1, 2, 3, 4);
+        double iou = box.getIoU(rect);
+        Assert.assertEquals(iou, 0.47058823529411764, 0.00001);
+
+        rect = new Rectangle(6, 2, 3, 4);
+        iou = box.getIoU(rect);
+        Assert.assertEquals(iou, 0);
+    }
+}
diff --git a/api/src/test/java/ai/djl/modality/cv/output/package-info.java b/api/src/test/java/ai/djl/modality/cv/output/package-info.java
new file mode 100644
index 000000000..e3611e02f
--- /dev/null
+++ b/api/src/test/java/ai/djl/modality/cv/output/package-info.java
@@ -0,0 +1,14 @@
+/*
+ * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
+ * with the License. A copy of the License is located at
+ *
+ * http://aws.amazon.com/apache2.0/
+ *
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+ * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
+ * and limitations under the License.
+ */
+/** Contains tests for {@link ai.djl.modality.cv.output}. */
+package ai.djl.modality.cv.output;