def make_kitti_data_loader(size=(128, 128), norm="norm"): # print ('dataset:',DATASET_PATH) path_dir = os.listdir(PATH_CAM) print(f"path_dir: {path_dir}") ''' ct = 0 for path in path_dir: path_left = os.path.join(PATH_CAM, path , 'image_00/data/') path_right = os.path.join(PATH_CAM, path , 'image_01/data/') if not (os.path.isdir(path_left) and os.path.isdir(path_right)): continue left_filenames = os.listdir(path_left) right_filenames = os.listdir(path_right) for l_path, r_path in zip(left_filenames, right_filenames): left_path = os.path.join(path_left, l_path) right_path = os.path.join(path_right, r_path) if not (os.path.isfile(left_path) and os.path.isfile(right_path)): continue # print ('pre-processing image: ', left_path) F, pts1, pts2 = get_FMat(left_path, right_path) if F is None: continue ct += 1 ''' N = 0 print('path_dir: ', path_dir) for path in path_dir: path_new = os.path.join(PATH_CAM, path, 'image_00/data/') if not os.path.isdir(path_new): continue print('path new: ', path_new) filenames = os.listdir(path_new) N += len(filenames) W, H = size W, H = 1392, 512 # size before rectification @Eason # N = len(filenames) print("How many imgs:", N) N = 2000 print('N: ', N) X = np.zeros( (N, H, W, 2)) # H, W interchanged here since numpy takes H,W as input Y = np.zeros((N, 9)) P1_lst, P2_lst = [], [] kpts_cnt = 1000 # max 100 kpts i = 0 for path in path_dir: path_left = os.path.join(PATH_CAM, path, 'image_00/data/') print(f"path_left: {path_left}") path_right = os.path.join(PATH_CAM, path, 'image_01/data/') print(f"path_right: {path_right}") if not (os.path.isdir(path_left) and os.path.isdir(path_right)): continue left_filenames = os.listdir(path_left) right_filenames = os.listdir(path_right) for l_path, r_path in zip(left_filenames, right_filenames): left_path = os.path.join(path_left, l_path) right_path = os.path.join(path_right, r_path) if not (os.path.isfile(left_path) and os.path.isfile(right_path)): continue print('pre-processing image: ', left_path, 'i: ', i) F, pts1, pts2 = get_FMat( left_path, right_path ) # without method input means get the F_GT from camera parameters if F is None or pts1 is None or pts2 is None: continue left_img = img_prep(left_path, target_size=(W, H)) right_img = img_prep(right_path, target_size=(W, H)) # print ('left image: ', left_img.shape) imgs = np.zeros( (2, H, W)) # H, W interchanged here since numpy takes H,W as input imgs[0, :, :] = left_img imgs[1, :, :] = right_img X[i, :] = np.moveaxis(imgs, [0, 1, 2], [2, 0, 1]) # print ('X shape: ', X[i,:].shape) Y[i, :] = np.resize(F, (1, 9)) # pts1, pts2 = note['kpts'] kpts_cnt = min(len(pts1), kpts_cnt) P1_lst.append(np.array(pts1)) P2_lst.append(np.array(pts2)) i += 1 if i >= N: break if i >= N: break # X = X.astype(np.float32) X, Y = X.astype(np.float32), Y.astype(np.float32) tot_len = len(P1_lst) # cut the data by the length of matching points list's length ? # P1_lst is a [nums of imgs, nums of points per img, 2] X, Y = X[:tot_len, :], Y[:tot_len, :] # # changed here # Normalize F-matrices if norm == "abs": print("[data loader] Use max abs value to normalize the F-matrix") Y = Y / (np.abs(Y).max(axis=1)[:, np.newaxis] + 1e-8) elif norm == "norm": print("[data loader] Use L2 norm to normalize the F-matrix") Y = Y / (np.linalg.norm(Y, axis=1)[:, np.newaxis] + 1e-8) elif norm == "last": print("[data loader] Use last index to normalize the F-matrix") Y = Y / (Y[:, -1].reshape(-1)[np.newaxis, 1] + 1e-8) else: raise Exception("Unrecognized normalization methods:%s" % norm) P1_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P1_lst] P2_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P2_lst] P1, P2 = np.concatenate(P1_lst, axis=0), np.concatenate(P2_lst, axis=0) print(X.shape) print(Y.shape) print(P1.shape) print(P2.shape) X /= 255. # Normalize to [0,1] # return X return (lambda: data_spliter([X, Y, P1, P2]))
def make_aloi_data_loader(size=(128, 128), norm="norm"): filenames = os.listdir(PATH_ALOI) W, H = size # W, H = 768, 576 W, H = 342, 256 N = len(filenames) # N = 10 # testing code for total valid files ct = 0 for i in range(N): left_path = os.path.join(PATH_ALOI, '%s/%s_l.png' % (filenames[i], filenames[i])) right_path = os.path.join(PATH_ALOI, '%s/%s_r.png' % (filenames[i], filenames[i])) if not (os.path.isfile(left_path) and os.path.isfile(right_path)): continue # print ('pre-processing image: ', filenames[i]) F, pts1, pts2 = get_FMat(left_path, right_path) if F is None: continue ct += 1 print('total data: ', N) X = np.zeros( (ct, H, W, 2)) # H, W interchanged here since numpy takes H,W as input Y = np.zeros((ct, 9)) P1_lst, P2_lst = [], [] kpts_cnt = 200 # max 100 kpts ind = 0 for i in range(N): left_path = os.path.join(PATH_ALOI, '%s/%s_l.png' % (filenames[i], filenames[i])) right_path = os.path.join(PATH_ALOI, '%s/%s_r.png' % (filenames[i], filenames[i])) if not (os.path.isfile(left_path) and os.path.isfile(right_path)): continue print('pre-processing image: ', filenames[i]) F, pts1, pts2 = get_FMat(left_path, right_path) if F is None: continue left_img = img_prep(left_path, target_size=(W, H)) right_img = img_prep(right_path, target_size=(W, H)) # print ('left image: ', left_img.shape) imgs = np.zeros( (2, H, W)) # H, W interchanged here since numpy takes H,W as input imgs[0, :, :] = left_img imgs[1, :, :] = right_img X[ind, :] = np.moveaxis(imgs, [0, 1, 2], [2, 0, 1]) # print ('X shape: ', X[i,:].shape) Y[ind, :] = np.resize(F, (1, 9)) # pts1, pts2 = note['kpts'] kpts_cnt = min(len(pts1), kpts_cnt) P1_lst.append(np.array(pts1)) P2_lst.append(np.array(pts2)) ind += 1 X, Y = X.astype(np.float32), Y.astype(np.float32) # Normalize F-matrices if norm == "abs": print("[data loader] Use max abs value to normalize the F-matrix") Y = Y / (np.abs(Y).max(axis=1)[:, np.newaxis] + 1e-8) elif norm == "norm": print("[data loader] Use L2 norm to normalize the F-matrix") Y = Y / (np.linalg.norm(Y, axis=1)[:, np.newaxis] + 1e-8) elif norm == "last": print("[data loader] Use last index to normalize the F-matrix") Y = Y / (Y[:, -1].reshape(-1)[np.newaxis, 1] + 1e-8) else: raise Exception("Unrecognized normalization methods:%s" % norm) P1_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P1_lst] P2_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P2_lst] P1, P2 = np.concatenate(P1_lst, axis=0), np.concatenate(P2_lst, axis=0) # print(X.shape) # print(Y.shape) # print(P1.shape) # print(P2.shape) X /= 255. # Normalize to [0,1] return (lambda: data_spliter([X, Y, P1, P2]))
def make_mvs_data_loader(size=(128, 128), norm="norm"): path_dir = os.listdir(PATH_MVS) N = 0 for path in path_dir: filenames = os.listdir(os.path.join(PATH_MVS, path)) N += len(filenames) W, H = size W, H = 342, 256 # N = 10 X = np.zeros( (N, H, W, 2)) # H, W interchanged here since numpy takes H,W as input Y = np.zeros((N, 9)) P1_lst, P2_lst = [], [] kpts_cnt = 1000 # max 100 kpts i = 0 for path in path_dir: filenames = os.listdir(os.path.join(PATH_MVS, path)) filenames.sort() for j in range( 8): #since there are total of 8 different lighting conditions left_path = os.path.join(PATH_MVS, path, filenames[j]) right_path = os.path.join(PATH_MVS, path, filenames[j + 8]) if not (os.path.isfile(left_path) and os.path.isfile(right_path)): continue print('pre-processing image: ', left_path) F, pts1, pts2 = get_FMat(left_path, right_path) if F is None: continue # print ('F:', F.shape, 'pts1: ', len(pts1), 'pts2: ', len(pts2)) left_img = img_prep(left_path, target_size=(W, H)) right_img = img_prep(right_path, target_size=(W, H)) # print ('left image: ', left_img.shape) imgs = np.zeros( (2, H, W)) # H, W interchanged here since numpy takes H,W as input imgs[0, :, :] = left_img imgs[1, :, :] = right_img X[i, :] = np.moveaxis(imgs, [0, 1, 2], [2, 0, 1]) # print ('X shape: ', X[i,:].shape) Y[i, :] = np.resize(F, (1, 9)) # pts1, pts2 = note['kpts'] kpts_cnt = min(len(pts1), kpts_cnt) P1_lst.append(np.array(pts1)) P2_lst.append(np.array(pts2)) i += 1 if i >= N: break if i >= N: break X, Y = X.astype(np.float32), Y.astype(np.float32) tot_len = len(P1_lst) X, Y = X[:tot_len, :], Y[:tot_len, :] # Normalize F-matrices if norm == "abs": print("[data loader] Use max abs value to normalize the F-matrix") Y = Y / (np.abs(Y).max(axis=1)[:, np.newaxis] + 1e-8) elif norm == "norm": print("[data loader] Use L2 norm to normalize the F-matrix") Y = Y / (np.linalg.norm(Y, axis=1)[:, np.newaxis] + 1e-8) elif norm == "last": print("[data loader] Use last index to normalize the F-matrix") Y = Y / (Y[:, -1].reshape(-1)[np.newaxis, 1] + 1e-8) else: raise Exception("Unrecognized normalization methods:%s" % norm) P1_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P1_lst] P2_lst = [x[:kpts_cnt, :].reshape((1, kpts_cnt, 2)) for x in P2_lst] P1, P2 = np.concatenate(P1_lst, axis=0), np.concatenate(P2_lst, axis=0) # print(X.shape) # print(Y.shape) # print(P1.shape) # print(P2.shape) X /= 255. # Normalize to [0,1] return (lambda: data_spliter([X, Y, P1, P2]))