Ejemplo n.º 1
0
def test(args, test_loader, model):
    """
    args:
      test_loader: loaded for test dataset
      model: model
    return: class IoU and mean IoU
    """
    # evaluation or test mode
    model.eval()
    total_batches = len(test_loader)

    metric = SegmentationMetric(numClass=args.classes)
    pbar = tqdm(iterable=enumerate(test_loader),
                total=total_batches,
                desc='Valing')
    for i, (input, gt, size, name) in pbar:
        with torch.no_grad():
            input_var = Variable(input).cuda()

        output = model(input_var)
        torch.cuda.synchronize()

        output = output.cpu().data[0].numpy()
        output = output.transpose(1, 2, 0)
        output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
        gt = np.asarray(gt[0], dtype=np.uint8)

        # 计算miou
        metric.addBatch(imgPredict=output.flatten(), imgLabel=gt.flatten())

        # save the predicted image
        if args.save:
            save_predict(output,
                         gt,
                         name[0],
                         args.dataset,
                         args.save_seg_dir,
                         output_grey=False,
                         output_color=True,
                         gt_color=True)

    pa = metric.pixelAccuracy()
    cpa = metric.classPixelAccuracy()
    mpa = metric.meanPixelAccuracy()
    Miou, PerMiou_set = metric.meanIntersectionOverUnion()
    FWIoU = metric.Frequency_Weighted_Intersection_over_Union()

    return Miou, PerMiou_set, FWIoU, pa, mpa
Ejemplo n.º 2
0
def predict(args, test_loader, model):
    """
    args:
      test_loader: loaded for test dataset, for those that do not provide label on the test set
      model: model
    return: class IoU and mean IoU
    """
    # evaluation or test mode
    model.eval()
    total_batches = len(test_loader)

    metric = SegmentationMetric(numClass=args.classes)
    pbar = tqdm(iterable=enumerate(test_loader), total=total_batches, desc='Valing')
    for i, (input, gt, size, name) in pbar:
        with torch.no_grad():
            input_var = input.cuda()

        output = model(input_var)
        torch.cuda.synchronize()

        output = output.cpu().data[0].numpy()
        output = output.transpose(1, 2, 0)
        output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
        gt = np.asarray(gt[0], dtype=np.uint8)

        # 计算miou
        metric.addBatch(imgPredict=output.flatten(), imgLabel=gt.flatten())

        # save the predicted image
        save_predict(output, gt, name[0], args.dataset, args.save_seg_dir,
                         output_grey=False, output_color=True, gt_color=True)

    pa = metric.pixelAccuracy()
    cpa = metric.classPixelAccuracy()
    mpa = metric.meanPixelAccuracy()
    Miou, PerMiou_set = metric.meanIntersectionOverUnion()
    FWIoU = metric.Frequency_Weighted_Intersection_over_Union()
    print('miou {}\nclass iou {}'.format(Miou, PerMiou_set))
    result = args.save_seg_dir + '/results.txt'
    with open(result, 'w') as f:
        f.write(str(Miou))
        f.write('\n{}'.format(PerMiou_set))
Ejemplo n.º 3
0
def val(args, val_loader, criteria, model):
    """
    args:
      val_loader: loaded for validation dataset
      model: model
    return: mean IoU and IoU class
    """
    # evaluation mode
    model.eval()
    total_batches = len(val_loader)

    val_loss = []
    metric = SegmentationMetric(args.classes)
    pbar = tqdm(iterable=enumerate(val_loader),
                total=total_batches,
                desc='Val')
    for iteration, (input, label, size, name) in pbar:
        with torch.no_grad():
            input_var = input.cuda().float()
            output = model(input_var)
            if type(output) is tuple:
                output = output[0]

        loss = criteria(output, label.long().cuda())
        val_loss.append(loss)
        output = output.cpu().data[0].numpy()
        gt = np.asarray(label[0].numpy(), dtype=np.uint8)
        output = output.transpose(1, 2, 0)
        output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
        # 计算miou
        metric.addBatch(imgPredict=output.flatten(), imgLabel=gt.flatten())

    val_loss = sum(val_loss) / len(val_loss)

    pa = metric.pixelAccuracy()
    cpa = metric.classPixelAccuracy()
    mpa = metric.meanPixelAccuracy()
    Miou, PerMiou_set = metric.meanIntersectionOverUnion()
    FWIoU = metric.Frequency_Weighted_Intersection_over_Union()

    return val_loss, FWIoU, Miou, PerMiou_set
Ejemplo n.º 4
0
def predict_sliding(args, net, image, tile_size, classes):
    total_batches = len(image)
    metric = SegmentationMetric(args.classes)  # args.classes表示有args.classes个分类
    pbar = tqdm(iterable=enumerate(image),
                total=total_batches,
                desc='Predicting')
    for i, (input, gt, size, name) in pbar:
        image_size = input.shape  # (1,3,3328,3072)
        overlap = 1 / 3  # 每次滑动的覆盖率为1/3
        # print(image_size, tile_size)
        stride = ceil(
            tile_size[0] *
            (1 - overlap))  # 滑动步长:512*(1-1/3) = 513     512*(1-1/3)= 342
        tile_rows = int(ceil((image_size[2] - tile_size[0]) / stride) +
                        1)  # 行滑动步数:(3072-512)/342+1=9
        tile_cols = int(ceil((image_size[3] - tile_size[1]) / stride) +
                        1)  # 列滑动步数:(3328-512)/342+1=10
        full_probs = np.zeros((image_size[2], image_size[3],
                               classes))  # 初始化全概率矩阵shape(3072,3328,3)
        count_predictions = np.zeros((image_size[2], image_size[3],
                                      classes))  # 初始化计数矩阵shape(3072,3328,3)

        for row in range(tile_rows):  # row = 0,1     0,1,2,3,4,5,6,7,8
            for col in range(
                    tile_cols):  # col = 0,1,2,3     0,1,2,3,4,5,6,7,8,9
                x1 = int(col * stride)  # 起始位置x1 = 0 * 513 = 0   0*342
                y1 = int(row * stride)  # y1 = 0 * 513 = 0   0*342
                x2 = min(x1 + tile_size[1],
                         image_size[3])  # 末位置x2 = min(0+512, 3328)
                y2 = min(y1 + tile_size[0],
                         image_size[2])  # y2 = min(0+512, 3072)
                x1 = max(int(x2 - tile_size[1]),
                         0)  # 重新校准起始位置x1 = max(512-512, 0)
                y1 = max(int(y2 - tile_size[0]), 0)  # y1 = max(512-512, 0)

                img = input[:, :, y1:y2,
                            x1:x2]  # 滑动窗口对应的图像 imge[:, :, 0:512, 0:512]
                padded_img = pad_image(img,
                                       tile_size)  # padding 确保扣下来的图像为512*512
                # plt.imshow(padded_img)
                # plt.show()

                # 将扣下来的部分传入网络,网络输出概率图。
                with torch.no_grad():
                    input_var = torch.from_numpy(padded_img).cuda().float()
                    padded_prediction = net(input_var)

                    if type(padded_prediction) is tuple:
                        padded_prediction = padded_prediction[0]

                    torch.cuda.synchronize()

                if isinstance(padded_prediction, list):
                    padded_prediction = padded_prediction[
                        0]  # shape(1,3,512,512)

                padded_prediction = padded_prediction.cpu().data[0].numpy(
                ).transpose(1, 2, 0)  # 通道位置变换(512,512,3)
                prediction = padded_prediction[
                    0:img.shape[2],
                    0:img.shape[3], :]  # 扣下相应面积 shape(512,512,3)
                count_predictions[y1:y2, x1:x2] += 1  # 窗口区域内的计数矩阵加1
                full_probs[y1:y2, x1:x2] += prediction  # 窗口区域内的全概率矩阵叠加预测结果

        # average the predictions in the overlapping regions
        full_probs /= count_predictions  # 全概率矩阵 除以 计数矩阵 即得 平均概率
        # visualize normalization Weights
        # plt.imshow(np.mean(count_predictions, axis=2))
        # plt.show()
        full_probs = np.asarray(np.argmax(full_probs, axis=2), dtype=np.uint8)
        '''设置输出原图和预测图片的颜色灰度还是彩色'''
        gt = gt[0].numpy()
        # 计算miou
        metric.addBatch(full_probs, gt)
        save_predict(full_probs,
                     gt,
                     name[0],
                     args.dataset,
                     args.save_seg_dir,
                     output_grey=False,
                     output_color=True,
                     gt_color=True)

    pa = metric.pixelAccuracy()
    cpa = metric.classPixelAccuracy()
    mpa = metric.meanPixelAccuracy()
    Miou, PerMiou_set = metric.meanIntersectionOverUnion()
    FWIoU = metric.Frequency_Weighted_Intersection_over_Union()

    print('miou {}\nclass iou {}'.format(Miou, PerMiou_set))
    result = args.save_seg_dir + '/results.txt'
    with open(result, 'w') as f:
        f.write(str(Miou))
        f.write('\n{}'.format(PerMiou_set))
Ejemplo n.º 5
0
def test(args, test_loader, model):
    """
    args:
      test_loader: loaded for test dataset
      model: model
    return: class IoU and mean IoU
    """
    # evaluation or test mode
    model.eval()
    total_batches = len(test_loader)

    Miou_list = []
    Iou_list = []
    Pa_list = []
    Mpa_list = []
    Fmiou_list = []
    pbar = tqdm(iterable=enumerate(test_loader),
                total=total_batches,
                desc='Valing')
    for i, (input, gt, size, name) in pbar:
        with torch.no_grad():
            input_var = Variable(input).cuda()
        start_time = time.time()
        output = model(input_var)
        torch.cuda.synchronize()
        time_taken = time.time() - start_time
        pbar.set_postfix(cost_time='%.3f' % time_taken)
        output = output.cpu().data[0].numpy()
        output = output.transpose(1, 2, 0)
        output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
        gt = np.asarray(gt[0], dtype=np.uint8)

        # 计算miou
        metric = SegmentationMetric(numClass=args.classes)
        metric.addBatch(imgPredict=output, imgLabel=gt)
        miou, iou = metric.meanIntersectionOverUnion()
        fmiou = metric.Frequency_Weighted_Intersection_over_Union()
        pa = metric.pixelAccuracy()
        mpa = metric.meanPixelAccuracy()
        Miou_list.append(miou)
        Fmiou_list.append(fmiou)
        Pa_list.append(pa)
        Mpa_list.append(mpa)
        iou = np.array(iou)
        Iou_list.append(iou)

        # save the predicted image
        if args.save:
            save_predict(output,
                         gt,
                         name[0],
                         args.dataset,
                         args.save_seg_dir,
                         output_grey=False,
                         output_color=True,
                         gt_color=True)

    miou = np.mean(Miou_list)
    fmiou = np.mean(Fmiou_list)
    pa = np.mean(Pa_list)
    mpa = np.mean(Mpa_list)
    Iou_list = np.asarray(Iou_list)
    iou = np.mean(Iou_list, axis=0)
    cls_iu = dict(zip(range(args.classes), iou))
    return miou, cls_iu, fmiou, pa, mpa