예제 #1
0
def validate(data_loader, net, loss, epoch, num_class):
    start_time = time.time()
    net.eval()
    metrics = []
    iou = 0
    acc = 0
    mean_acc = 0
    for i, batch in enumerate(tqdm(data_loader)):
        data, heatmaps, depth = batch['image'], batch['label'], batch['depth']
        data = data.cuda(async=True)
        #depth = depth.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        prediction = net(data)
        loss_output = loss(prediction, heatmaps)
        iou_, acc_, mean_acc_ = helper.cal_metric(heatmaps, prediction,
                                                  num_class)
        iou += iou_
        acc += acc_
        mean_acc += mean_acc_
        metrics.append(loss_output.item())
        if i == (len(data_loader) - 2):
            output_img = batch['image']
            out_label = batch['label']
            out_prediction = prediction
    iou /= len(data_loader)
    acc /= len(data_loader)
    mean_acc /= len(data_loader)
    img = helper.make_validation_img(output_img.numpy(), out_label.numpy(),
                                     out_prediction.cpu().numpy())
    cv2.imwrite('%s/validate_%d_%.4f.png' % (save_dir, epoch, iou),
                img[:, :, ::-1])

    end_time = time.time()
    metrics = np.asarray(metrics, np.float32)
    return metrics, end_time - start_time, iou, acc, mean_acc
예제 #2
0
def validate(data_loader, net, loss, epoch, num_class):
    start_time = time.time()
    net.eval()
    metrics = []
    iou = 0
    acc = 0
    mean_acc = 0
    d_iou = 0
    d_acc = 0
    d_mean_acc = 0
    k = 0


    for i, batch in enumerate(tqdm(data_loader)):
        data, heatmaps, depth = batch['image'], batch['label'], batch['depth']
        data = data.cuda(async=True)
        depth = depth.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        #prediction_d = net(data,depth)
        prediction_d = net(data,torch.zeros(depth.size()))
        #print(prediction_d.shape,prediction.shape)
        #loss_output = loss(prediction, heatmaps)
        #iou_, acc_, mean_acc_ = helper.cal_metric(heatmaps, prediction, num_class)
        iou_d, acc_d, mean_acc_d = helper.cal_metric(heatmaps, prediction_d, num_class)
        #iou += iou_
        #acc += acc_
        #mean_acc += mean_acc_
        d_iou += iou_d
        d_acc += acc_d
        d_mean_acc += mean_acc_d
        k += 1
        if k==(len(data_loader)-2):
        	output_img = batch['image']
        	out_label = batch['label']
        	out_prediction_d = prediction_d
        	#out_prediction = prediction

        #metrics.append(loss_output.item())
    assert k==len(data_loader)
    #iou /= len(data_loader)
    #acc /= len(data_loader)
    #mean_acc /= len(data_loader)
    d_iou /= len(data_loader)
    d_acc /= len(data_loader)
    d_mean_acc /= len(data_loader)
    #acc, mean_acc, iou, fwavacc = evaluate(predictions_all, gts_all, voc.num_classes)
    img = helper.make_validation_img(output_img.numpy(),
                                    out_label.numpy(),
                                    out_prediction_d.cpu().numpy())
    cv2.imwrite('%s/test_withdepth_%.4f.png'%(save_dir, iou),
                img[:, :, ::-1])


    end_time = time.time()
    metrics = np.asarray(metrics, np.float32)
    return metrics, end_time - start_time, d_iou, d_acc, d_mean_acc
예제 #3
0
def validate(data_loader, data_loader_1, data_loader_2, data_loader_3, net,
             loss, epoch, num_class):
    start_time = time.time()
    net.eval()
    metrics = []
    iou = 0
    acc = 0
    mean_acc = 0

    for batch, batch_1, batch_2, batch_3 in zip(data_loader, data_loader_1,
                                                data_loader_2, data_loader_3):
        data, heatmaps, depth = batch['image'], batch['label'], batch['depth']
        data = data.cuda(async=True)
        heatmaps_0 = heatmaps.cuda(async=True)
        depth = depth.cuda(async=True)
        prediction, loss_output, loss_1, loss_2, loss_3, loss_4 = net(
            data, epoch, depth.squeeze(1), label=heatmaps, train=False)
        prediction = F.interpolate(prediction,
                                   scale_factor=1,
                                   mode='bilinear',
                                   align_corners=False).cpu()

        data, heatmaps, depth = batch_1['image'], batch_1['label'], batch_1[
            'depth']
        data = data.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        depth = depth.cuda(async=True)
        prediction_1, loss_output, loss_1, loss_2, loss_3, loss_4 = net(
            data, epoch, depth.squeeze(1), label=heatmaps, train=False)
        prediction_1 = F.interpolate(prediction_1,
                                     scale_factor=5 / 12,
                                     mode='bilinear',
                                     align_corners=False).cpu()

        data, heatmaps, depth = batch_2['image'], batch_2['label'], batch_2[
            'depth']
        data = data.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        depth = depth.cuda(async=True)
        prediction_2, loss_output, loss_1, loss_2, loss_3, loss_4 = net(
            data, epoch, depth.squeeze(1), label=heatmaps, train=False)
        prediction_2 = F.interpolate(prediction_2,
                                     scale_factor=1.25,
                                     mode='bilinear',
                                     align_corners=False).cpu()

        data, heatmaps, depth = batch_3['image'], batch_3['label'], batch_3[
            'depth']
        data = data.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        depth = depth.cuda(async=True)
        prediction_3, loss_output, loss_1, loss_2, loss_3, loss_4 = net(
            data, epoch, depth.squeeze(1), label=heatmaps, train=False)
        prediction_3 = F.interpolate(prediction_3,
                                     scale_factor=0.625,
                                     mode='bilinear',
                                     align_corners=False).cpu()

        pred = (prediction + prediction_1 + prediction_2 + prediction_3) / 4
        iou_, acc_, mean_acc_ = helper.cal_metric(heatmaps_0.cpu(), pred,
                                                  num_class)
        #iou_1, acc_1, mean_acc_1 = helper.cal_metric(label_list[i], pred_list_1[i], num_class)
        #iou_2, acc_2, mean_acc_2 = helper.cal_metric(label_list[i], pred_list_2[i], num_class)
        iou += iou_
        #iou1 += iou_1
        #iou2 += iou_2
        acc += acc_
        mean_acc += mean_acc_

    iou /= len(data_loader)
    #iou1 /= len(data_loader)
    #iou2 /= len(data_loader)
    acc /= len(data_loader)
    mean_acc /= len(data_loader)
    #img = helper.make_validation_img(batch['image'].numpy(),
    #                                batch['label'].numpy(),
    #                                prediction.cpu().numpy())
    #cv2.imwrite('%s/validate_%d_%.4f.png'%(save_dir, epoch, iou),
    #            img[:, :, ::-1])

    end_time = time.time()

    return end_time - start_time, iou, acc, mean_acc