Beispiel #1
0
def validate_kitti_colorjitter(gpu, model, args, ngpus_per_node, eval_entries, iters=24):
    """ Peform validation using the KITTI-2015 (train) split """
    interval = np.floor(len(eval_entries) / ngpus_per_node).astype(np.int).item()
    if gpu == ngpus_per_node - 1:
        stidx = int(interval * gpu)
        edidx = len(eval_entries)
    else:
        stidx = int(interval * gpu)
        edidx = int(interval * (gpu + 1))
    print("Initialize Instance on Gpu %d, from %d to %d, total %d" % (gpu, stidx, edidx, len(eval_entries)))
    from tqdm import tqdm
    model.eval()
    model.cuda(gpu)
    with torch.no_grad():
        for val_id, entry in enumerate(tqdm(remove_dup(eval_entries[stidx : edidx]))):
            seq, index = entry.split(' ')
            index = int(index)

            if os.path.exists(os.path.join(args.dataset, seq, 'image_02', 'data', "{}.png".format(str(index).zfill(10)))):
                tmproot = args.dataset
            else:
                tmproot = args.odom_root

            img1path = os.path.join(tmproot, seq, 'image_02', 'data', "{}.png".format(str(index).zfill(10)))
            img2path = os.path.join(tmproot, seq, 'image_02', 'data', "{}.png".format(str(index + 1).zfill(10)))

            if not os.path.exists(img2path):
                img2path = img1path

            image1 = frame_utils.read_gen(img1path)
            image2 = frame_utils.read_gen(img2path)

            image1 = np.array(image1).astype(np.uint8)
            image2 = np.array(image2).astype(np.uint8)

            image1 = torch.from_numpy(image1).permute([2, 0, 1]).float()
            image2 = torch.from_numpy(image2).permute([2, 0, 1]).float()

            svfold = os.path.join(args.exportroot, seq, 'image_02')
            svpath = os.path.join(args.exportroot, seq, 'image_02', "{}.png".format(str(index).zfill(10)))
            os.makedirs(svfold, exist_ok=True)

            image1 = image1[None].cuda(gpu)
            image2 = image2[None].cuda(gpu)

            padder = InputPadder(image1.shape, mode='kitti')
            image1, image2 = padder.pad(image1, image2)

            flow_low, flow_pr = model(image1, image2, iters=iters, test_mode=True)
            flow = padder.unpad(flow_pr[0]).cpu()

            frame_utils.writeFlowKITTI(svpath, flow.permute(1, 2, 0).numpy())
            # Image.fromarray(flow_viz.flow_to_image(flow.permute(1, 2, 0).numpy())).show()
            # tensor2rgb(image1 / 255.0, viewind=0).show()
    return
Beispiel #2
0
def create_kitti_submission(model, iters=24, output_path='kitti_submission'):
    """ Create submission for the Sintel leaderboard """
    model.eval()
    test_dataset = datasets.KITTI(split='testing', aug_params=None)

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for test_id in range(len(test_dataset)):
        image1, image2, (frame_id, ) = test_dataset[test_id]
        padder = InputPadder(image1.shape, mode='kitti')
        image1, image2 = padder.pad(image1[None].to(DEVICE),
                                    image2[None].to(DEVICE))

        _, flow_pr = model(image1, image2, iters=iters, test_mode=True)
        flow = padder.unpad(flow_pr[0]).permute(1, 2, 0).cpu().numpy()

        output_filename = os.path.join(output_path, frame_id)
        frame_utils.writeFlowKITTI(output_filename, flow)
Beispiel #3
0
def validate_kitti_colorjitter(model, args, iters=24):
    """ Peform validation using the KITTI-2015 (train) split """
    from tqdm import tqdm
    model.eval()

    expandentries = read_splits_mapping(args)

    for val_id, entry in enumerate(tqdm(expandentries)):
        seq, idx, _ = entry.split(' ')
        idx = int(idx)
        img1path = os.path.join(args.dataset, seq, 'image_02', 'data', "{}.png".format(str(idx).zfill(10)))
        img2path = os.path.join(args.dataset, seq, 'image_02', 'data', "{}.png".format(str(idx + 1).zfill(10)))

        if not os.path.exists(img2path):
            img2path = img1path

        img1 = np.array(Image.open(img1path)).astype(np.float32)
        img2 = np.array(Image.open(img2path)).astype(np.float32)

        img1 = torch.from_numpy(img1).permute([2, 0, 1]).unsqueeze(0)
        img2 = torch.from_numpy(img2).permute([2, 0, 1]).unsqueeze(0)

        svfold = os.path.join(args.exportroot, seq, 'image_02')
        svpath = os.path.join(args.exportroot, seq, 'image_02', "{}.png".format(str(idx).zfill(10)))
        os.makedirs(svfold, exist_ok=True)

        image1 = img1.cuda()
        image2 = img2.cuda()

        padder = InputPadder(image1.shape, mode='kitti')
        image1, image2 = padder.pad(image1, image2)

        flow_low, flow_pr = model(image1, image2, iters=iters, test_mode=True)
        flow = padder.unpad(flow_pr[0]).cpu()

        frame_utils.writeFlowKITTI(svpath, flow.permute(1, 2, 0).numpy())
    return
Beispiel #4
0
def validate_kitti_colorjitter(gpu,
                               model,
                               args,
                               ngpus_per_node,
                               eval_entries,
                               iters=24):
    """ Peform validation using the KITTI-2015 (train) split """
    interval = np.floor(len(eval_entries) / ngpus_per_node).astype(
        np.int).item()
    if gpu == ngpus_per_node - 1:
        stidx = int(interval * gpu)
        edidx = len(eval_entries)
    else:
        stidx = int(interval * gpu)
        edidx = int(interval * (gpu + 1))
    print("Initialize Instance on Gpu %d, from %d to %d, total %d" %
          (gpu, stidx, edidx, len(eval_entries)))
    from tqdm import tqdm
    model.eval()
    model.cuda(gpu)
    with torch.no_grad():
        for val_id, entry in enumerate(
                tqdm(remove_dup(eval_entries[stidx:edidx]))):
            seq, index = entry.split(' ')
            index = int(index)

            img1path = os.path.join(args.dataset, seq,
                                    'rgb_{}.png'.format(str(index).zfill(5)))
            img2path = os.path.join(
                args.dataset, seq,
                'rgb_{}.png'.format(str(index + 1).zfill(5)))

            if not os.path.exists(img2path) and not os.path.exists(
                    img2path.replace('.png', '.jpg')):
                img2path = img1path

            if not os.path.exists(img1path):
                img1path = img1path.replace('.png', '.jpg')
                img2path = img2path.replace('.png', '.jpg')

            image1 = frame_utils.read_gen(img1path)
            image2 = frame_utils.read_gen(img2path)

            image1 = np.array(image1).astype(np.uint8)
            image2 = np.array(image2).astype(np.uint8)

            image1 = torch.from_numpy(image1).permute([2, 0, 1]).float()
            image2 = torch.from_numpy(image2).permute([2, 0, 1]).float()

            svfold = os.path.join(
                args.exportroot,
                seq,
            )
            svpath = os.path.join(args.exportroot, seq,
                                  '{}.png'.format(str(index).zfill(5)))
            os.makedirs(svfold, exist_ok=True)

            image1 = image1[None].cuda(gpu)
            image2 = image2[None].cuda(gpu)

            flow_low, flow_pr = model(image1,
                                      image2,
                                      iters=iters,
                                      test_mode=True)
            flow = flow_pr.squeeze(0)
            flow_numpy = flow.cpu().permute(1, 2, 0).numpy()

            frame_utils.writeFlowKITTI(svpath, flow_numpy)

            if args.dovls:
                vlsflow = Image.fromarray(flow_viz.flow_to_image(flow_numpy))
                vlsrgb1 = tensor2rgb(image1 / 255.0, viewind=0)
                vlsrgb2 = tensor2rgb(image2 / 255.0, viewind=0)

                w, h = vlsrgb2.size
                xx, yy = np.meshgrid(range(w), range(h), indexing='xy')
                xxf = xx.flatten()
                yyf = yy.flatten()
                rndidx = np.random.randint(0, xxf.shape[0], 100)

                xxf_rnd = xxf[rndidx]
                yyf_rnd = yyf[rndidx]

                flowx = flow_numpy[yyf_rnd, xxf_rnd, 0]
                flowy = flow_numpy[yyf_rnd, xxf_rnd, 1]

                fig = plt.figure(figsize=(12, 9))
                plt.subplot(2, 1, 1)
                plt.scatter(xxf_rnd, yyf_rnd, 1, 'r')
                plt.imshow(vlsrgb1)
                plt.subplot(2, 1, 2)
                plt.scatter(flowx + xxf_rnd, flowy + yyf_rnd, 1, 'r')
                plt.imshow(vlsrgb2)
                plt.savefig(
                    os.path.join(
                        "/media/shengjie/disk1/Prediction/nyuv2_flow_vls",
                        "{}_{}.jpg".format(seq,
                                           str(index).zfill(5))))
                plt.close()

                # combined_left = np.concatenate([np.array(vlsrgb1), np.array(vlsrgb2)], axis=0)
                # combined_right = np.concatenate([np.array(vlsflow), np.array(vlsflow)], axis=0)
                # combined = np.concatenate([combined_left, combined_right], axis=1)
                # vls_sv_root = os.makedirs("/media/shengjie/disk1/Prediction/nyuv2_flow_vls", exist_ok=True)
                # Image.fromarray(combined).save(os.path.join("/media/shengjie/disk1/Prediction/nyuv2_flow_vls", "{}_{}.jpg".format(seq, str(index).zfill(5))))

    return