coco注释数据集的滑动窗口

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

我正在尝试使用大图像(20.000px)数据集训练模型,目的是检测异常。

因为缺陷可能非常小,所以我使用滑动窗口方法将图像切割成 448x488px 的块(使用 SAHI python 包)。

我遇到以下问题: 每个断层都用多边形注释。但正如您所看到的,多边形比断层本身要大一些。所以现在滑动窗口(红色)与多边形(蓝色)重叠。这意味着该窗口被归类为有缺陷的,而实际上它是正确的。

现在这会影响准确性、召回率和精确度分数。我想过在每个窗口上使用最小面积注释阈值,但这是不可能的,因为有些错误非常小。

还尝试使注释更加精确,这导致召回率提高了 20%。

这种情况有好的做法吗?

谢谢!

deep-learning pytorch computer-vision conv-neural-network
1个回答
0
投票

我想到在每个窗口上使用最小区域注释阈值, 但这是不可能的,因为有些故障非常小。

这个,但是调整你的最小面积计算,使其仅与断层区域的面积相关(在我的代码框1 - 蓝色)。

union_area = (box1[2] - box1[0]) * (box1[3] - box1[1])

带有附加脚手架的完整代码,因此您可以轻松地尝试自己定制的 IoU 方程和规范。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches

def calculate_iou(box1, box2, use_custom_union=True):
    # Box format: (x1, y1, x2, y2)
    x1_i = max(box1[0], box2[0])
    y1_i = max(box1[1], box2[1])
    x2_i = min(box1[2], box2[2])
    y2_i = min(box1[3], box2[3])

    intersection_area = max(0, x2_i - x1_i) * max(0, y2_i - y1_i)

    if use_custom_union:
        # Customize the union to be related to only one bounding box (box1 in this case)
        union_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
    else:
        # Regular union considering both bounding boxes
        union_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) + (box2[2] - box2[0]) * (box2[3] - box2[1]) - intersection_area

    iou = intersection_area / union_area
    return iou


def visualize_boxes(box1, box2):
    plt.figure()

    # Plot box1
    plt.plot([box1[0], box1[2], box1[2], box1[0], box1[0]], [box1[1], box1[1], box1[3], box1[3], box1[1]], label='Box1', color='blue')

    # Plot box2
    plt.plot([box2[0], box2[2], box2[2], box2[0], box2[0]], [box2[1], box2[1], box2[3], box2[3], box2[1]], label='Box2', color='red')

    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.legend()
    plt.show()

# Example bounding boxes
box1 = [4, 4, 6, 5]
box2 = [5, 4, 10, 8]

# Calculate IoU with custom union
iou_custom_union = calculate_iou(box1, box2, use_custom_union=True)
print(f"IoU with custom union: {iou_custom_union}")

# Calculate IoU with regular union for comparison
iou_regular_union = calculate_iou(box1, box2, use_custom_union=False)
print(f"IoU with regular union: {iou_regular_union}")

# Visualize bounding boxes
visualize_boxes(box1, box2)

上述代码中定义的示例可视化。

现在效果如何? 通过定制的方程,您将得到 0.5 的 IoU(意味着 50% 的故障在框内 - 这对您来说是简单直观的阈值。

而不是常规 IoU 的 0.0476%,正如您所经历的那样,这使得很难设置阈值。

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