【问题标题】:What exactly is label for image segmentation task in computer vision计算机视觉中图像分割任务的标签到底是什么
【发布时间】:2018-12-24 13:54:50
【问题描述】:

我最近一直在从事一些图像分割任务,并想从头开始应用一个。

据我所知,分割是对它所属位置的每像素预测 - 对象实例(事物),背景片段实例(东西)。

根据最新算法Mask RCNN所基于的COCO数据集:

事物是可数的对象,例如人、动物、工具。材料类是具有相似纹理或材料的无定形区域,例如草、天空、道路。

根据 Mask Rcnn 论文,最终分类是采用每个像素 sigmoid 的二元交叉熵损失函数(以避免类内竞争)。该管道基于 FRCNN 对象检测管道的顶部,从中获取感兴趣区域 (roi) 并将它们传递给 ROI-align 类以保持空间信息完整。

我感到困惑的是以下内容。下面给出一个非常简单的代码 sn-p,用于应用二元交叉熵损失来分离 3 个完全连接的层(一些带有尺度的随机实验):

class ModelMain(nn.Module):
    def __init__(self, config, is_training=True):
        super(ModelMain, self).__init__()
        self.fc_1 = torch.nn.Linear(incoming_size_1, outgoing_size_1)
        self.fc_2 = torch.nn.Linear(incoming_size_2, outgoing_size_2)
        self.fc_3 = torch.nn.Linear(incoming_size_3, outgoing_size_3)

    def forward(self, x):
        y_1 = F.sigmoid(self.fc_1(x)) 
        y_2 = F.sigmoid(self.fc_2(x)) 
        y_3 = F.sigmoid(self.fc_3(x)) 

        return y_1, y_2, y_3


model = ModelMain()
criterion = torch.nn.BCELoss(size_average = True) 
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)

def run_epoch():
    batchsize = 10
    for epoch in range(batchsize):
        # Find image segment predicted by running forward pass: 
        y_predicted_1, y_predicted_2, y_predicted_3  = model(batch_data_x)

        # Compute and print loss : 
        loss_1 = criterion(y_predicted_1, batch_data_y)
        loss_2 = criterion(y_predicted_2, batch_data_y)
        loss_3 = criterion(y_predicted_3, batch_data_y)

        print( "Epoch ", epoch, "Loss : ", loss_1, loss_2, loss_3)

        # Perform Backward pass : 
        optimizer.zero_grad()
        loss_1.backward()
        loss_2.backward()
        loss_3.backward()
        optimizer.step()

...我们在这里提供的究竟是什么标签?

来自数据集:

Formatted JSON Data

图片:

 {
       "license":2,
       "file_name":"000000000139.jpg",
       "coco_url":"http://images.cocodataset.org/val2017/000000000139.jpg",
       "height":426,
       "width":640,
       "date_captured":"2013-11-21 01:34:01",
       "flickr_url":"http://farm9.staticflickr.com/8035/8024364858_9c41dc1666_z.jpg",
       "id":139
    }

细分信息:

{
   "segments_info":[
      {
         "id":3226956,
         "category_id":1,
         "iscrowd":0,
         "bbox":[
            413,
            158,
            53,
            138
         ],
         "area":2840
      },
      {
         "id":6979964,
         "category_id":1,
         "iscrowd":0,
         "bbox":[
            384,
            172,
            16,
            36
         ],
         "area":439
      },
      {
         "id":3103374,
         "category_id":62,
         "iscrowd":0,
         "bbox":[
            413,
            223,
            30,
            81
         ],
         "area":1250
      },
      {
         "id":2831194,
         "category_id":62,
         "iscrowd":0,
         "bbox":[
            291,
            218,
            62,
            98
         ],
         "area":1848
      },
      {
         "id":3496593,
         "category_id":62,
         "iscrowd":0,
         "bbox":[
            412,
            219,
            10,
            13
         ],
         "area":90
      },
      {
         "id":2633066,
         "category_id":62,
         "iscrowd":0,
         "bbox":[
            317,
            219,
            22,
            12
         ],
         "area":212
      },
      {
         "id":3165572,
         "category_id":62,
         "iscrowd":0,
         "bbox":[
            359,
            218,
            56,
            103
         ],
         "area":2251
      },
      {
         "id":8824489,
         "category_id":64,
         "iscrowd":0,
         "bbox":[
            237,
            149,
            24,
            62
         ],
         "area":369
      },
      {
         "id":3032951,
         "category_id":67,
         "iscrowd":0,
         "bbox":[
            321,
            231,
            126,
            89
         ],
         "area":2134
      },
      {
         "id":2038814,
         "category_id":72,
         "iscrowd":0,
         "bbox":[
            7,
            168,
            149,
            95
         ],
         "area":13247
      },
      {
         "id":3289671,
         "category_id":72,
         "iscrowd":0,
         "bbox":[
            557,
            209,
            82,
            79
         ],
         "area":5846
      },
      {
         "id":2437710,
         "category_id":78,
         "iscrowd":0,
         "bbox":[
            512,
            206,
            15,
            16
         ],
         "area":224
      },
      {
         "id":4159376,
         "category_id":82,
         "iscrowd":0,
         "bbox":[
            493,
            174,
            20,
            108
         ],
         "area":2056
      },
      {
         "id":3423599,
         "category_id":84,
         "iscrowd":0,
         "bbox":[
            613,
            308,
            13,
            46
         ],
         "area":324
      },
      {
         "id":3094634,
         "category_id":84,
         "iscrowd":0,
         "bbox":[
            605,
            306,
            14,
            45
         ],
         "area":331
      },
      {
         "id":3296100,
         "category_id":85,
         "iscrowd":0,
         "bbox":[
            448,
            121,
            14,
            22
         ],
         "area":227
      },
      {
         "id":6054280,
         "category_id":86,
         "iscrowd":0,
         "bbox":[
            241,
            195,
            14,
            18
         ],
         "area":187
      },
      {
         "id":5942189,
         "category_id":86,
         "iscrowd":0,
         "bbox":[
            549,
            309,
            36,
            90
         ],
         "area":2171
      },
      {
         "id":4086154,
         "category_id":86,
         "iscrowd":0,
         "bbox":[
            351,
            209,
            11,
            22
         ],
         "area":178
      },
      {
         "id":7438777,
         "category_id":86,
         "iscrowd":0,
         "bbox":[
            337,
            200,
            10,
            16
         ],
         "area":120
      },
      {
         "id":3031159,
         "category_id":118,
         "iscrowd":0,
         "bbox":[
            0,
            269,
            564,
            157
         ],
         "area":49754
      },
      {
         "id":9284267,
         "category_id":119,
         "iscrowd":0,
         "bbox":[
            338,
            166,
            29,
            50
         ],
         "area":842
      },
      {
         "id":6068135,
         "category_id":130,
         "iscrowd":0,
         "bbox":[
            212,
            11,
            321,
            127
         ],
         "area":3391
      },
      {
         "id":2567230,
         "category_id":156,
         "iscrowd":0,
         "bbox":[
            129,
            168,
            351,
            162
         ],
         "area":5699
      },
      {
         "id":10334639,
         "category_id":181,
         "iscrowd":0,
         "bbox":[
            204,
            63,
            234,
            174
         ],
         "area":15587
      },
      {
         "id":6266027,
         "category_id":186,
         "iscrowd":0,
         "bbox":[
            136,
            0,
            473,
            116
         ],
         "area":20106
      },
      {
         "id":5274512,
         "category_id":188,
         "iscrowd":0,
         "bbox":[
            0,
            38,
            549,
            297
         ],
         "area":25483
      },
      {
         "id":7238567,
         "category_id":189,
         "iscrowd":0,
         "bbox":[
            457,
            350,
            183,
            76
         ],
         "area":9421
      },
      {
         "id":4224910,
         "category_id":199,
         "iscrowd":0,
         "bbox":[
            0,
            0,
            640,
            358
         ],
         "area":83201
      },
      {
         "id":6391959,
         "category_id":200,
         "iscrowd":0,
         "bbox":[
            135,
            359,
            336,
            67
         ],
         "area":12618
      }
   ],
   "file_name":"000000000139.png",
   "image_id":139
}

面具图片:

原图:

对于对象检测任务,我们有边界框,但对于图像分割,我需要使用提供的掩码计算损失。 那么上面代码中batch_data_y 的值应该是多少。 它会是遮罩图像的矢量吗?但这不会训练我的网络了解某个部分是什么颜色吗?还是我错过了其他一些片段注释?

【问题讨论】:

  • 尝试打印掩码图像中的值。它可能包含调色板,并且它包含的值实际上是您所期望的类标签。
  • 根据作者的说法,“掩码损失 Lmask 仅在正 RoI 上定义。掩码目标是 RoI 与其关联的真实掩码之间的交集”。我应该写一个增强步骤来找到我的两个张量之间的交集吗?这些值只是像素值,3 通道图像

标签: computer-vision mask image-segmentation pytorch cross-entropy


【解决方案1】:

@Aldream 的直觉是正确的,但是对于他们提供二进制掩码的 coco 数据集,他们明确表示,他们网站上的文档不是很好:

用于操作以 RLE 格式存储的掩码的接口。

RLE 是一种用于存储二进制掩码的简单而有效的格式。 RLE 首先将一个向量(或向量化图像)分成一系列分段 恒定区域,然后为每一块简单地存储 那块。例如,给定 M=[0 0 1 1 1 0 1],RLE 计数将 是 [2 3 1 1],或者对于 M=[1 1 1 1 1 1 0],计数将是 [0 6 1] (请注意,奇数始终是零的数量)。代替 直接存储计数,额外的压缩是通过一个 基于称为 LEB128 的通用方案的可变比特率表示。 来源:link

虽然我确实为平均二元交叉熵损失编写了自己的自定义函数:

def l_cross_entropy2d(input, target, weight=None, size_average=True):

    n, c, h, w = input.size()
    nt, ct, ht, wt = target.size()

    # Handle inconsistent size between input and target
    if h > ht and w > wt: # upsample labels
        target = target.unsqueeze(1)
        target = F.upsample(target, size=(h, w), mode='nearest')
        target = target.sequeeze(1)
    elif h < ht and w < wt: # upsample images
        input = F.upsample(input, size=(ht, wt), mode='bilinear')
    elif h != ht and w != wt:
        raise Exception("Only support upsampling")

    # take per pixel sigmoid  
    sigm = F.sigmoid(input)
    # change dimension to create 2d matrix where rows -> pixels and columns -> classes
    # takes input tensor <n X c X h X w> outputs tensor < n*h*w X c >
    sigm = sigm.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)

    # change target to column tensor for calculating cross entropy and repeat it number of classes times
    # Get all values from sigmoid tensor >= 0 (all pixels that have value) 
    sigm = sigm[target.view(-1, 1).repeat(1, c) >= 0]
    sigm = sigm.view(-1, c)

    mask = target >= 0
    target = target[mask]
    loss = F.nll_loss(sigm, target, ignore_index=250,
                      weight=weight, size_average=False)
    if size_average:
        loss /= mask.data.sum()
    return loss

【讨论】:

    【解决方案2】:

    正如@hkchengrex 在他的评论中所提到的,掩码图像中的颜色似乎是从真实图像中挑选出来的,这要么是巧合,要么是一些可视化后处理的结果。

    语义掩码通常表示/存储为图像,每个像素的值代表实际图片中的类。例如,假设您正在考虑C 类,图片I 的语义掩码M 可以表示为图像,其中M(i,j) = c 表示像素I(i,j) 应归类为属于语义类c (c in [0; C[ ; i in [0, H[, j in [0, W[, (H, W) 尺寸为I)。

    现在,由于类彼此独立,网络预测它们的最佳方法是输出形状为(H, W, C) 的概率图P,其中P(i,j,c) 表示估计的概率(在0 之间)和1,因此是像sigmoid这样的激活函数)I(i,j)属于c类。

    正如您详细说明的那样,通过这样的输出,您可以使用二进制交叉熵来训练您的网络——假设您预处理了您的真值掩码 M,以将它们从 HxW 图像转换将 [0,C] (logits) 中的值映射到 HxWxC 中的值与 [0,1] 的映射。这种预处理称为“单热转换”,可以使用 Pytorch 使用 scatter() c.f. 完成。这个thread

    import torch
    
    M_onehot = torch.cuda.FloatTensor(C, H, W)
    M_onehot.zero_()
    M_onehot.scatter_(1, M, 1)
    

    但是,另一种解决方案 - 可能不太适合您的问题(如果您想避免使用 softmax,因为它包含此操作) - 是使用(非二进制)交叉熵损失。 torch.nn.CrossEntropyLoss() 会直接以P(形状为(H, W, C))为预测,M(形状为(H, W))为目标。

    【讨论】:

    • 来自论文:“掩码分支对每个 RoI 都有一个 Km2 维输出,它编码了 K 个分辨率为 m×m 的二进制掩码,每个掩码对应 K 个类别。对此我们应用per-pixel sigmoid,并将 L mask 定义为平均二元交叉熵损失。对于与 ground-truth 类 k 相关的 RoI,L mask 仅在第 k 个 mask 上定义(其他 mask 输出对损失没有贡献)。 " 你能解释一下如何获得 Km2 o/p 吗?对于 coco 中的 80 个类,K =80。但是 th m2 是针对整个图像,还是每个对象的每个掩码都检测到一个单独的 m*m 张量?
    • 如果您将K 替换为C 并将(H, W) 替换为(m, m)(与输入图像相比,它们可能对掩码进行了子采样),我相信我的回答已经提供了一些解释.基本上,您的估计/基本事实是 K 大小为 (m,m) 的张量的堆栈。每个K 子张量代表其对应类的掩码(1 如果该类肯定表示在此像素/区域中;0 如果类不存在)。
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2021-08-05
    • 2018-05-13
    • 1970-01-01
    • 2015-02-28
    • 2014-08-16
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多