1、 基本思路
用手机对一盒泡面环绕拍摄70张图像
对手机图像进行裁剪处理
用目标分割算法从图像中分割出泡面
将处理好的图像送入Colmap计算出每张图像对应的位姿信息,可以参考Colmap重建
将图像和姿态送入Nerf训练生成视频
2、Nerf环境搭建
2.1 创建Conda环境
conda create-n nerf_pytorch python=3.9# 如果环境有问题可以用下面指令删除:conda remove-n nerf_pytorch--all2.2 下载代码并跑通例子
习惯使用pytorch,就下载了pytorch版本的nerf
git clone https://github.com/yenchenlin/nerf-pytorch.git用vscode打开工程,然后选择刚配置的nerf_pytorch环境,然后打开终端,执行:
pip install-r requirements.txt在windows下直接执行download_example_data.sh如果报错可以手动下载数据集,download_example_data.sh内容如下:
wget http://cseweb.ucsd.edu/~viscomp/projects/LF/papers/ECCV20/nerf/tiny_nerf_data.npz mkdir-p data cd data wget http://cseweb.ucsd.edu/~viscomp/projects/LF/papers/ECCV20/nerf/nerf_example_data.zipunzip nerf_example_data.zipcd..直接在浏览器下载tiny_nerf_data.npz放在download_example_data.sh平级目录,然后新建data文件夹,将nerf_example_data.zip放到data文件夹并解压。
然后在vscode终端根目录下执行:
python run_nerf.py--config configs/lego.txt执行报错了
A module that was compiled using NumPy1.x cannot be runinNumPy2.0.2asit may crash.To support both1.xand2.x versions of NumPy,modules must be compiledwithNumPy2.0.Some module may need to rebuild instead e.g.with'pybind11>=2.12'.TypeError:typetorch.cuda.FloatTensornotavailable.TorchnotcompiledwithCUDA enabled.从错误信息上看是numpy版本太高了,并且下载的是cpu版本的torch,pip list检查发现环境中版本如下:
numpy2.0.2torch1.11.0torchvision0.12.0解决方法:
pip uninstall numpy opencv-python torch torchvision-y pip install numpy==1.26.4opencv-python==4.8.1.78torch==1.12.1+cu113 torchvision==0.13.1+cu113-i https://pypi.tuna.tsinghua.edu.cn/simple--extra-index-url https://download.pytorch.org/whl/cu113注意:本机windows安装的是cuda11.8
再次执行:
python run_nerf.py--config configs/lego.txt报错如下:
(nerf_pytorch)PS C:\My_Project\src\nerf\src\nerf-pytorch>python run_nerf.py--config configs/lego.txt Traceback(most recent call last):File"C:\My_Project\src\nerf\src\nerf-pytorch\run_nerf.py",line878,in<module>train()File"C:\My_Project\src\nerf\src\nerf-pytorch\run_nerf.py",line570,intrain images,poses,render_poses,hwf,i_split=load_blender_data(args.datadir,args.half_res,args.testskip)File"C:\My_Project\src\nerf\src\nerf-pytorch\load_blender.py",line41,inload_blender_datawithopen(os.path.join(basedir,'transforms_{}.json'.format(s)),'r')asfp:FileNotFoundError:[Errno2]No suchfileordirectory:'./data/nerf_synthetic/lego\\transforms_train.json'根据报错将.\data\nerf_example_data\nerf_synthetic文件夹直接拷贝到.\data目录下,或者修改configs.lego.txt中的datadir再次执行:
python run_nerf.py--config configs/lego.txt报错如下:
python run_nerf.py--config configs/lego.txt Traceback(most recent call last):File"C:\My_Project\src\nerf\src\nerf-pytorch\run_nerf.py",line878,in<module>train()File"C:\My_Project\src\nerf\src\nerf-pytorch\run_nerf.py",line570,intrain images,poses,render_poses,hwf,i_split=load_blender_data(args.datadir,args.half_res,args.testskip)File"C:\My_Project\src\nerf\src\nerf-pytorch\load_blender.py",line41,inload_blender_datawithopen(os.path.join(basedir,'transforms_{}.json'.format(s)),'r')asfp:FileNotFoundError:[Errno2]No suchfileordirectory:'./data/nerf_synthetic/lego\\transforms_train.json'(nerf_pytorch)PS C:\My_Project\src\nerf\src\nerf-pytorch>python run_nerf.py--config configs/lego.txt C:\Users\lx\.conda\envs\nerf_pytorch\lib\site-packages\torch\cuda\__init__.py:146:UserWarning:NVIDIA GeForce RTX5060TiwithCUDA capability sm_120isnotcompatiblewiththe current PyTorch installation.The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 sm_80 sm_86 compute_37.If you want to use the NVIDIA GeForce RTX5060Ti GPUwithPyTorch,please check the instructions at https://pytorch.org/get-started/locally/warnings.warn(incompatible_device_warn.format(device_name,capability," ".join(arch_list),device_name))解决方法:
pip uninstall torch torchvision torchaudio-y pip install torch==2.0.1torchvision==0.15.2--index-url https://download.pytorch.org/whl/cu118执行训练脚本,正常训练
训练的模型文件存放在:.\logs\blender_paper_lego\200000.tar
验证模型效果:
python run_nerf.py--config configs/lego.txt--render_only--render_test--render_only:核心参数!告诉程序「不训练,只加载已有的模型权重进行渲染」,不会更新模型,纯推理生成结果--render_test:指定渲染测试集视角,共 25 个视角,都是训练时模型没见过的新视角,最能验证模型效果
程序会自动找到logs/lego/model_200000.tar权重文件,无需手动指定路径
验证结果存放在:.\logs\blender_paper_lego\renderonly_test_199999
3、Colmap生成Nerf所需数据
手机图像如下:
结果:稠密重建结果如下:
相关colmap命令
colmap feature_extractor--database_path/root/code/colmap-test/pm43/database.db--image_path/root/code/colmap-test/pm43/images colmap exhaustive_matcher--database_path/root/code/colmap-test/pm43/database.db mkdir/root/code/colmap-test/pm43/sparse colmap mapper--database_path/root/code/colmap-test/pm43/database.db--image_path/root/code/colmap-test/pm43/images--output_path/root/code/colmap-test/pm43/sparse colmap image_undistorter--image_path/root/code/colmap-test/pm43/images--input_path/root/code/colmap-test/pm43/sparse/0--output_path/root/code/colmap-test/pm43/dense--output_type COLMAP colmap patch_match_stereo--workspace_path/root/code/colmap-test/pm43/dense--workspace_format COLMAP--PatchMatchStereo.geom_consistency true colmap stereo_fusion--workspace_path/root/code/colmap-test/pm43/dense--workspace_format COLMAP--input_type geometric--output_path/root/code/colmap-test/pm43/dense/fused.ply4、Nerf基于Colmap结果的重建
先结合训练指令构建custom.txt配置文件:
python run_nerf.py--config configs/custom.txtColmap支持多种数据格式的输入,常用的有LLFF格式和Blender格式,注意结合自己的需求组织相应的训练数据就行了。在这里只需要整理好Colmap的输出和训练配置参数。
从LLFF下载转换程序,调用imgs2poses.py将Colmap结果转换成Nerf能用的数据,这个程序会直接调用Colmap,所以如果Colmap环境没有问题,直接可以通过这个脚本生成Nerf需要的所有数据。
python imgs2poses.py"C:/My_Project/tool/paomian_2048"我比较喜欢直接debug程序,直接修改了源码,修改内容都标记如下图所示,一共不超过10行代码:
修改完成后运行imgs2poses.py代码,这个程序会自动调用Colmap,不需要在人为调用Colmap的GUI处理,这个脚本执行完成后直接生成Nerf所需要的所有文件。
将imgs2poses.py生成的数据直接拷贝到custom目录下,注意custom.txt配置文件中factor = 4需要将images中的2048图像缩小4倍然后存到images_4中。
Nerf训练100000轮效果如下图所示,训练的效果不好,可以加长训练轮数调整参数试试:
GPU消耗如下图所示:
5、工作拓展
可以结合深度学习算法分割出某个场景下不同视图中的目标,然后只对感兴趣的目标进行重建。
6、参考文章
https://zhuanlan.zhihu.com/p/5764165307、拓展文章
NVIDIA Isaac Sim中重建真实场景