| import os |
| import pandas as pd |
| import pyarrow as pa |
| import pyarrow.parquet as pq |
| from tqdm import tqdm |
| import subprocess |
| import gc |
|
|
| def get_inode_count(path): |
| """获取指定路径下的 inode 使用数 (仅限 Linux/macOS)""" |
| try: |
| count = subprocess.check_output(['find', path, '-printf', 'i'], stderr=subprocess.DEVNULL) |
| return len(count) |
| except: |
| return "无法获取 (可能非类Unix系统)" |
|
|
| def convert_trajectory_dataset(src_root, output_parquet, max_samples=None, chunk_size=500): |
| initial_inodes = get_inode_count(src_root) |
| print(f"[*] 初始路径: {src_root}") |
| print(f"[*] 原始目录估算占用 Inodes: {initial_inodes}") |
|
|
| |
| traj_folders = [f for f in os.listdir(src_root) if os.path.isdir(os.path.join(src_root, f))] |
| |
| |
| traj_folders.sort(key=lambda x: int(x) if x.isdigit() else x) |
| |
| |
| if max_samples is not None: |
| traj_folders = traj_folders[:max_samples] |
| print(f"[*] ⚠️ 测试模式开启: 仅处理前 {max_samples} 个轨迹文件夹") |
| else: |
| print(f"[*] 🚀 全量模式开启: 准备处理全部 {len(traj_folders)} 个轨迹...") |
|
|
| |
| writer = None |
| chunk_data = [] |
|
|
| print(f"[*] 采用流式写入,每 {chunk_size} 个轨迹刷新一次内存...") |
| |
| for i, traj_id in enumerate(tqdm(traj_folders)): |
| traj_path = os.path.join(src_root, traj_id) |
| img_files = sorted([f for f in os.listdir(traj_path) if f.endswith('.png')]) |
| |
| images_binary = [] |
| instructions = [] |
| |
| for img_name in img_files: |
| img_path = os.path.join(traj_path, img_name) |
| with open(img_path, 'rb') as f: |
| images_binary.append(f.read()) |
| |
| instruction = img_name.replace('step_', '').replace('.png', '') |
| instructions.append(instruction) |
| |
| chunk_data.append({ |
| 'trajectory_id': traj_id, |
| 'steps': instructions, |
| 'images': images_binary |
| }) |
|
|
| |
| is_last_item = (i + 1) == len(traj_folders) |
| if (i + 1) % chunk_size == 0 or is_last_item: |
| |
| df_chunk = pd.DataFrame(chunk_data) |
| table = pa.Table.from_pandas(df_chunk) |
|
|
| |
| if writer is None: |
| writer = pq.ParquetWriter(output_parquet, table.schema, compression='snappy') |
|
|
| |
| writer.write_table(table) |
|
|
| |
| del chunk_data |
| del df_chunk |
| del table |
| chunk_data = [] |
| gc.collect() |
| |
| |
| if writer is not None: |
| writer.close() |
| |
| |
| print(f"\n[+] 转换完成! 文件已保存至: {output_parquet}") |
| print(f"[*] 当前 Parquet 文件占用 Inode: 1") |
| |
| |
| if isinstance(initial_inodes, int) and max_samples is None: |
| print(f"[!] 全量转换理论节省 Inode 数量: {initial_inodes - 1}") |
| elif max_samples is not None: |
| estimated_saved = sum([len(os.listdir(os.path.join(src_root, d))) for d in traj_folders]) + len(traj_folders) |
| print(f"[!] 本次测试批次理论节省 Inode 数量: {estimated_saved} (包括文件夹与文件)") |
|
|
| if __name__ == "__main__": |
| source_directory = "/home/catlab/Project/JanusVLN-main/data/trajectory_data/R2R-CE-640x480/train" |
| |
| |
| output_dir = os.path.dirname(source_directory) |
| |
| |
| |
| |
| TEST_BATCH_SIZE = None |
| CHUNK_SIZE = 500 |
| |
| if TEST_BATCH_SIZE is not None: |
| output_filename = f"r2r_train_test_{TEST_BATCH_SIZE}.parquet" |
| else: |
| output_filename = "r2r_train_full.parquet" |
| |
| output_filepath = os.path.join(output_dir, output_filename) |
| |
| if os.path.exists(source_directory): |
| print(f"[*] 计划将文件输出至: {output_filepath}") |
| convert_trajectory_dataset( |
| source_directory, |
| output_filepath, |
| max_samples=TEST_BATCH_SIZE, |
| chunk_size=CHUNK_SIZE |
| ) |
| else: |
| print(f"错误: 找不到源路径 {source_directory},请检查当前工作目录。") |