Exemple #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
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
Exemple #3
0
def validate(data_loader, net, loss, loss_l1, lamda, beta, epoch):
    start_time = time.time()
    print("IN VALIDATION")
    net.eval()
    metrics = []
    warp_metrics = []
    warp_feature_metrics = []
    iou = 0
    for i, batch in enumerate(tqdm(data_loader, ncols=80, ascii=True)):
        prev, cur, heatmaps, prev_label, flow1, flow2 = batch['prev'], batch[
            'cur'], batch['label'], batch['prev_label'], batch['flow1'], batch[
                'flow2']
        #print("shape:",prev.shape,heatmaps.shape, flow1.shape)
        #print("dtype:",prev.dtype, cur.dtype, heatmaps.dtype, flow1.dtype, flow2.dtype)
        batch_size = len(prev)
        prev = prev.cuda(async=True)
        cur = cur.cuda(async=True)
        heatmaps = heatmaps.cuda(async=True)
        prev_label = prev_label.cuda(async=True).float()
        flow1 = flow1.cuda(async=True)
        flow2 = flow2.cuda(async=True)

        # with torch.no_grad():
        #     pf3,pf4,pf5,pf6,pf7,pf8, prev_prediction = net(prev)
        # cf3,cf4,cf5,cf6,cf7,cf8, cur_prediction = net(cur)

        prev_prediction, prev_feature = net.forward_all(prev)
        prev_global, _ = net.global_feature(prev, prev_label, prev_feature)

        pf3, pf4, pf5, pf6, pf7, pf8 = prev_feature

        cur_prediction, cur_feature = net.forward_all(cur, prev_global)
        cf3, cf4, cf5, cf6, cf7, cf8 = cur_feature

        #H W C N
        warp_prev_output = [
            get_warp_label(flow1[i].permute(1, 2,
                                            0), flow2[i].permute(1, 2, 0),
                           prev_prediction[i].permute(1, 2, 0))
            for i in range(batch_size)
        ]
        warp_prev_output = torch.stack(warp_prev_output, dim=3).permute(
            3, 2, 0, 1)  #float32, cuda,  N 2 512 512

        wf3 = []
        wf4 = []
        wf5 = []
        wf6 = []
        wf7 = []
        wf8 = []

        for i in range(batch_size):
            f1_3 = F.avg_pool2d(flow1[i], (4, 4)) / 4
            f1_4 = F.avg_pool2d(flow1[i], (8, 8)) / 8
            f1_5 = F.avg_pool2d(flow1[i], (16, 16)) / 16
            f1_7 = F.avg_pool2d(flow1[i], (32, 32)) / 32
            f2_3 = F.avg_pool2d(flow2[i], (4, 4)) / 4
            f2_4 = F.avg_pool2d(flow2[i], (8, 8)) / 8
            f2_5 = F.avg_pool2d(flow2[i], (16, 16)) / 16
            f2_7 = F.avg_pool2d(flow2[i], (32, 32)) / 32

            wf3.append(
                get_warp_label(f1_3.permute(1, 2, 0), f2_3.permute(1, 2, 0),
                               pf3[i].permute(1, 2, 0)))
            wf4.append(
                get_warp_label(f1_4.permute(1, 2, 0), f2_4.permute(1, 2, 0),
                               pf4[i].permute(1, 2, 0)))
            wf5.append(
                get_warp_label(f1_5.permute(1, 2, 0), f2_5.permute(1, 2, 0),
                               pf5[i].permute(1, 2, 0)))
            wf6.append(
                get_warp_label(f1_5.permute(1, 2, 0), f2_5.permute(1, 2, 0),
                               pf6[i].permute(1, 2, 0)))
            wf7.append(
                get_warp_label(f1_7.permute(1, 2, 0), f2_7.permute(1, 2, 0),
                               pf7[i].permute(1, 2, 0)))
            wf8.append(
                get_warp_label(f1_7.permute(1, 2, 0), f2_7.permute(1, 2, 0),
                               pf8[i].permute(1, 2, 0)))

        wf3 = torch.stack(wf3, dim=3).permute(3, 2, 0, 1)
        wf4 = torch.stack(wf4, dim=3).permute(3, 2, 0, 1)
        wf5 = torch.stack(wf5, dim=3).permute(3, 2, 0, 1)
        wf6 = torch.stack(wf6, dim=3).permute(3, 2, 0, 1)
        wf7 = torch.stack(wf7, dim=3).permute(3, 2, 0, 1)
        wf8 = torch.stack(wf8, dim=3).permute(3, 2, 0, 1)
        '''
        softmax = nn.Softmax().cuda()
        cur_pre_softmax = softmax(cur_prediction)
        warp_res =softmax(warp_prev_output)
        #l1_loss = loss_L1(cur_pre_softmax, warp_res)
        
        wfm3 = torch.argmax(wf3, dim = 1)
        wfm4 = torch.argmax(wf4, dim = 1)
        wfm5 = torch.argmax(wf5, dim = 1)
        wfm6 = torch.argmax(wf6, dim = 1)
        wfm7 = torch.argmax(wf7, dim = 1)
        wfm8 = torch.argmax(wf8, dim = 1)
        '''

        warp_output_max = torch.argmax(warp_prev_output, dim=1)
        heatmaps = heatmaps[:, 0, :, :]

        ce_loss = loss(cur_prediction, heatmaps)
        warp_output_loss = loss(cur_prediction, warp_output_max)
        wf3_loss = loss_l1(cf3, wf3)
        wf4_loss = loss_l1(cf4, wf4)
        wf5_loss = loss_l1(cf5, wf5)
        wf6_loss = loss_l1(cf6, wf6)
        wf7_loss = loss_l1(cf7, wf7)
        wf8_loss = loss_l1(cf8, wf8)
        wf_loss = wf3_loss + wf4_loss + wf5_loss + wf6_loss + wf7_loss + wf8_loss

        loss_output = ce_loss + lamda * warp_output_loss + beta * wf_loss

        iou += helper.cal_iou(heatmaps, cur_prediction)
        metrics.append(loss_output.item())
        warp_metrics.append(warp_output_loss.item())
        warp_feature_metrics.append(wf_loss.item())

    iou /= len(data_loader)

    img = helper.make_validation_img(batch['cur'].numpy()[:, :, :, :],
                                     batch['label'].numpy(),
                                     cur_prediction.cpu().numpy())
    print("img.shape:", img.shape)
    cv2.imwrite('%s/validate_%d_%.4f.png' % (save_dir, epoch, iou),
                img[:, :, ::-1])

    end_time = time.time()
    metrics = np.asarray(metrics, np.float32)
    warp_metrics = np.asarray(warp_metrics, np.float32)
    warp_feature_metrics = np.asarray(warp_feature_metrics, np.float32)
    return metrics, warp_metrics, warp_feature_metrics, end_time - start_time, iou