【问题标题】:Random erasing at multiple random regions of image在图像的多个随机区域随机擦除
【发布时间】:2021-05-15 10:33:08
【问题描述】:

我想实现一个自定义的随机擦除功能。

此函数将使用输入图像和百分比进行遮罩,但随后会遮盖 1 到 4 个随机矩形,其总面积加起来等于遮罩百分比。

例如,假设我的图像是 100100 像素,我的蒙版百分比是 15%,所以我随机选择创建 3 个具有随机形状的矩形,这样它们的组合面积总和为 100100* 0.15 像素。

到目前为止,我设法编写了决定矩形的宽度和高度以及数量的代码,但我在确保它们不会掩盖同一点的部分上遇到了困难。

img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1]
        area = img_h * img_w

        for _ in range(10):
            block_num = torch.randint(1,4,(1,)).item()
            block_sizes = torch.rand((block_num))
            block_sizes = torch.round((block_sizes / block_sizes.sum()) * (area * mask_percent))
            h = torch.round((torch.rand(block_num)+0.5) * block_sizes.sqrt())
            w = torch.round(block_sizes / h)

            
            xs = []
            ys = []
            if not (any(h < img_h) and any(w < img_w)):
                continue
            term = True
            while term:
                xs = [torch.randint(0, img_h - h_ + 1, size=(1, )).item() for h_ in h]
                ys = [torch.randint(0, img_w - w_ + 1, size=(1, )).item() for w_ in w]
                for iter,x in enumerate(xs):
                    if (x+h[iter]-xs)<0

#here i get all confused. should have a loop that goes over each point and checks that the location + axial size 
#doesn't go over another point. it's confusing because should i also take care  vice versa? maybe someone knows of a ready made solution? 

            return i, j, h, w, v

        # Return original image
        return 0, 0, img_h, img_w, img

一旦随机位置生成器生成与条款相对应的位置,while 循环就会被释放。

编辑 >_____________

我最近的尝试似乎有效,但总是退出未解决的循环!是不是很可能是一组参数?

img = torch.rand(1,160,1024)
img_c, img_h, img_w = img.shape[-3], img.shape[-2], img.shape[-1]
        area = img_h * img_w

        for _ in range(100):
            block_num = torch.randint(1,3,(1,)).item()
            block_sizes = torch.rand((block_num))
            block_sizes = torch.round((block_sizes / block_sizes.sum()) * (area * 0.15))
            h = torch.round((torch.rand(block_num)+0.5) * block_sizes.sqrt()).to(dtype=torch.long)
            w = torch.round(block_sizes / h).to(dtype=torch.long)

            if (h > img_h).any() or (w > img_w).any():
                continue
            overlap1 = torch.zeros(img.shape)
            xs = [torch.randint(0, img_h - h_.item() + 1, size=(1, )).item() for h_ in h]
            ys = [torch.randint(0, img_w - w_.item() + 1, size=(1, )).item() for w_ in w]
            for iter,(x,y) in enumerate(zip(xs,ys)):
                overlap1[0,x:x+h[iter],y:y+w[iter]] += 1
            if (overlap1>1).any():
                continue

【问题讨论】:

    标签: python image-processing pytorch


    【解决方案1】:

    当您检查矩形 2 不与矩形 1 重叠时,只需检查它们的相交区域。如果相交面积大于 0,则拒绝矩形 2 并检查下一个随机矩形。用新的矩形重复这个过程。

    检查交叉点 - 使用 sklearn 的 Jaccard 分数(避免重新发明轮子)。为了能够使用 Jaccard 进行比较,两个数组(图像)应该具有相同的大小。所以分别从你的 rectangle1 和 rectangle2 生成原始图像大小等效掩码(掩码 1 和掩码 2),然后计算掩码 1 和掩码 2 的 Jaccard。

    import numpy as np
    from sklearn.metrics import jaccard_score
    
    mask1 = np.array([[0, 1, 1],
                     [1, 1, 0]])
    mask2 = np.array([[1, 1, 1],
                     [1, 0, 0]])
    
    jaccard_score(mask1, mask2)
    

    0.6666

    【讨论】:

    • 好的,这可能有效。但是在考虑计算费用时 - 生成带有 jaccard 分数的图像不是比一些检查是否存在重叠的 for 循环更昂贵吗?考虑每次将输入输入神经网络时都会发生这种随机擦除
    • 是的,当然会很贵。如果你想要效率,你需要设计一个基于动态规划的解决方案。
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多