# LayoutVLM 完整Pipeline指南 ## 📋 项目概述 LayoutVLM是一个基于Vision-Language Models的3D场景布局优化系统,能够根据文本描述生成合理的室内布局。 ## 🔄 完整工作流程 ### Pipeline架构 ``` 用户文本描述 ↓ 1. 生成房间规格 (layout_criteria_gen.py) ↓ 2. 生成物体列表 (object_list_gen.py / object_list_gen2.py) ↓ 3. 检索3D Assets (retrive_3d.py) ↓ 4. 生成Scene JSON (benchmark_tasks格式) ↓ 5. LayoutVLM优化布局 (main.py) ↓ 6. 渲染最终场景 (render_final_scene.py) ↓ 最终3D渲染图 ``` --- ## 📝 详细步骤说明 ### Step 1: 生成房间规格和布局标准 **脚本**: `tools/layout_criteria_gen.py` **功能**: 使用Azure OpenAI (o1模型) 根据房间类型生成: - 房间尺寸 (width, length, height) - 布局标准 (layout_criteria) **输出**: `curated_data/{room_type}/{uuid}.json` **示例输出**: ```json { "room_size": { "width": 4.0, "length": 4.0, "height": 2.4, "area": 16.0 }, "layout_criteria": "The layout criteria should position the bed against the center wall with ample access space..." } ``` --- ### Step 2: 生成物体列表 **脚本**: `tools/object_list_gen2.py` **功能**: 根据房间规格和布局标准,使用GPT-4生成需要放置的物体列表 **输入**: - task_description - layout_criteria - room_size **输出**: 物体字典 ```json { "bookshelf": { "count": 4, "types": 1, "description": "A tall bookshelf with multiple shelves..." }, "desk": { "count": 1, "types": 1, "description": "A rectangular wooden desk..." } } ``` --- ### Step 3: 检索3D Assets **脚本**: `tools/retrive_3d.py` **功能**: 使用OpenShape embeddings从Objaverse数据库检索对应的3D模型 **技术栈**: - CLIP模型 (laion/CLIP-ViT-bigG-14-laion2B-39B-b160k) - OpenShape embeddings - 语义相似度检索 **输出**: 每个物体对应的Objaverse UID --- ### Step 4: 生成完整Scene JSON **格式**: 类似 `benchmark_tasks/bedroom/bedroom_2.json` **必需字段**: ```json { "task_description": "a minimalist bedroom...", "layout_criteria": "Position the bed...", "boundary": { "floor_vertices": [[x, y, z], ...], "wall_height": 2.4 }, "assets": { "asset_uid-0": {}, "asset_uid-1": {}, ... } } ``` --- ### Step 5: LayoutVLM布局优化 **脚本**: `main.py` **功能**: 1. 加载scene JSON和3D assets元数据 2. 初始化LayoutVLM solver 3. 使用VLM生成约束代码 4. 通过梯度优化调整物体位置 5. 输出优化后的布局 **关键组件**: - **SandBoxEnv**: 场景环境模拟 - **ConstraintSolver**: 约束求解器 - **VLM Prompts**: 引导模型生成合理约束 **输出**: - `layout.json`: 所有物体的最终位置和旋转 - `final.gif`: 优化过程动画 - `group_0/`: 中间渲染结果 **命令**: ```bash python main.py \ --scene_json_file benchmark_tasks/bedroom/bedroom_2.json \ --asset_dir /path/to/objaverse_processed \ --save_dir ./results/my_scene ``` --- ### Step 6: 渲染最终3D场景 **脚本**: `render_final_scene.py` **功能**: 使用Blender Cycles渲染引擎,将优化后的布局渲染成高质量3D图片 **特性**: - ✅ 渲染所有放置的3D物体 - ✅ 俯视图 (top_down_rendering) - ✅ 侧视图 (side_rendering) - ✅ 物体标注 - ✅ 坐标系可视化 **命令**: ```bash python render_final_scene.py \ --scene_json benchmark_tasks/bedroom/bedroom_2.json \ --layout_json results/my_scene/layout.json \ --asset_dir /path/to/objaverse_processed \ --output_dir results/my_scene/final_rendering ``` **输出**: - `final_scene_top_down.png`: 俯视渲染图 - `final_scene_side_view.png`: 侧视渲染图 --- ## 🚀 快速开始 ### 使用已有场景配置 如果你已经有 `benchmark_tasks` 中的场景配置: ```bash # 1. 运行布局优化 python main.py \ --scene_json_file benchmark_tasks/bookstore/bookstore_2.json \ --save_dir ./results/test_run # 2. 渲染最终场景 python render_final_scene.py \ --scene_json benchmark_tasks/bookstore/bookstore_2.json \ --layout_json results/test_run/layout.json \ --output_dir results/test_run/final_rendering ``` ### 从文本描述开始 (端到端) 如果你想从用户文本开始: ```bash python end_to_end_pipeline.py \ --user_text "a cozy reading nook with bookshelves and comfortable seating" \ --save_dir ./results/reading_nook ``` --- ## 📂 项目结构 ``` LayoutVLM/ ├── main.py # 布局优化主程序 ├── render_final_scene.py # 最终场景渲染脚本 ├── end_to_end_pipeline.py # 端到端pipeline脚本 ├── benchmark_tasks/ # 示例场景配置 │ ├── bedroom/ │ ├── bookstore/ │ └── ... ├── tools/ # 数据生成工具 │ ├── layout_criteria_gen.py # 生成房间规格 │ ├── object_list_gen2.py # 生成物体列表 │ └── retrive_3d.py # 检索3D assets ├── src/layoutvlm/ # LayoutVLM核心代码 │ ├── layoutvlm.py # 主求解器 │ ├── scene.py # 场景定义 │ └── sandbox.py # 沙盒环境 ├── utils/ # 工具函数 │ ├── blender_render.py # Blender渲染 │ └── plot_utils.py # 可视化工具 └── prompts/ # LLM提示词 └── layoutvlm/ ``` --- ## 🔧 配置要求 ### 必需组件 1. **Python 3.10+** 2. **Blender 4.3** (with Python API) 3. **Azure OpenAI Access** - o1_2024-12-17 (layout_criteria生成) - gpt-4o_2024-11-20 (物体列表生成) 4. **Objaverse Processed Assets** - 下载地址: [Holodeck/Objathor](https://github.com/allenai/Holodeck) - 预处理后的目录结构: ``` objaverse_processed/ ├── {uid}/ │ ├── {uid}.glb │ └── data.json ``` ### Python依赖 ```bash pip install -r requirements.txt ``` 主要依赖: - `torch` - 优化引擎 - `transformers` - CLIP模型 - `openai` - Azure OpenAI客户端 - `bpy` - Blender Python API - `trimesh` - 3D网格处理 --- ## 📊 输出说明 ### 布局优化输出 (`results/{save_dir}/`) ``` results/test_run/ ├── layout.json # ✅ 最终布局(物体位置和旋转) ├── final.gif # 优化过程动画 ├── complete_sandbox_program.py # 完整约束代码 └── group_0/ # 第一组物体的优化过程 ├── prompt.txt # LLM输入提示 ├── llm_output_program_0.py # LLM生成的代码 ├── top_down_rendering.png # 俯视图(仅地板) ├── side_rendering_45_3.png # 侧视图(仅地板) ├── out.gif # 优化过程GIF └── temp_0/ # 优化过程帧 ├── frame_0000.png ├── ... └── saved_intermediate_states.json ``` ### 最终渲染输出 (`results/{save_dir}/final_rendering/`) 渲染脚本会生成**两个版本**的图片: ``` final_rendering/ ├── scene_top_down_annotated.png # 📊 带标注的俯视图(包含坐标、物体标签) ├── scene_side_view_annotated.png # 📊 带标注的侧视图 ├── scene_top_down_clean.png # 🎬 干净的俯视图(纯3D场景,无标注) └── scene_side_view_clean.png # 🎬 干净的侧视图(用于最终展示) ``` **版本说明**: - **带标注版本** (`*_annotated.png`): 包含坐标系、物体标签、墙壁标注等,方便调试和分析布局 - **干净版本** (`*_clean.png`): 纯3D渲染,无任何标注,适合最终展示和演示 --- ## 🎯 关键发现 ### 渲染时机说明 1. **优化过程中的渲染** (`group_0/top_down_rendering.png`) - 时机: 在每个group开始优化前 - 内容: **仅包含地板和坐标系**,不包含3D物体 - 目的: 为LLM提供参考图 2. **优化过程的可视化** (`temp_0/frame_*.png`) - 时机: 优化过程中的每一步 - 内容: **2D俯视图**显示物体边界框 - 目的: 监控优化进度 3. **最终场景渲染** (`final_rendering/*.png`) - 时机: 优化完成后手动调用 - 内容: **完整的3D渲染**,包含所有物体 - 目的: 展示最终效果 ### 为什么需要单独的渲染脚本? `main.py` 只输出 `layout.json`,**不会自动渲染最终的3D场景**。需要使用 `render_final_scene.py` 来: 1. 读取 `layout.json` 中的物体位置 2. 加载对应的.glb 3D模型 3. 使用Blender渲染完整场景 --- ## 🐛 常见问题 ### Q1: 渲染图中只有地板,没有3D物体? **原因**: 使用了优化过程中的渲染图,而不是最终渲染 **解决**: 运行 `render_final_scene.py` ### Q2: layout.json 中的坐标是什么坐标系? - X-Y平面是地板 - Z轴是垂直方向(向上) - 原点在房间中心 - 旋转使用度数(degree) ### Q3: 如何调整渲染质量? 修改 `utils/blender_render.py` 中的: - `samples`: 增加采样数(默认128) - `resolution_x/y`: 增加分辨率 - `high_res=True`: 使用高分辨率 ### Q4: 找不到某些assets? 确保: 1. Assets已下载并预处理 2. `asset_dir` 路径正确 3. 每个asset目录包含 `{uid}.glb` 和 `data.json` --- ## 📚 参考资料 - **论文**: [LayoutVLM: Differentiable optimization of 3d layout via vision-language models](https://ai.stanford.edu/~sunfanyun/layoutvlm) - **Objaverse**: https://objaverse.allenai.org/ - **Holodeck**: https://github.com/allenai/Holodeck - **OpenShape**: https://github.com/Colin97/OpenShape --- ## 🎉 总结 完整的Pipeline允许你: 1. ✅ **从文本到3D场景**: 输入文本描述,输出完整的3D渲染图 2. ✅ **灵活的工作流**: 可以在任何步骤介入和定制 3. ✅ **高质量渲染**: 使用Blender Cycles获得照片级渲染 4. ✅ **VLM驱动的布局**: 利用大语言模型的常识进行合理布局 **核心价值**: LayoutVLM不是内容生成系统,而是**布局优化系统**。它假设你已经有了3D assets,然后帮你以合理的方式摆放它们。 --- **作者**: LayoutVLM Project Team **最后更新**: 2025-01-13