def cal_acc(data_list, pred_folder, classes, names):
    intersection_meter = AverageMeter()
    union_meter = AverageMeter()
    target_meter = AverageMeter()
    colors = np.loadtxt(args.colors_path).astype('uint8')
    names = [line.rstrip('\n') for line in open(args.names_path)]

    for i, (image_path, target_path) in enumerate(data_list):
        fd_name=image_path.split('/')[-3]
        image_name = image_path.split('/')[-1].split('.')[0]
        pred = cv2.imread(os.path.join(pred_folder, image_name+'.png'), cv2.IMREAD_GRAYSCALE)
        target = cv2.imread(target_path, cv2.IMREAD_GRAYSCALE)
        
        color = colorize(target, colors)
        
        color_path = os.path.join('/local/xjqi/2ddata/tasks/semseg_50_bn/exp/ade20k/pspnet50/result/epoch_100/val/ss/gt/', fd_name+"_"+image_name + '.png')
        color.save(color_path)
        
        
        intersection, union, target = intersectionAndUnion(pred, target, classes)
        intersection_meter.update(intersection)
        union_meter.update(union)
        target_meter.update(target)
        accuracy = sum(intersection_meter.val) / (sum(target_meter.val) + 1e-10)
        #logger.info('Evaluating {0}/{1} on image {2}, accuracy {3:.4f}.'.format(i + 1, len(data_list), image_name+'.png', accuracy))

    iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
    accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
    mIoU = np.mean(iou_class)
    mAcc = np.mean(accuracy_class)
    allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

    logger.info('Eval result: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(mIoU, mAcc, allAcc))
    for i in range(classes):
        logger.info('Class_{} result: iou/accuracy {:.4f}/{:.4f}, name: {}.'.format(i, iou_class[i], accuracy_class[i], names[i]))
Beispiel #2
0
def test(model, image_path, classes, mean, std, base_size, crop_h, crop_w,
         scales, colors):
    image = cv2.imread(
        image_path,
        cv2.IMREAD_COLOR)  # BGR 3 channel ndarray wiht shape H * W * 3
    image = cv2.cvtColor(
        image, cv2.COLOR_BGR2RGB
    )  # convert cv2 read image from BGR order to RGB order
    h, w, _ = image.shape
    prediction = np.zeros((h, w, classes), dtype=float)
    for scale in scales:
        long_size = round(scale * base_size)
        new_h = long_size
        new_w = long_size
        if h > w:
            new_w = round(long_size / float(h) * w)
        else:
            new_h = round(long_size / float(w) * h)
        image_scale = cv2.resize(image, (new_w, new_h),
                                 interpolation=cv2.INTER_LINEAR)
        prediction += scale_process(model, image_scale, classes, crop_h,
                                    crop_w, h, w, mean, std)
    prediction = np.argmax(prediction, axis=2)
    gray = np.uint8(prediction)
    color = colorize(gray, colors)
    image_name = image_path.split('/')[-1].split('.')[0]
    gray_path = os.path.join('./figure/demo/', image_name + '_gray.png')
    color_path = os.path.join('./figure/demo/', image_name + '_color.png')
    cv2.imwrite(gray_path, gray)
    color.save(color_path)
    logger.info("=> Prediction saved in {}".format(color_path))
Beispiel #3
0
def test(test_loader, data_list, model, classes, mean, std, base_size, crop_h,
         crop_w, scales, gray_folder, color_folder, colors):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (input, _, image_paths) in enumerate(test_loader):
        data_time.update(time.time() - end)
        input = np.squeeze(input.numpy(), axis=0)
        image = np.transpose(input, (1, 2, 0))
        # print(np.amax(input), np.amin(input), np.median(input))
        # print(np.amax(image), np.amin(image), np.median(image))
        h, w, _ = image.shape
        prediction = np.zeros((h, w, classes), dtype=float)
        for scale in scales:
            long_size = round(scale * base_size)
            new_h = long_size
            new_w = long_size
            if h > w:
                new_w = round(long_size / float(h) * w)
            else:
                new_h = round(long_size / float(w) * h)
            image_scale = cv2.resize(image, (new_w, new_h),
                                     interpolation=cv2.INTER_LINEAR)
            prediction += scale_process(model, image_scale, classes, crop_h,
                                        crop_w, h, w, mean, std)
        prediction /= len(scales)
        prediction = np.argmax(prediction, axis=2)
        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))
        check_makedirs(gray_folder)
        check_makedirs(color_folder)
        gray = np.uint8(prediction)
        color = colorize(gray, colors)
        image_path, _ = data_list[i]
        image_name = image_path.split('/')[-1].split('.')[0]
        gray_path = os.path.join(gray_folder, image_name + '.png')
        color_path = os.path.join(color_folder, image_name + '.png')
        cv2.imwrite(gray_path, gray)
        color.save(color_path)
        cv2.imwrite(color_path.replace('.png', '_RGB_scale.png'), image_scale)
        # os.system('cp -r %s %s'%(image_path, color_path.replace('.png', '_RGB.png')))
        image_RGB = args.read_image(image_path)
        cv2.imwrite(color_path.replace('.png', '_RGB.png'), image_RGB)
        print('Result saved to %s; originally from %s' %
              (color_path, image_path))
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
Beispiel #4
0
def test(test_loader, data_list, model, classes, mean, std, base_size, crop_h,
         crop_w, scales, gray_folder, color_folder, colors):
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (input, _) in enumerate(test_loader):
        data_time.update(time.time() - end)
        input = np.squeeze(input.numpy(), axis=0)
        image = np.transpose(input, (1, 2, 0))
        h, w, _ = image.shape
        prediction = np.zeros((h, w, classes), dtype=float)
        for scale in scales:
            long_size = round(scale * base_size)
            new_h = long_size
            new_w = long_size
            if h > w:
                new_w = round(long_size / float(h) * w)
            else:
                new_h = round(long_size / float(w) * h)
            image_scale = cv2.resize(image, (new_w, new_h),
                                     interpolation=cv2.INTER_LINEAR)
            prediction += scale_process(model, image_scale, classes, crop_h,
                                        crop_w, h, w, mean, std)
        prediction /= len(scales)
        prediction = np.argmax(prediction, axis=2)
        batch_time.update(time.time() - end)
        end = time.time()

        check_makedirs(gray_folder)
        check_makedirs(color_folder)
        gray = np.uint8(prediction)
        color = colorize(gray, colors)
        image_path, _ = data_list[i]
        image_name = image_path.split('/')[-1].split('.')[0]
        gray_path = os.path.join(gray_folder, image_name + '.png')
        color_path = os.path.join(color_folder, image_name + '.png')
        cv2.imwrite(gray_path, gray)
        color.save(color_path)
Beispiel #5
0
def test(test_loader, data_list, model, gray_folder, colors, color_folder):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (input, _) in enumerate(test_loader):
        data_time.update(time.time() - end)
        prediction = predict_whole_img(model, input)
        # prediction=prediction[0].numpy()
        prediction = np.argmax(prediction, axis=3)
        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))

        for g in range(0, prediction.shape[0]):
            check_makedirs(gray_folder)
            check_makedirs(color_folder)
            gray = np.uint8(prediction[g])
            color = colorize(gray, colors)

            image_path, _ = data_list[i * args.batch_size_gen + g]
            image_name = image_path.split('/')[-1].split('.')[0]
            gray_path = os.path.join(gray_folder, image_name + '.png')
            cv2.imwrite(gray_path, gray)
            color_path = os.path.join(color_folder, image_name + '.png')
            color.save(color_path)
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
Beispiel #6
0
def test(test_loader, data_list, model, classes, mean, std, gray_folder,
         color_folder, derain_folder, edge_folder, colors):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (_, input, _, _) in enumerate(test_loader):
        data_time.update(time.time() - end)

        with torch.no_grad():
            derain_outs, seg_outs, edge_outs = model(input)

        derain_outs = derain_outs.cpu().numpy()
        seg_outs = seg_outs.cpu().numpy()
        edge_outs = edge_outs.cpu().numpy()

        # process derain img
        derain_outs = np.transpose(derain_outs, (0, 2, 3, 1)).squeeze(axis=0)
        derain_outs *= std
        derain_outs += mean
        derain_outs = np.clip(derain_outs, a_max=255, a_min=0)
        derain_outs = derain_outs.astype('uint8')
        derain_outs = cv2.cvtColor(derain_outs.astype('uint8'),
                                   cv2.COLOR_RGB2BGR)

        # process seg pred
        seg_outs = np.transpose(seg_outs, (0, 2, 3, 1))
        seg_outs = np.argmax(seg_outs, axis=3).squeeze(axis=0)

        # process edge pred
        edge_outs = np.transpose(edge_outs,
                                 (0, 2, 3, 1)).squeeze(axis=3).squeeze(axis=0)
        edge_outs = np.clip(edge_outs, a_max=1, a_min=0)
        edge_outs = (edge_outs * 255).astype('uint8')

        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))
        check_makedirs(gray_folder)
        check_makedirs(color_folder)
        check_makedirs(derain_folder)
        check_makedirs(edge_folder)

        gray = np.uint8(seg_outs)
        color = colorize(gray, colors)
        image_path, _, _ = data_list[i]
        image_name = image_path.split('/')[-1].split('.')[0]
        gray_path = os.path.join(gray_folder, image_name + '.png')
        color_path = os.path.join(color_folder, image_name + '.png')
        derain_path = os.path.join(derain_folder, image_name + '.png')
        edge_path = os.path.join(edge_folder, image_name + '.png')
        cv2.imwrite(gray_path, gray)
        cv2.imwrite(derain_path, derain_outs)
        cv2.imwrite(edge_path, edge_outs)
        color.save(color_path)
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
def test(test_loader, data_list, model, classes, mean, std, base_size, crop_h,
         crop_w, scales, gray_folder, color_folder, derain_folder, edge_folder,
         colors):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (_, input, _, _) in enumerate(test_loader):
        data_time.update(time.time() - end)
        input = np.squeeze(input.numpy(), axis=0)
        image = np.transpose(input, (1, 2, 0))
        h, w, _ = image.shape
        derain_prediction = np.zeros((h, w, 3), dtype=float)
        seg_prediction = np.zeros((h, w, classes), dtype=float)
        edge_prediction = np.zeros((h, w), dtype=float)
        for scale in scales:
            long_size = round(scale * base_size)
            new_h = long_size
            new_w = long_size
            if h > w:
                new_w = round(long_size / float(h) * w)
            else:
                new_h = round(long_size / float(w) * h)
            image_scale = cv2.resize(image, (new_w, new_h),
                                     interpolation=cv2.INTER_LINEAR)
            temp_derain_prediction, temp_seg_prediction, temp_edge_prediction = scale_process(
                model, image_scale, classes, crop_h, crop_w, h, w, mean, std)
            derain_prediction += temp_derain_prediction
            seg_prediction += temp_seg_prediction
            edge_prediction += temp_edge_prediction
        derain_prediction /= len(scales)
        seg_prediction /= len(scales)
        edge_prediction /= len(scales)
        # seg_prediction = np.argmax(seg_prediction, axis=2)

        # process derain img
        derain_outs = derain_prediction
        derain_outs *= std
        derain_outs += mean
        derain_outs = np.clip(derain_outs, a_max=255, a_min=0)
        derain_outs = derain_outs.astype('uint8')
        derain_outs = cv2.cvtColor(derain_outs.astype('uint8'),
                                   cv2.COLOR_RGB2BGR)

        # process seg pred
        seg_outs = seg_prediction
        seg_outs = np.argmax(seg_outs, axis=2).squeeze()

        # process edge pred
        edge_outs = edge_prediction
        edge_outs = np.clip(edge_outs, a_max=1, a_min=0)
        edge_outs = (edge_outs * 255).astype('uint8')

        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))
        check_makedirs(gray_folder)
        check_makedirs(color_folder)
        check_makedirs(derain_folder)
        check_makedirs(edge_folder)
        gray = np.uint8(seg_outs)
        color = colorize(gray, colors)
        image_path, _, _ = data_list[i]
        image_name = image_path.split('/')[-1].split('.')[0]
        gray_path = os.path.join(gray_folder, image_name + '.png')
        color_path = os.path.join(color_folder, image_name + '.png')
        derain_path = os.path.join(derain_folder, image_name + '.png')
        edge_path = os.path.join(edge_folder, image_name + '.png')
        cv2.imwrite(gray_path, gray)
        cv2.imwrite(derain_path, derain_outs)
        cv2.imwrite(edge_path, edge_outs)
        color.save(color_path)
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
Beispiel #8
0
def run_test(test_loader, data_list, model, classes, mean, std, base_size,
             crop_h, crop_w, scales, gray_folder, color_folder, colors,
             is_flip):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    is_test = 'test' in data_list[0][0]
    for i, (input, _) in enumerate(test_loader):
        data_time.update(time.time() - end)
        input = np.squeeze(input.numpy(), axis=0)
        image = np.transpose(input, (1, 2, 0))
        h, w, _ = image.shape
        prediction = np.zeros((h, w, classes), dtype=float)
        for scale in scales:
            long_size = round(scale * base_size)
            new_h = long_size
            new_w = long_size
            if h > w:
                new_w = round(long_size / float(h) * w)
            else:
                new_h = round(long_size / float(w) * h)
            image_scale = cv2.resize(image, (new_w, new_h),
                                     interpolation=cv2.INTER_LINEAR)
            prediction += scale_process(model,
                                        image_scale,
                                        classes,
                                        crop_h,
                                        crop_w,
                                        h,
                                        w,
                                        mean,
                                        std,
                                        FLIP=is_flip)
        prediction /= len(scales)
        prediction = np.argmax(prediction, axis=2)
        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))
        check_makedirs(gray_folder)
        check_makedirs(color_folder)
        gray = np.uint8(prediction)
        color = colorize(gray, colors)
        image_path, _ = data_list[i]
        image_name = image_path.split('/')[-1].split('.')[0]
        gray_path = os.path.join(gray_folder, image_name + '.png')
        color_path = os.path.join(color_folder, image_name + '.png')

        if is_test:
            gray_labelid = trainID2labelID(gray)
            cv2.imwrite(gray_path, gray_labelid)
        else:
            cv2.imwrite(gray_path, gray)
        color.save(color_path)
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
Beispiel #9
0
def test(test_loader,
         data_list,
         model,
         classes,
         mean,
         std,
         base_size,
         crop_h,
         crop_w,
         scales,
         gray_folder,
         color_folder,
         colors,
         is_multi_patch=True,
         is_med=False):
    """
    crop_h, crop_w = 713, 713
    
    """
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    for i, (input, gt) in enumerate(test_loader):
        data_time.update(time.time() - end)
        input = np.squeeze(input.numpy(), axis=0)
        image = np.transpose(input, (1, 2, 0))
        h, w, _ = image.shape  #原始图像尺寸
        prediction = np.zeros((h, w, classes), dtype=float)
        for scale in scales:
            long_size = round(scale * base_size)  # 推理中图像底板设置成2048
            new_h = long_size
            new_w = long_size
            if h > w:
                new_w = round(long_size / float(h) * w)
            else:
                new_h = round(long_size / float(w) * h)
            image_scale = cv2.resize(image, (new_w, new_h),
                                     interpolation=cv2.INTER_LINEAR)  #
            if is_multi_patch:
                print('inplace multi inference with patches')
                prediction += scale_process(model, image_scale, classes,
                                            crop_h, crop_w, h, w, mean, std)
            else:
                print('inplane one inference')
                #prediction += scale_process_direct(model, image_scale, classes, crop_h, crop_w, h, w, mean, std)
                prediction += scale_process(model, image_scale, classes,
                                            new_h + 1, new_w + 1, h, w, mean,
                                            std)
        prediction /= len(scales)
        prediction = np.argmax(prediction, axis=2)
        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info(
                'Test: [{}/{}] '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(
                    i + 1,
                    len(test_loader),
                    data_time=data_time,
                    batch_time=batch_time))

        _, target_path = data_list[i]
        image_name = get_image_name(target_path, is_med)
        # print(image_name)

        check_makedirs(gray_folder)
        gray = np.uint8(prediction)
        gray_path = os.path.join(gray_folder, image_name)
        check_makedirs(Path(gray_path).parent)
        cv2.imwrite(gray_path, gray)

        check_makedirs(color_folder)
        color = colorize(gray, colors)
        color_path = os.path.join(color_folder, image_name)
        color.save(color_path)

    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
Beispiel #10
0
def test(test_loader, data_list, model, classes, mean, std, base_size, crop_h, crop_w, scales, gray_folder, color_folder, colors):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.eval()
    end = time.time()
    pred_path_list = []
    target_path_list = []
    check_makedirs(gray_folder)
    check_makedirs(color_folder)

    for i, (input, target, image_paths) in tqdm(enumerate(test_loader)):
        data_time.update(time.time() - end)
        # input = np.squeeze(input.numpy(), axis=0)
        # image = np.transpose(input, (1, 2, 0))
        # # print(np.amax(input), np.amin(input), np.median(input))
        # # print(np.amax(image), np.amin(image), np.median(image))
        # h, w, _ = image.shape
        # prediction = np.zeros((h, w, classes), dtype=float)
        # for scale in scales:
        #     long_size = round(scale * base_size)
        #     new_h = long_size
        #     new_w = long_size
        #     if h > w:
        #         new_w = round(long_size/float(h)*w)
        #     else:
        #         new_h = round(long_size/float(w)*h)
        #     image_scale = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
        #     prediction += scale_process(model, image_scale, classes, crop_h, crop_w, h, w, mean, std)
        # prediction /= len(scales)
        # prediction = np.argmax(prediction, axis=2)

        input = input.cuda(non_blocking=True)
        output = model(input)
        if args.zoom_factor != 8:
            output = F.interpolate(output, size=target.size()[1:], mode='bilinear', align_corners=True)
        prediction = torch.argmax(output, 1).cpu().numpy().squeeze()

        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % 10 == 0) or (i + 1 == len(test_loader)):
            logger.info('Test: [{}/{}] '
                        'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                        'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}).'.format(i + 1, len(test_loader),
                                                                                    data_time=data_time,
                                                                                    batch_time=batch_time))
        gray = np.uint8(prediction)
        if args.test_in_nyu_label_space:
            gray = map_to_nyu(gray, args.dataset_name_pred)

        gray_path = os.path.join(gray_folder, '%02d.png'%i)
        pred_path_list.append(gray_path)
        cv2.imwrite(gray_path, gray)

        if args.test_has_gt:
            target = np.uint8(target.squeeze().cpu().numpy())
            if args.test_in_nyu_label_space:
                target = map_to_nyu(target, args.dataset_name)
            gray_target_path = os.path.join(gray_folder, '%02d_target.png'%i)
            cv2.imwrite(gray_target_path, target)
            target_path_list.append(gray_target_path)

        if i <= 100:
            color = colorize(gray, colors)
            image_path, _ = data_list[i]
            image_name = image_path.split('/')[-1].split('.')[0]
            color_path = os.path.join(color_folder, '%02d.png'%i)
            color.save(color_path)
            image_RGB = args.read_image(image_path, resize_to_target_size=True)
            cv2.imwrite(color_path.replace('.png', '_RGB.png'), image_RGB)
            target_color = colorize(target, colors)
            color_path = os.path.join(color_folder, '%02d_GT.png'%i)
            target_color.save(color_path)
            print('Result saved to %s; originally from %s'%(color_path, image_path))
        # if i > 100:
        #     break
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
    
    assert len(pred_path_list) == len(target_path_list)
    return pred_path_list, target_path_list
Beispiel #11
0
def validate(val_loader, model, criterion, args):
    if main_process():
        logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    batch_time = AverageMeter()
    data_time = AverageMeter()
    loss_meter = AverageMeter()
    intersection_meter = AverageMeter()
    union_meter = AverageMeter()
    target_meter = AverageMeter()

    colors = np.loadtxt(args.colors_path).astype('uint8')

    model.eval()
    end = time.time()
    return_dict = {}
    color_GT_list = []
    color_pred_list = []
    im_list = []
    image_name_list = []
    summary_idx = 0
    for i, (input, target, image_paths) in enumerate(val_loader):
        # if i > 20:
        #     break
        data_time.update(time.time() - end)
        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)
        output = model(input)
        if args.zoom_factor != 8:
            output = F.interpolate(output,
                                   size=target.size()[1:],
                                   mode='bilinear',
                                   align_corners=True)
        loss = criterion(output, target)

        # print(output.shape, target.shape) # torch.Size([8, 21, 241, 321]) torch.Size([8, 241, 321])
        if summary_idx > 12 and args.just_vis:
            break

        if summary_idx <= 12:
            prediction = torch.argmax(output, 1).cpu().numpy()
            label = target.cpu().numpy()
            for sample_idx in range(output.shape[0]):
                gray_GT = np.uint8(label[sample_idx])
                color_GT = np.array(colorize(gray_GT, colors).convert('RGB'))
                gray_pred = np.uint8(prediction[sample_idx])
                color_pred = np.array(
                    colorize(gray_pred, colors).convert('RGB'))
                image_path = image_paths[sample_idx]
                # image_name = image_path.split('/')[-1].split('.')[0]
                # print(color_GT.shape, color_GT.dtype, color_pred.shape, color_pred.dtype, image_path)
                # print(np.amax(color_GT), np.amin(color_GT), np.median(color_GT))
                image_name_list.append(image_path)
                im_list.append(args.read_image(image_path))
                color_GT_list.append(color_GT)
                color_pred_list.append(color_pred)
                summary_idx += 1
                if summary_idx > 12:
                    break

            return_dict.update({
                'image_name_list': image_name_list,
                'im_list': im_list,
                'color_GT_list': color_GT_list,
                'color_pred_list': color_pred_list
            })

        n = input.size(0)
        if args.multiprocessing_distributed:
            loss = loss * n  # not considering ignore pixels
            count = target.new_tensor([n], dtype=torch.long)
            dist.all_reduce(loss), dist.all_reduce(count)
            n = count.item()
            loss = loss / n
        else:
            loss = torch.mean(loss)

        output = output.max(1)[1]
        intersection, union, target = intersectionAndUnionGPU(
            output, target, args.classes, args.ignore_label)
        if args.multiprocessing_distributed:
            dist.all_reduce(intersection), dist.all_reduce(
                union), dist.all_reduce(target)
        intersection, union, target = intersection.cpu().numpy(), union.cpu(
        ).numpy(), target.cpu().numpy()
        intersection_meter.update(intersection), union_meter.update(
            union), target_meter.update(target)

        accuracy = sum(
            intersection_meter.val) / (sum(target_meter.val) + 1e-10)
        loss_meter.update(loss.item(), input.size(0))
        batch_time.update(time.time() - end)
        end = time.time()
        if ((i + 1) % args.print_freq == 0) and main_process():
            logger.info('Test: [{}/{}] '
                        'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                        'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
                        'Loss {loss_meter.val:.4f} ({loss_meter.avg:.4f}) '
                        'Accuracy {accuracy:.4f}.'.format(
                            i + 1,
                            len(val_loader),
                            data_time=data_time,
                            batch_time=batch_time,
                            loss_meter=loss_meter,
                            accuracy=accuracy))

    iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
    accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
    mIoU = np.mean(iou_class)
    mAcc = np.mean(accuracy_class)
    allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)
    if main_process():
        logger.info(
            'Val result: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(
                mIoU, mAcc, allAcc))
        for i in range(args.classes):
            logger.info('Class_{} Result: iou/accuracy {:.4f}/{:.4f}.'.format(
                i, iou_class[i], accuracy_class[i]))
        logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
    return loss_meter.avg, mIoU, mAcc, allAcc, return_dict