【问题标题】:How to define loss for sparse line detection?如何定义稀疏线检测的损失?
【发布时间】:2021-03-23 21:01:52
【问题描述】:

所以我看一下图像->蒙版问题。在 256x256 图像上具有大约三个 2 像素宽 x 100 像素以上长度的线条(曲线)。我想最大化

  1. 行长
  2. 线薄结构
  3. 线精度

同时。要设计一个专注于细长线的损失吗?

所以目前我尝试:

def lines_loss(y_true, y_pred):
        mask_lines = K.greater_equal(y_true, 0.95)
        mask_empty = K.less(y_true, 0.95)
        lines_masked = tf.boolean_mask(y_pred - y_true, mask_lines)
        empty_masked = tf.boolean_mask(y_pred - y_true, mask_empty)
        base = K.mean(K.square(lines_masked)) +  K.mean(K.square(empty_masked))
        loss_value = base 

        return loss_value

它保持相对平衡,但网络似乎过度训练太快(验证损失 >> 训练损失)并产生像雨一样的图像 - 所有的线都很短而且它们太多(想象一下雨滴)。示例:

必须只有 3 长行:

【问题讨论】:

  • 能否提供更多信息,例如,您的模型代码、一些输入、模型的输出和预期结果?
  • 如果我是你,我会研究 Dice Loss,它通常非常适合边缘检测/分割。但是,如果没有更多用例示例,很难告诉您更多信息。
  • 如果对你有用,你能接受答案吗?

标签: python machine-learning keras neural-network loss


【解决方案1】:

鉴于我正确理解了问题定义,我的 2 美分。

我建议您 i) 检测并 ii) 首先使用 openCV 分割线以创建掩码,即(伪)ground-truth。分割线意味着您有成对的(图像,line_segmentations_inside_image)。最后,您可以使用那些(图像,line_segmentations_inside_image)选择和训练任何具有标准文献损失(例如 MSE 等)的体面的 CNN(在众多现成的 CNN 中)。

(当然也可以直接应用已经训练好的可以分割物体的CNN,见迁移学习)

请找一些可以为你做线段分割的openCV代码。

首先,得到灰度图,处理GaussianBlur。

img = cv2.imread('src.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

二、处理边缘检测使用Canny。

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

然后,使用 HoughLinesP 获取线条。您可以根据您提到的值调整参数以获得更好的性能,并根据您的问题自定义。

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
    cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

# 可选的可视化:在原始图像上画线

lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

【讨论】:

    猜你喜欢
    • 2020-10-12
    • 2020-04-28
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2020-12-22
    • 2017-12-26
    • 2020-08-31
    相关资源
    最近更新 更多