近似相等对象的交集

问题描述 投票:0回答:1

我正在检测图像中的物体,并尝试使用不同的检测算法。我想比较数据集上算法的结果。我已经将每种算法的结果存储为[filepath,detection_box_coordinates,otherstuff]的列表。我想考虑两个检测是相同的,如果文件路径相同并且检测框中的重叠部分超过某个阈值。我想为任意两个算法结果A和B生成列表only_in_A,only_in_B和in_both。我希望找到一种“并且最好只有一种明显的方法”,但是到目前为止,我的搜索已经产生了几种不必要的明显方法。

由于计算in_both,我考虑过要进行以下操作:

  1. 遍历每个列表的每个元素并进行比较
  2. 按文件路径对列表进行排序和分区,然后针对每个文件路径,遍历每个列表中的元素
  3. [带有自定义匹配功能的itertools.product(A,B)和match(x)中的x,用于x]
  4. 进行类检测并将__eq__定义为我的匹配函数
  5. 实现我自己的Intersector类,如此处所示:Python intersection with custom equality
  6. 使用lambda函数

现在,我可以看到以下与这些想法有关的缺点:

  1. 非常慢
  2. 比设置操作还慢,并且可能会重新编写一堆代码
  3. 可能很慢并且占用大量内存
  4. __ eq__将是反身和对称的,但不能传递。另外,两个项目有可能使__eq__返回true但具有不同的哈希值。
  5. 与上述相同。不确定性能。
  6. 还不清楚如何实现。

我的哪个主意是好还是坏?有什么明显的方法我想念的吗?

python set operators intersection equality
1个回答
0
投票

我认为通过进行一些修改后,检测类将是一个好主意:

class Detection():
    Instances = dict()
    In_A = dict()
    In_B = dict()
    def __new__(cls,*args,**kwargs):
        if filepath in Detection.Instances.keys():
            _instance = Detection.Instances[filepath]
        else:
            _instance = super(Detection, cls).__new__(cls, *args, **kwargs)
        return _instance

    def __init__(self,filepath,coords,other,From_Algo):     
        if From_Algo = "A"  
            self.filepath = filepath 
            self.coords_A = coords
            self.in_A = True    
            self.other_A = other
            Detection.In_A[filepath] = self # Make sure that filepath is hashable
        if From_Algo = "B"  
            self.filepath = filepath 
            self.coords_B = coords
            self.in_B = True    
            self.other_B = other
            Detection.In_B[filepath] = self # Make sure that filepath is hashable
        Detection.Instances[filepath]=self # Make sure that filepath is hashable

    @classmethod
    def Check_coord_relation(cls,A_coords,B_coords):
        ...
        # compare A_coords and B_coords
        ...
        return Result

    @classmethod
    def Get_In_Both(cls):
        cls._Get_In_Both = [Det in Det for cls.Instances.values() if (hasattr(Det,"In_A") and hasattr(Det,"In_B") and cls.Check_coord_relation(Det.coords_A,coords_B))]

    @classmethod
    def Get_Only_In_A(cls):
        cls._Only_In_A = [Det in Det for cls.In_A.values() if Det not in cls._Get_In_Both]

    @classmethod
    def Get_Only_In_B(cls):
        cls._Only_In_B = [Det in Det for cls.In_B.values() if Det not in cls._Get_In_Both]

    @classmethod
    def Calc_Interseciton(cls):
        cls.Get_In_Both()
        cls.Get_Only_In_A()
        cls.Get_Only_In_B()

您可以使用__new__来检查实例是否已经存在,因此可以更新与算法不同的属性,然后该类可以处理所有创建的实例。

首先检查两个算法中的检测,然后将它们从A和B中删除。

我无法尝试此操作,希望这对您有所帮助或为您提供新的想法。

类变量似乎不需要是字典,但是字典确实非常快。

© www.soinside.com 2019 - 2024. All rights reserved.