Ejemplo n.º 1
0
def disparity_loader(path):
    if '.png' in path:
        data = Image.open(path)
        data = np.ascontiguousarray(data, dtype=np.float32) / 256
        return data
    else:
        return rp.readPFM(path)[0]
Ejemplo n.º 2
0
def disparity_loader(path, flip_disp=False):
    if '.png' in path:
        data = Image.open(path)
        data = np.ascontiguousarray(data, dtype=np.float32) / 256
    elif '.npy' in path:
        data = np.load(path)
    else:
        data = rp.readPFM(path)[0]
    if flip_disp:
        data = np.flipud(data)
    return data
def disparity_loader(path):

    if path.endswith('.png'):
        data = Image.open(path)
        data = np.ascontiguousarray(data,dtype=np.float32)/256
        out = data
    elif path.endswith(".pfm"):
        out = rp.readPFM(path)[0]
    elif path.endswith(".npy"):
        out = np.load(path)
    else:
        raise ValueError("Disparity file is in unrecognized format: " + path)

    for s in out.strides:
        if s < 0:
            out = out.copy()
        break
    return out
Ejemplo n.º 4
0
t.set_cols_width([32, 10, 10, 10, 10, 10, 10])
t.add_row(['Image', 'avgerr', 'rms', 'bad-1', 'bad-2', 'bad-4', 'time'])

for level in levels:
    imgnames = [i for i in glob.glob('%s/*' % args.indir) if os.path.isdir(i)]
    avgerrs = np.zeros(len(imgnames) + 1)
    rmss = np.zeros(len(imgnames) + 1)
    bad5s = np.zeros(len(imgnames) + 1)
    bad10s = np.zeros(len(imgnames) + 1)
    bad20s = np.zeros(len(imgnames) + 1)
    times = np.zeros(len(imgnames) + 1)

    for i, imgname in enumerate(imgnames):
        if not os.path.isdir(imgname):
            continue
        gt = readPFM('%s/%s/disp0GT.pfm' %
                     (args.gtdir, imgname.split('/')[-1]))[0]
        mask = gt != np.inf
        gt_depth = gt.copy()
        gt_depth[mask] = numr / gt_depth[mask]
        mask = mask * (gt_depth > level[0]) * (gt_depth < level[1])
        if imgname.split('/')[-1] in list(blist.keys()):
            for b in blist[imgname.split('/')[-1]]:
                mask[int(b[0][1]):int(b[1][1]),\
                     int(b[0][0]):int(b[1][0])] = 0

        disp = readPFM('%s/disp0%s.pfm' % (imgname, method))[0]
        if disp.shape != gt.shape:
            ratio = float(gt.shape[1]) / disp.shape[1]
            disp = cv2.resize(disp, (gt.shape[1], gt.shape[0])) * ratio

        errmap = np.abs(gt - disp) * mask
Ejemplo n.º 5
0
def disparity_loader(path):
    return rp.readPFM(path)
Ejemplo n.º 6
0
def main():
    processed = get_transform()
    model.eval()

    # save predictions
    out_path = os.path.join("./mb_submission_output", args.name)
    if os.path.exists(out_path):
        raise FileExistsError
    os.mkdir(out_path)

    for (left_img_path, right_img_path, disp_path) in zip(left_val, right_val, disp_val_L):
        print(left_img_path)
        imgL_o = (skimage.io.imread(left_img_path).astype('float32'))[:, :, :3]
        imgR_o = (skimage.io.imread(right_img_path).astype('float32'))[:, :, :3]
        gt_o = readPFM(disp_path)[0]

        imgsize = imgL_o.shape[:2]

        with open(os.path.join(left_img_path[:-7], 'calib.txt')) as f:
            lines = f.readlines()
            max_disp = int(int(lines[6].split('=')[-1]))

        ## change max disp
        tmpdisp = int(max_disp * args.testres // 64 * 64)
        if (max_disp * args.testres / 64 * 64) > tmpdisp:
            model.module.maxdisp = tmpdisp + 64
        else:
            model.module.maxdisp = tmpdisp
        if model.module.maxdisp == 64: model.module.maxdisp = 128
        model.module.disp_reg8 = disparityregression(model.module.maxdisp, 16).cuda()
        model.module.disp_reg16 = disparityregression(model.module.maxdisp, 16).cuda()
        model.module.disp_reg32 = disparityregression(model.module.maxdisp, 32).cuda()
        model.module.disp_reg64 = disparityregression(model.module.maxdisp, 64).cuda()

        # resize
        imgL_o = cv2.resize(imgL_o, None, fx=args.testres, fy=args.testres, interpolation=cv2.INTER_CUBIC)
        imgR_o = cv2.resize(imgR_o, None, fx=args.testres, fy=args.testres, interpolation=cv2.INTER_CUBIC)
        imgL = processed(imgL_o).numpy()
        imgR = processed(imgR_o).numpy()

        imgL = np.reshape(imgL, [1, 3, imgL.shape[1], imgL.shape[2]])
        imgR = np.reshape(imgR, [1, 3, imgR.shape[1], imgR.shape[2]])

        ##fast pad
        max_h = int(imgL.shape[2] // 64 * 64)
        max_w = int(imgL.shape[3] // 64 * 64)
        if max_h < imgL.shape[2]: max_h += 64
        if max_w < imgL.shape[3]: max_w += 64

        top_pad = max_h - imgL.shape[2]
        left_pad = max_w - imgL.shape[3]
        imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0)
        imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0)



        # test
        imgL = torch.FloatTensor(imgL)
        imgR = torch.FloatTensor(imgR)
        wandb.log(
            {"imgL": wandb.Image(imgL, caption=str(imgL.shape)), "imgR": wandb.Image(imgR, caption=str(imgR.shape))})
        imgL = imgL.cuda()
        imgR = imgR.cuda()
        with torch.no_grad():
            torch.cuda.synchronize()
            start_time = time.time()
            pred_disp, entropy = model(imgL, imgR)
            torch.cuda.synchronize()
            ttime = (time.time() - start_time)

        pred_disp = torch.squeeze(pred_disp).data.cpu().numpy()

        top_pad = max_h - imgL_o.shape[0]
        left_pad = max_w - imgL_o.shape[1]
        entropy = entropy[top_pad:, :pred_disp.shape[1] - left_pad].cpu().numpy()
        pred_disp = pred_disp[top_pad:, :pred_disp.shape[1] - left_pad]

        # save predictions
        idxname = left_img_path.split('/')[-2]
        if not os.path.exists('%s/%s'%(out_path,idxname)):
            os.makedirs('%s/%s'%(out_path,idxname))
        idxname = '%s/disp0HSM'%(idxname)

        with open('%s/%s.pfm'% (out_path, idxname),'w') as f:
            save_pfm(f,pred_disp[::-1,:])
        with open('%s/%s/timeHSM.txt'%(out_path,idxname.split('/')[0]),'w') as f:
             f.write(str(ttime))

        # resize to highres
        pred_disp = cv2.resize(pred_disp / args.testres, (imgsize[1], imgsize[0]), interpolation=cv2.INTER_LINEAR)

        # clip while keep inf
        invalid = np.logical_or(pred_disp == np.inf, pred_disp != pred_disp)
        pred_disp[invalid] = np.inf

        pred_disp_png = (pred_disp * 256).astype('uint16')
        cv2.imwrite(os.path.join(out_path, idxname.split('/')[0] + ".png"), pred_disp_png)
        entropy_png = (entropy * 256).astype('uint16')
        # cv2.imwrite(os.path.join(out_dir, img_name), entropy_png)

        wandb.log({"disp": wandb.Image(pred_disp_png, caption=str(pred_disp_png.shape)),
                   "entropy": wandb.Image(entropy_png, caption=str(entropy_png.shape))})

        metrics = get_metrics(gt_o, pred_disp, max_disp)

        for (key, val) in metrics.items():
            if key not in score_avg.keys():
                score_avg[key] = []
            score_avg[key].append(val)

        torch.cuda.empty_cache()

    for (key, val) in score_avg.items():
        score_avg[key] = mean(score_avg[key])

    print(score_avg)
    with open(os.path.join(out_path, "metrrics.txt")) as file:
        file.write(str(score_avg))
Ejemplo n.º 7
0
    def __getitem__(self, item):
        index = item
        for (key, val) in self.stereo_pair_subfolders.items():
            if index >= len(val):
                index = index - len(val)

                if index < 0:
                    raise ValueError(
                        "Something went wrong during index calculation")

            else:
                break
        folder = key
        img_name = val[index]
        dataset_type = -1
        if self.variable_testres and self.testres == -1:
            if "middlebury" in img_name.lower():  # Middlebury
                dataset_type = 0
                testres = self.middlebury_testres
            elif "kitti" in img_name.lower():  # Kitti
                dataset_type = 2
                testres = self.kitti_testres
            elif "eth3d" in img_name.lower(
            ):  # ETH: Gengsahn said it's between 3~4. Find with linear grid search
                dataset_type = 1
                testres = self.eth_testres
            else:
                raise ValueError(
                    "name of the folder does not contain any of: kitti, middlebury, eth3d"
                )
        else:
            testres = self.testres

        im0_path = os.path.join(folder, img_name, 'im0.png')
        im1_path = os.path.join(folder, img_name, 'im1.png')
        imgL_o = (skimage.io.imread(im0_path).astype('float32')
                  )[:, :, :3]  # (375, 1242, 3)
        imgR_o = (skimage.io.imread(im1_path).astype('float32'))[:, :, :3]
        original_image_size = imgL_o.shape[:2]

        gt_path = os.path.join(folder, img_name, "disp0GT.pfm")
        gt_disp_raw = readPFM(gt_path)[0].copy(
        )  # hotfix to prevent error when converting np.array with negative stride to Tensor
        # [375 x 1242]
        # np.inf == 353307
        # np.NINF == 0

        path_to_replace = os.path.basename(os.path.normpath(im0_path))
        with open(im0_path.replace(path_to_replace, 'calib.txt')) as f:
            lines = f.readlines()
            max_disp = int(int(lines[6].split('=')[-1]))

        imgL_o = cv2.resize(imgL_o,
                            None,
                            fx=testres,
                            fy=testres,
                            interpolation=cv2.INTER_CUBIC)  # [675 x 2236]
        imgR_o = cv2.resize(imgR_o,
                            None,
                            fx=testres,
                            fy=testres,
                            interpolation=cv2.INTER_CUBIC)
        #gt_disp_raw = cv2.resize(gt_disp_raw, None, fx=testres, fy=testres, interpolation=cv2.INTER_CUBIC) # [675 x 2236]
        # print(dict(zip(*np.unique(gt_disp_raw, return_counts=True)))[np.inf])
        # print(dict(zip(*np.unique(gt_disp_raw, return_counts=True)))[np.NINF])
        # [675, 2236] min=4.5859375, max=inf

        # if self.debug:
        #     input_img_path = '%s/%s/' % (self.args.name, img_name)
        #     assert (cv2.imwrite(input_img_path + "imgR.png", imgR_o))
        #     assert (cv2.imwrite(input_img_path + "imgL.png", imgL_o))

        imgL = self.transform(imgL_o).numpy()
        imgR = self.transform(imgR_o).numpy()

        imgL = np.reshape(imgL, [3, imgL.shape[1], imgL.shape[2]])
        imgR = np.reshape(imgR, [3, imgR.shape[1], imgR.shape[2]])

        ##fast pad
        max_h = int(imgL.shape[1] // 64 * 64)
        max_w = int(imgL.shape[2] // 64 * 64)
        if max_h < imgL.shape[1]: max_h += 64
        if max_w < imgL.shape[2]: max_w += 64

        top_pad = max_h - imgL.shape[1]
        left_pad = max_w - imgL.shape[2]
        imgL = np.lib.pad(imgL, ((0, 0), (top_pad, 0), (0, left_pad)),
                          mode='constant',
                          constant_values=0)
        imgR = np.lib.pad(imgR, ((0, 0), (top_pad, 0), (0, left_pad)),
                          mode='constant',
                          constant_values=0)

        # test
        imgL = torch.FloatTensor(imgL)
        imgR = torch.FloatTensor(imgR)

        return (imgL, imgR, gt_disp_raw, max_disp, original_image_size,
                top_pad, left_pad, testres, dataset_type,
                os.path.join(folder, img_name))