def benchmark_segmentation(pred_dir, gt_dir, num_classes, string_replace):
  """Benchmark segmentaion on mean Intersection over Union (mIoU).
  """
  print('Benchmarking semantic segmentation.')
  assert(os.path.isdir(pred_dir))
  assert(os.path.isdir(gt_dir))
  tp_fn = np.zeros(num_classes, dtype=np.float64)
  tp_fp = np.zeros(num_classes, dtype=np.float64)
  tp = np.zeros(num_classes, dtype=np.float64)
  for dirpath, dirnames, filenames in os.walk(pred_dir):
    for filename in filenames:
      predname = os.path.join(dirpath, filename)
      gtname = predname.replace(pred_dir, gt_dir)
      if string_replace != '':
        stra, strb = string_replace.split(',')
        gtname = gtname.replace(stra, strb)

      pred = np.asarray(
        Image.open(predname).convert(mode='L'),
        dtype=np.uint8
      )
      gt = np.asarray(
        Image.open(gtname).convert(mode='L'),
        dtype=np.uint8
      )
      _tp_fn, _tp_fp, _tp = iou_stats(
        pred,
        gt,
        num_classes=num_classes,
        background=0
      )
      tp_fn += _tp_fn
      tp_fp += _tp_fp
      tp += _tp

  iou = tp / (tp_fn + tp_fp - tp + 1e-12) * 100.0

  class_names = ['beam', 'board', 'bookcase', 'ceiling', 'chair', 'clutter',
                 'column', 'door', 'floor', 'sofa', 'table', 'wall', 'window']

  for i in range(num_classes):
    print('class {:10s}: {:02d}, acc: {:4.4f}%'.format(
      class_names[i], i, iou[i])
    )
  mean_iou = iou.sum() / num_classes
  print('mean IOU: {:4.4f}%'.format(mean_iou))

  mean_pixel_acc = tp.sum() / (tp_fp.sum() + 1e-12)
  print('mean Pixel Acc: {:4.4f}%'.format(mean_pixel_acc))
コード例 #2
0
def calcu_voc_mIou(pred_dir, gt_dir):
    assert os.path.isdir(pred_dir)
    assert os.path.isdir(gt_dir)

    print('......')

    n_class = 21
    tp_fn = np.zeros(n_class, dtype=np.float64)
    tp_fp = np.zeros(n_class, dtype=np.float64)
    tp = np.zeros(n_class, dtype=np.float64)

    for parent, dirs, files in os.walk(pred_dir):
        for file in files:
            pred_img_file = os.path.join(parent, file)
            gt_img_file = pred_img_file.replace(pred_dir, gt_dir)
            # if args.string_replace != '':
            # 	stra, strb = args.string_replace.split(',')
            # 	gtname = gtname.replace(stra, strb)
            pred = np.asarray(Image.open(pred_img_file).convert(mode='L'),
                              dtype=np.uint8)
            gt = np.asarray(Image.open(gt_img_file).convert(mode='P'),
                            dtype=np.uint8)
            _tp_fn, _tp_fp, _tp = iou_stats(pred,
                                            gt,
                                            num_classes=n_class,
                                            background=0)

            tp_fn += _tp_fn
            tp_fp += _tp_fp
            tp += _tp

    iou = tp / (tp_fn + tp_fp - tp + 1e-12) * 100.0

    class_names = [
        'Background', 'Aero', 'Bike', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
        'Cat', 'Chair', 'Cow', 'Table', 'Dog', 'Horse', 'MBike', 'Person',
        'Plant', 'Sheep', 'Sofa', 'Train', 'TV'
    ]

    for i in range(n_class):
        print('class {:10s}: {:02d}, acc: {:4.4f}%'.format(
            class_names[i], i, iou[i]))

    mean_iou = iou.sum() / n_class
    print('mean IOU: {:4.4f}%'.format(mean_iou))

    mean_pixel_acc = tp.sum() / (tp_fp.sum() + 1e-12)
    print('mean Pixel Acc: {:4.4f}%'.format(mean_pixel_acc))
コード例 #3
0
        instname = predname.replace(args.pred_dir, args.inst_dir)
        if args.string_replace != '':
            stra, strb = args.string_replace.split(',')
            gtname = gtname.replace(stra, strb)
            instname = instname.replace(stra, strb)

        pred = np.asarray(Image.open(predname).convert(mode='L'),
                          dtype=np.uint8)
        gt = np.asarray(Image.open(gtname).convert(mode='L'), dtype=np.uint8)
        inst = np.asarray(Image.open(instname).convert(mode='P'),
                          dtype=np.uint8)

        # Compute true-positive, false-positive
        # and false-negative
        _tp_fn, _tp_fp, _tp = iou_stats(pred,
                                        gt,
                                        num_classes=args.num_classes,
                                        background=0)

        # Compute num. of instances per class
        inst_inds = np.unique(inst)
        ninst_ = np.zeros(args.num_classes, dtype=np.float64)
        for i in range(inst_inds.size):
            if i < 255:
                inst_ind = inst_inds[i]
                inst_mask = inst == inst_ind
                seg_mask = gt[inst_mask]
                npixel, _ = np.histogram(
                    seg_mask,
                    bins=args.num_classes,
                    range=(0, args.num_classes -
                           1))  # num. pixel per class of this inst.