Code Monkey home page Code Monkey logo

Comments (6)

lilanxiao avatar lilanxiao commented on May 29, 2024

hi,
it's because oriented_box_intersection_2d expects the corner points are counter-clockwise ordered. But your points seem clockwise ordered. I only test my code with counter-clockwise order so it might have a strange behavior with the other orders.

I modify your test case by reordering the points. The results are then consistent.

import torch
import numpy as np

from box_intersection_2d import oriented_box_intersection_2d

device = torch.device("cuda:0")

# load 2 group of boxes, each group contains 9 boxes.
boxes1 = torch.from_numpy(np.load('boxes1.npy')).to(device).float()
boxes2 = torch.from_numpy(np.load('boxes2.npy')).to(device).float()
# reorder so that points are counter-clockwise ordered
boxes1[:, :, [0, 1, 2, 3], :] = boxes1[:, :, [3, 2, 1, 0], :].contiguous()
boxes2[:, :, [0, 1, 2, 3], :] = boxes2[:, :, [3, 2, 1, 0], :].contiguous()

area, _ = oriented_box_intersection_2d(boxes1,boxes2)
print(area)

# calculate intersetion of boxes in the center 
box1 = boxes1[1,1].reshape(1,1,4,2)
box2 = boxes2[1,1].reshape(1,1,4,2)

area1, _ = oriented_box_intersection_2d(box1,box2)
print(area1)

# calculate intersetion of boxes in the center row
box_row1 = boxes1[1,:].reshape(3,1,4,2)
box_row2 = boxes2[1,:].reshape(3,1,4,2)

area_row, _ = oriented_box_intersection_2d(box_row1,box_row2)
print(area_row)

# calculate intersetion of boxes in the center column
box_col1 = boxes1[:,1].reshape(1,3,4,2)
box_col2 = boxes2[:,1].reshape(1,3,4,2)

area_col, _ = oriented_box_intersection_2d(box_col1,box_col2)
print(area_col)

The results are:

tensor([[1920.7974,  221.0000,    0.0000],
        [1980.3770,  221.0000,    0.0000],
        [ 570.9585,   32.7373,    0.0000]], device='cuda:0')
tensor([[221.]], device='cuda:0')
tensor([[1980.3770],
        [ 221.0000],
        [   0.0000]], device='cuda:0')
tensor([[221.0000, 221.0000,  32.7373]], device='cuda:0')

from rotated_iou.

OneOneLiu avatar OneOneLiu commented on May 29, 2024

Got it! Thanks a lot for your help.

from rotated_iou.

OneOneLiu avatar OneOneLiu commented on May 29, 2024

大佬你好,我切换为逆时针后测试了一会发现还是有问题,总结如下:

  • 大部分情况下,逆时针顺时针计算所得结果是一样的,而且是正确的
  • 两者偶尔都会出现错误值,体现在计算出来的intersection过大(过大的会导致iou大于1,很容易看出来,不知道会不会偏小)
  • 存在两者一个错一个对的情况,也存在都错的情况,整体来看逆时针相对较稳定些.

我觉得这不太合理,如果真的是这么不稳定的话就没法用了,所以我考虑是不是我哪里搞错了,麻烦您有时间帮忙看一下,这是我选的一组两个结果都不对的数据,这次载入的数据就是逆时针的,后面改成顺时针又重复计算了一次:

import torch
import numpy as np

from box_intersection_2d import oriented_box_intersection_2d

device = torch.device("cuda:0")

# load 2 group of boxes, each group contains 9 boxes.
print('载入的框角点顺序已修改为逆时针')
boxes1 = torch.from_numpy(np.load('boxes1.npy')).to(device).float()
boxes2 = torch.from_numpy(np.load('boxes2.npy')).to(device).float()

area, _ = oriented_box_intersection_2d(boxes1,boxes2)
print(area)

# calculate intersetion of boxes in the center 
box1 = boxes1[1,1].reshape(1,1,4,2).float()
box2 = boxes2[1,1].reshape(1,1,4,2).float()

area1, _ = oriented_box_intersection_2d(box1,box2)
print(area1)

# calculate intersetion of boxes in the center row
box_row1 = boxes1[1,:].reshape(3,1,4,2)
box_row2 = boxes2[1,:].reshape(3,1,4,2)

area_row, _ = oriented_box_intersection_2d(box_row1,box_row2)
print(area_row)

# calculate intersetion of boxes in the center column
box_col1 = boxes1[:,1].reshape(1,3,4,2)
box_col2 = boxes2[:,1].reshape(1,3,4,2)

area_col, _ = oriented_box_intersection_2d(box_col1,box_col2)
print(area_col)

# 把点的顺序切换回顺时针
print('把点的顺序切换回顺时针再计算')
boxes1[:, :, [0, 1, 2, 3], :] = boxes1[:, :, [3, 2, 1, 0], :].contiguous()
boxes2[:, :, [0, 1, 2, 3], :] = boxes2[:, :, [3, 2, 1, 0], :].contiguous()

area, _ = oriented_box_intersection_2d(boxes1,boxes2)
print(area)

# calculate intersetion of boxes in the center 
box1 = boxes1[1,1].reshape(1,1,4,2)
box2 = boxes2[1,1].reshape(1,1,4,2)

area1, _ = oriented_box_intersection_2d(box1,box2)
print(area1)

# calculate intersetion of boxes in the center row
box_row1 = boxes1[1,:].reshape(3,1,4,2)
box_row2 = boxes2[1,:].reshape(3,1,4,2)

area_row, _ = oriented_box_intersection_2d(box_row1,box_row2)
print(area_row)

# calculate intersetion of boxes in the center column
box_col1 = boxes1[:,1].reshape(1,3,4,2)
box_col2 = boxes2[:,1].reshape(1,3,4,2)

area_col, _ = oriented_box_intersection_2d(box_col1,box_col2)
print(area_col)

下面是程序的输出

载入的框角点顺序已修改为逆时针
tensor([[1086.5820, 1474.9424,  315.2656],
        [1020.2642, 2082.3574,  318.6777],
        [ 960.2954, 1356.2568,  327.6221]], device='cuda:0')
tensor([[2082.3574]], device='cuda:0')
tensor([[1020.2642],
        [2082.3574],
        [2905.8066]], device='cuda:0')
tensor([[1474.9424, 2082.3574, 1356.2568]], device='cuda:0')
把点的顺序切换回顺时针再计算
tensor([[1086.5820, 1474.9424,  315.2656],
        [1020.2651, 1872.7114,  318.6777],
        [ 960.2939, 1356.2573,  327.6230]], device='cuda:0')
tensor([[1872.7114]], device='cuda:0')
tensor([[1020.2651],
        [1872.7114],
        [ 169.1211]], device='cuda:0')
tensor([[1474.9424, 1872.7114, 1356.2573]], device='cuda:0')

经过我画图实测,中间位置的两个box iou应该是1382左右,顺时针和逆时针的计算结果一个是2082,一个是1872,都是错误的.下面是相关文件链接,拜托您有时间帮忙看一下,不胜感激.
boxes1
boxes2
iou figure

from rotated_iou.

lilanxiao avatar lilanxiao commented on May 29, 2024

你好,你数据里有的图形好像不是严格的矩形。比如 boxes1[1,1] 的四个顶点分别是

A = [144, 170]
B = [92, 130]
C = [111, 106]
D = [162, 145]

理论上应该存在严格的 x_A - x_B = x_D - x_C 和 y_A - y_B = y_D - y_C。 但是在这个数据里稍微偏离了一些。我的代码在判断一些几何关系的时候利用了矩形的性质(例如直角,平行),对于不严格的数据,结果就不对了(虽然结果差这么多我也挺意外的)。我的建议是不要直接输入四个坐标点,而是输入中心坐标,长宽和旋转角,通过box2corners_th函数计算四个角的坐标。这样可以保证四个角点是严格的矩形。

另外我自己用这个代码train过目标检测的网络,还没有遇到过IoU大于1的情况,所以我感觉输入数据有异常的可能性比较大。

from rotated_iou.

OneOneLiu avatar OneOneLiu commented on May 29, 2024

好的,感觉应该是就是这个原因了,我明天输入中心坐标,长宽和旋转角,使用您的函数反求再测试一下,感谢您的回复与帮助。

from rotated_iou.

OneOneLiu avatar OneOneLiu commented on May 29, 2024

使用box2corners_th函数反求坐标点之后,测试了这么几天都没有再出现过问题了,再次感谢您的帮助

from rotated_iou.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.