项目资源:
nerf:https://github.com/bmild/nerf
COLMAP:COLMAP - Structure-From-Motion and Multi-View Stereo
torch:https://download.pytorch.org/whl/torch_stable.html
wget:GNU Wget 1.21.4 for Windows
tips:建议为每个项目配置相应的环境,方便进行依赖库/包的版本管理
一、环境配置
1.1 创建虚拟环境报错
建立环境报错
conda create --name nerf -y python=3.8
网上找了解决方案,大多是【. condarc】文件有问题,尝试解决之后发现仍然无法解决创建虚拟环境报错的问题;灵光闪现,有可能是【梯子】的影响,遂把梯子关了之后成功建立虚拟环境
虚拟环境地址一般在C盘,以我的为例:C:\ProgramData\Miniconda3\envs\nerf
1.2 正确安装cuda版本torch
根据nerf项目的【requirements.txt】安装的torch是没有cuda版本(没有gpu)的。
python
import torch
print(torch.__version__) #查看torch的版本
print(torch.cuda.is_available()) #查看torch是否支持cuda
需要将其在虚拟环境中卸载后重新安装。
pip uninstall torch #用pip安装的torch
conda uninstall torch #用conda安装的torch
下载指定版本的cuda版torch:https://download.pytorch.org/whl/torch_stable.html
torch分cpu版本和cuda版本(可以gpu),常用的库有torchvision和torchaudio,前者处理图像后者处理视频。
demo测试环境,需要运行【download_example_data.sh】文件下载nerf项目自带的数据集,并运行【run_nerf.py】文件。
二、demo测试环境
2.1 .bh文件下载demo数据
(linux:bash download_example_data.sh #下载作者提供的数据)
windows:下载【Git软件:Git中文网 官网】,并安装,安装过程中,其他均默认,只需要确认勾选Git Bash与Git GUI即可。
安装完成后,使用Git Bash运行【.sh】文件:移动至nerf项目【.sh】文件所在目录下,空白处右击鼠标打开Git Bash → 输入sh xxx.sh即可运行文件。(也可以打开Git Bash,然后用cd一步步转到.sh文件所在的目录下,再使用sh xxx.sh命令运行文件)
报错:
$ sh download_example_data.sh
download_example_data.sh: line 1: wget: command not found
download_example_data.sh: line 4: wget: command not found
unzip: cannot find or open nerf_example_data.zip, nerf_example_data.zip.zip or nerf_example_data.zip.ZIP.
解决:没有安装wget工具,下载工具安装即可:wget工具下载【GNU Wget 1.21.4 for Windows】→下载后将wget.exe文件放入安装Git目录下的.\Git\mingw64\bin中。重新运行代码即可。
2.2 运行【run_nerf.py】文件
python run_nerf.py --config configs/fern.txt
报错:运行上述代码报错
TypeError: read() got an unexpected keyword argument 'ignoregamma'
解决:根据错误提示'ignoregamma',找到其在的文件→定位特定错误信息产生行→找到引发错误的库,了解其参数构成→修改如下。即可运行文件,进行训练。
return imageio.imread(f, format="PNG-PIL", ignoregamma=True)
2.3 demo跑通
环境没问题,大成功。
训练自己的数据,需要模仿nerf项目自带的数据形式进行文件的组织(注意命名、路径等信息)。自己创建llff格式数据集,需要利用原始数据在COLMAP上获取位姿信息、相机信息以及图像信息,然后利用llff项目转化为llff格式(.npy),然后设定配置文件进行训练。
三、自己的数据训练
3.1 图像数据储存
C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftruck\images
黑色部分为nerf项目下的路径,找到nerf_llff_data后去创建红色和绿色两部分的文件夹
红色部分为自己建立的项目的名称,名称自拟(不要用中文;配置文件时还会用到)
绿色部分为固定的名称【images】,图像数据储存在images下
3.2 位姿等数据获取(需要进一步理解这一步的工作)
位姿数据获取用到的代码/软件为【COLMAP】,项目地址:
COLMAP - Structure-From-Motion and Multi-View Stereo。
使用可以参考文章:http://t.csdnimg.cn/ePeLH
tips:位姿、相机参数、图片参数等数据获取的时候确认是否每张图片都成功匹配。这会影响到后续转【llff格式】时能否成功。
tips:输入图片尺寸不同导致报错COLMAP只能匹配尺寸相同的图片之间的位姿,因此在进行数据集制作的时候一定注意使用相同分辨率的图片。分辨率的大小对于位姿匹配的准确性有着非常大的影响,分辨率越大,进行匹配时的样本点就越多,匹配的位姿就越准,所以在内存足够的情况下建议使用尽可能大的分辨率。
tips:图片匹配出的位姿较少或匹配到的位姿数目小于图片数目。首先,对于位姿匹配较少的情况,大概率是因为拍摄的数据质量无法满足COLMAP匹配位姿的要求,建议重新拍摄数据,要求拍摄的图片:分辨率尽可能大(2k及以上最好)、尽量不要有模糊、光照均匀(锁定曝光)、光照良好(不能过亮或过暗)其次,对于匹配到的位姿数目小于图片数目时,通常来讲缺少几张是正常的,但是如果这一步有部分图片没有位姿,在后面进行格式转换时可能会出现报错。
3.2 格式转化:位姿等数据转化为llff格式(.npy)
llff格式数据可以将对应图片参数、相机位姿和相机参数简洁有效地存储在一个npy文件中,以方便python读取,且NeRF模型源码拥有直接对LLFF格式数据集进行训练的配置和模块,便于研究者使用。下面将详细展示其制作流程。
项目地址:GitHub - Fyusion/LLFF: Code release for Local Light Field Fusion at SIGGRAPH 2019 。项目下载好后,为其建立一个虚拟环境,运行安装其需要的库(包)。
conda create --name llff -y python=3.8
conda activate llff
pip install -r requirements.txt
【llff】环境下安装【requirements.txt】报错→Sol:将报错的库删除,重新安装【requirements.txt】,之后再单独将报错的库安装下载(可以不指定版本,能去官网找到对应的版本最好)
tips:这一步安装的包下载都很慢,建议直接下载whl或者包,离线安装。这样是最快最省时间的。
运行llff项目下的【imgs2poses.py】文件,参数是自己项目的目录
python imgs2poses.py C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftruck\
tips:cmd执行项目源码的【imgs2poses.py】文件,无法自动打印成功匹配的图像信息。我们可以通过在 LLFF/llff/poses/pose_utils.py 文件的32行左右添加如下代码进行打印:(需要根据信息将没有成功匹配的图像文件手动删除)
for i in np.argsort(names):
print(names[i],end=' ')
3.3 模型训练
nerf环境下运行:
python run_nerf.py --config configs/llfftest.txt
报错:
Mismatch between imgs 0 and poses xxx !!!!
解决——情况1:降采样失败:【nerf】在运行的时候会首先进行下采样,默认是8倍下采样(config配置文件可以设定,factor=xxx),降采样后的图像文件会放在【\nerf-pytorch-master\data\nerf_llff_data\llfftruck\images_8】下,降采样代码不兼容导致降采样失败,在【images_8】文件夹下没有任何的图片生成。
可以手动降采样:原始路径是images,降采样结果路径是images_8
import cv2
# import def_Gaussian as dg
# import time
import os.path
#手动降采样
# import glob
# 读取文件夹里面的图像数量 并返回filenum
def countFile(dir):
# 输入文件夹
tmp = 0
for item in os.listdir(dir):
if os.path.isfile(os.path.join(dir, item)):
tmp += 1
else:
tmp += countFile(os.path.join(dir, item))
return tmp
filenum = countFile(r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images") # 返回的是图片的张数
print(filenum)
# filenum
n = 8
index = 1 # 保存图片编号
num = 0 # 处理图片计数
for i in range(1, filenum + 1):
########################################################
# 1.读取原始图片
if index < 10:
filename = r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images\\" + str(
i) + ".jpg"
elif index < 100:
filename = r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images\\" + str(
i) + ".jpg"
else:
filename = r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images\\" + str(
i) + ".jpg"
print(filename)
original_image = cv2.imread(filename)
# 2.下采样
if n == 4:
img_1 = cv2.pyrDown(original_image)
img_1 = cv2.pyrDown(img_1)
if n == 8:
img_1 = cv2.pyrDown(original_image)
img_1 = cv2.pyrDown(img_1)
img_1 = cv2.pyrDown(img_1)
# 3.将下采样图片保存到指定路径当中
if index < 10:
cv2.imwrite(r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images_8\\" + str(
index) + ".jpg", img_1)
elif index < 100:
cv2.imwrite(r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images_8\\" + str(
index) + ".jpg", img_1)
else:
cv2.imwrite(r"C:\Users\CDX\Desktop\nerf\nerf-pytorch-master\data\nerf_llff_data\llfftest\images_8\\" + str(
index) + ".jpg", img_1)
num = num + 1
print("正在为第" + str(num) + "图片采样......")
index = index + 1
或者修改源码(/nerf-pytorch-maseter/load_llff.py,76行,_minify指令,需要去研究怎样修改):
解决——情况2:位姿匹配数据和图像数量不同:重新完成COLMAP和LLFF,并检查获取的数据和图像数量是否匹配。