用pytorch搭建三层神经网络 (用PyTorch从零开局构建完整的NeRF模型)

用PyTorch从零开局构建完整的NeRF模型


在解释代码之前,首先对NeRF(神经辐射场)的原理与含意启动繁难回忆。而NeRF论文中是这样解释NeRF算法流程的:


咱们提出了一个以后最优的方法,运行于复杂场景下分解新视图的义务,详细的成功原理是经常使用一个稠密的输入视图汇合,而后一直优化底层的延续体素场景函数。咱们的算法,经常使用一个全衔接(非卷积)的深度网络,表示一个场景,这个深度网络的输入是一个独自的5D坐标(空间位置(x,y,z)和视图方向(xita,sigma)),其对应的输入则是体素密度和视图关联的辐射向量。咱们经过查问沿着相机射线的5D坐标分解新的场景视图,以及经过经常使用经典的体素渲染技术将输入色彩和密度投射到图像中。由于体素渲染具备自然的可变性,所以优化咱们的表示方法所需的惟一输入就是一组已知相机位姿的图像。咱们引见如何高效优化神经辐射场照度,以渲染具备复杂几何状态和外观的真切陈腐视图,并展现了由于之前神经渲染和视图分解上班的结果。


基于前文的原理,本节开局讲述详细的代码成功。首先,导入算法须要的Python库文件。

importosfromtypingimportOptional,Tuple,List,Union,Callableimportnumpyasnpimporttorchfromtorchimportnnimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportaxes3dfromtqdmimporttrange#设置GPU还是CPU设施device=torch.device('cuda'iftorch.cuda.is_available()else'cpu')


依据关系论文中的引见可知,NeRF的输入是一个蕴含空间位置坐标与视图方向的5D坐标。但是,在PyTorch构建NeRF环节中经常使用的数据集只是普通的3D到2D图像数据集,蕴含拍摄相机的内参:位姿和焦距。因此在前面的操作中,咱们会把输入数据集转为算法模型须要的输入方式。


在这一流程中经常使用乐高推土机图像作为繁难NeRF算法的数据集,如图2所示:(详细的数据链接请在文末检查)


▲图2|乐高推土机数据集©️【深蓝AI】


这项上班中经常使用的小型乐高数据集由106幅乐高推土机的图像组成,并配有位姿数据和罕用焦距数值。与其余数据集一样,这里保管前100张图像用于训练,并保管一张测试图像用于验证,详细的加载数据操作如下:

data=np.load('tiny_nerf_data.npz')#加载数据集images=>


回忆NeRF关系论文,本次代码成功须要的输入是一个独自的5D坐标(空间位置和视图方向)。因此,咱们须要针对上方经常使用的小型乐高数据做一个处置操作。


普通而言,为了搜集这些特点输入数据,算法中须要对输入图像启动反渲染操作。详细来讲就是经过每个像素点在三维空间中绘制投影线,并从中提取样本。


要从图像以外的三维空间采样输入数据点,首先就得从乐高照片集中失掉每台相机的初始位姿,而后经过一些矢量数学运算,将这些4×4姿态矩阵转换成「表示原点的三维坐标和表示方向的三维矢量」——这两类消息最终会结合起来形容一个矢量,该矢量用以表征拍摄照片时相机的指向。


下列代码则正是经过绘制箭头来形容这一操作,箭头表示每一帧图像的原点和方向:

#方向数据dirs=np.stack([np.sum([0,0,-1]*pose[:3,:3],axis=-1)forposeinposes])#原点数据origins=poses[:,:3,-1]#绘图的设置ax=plt.figure(figsize=(12,8)).add_subplot(projectinotallow='3d')_=ax.quiver(origins[...,0].flatten(),origins[...,1].flatten(),origins[...,2].flatten(),dirs[...,0].flatten(),dirs[...,1].flatten(),dirs[...,2].flatten(),length=0.5,normalize=True)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('z')plt.show()


最终绘制出来的箭头结果如下图所示:


▲图3|采样点相机拍摄指向©️【深蓝AI】


当有了这些相机位姿数据之后,咱们就可以沿着图像的每个像素找到投影线,而每条投影线都是由其原点(x,y,z)和方向联结定义。其中每个像素的原点或许相反,但方向普通是不同的。这些方向射线都稍微偏离中心,因此不会存在两条平行方向线,如下图所示:


依据图4所述的原理,咱们就可以确定每条射线的方向和原点,关系代码如下:

defget_rays(height:int,#图像高度width:int,#图像宽带focal_length:float,#焦距c2w:torch.Tensor)->Tuple[torch.Tensor,torch.Tensor]:"""经过每个像素和相机原点,找到射线的原点和方向。"""#运行针孔相机模型搜集每个像素的方向i,j=torch.meshgrid(torch.arange(width,dtype=torch.float32).to(c2w),torch.arange(height,dtype=torch.float32).to(c2w),)i,j=i.transpose(-1,-2),j.transpose(-1,-2)#方向数据directions=torch.stack([(i-width*.5)/focal_length,-(j-height*.5)/focal_length,-torch.ones_like(i)],dim=-1)#用相机位姿求出方向rays_d=torch.sum(directions[...,None,:]*c2w[:3,:3],dim=-1)#自动一切射线原点相反rays_o=c2w[:3,-1].expand(rays_d.shape)returnrays_o,rays_d


失掉每个像素对应的射线的方向数据和原点数据之后,就能够取得了NeRF算法中须要的五维数据输入,上方将这些数据调整为算法输入的格局:

#转为PyTorch的tensorimages=torch.from_numpy(data['images'][:n_training]).to(device)poses=torch.from_numpy(data['poses']).to(device)focal=torch.from_numpy(data['focal']).to(device)testimg=torch.from_numpy(data['images'][testimg_idx]).to(device)testpose=torch.from_numpy(data['poses'][testimg_idx]).to(device)#针对每个图像失掉射线height,width=images.shape[1:3]withtorch.no_grad():ray_origin,ray_direction=get_rays(height,width,focal,testpose)print('RayOrigin')print(ray_origin.shape)print(ray_origin[height//2,width//2,:])print('')print('RayDirection')print(ray_direction.shape)print(ray_direction[height//2,width//2,:])print('')


当算法输入模块有了NeRF算法须要的输入数据,也就是蕴含原点和方向向量组合的线条时,就可以在线条上启动采样。这一环节是驳回从粗到细的采样战略,即分层采样战略。


详细来说,分层采样就是将光线分红平均散布的小块,接着在每个小块内随机抽样。其中扰动的设置选择了是平均取样的,还是间接繁难经常使用分区中心作为采样点。详细操作代码如下所示:

#采样函数定义defsample_stratified(rays_o:torch.Tensor,#射线原点rays_d:torch.Tensor,#射线方向near:float,far:float,n_samples:int,#采样数量perturb:Optional[bool]=True,#扰动设置inverse_depth:bool=False#反向深度)->Tuple[torch.Tensor,torch.Tensor]:"""从规定的bin中沿着射线启动采样。"""#沿着射线抓取采样点t_vals=torch.linspace(0.,1.,n_samples,device=rays_o.device)ifnotinverse_depth:#由远到近线性采样z_vals=near*(1.-t_vals)+far*(t_vals)else:#在反向深度中线性采样z_vals=1./(1./near*(1.-t_vals)+1./far*(t_vals))#沿着射线从bins中一致采样ifperturb:mids=.5*(z_vals[1:]+z_vals[:-1])upper=torch.concat([mids,z_vals[-1:]],dim=-1)lower=torch.concat([z_vals[:1],mids],dim=-1)t_rand=torch.rand([n_samples],device=z_vals.device)z_vals=lower+(upper-lower)*t_randz_vals=z_vals.expand(list(rays_o.shape[:-1])+[n_samples])#运行相应的缩放参数pts=rays_o[...,None,:]+rays_d[...,None,:]*z_vals[...,:,None]returnpts,z_vals


接着就到了对这些采样点做可视化剖析的步骤。如图5中所述,未受扰动的蓝色点是bin的中心,而红点对应扰动点的采样。请留意,红点与上方的蓝点略有偏移,但一切点都在远近采样设定值之间。详细代码如下:

y_vals=torch.zeros_like(z_vals)#调用采样战略函数_,z_vals_unperturbed=sample_stratified(rays_o,rays_d,near,far,n_samples,perturb=False,inverse_depth=inverse_depth)#绘图关系plt.plot(z_vals_unperturbed[0].cpu().numpy(),1+y_vals[0].cpu().numpy(),'b-o')plt.plot(z_vals[0].cpu().numpy(),y_vals[0].cpu().numpy(),'r-o')plt.ylim([-1,2])plt.title('StratifiedSampling(blue)withPerturbation(red)')ax=plt.gca()ax.axes.yaxis.set_visible(False)plt.grid(True)


▲图5|采样结果示用意©️【深蓝AI】


与Transformer一样,NeRF也经常使用了位置编码器。因此NeRF就须要借助位置编码器将输入映射到更高的频率空间,以补偿神经网络在学习低频函数时的偏向。


这一环节将会为位置编码器建设一个繁难的torch.nn.Module模块,相反的编码器可同时用于对输入样本和视图方向的编码操作。留意,这些输入被指定了不同的参数。代码如下所示:

#位置编码类classPositionalEncoder(nn.Module):"""对输入点,做sine或许consine位置编码。"""def__init__(self,d_input:int,n_freqs:int,log_space:bool=False):super().__init__()self.d_input=d_inputself.n_freqs=n_freqsself.log_space=log_spaceself.d_output=d_input*(1+2*self.n_freqs)self.embed_fns=[lambdax:x]#定义线性或许log尺度的频率ifself.log_space:freq_bands=2.**torch.linspace(0.,self.n_freqs-1,self.n_freqs)else:freq_bands=torch.linspace(2.**0.,2.**(self.n_freqs-1),self.n_freqs)#交流sin和cosforfreqinfreq_bands:self.embed_fns.append(lambdax,freq=freq:torch.sin(x*freq))self.embed_fns.append(lambdax,freq=freq:torch.cos(x*freq))defforward(self,x)->torch.Tensor:"""实践经常使用位置编码的函数。"""returntorch.concat([fn(x)forfninself.embed_fns],dim=-1)


在此,定义一个NeRF模型——关键由线性层模块列表导致,而列表中进一步蕴含非线性激活函数和残差衔接。该模型有一个可选的视图方向输入,假设在实例化时提供详细的方向消息,那么会扭转模型结构。


(本成功基于原始论文NeRF:RepresentingScenesasNeuralRadianceFieldsforViewSynthesis的第3节,并经常使用相反的自动设置)

#定义NeRF模型classNeRF(nn.Module):"""神经辐射场模块。"""def__init__(self,d_input:int=3,n_layers:int=8,d_filter:int=256,skip:Tuple[int]=(4,),d_viewdirs:Optional[int]=None):super().__init__()self.d_input=d_input#输入self.skip=skip#残差衔接self.act=nn.functional.relu#激活函数self.d_viewdirs=d_viewdirs#视图方向#创立模型的层结构self.layers=nn.ModuleList([nn.Linear(self.d_input,d_filter)]+[nn.Linear(d_filter+self.d_input,d_filter)ifiinskip\elsenn.Linear(d_filter,d_filter)foriinrange(n_layers-1)])#Bottleneck层ifself.d_viewdirsisnotNone:#假设经常使用视图方向,分别alpha和RGBself.alpha_out=nn.Linear(d_filter,1)self.rgb_filters=nn.Linear(d_filter,d_filter)self.branch=nn.Linear(d_filter+self.d_viewdirs,d_filter//2)self.output=nn.Linear(d_filter//2,3)else:#假设不经常使用试图方向,则繁难输入self.output=nn.Linear(d_filter,4)defforward(self,x:torch.Tensor,viewdirs:Optional[torch.Tensor]=None)->torch.Tensor:r"""带有视图方向的前向流传"""#判别能否设置视图方向ifself.d_viewdirsisNoneandviewdirsisnotNone:raiseValueError('Cannotinputx_directionifd_viewdirswasnotgiven.')#运转bottleneck层之前的网络层x_input=xfori,layerinenumerate(self.layers):x=self.act(layer(x))ifiinself.skip:x=torch.cat([x,x_input],dim=-1)#运转bottleneckifself.d_viewdirsisnotNone:#Splitalphafromnetworkoutputalpha=self.alpha_out(x)#结果传入到rgb过滤器x=self.rgb_filters(x)x=torch.concat([x,viewdirs],dim=-1)x=self.act(self.branch(x))x=self.output(x)#拼接alpha一同作为输入x=torch.concat([x,alpha],dim=-1)else:#不拼接,繁难输入x=self.output(x)returnx


上方失掉NeRF模型的输入结果之后,仍需将NeRF的输入转换成图像。也就是经过渲染模块对每个像素沿光线方向的一切样本启动加权求和,从而失掉该像素的预计色彩值,此外每个RGB样本都会依据其Alpha值启动加权。其中Alpha值越高,标明采样区域不透明的或许性越大,因此沿射线方向越远的点越有或许被遮挡,累加乘积可确保更远处的点遭到克服。详细代码如下:

#体积渲染defcumprod_exclusive(tensor:torch.Tensor)->torch.Tensor:"""(Courtesyof和tf.math.cumprod(...,exclusive=True)配置相似参数:tensor(torch.Tensor):Tensorwhosecumprod(cumulativeproduct,see`torch.cumprod`)alongdim=-1istobecomputed.前往值:cumprod(torch.Tensor):cumprodofTensoralongdim=-1,mimicikingthefunctionalityoftf.math.cumprod(...,exclusive=True)(see`tf.math.cumprod`fordetails)."""#首先计算规定的cunprodcumprod=torch.cumprod(tensor,-1)cumprod=torch.roll(cumprod,1,-1)#用1交流首个元素cumprod[...,0]=1.returncumprod#输入到图像的函数defraw2outputs(raw:torch.Tensor,z_vals:torch.Tensor,rays_d:torch.Tensor,raw_noise_std:float=0.0,white_bkgd:bool=False)->Tuple[torch.Tensor,torch.Tensor,torch.Tensor,torch.Tensor]:"""将NeRF的输入转换为RGB输入。"""#沿着`z_vals`轴元素之间的差值.dists=z_vals[...,1:]-z_vals[...,:-1]dists=torch.cat([dists,1e10*torch.ones_like(dists[...,:1])],dim=-1)#将每个距离乘以相应方向射线的法线,转换为理想环球中的距离(思考非单位方向)。dists=dists*torch.norm(rays_d[...,None,:],dim=-1)#为模型预测密度增加噪音。可用于在训练环节中对网络启动正则化(防止发生浮点伪影)。noise=0.ifraw_noise_std>0.:noise=torch.randn(raw[...,3].shape)*raw_noise_std#Predictdensityofeachsamplealongeachray.Highervaluesimply#higherlikelihoodofbeingabsorbedatthispoint.[n_rays,n_samples]alpha=1.0-torch.exp(-nn.functional.relu(raw[...,3]+noise)*dists)#预测每条射线上每个样本的密度。数值越大,表示该点被排汇的或许性越大。[n_射线,n_样本]weights=alpha*cumprod_exclusive(1.-alpha+1e-10)#计算RGB图的权重。rgb=torch.sigmoid(raw[...,:3])#[n_rays,n_samples,3]rgb_map=torch.sum(weights[...,None]*rgb,dim=-2)#[n_rays,3]#预计预测距离的深度图。depth_map=torch.sum(weights*z_vals,dim=-1)#稠密图disp_map=1./torch.max(1e-10*torch.ones_like(depth_map),depth_map/torch.sum(weights,-1))#沿着每条射线加权。acc_map=torch.sum(weights,dim=-1)#要分解到红色背景上,请经常使用累积的alpha贴图。ifwhite_bkgd:rgb_map=rgb_map+(1.-acc_map[...,None])returnrgb_map,depth_map,acc_map,weights


理想上,三维空间中的遮挡物十分稠密,因此大少数点对渲染图像的奉献不大。所以,对积分有奉献的区域启动超采样会有更好的成果。这里,笔者对第一组样本运行基于归一化的权重来创立整个光线的概率密度函数,而后对该密度函数运行反变换采样来搜集第二组样本。详细代码如下:

#采样概率密度函数defsample_pdf(bins:torch.Tensor,weights:torch.Tensor,n_samples:int,perturb:bool=False)->torch.Tensor:"""运行反向转换采样到一组加权点。"""#正则化权重失掉概率密度函数。pdf=(weights+1e-5)/torch.sum(weights+1e-5,-1,keepdims=True)#[n_rays,weights.shape[-1]]#将概率密度函数转为累计散布函数。cdf=torch.cumsum(pdf,dim=-1)#[n_rays,weights.shape[-1]]cdf=torch.concat([torch.zeros_like(cdf[...,:1]),cdf],dim=-1)#[n_rays,weights.shape[-1]+1]#从累计散布函数中提取样本位置。perturb==0时为线性。ifnotperturb:u=torch.linspace(0.,1.,n_samples,device=cdf.device)u=u.expand(list(cdf.shape[:-1])+[n_samples])#[n_rays,n_samples]else:u=torch.rand(list(cdf.shape[:-1])+[n_samples],device=cdf.device)#[n_rays,n_samples]#沿累计散布函数找出u值所在的索引。u=u.contiguous()#前往具备相反值的延续张量。inds=torch.searchsorted(cdf,u,right=True)#[n_rays,n_samples]#夹住超出范围的索引。below=torch.clamp(inds-1,min=0)above=torch.clamp(inds,max=cdf.shape[-1]-1)inds_g=torch.stack([below,above],dim=-1)#[n_rays,n_samples,2]#从累计散布函数和相应的bin中心取样。matched_shape=list(inds_g.shape[:-1])+[cdf.shape[-1]]cdf_g=torch.gather(cdf.unsqueeze(-2).expand(matched_shape),dim=-1,index=inds_g)bins_g=torch.gather(bins.unsqueeze(-2).expand(matched_shape),dim=-1,index=inds_g)#将样本转换为射线长度。denom=(cdf_g[...,1]-cdf_g[...,0])denom=torch.where(denom<1e-5,torch.ones_like(denom),denom)t=(u-cdf_g[...,0])/denomsamples=bins_g[...,0]+t*(bins_g[...,1]-bins_g[...,0])returnsamples#[n_rays,n_samples]


此时应将上方一切内容整合在一同,经过模型计算一次性前向传递。


由于潜在的内存疑问,前向传递以块为单位启动计算,而后汇总到一个批次中。梯度流传是在整个批次处置终了后启动的,因此有块和批次之分。关于内存弛缓环境来说,分块处置尤为关键,由于该环境下提供的资源比原始论文中援用的资源更为有限。详细代码如下所示:

defget_chunks(inputs:torch.Tensor,chunksize:int=2**15)->List[torch.Tensor]:"""输入分块。"""return[inputs[i:i+chunksize]foriinrange(0,inputs.shape[0],chunksize)]defprepare_chunks(points:torch.Tensor,encoding_function:Callable[[torch.Tensor],torch.Tensor],chunksize:int=2**15)->List[torch.Tensor]:"""对点启动编码和分块,为NeRF模型做好预备。"""points=points.reshape((-1,3))points=encoding_function(points)points=get_chunks(points,chunksize=chunksize)returnpointsdefprepare_viewdirs_chunks(points:torch.Tensor,rays_d:torch.Tensor,encoding_function:Callable[[torch.Tensor],torch.Tensor],chunksize:int=2**15)->List[torch.Tensor]:r"""对视图方向启动编码和分块,为NeRF模型做好预备。"""viewdirs=rays_d/torch.norm(rays_d,dim=-1,keepdim=True)viewdirs=viewdirs[:,None,...].expand(points.shape).reshape((-1,3))viewdirs=encoding_function(viewdirs)viewdirs=get_chunks(viewdirs,chunksize=chunksize)returnviewdirsdefnerf_forward(rays_o:torch.Tensor,rays_d:torch.Tensor,near:float,far:float,encoding_fn:Callable[[torch.Tensor],torch.Tensor],coarse_model:nn.Module,kwargs_sample_stratified:dict=None,n_samples_hierarchical:int=0,kwargs_sample_hierarchical:dict=None,fine_model=None,viewdirs_encoding_fn:Optional[Callable[[torch.Tensor],torch.Tensor]]=None,chunksize:int=2**15)->Tuple[torch.Tensor,torch.Tensor,torch.Tensor,dict]:"""计算一次性前向流传"""#设置参数ifkwargs_sample_stratifiedisNone:kwargs_sample_stratified={}ifkwargs_sample_hierarchicalisNone:kwargs_sample_hierarchical={}#沿着每条射线的样本查问点。query_points,z_vals=sample_stratified(rays_o,rays_d,near,far,**kwargs_sample_stratified)#预备批次。batches=prepare_chunks(query_points,encoding_fn,chunksize=chunksize)ifviewdirs_encoding_fnisnotNone:batches_viewdirs=prepare_viewdirs_chunks(query_points,rays_d,viewdirs_encoding_fn,chunksize=chunksize)else:batches_viewdirs=[None]*len(batches)#稠密模型流程。predictions=[]forbatch,batch_viewdirsinzip(batches,batches_viewdirs):predictions.append(coarse_model(batch,viewdirs=batch_viewdirs))raw=torch.cat(predictions,dim=0)raw=raw.reshape(list(query_points.shape[:2])+[raw.shape[-1]])#口头可微分体积渲染,从新分解RGB图像。rgb_map,depth_map,acc_map,weights=raw2outputs(raw,z_vals,rays_d)outputs={'z_vals_stratified':z_vals}ifn_samples_hierarchical>0:#Savepreviousoutputstoreturn.rgb_map_0,depth_map_0,acc_map_0=rgb_map,depth_map,acc_map#对精细查问点启动分层抽样。query_points,z_vals_combined,z_hierarch=sample_hierarchical(rays_o,rays_d,z_vals,weights,n_samples_hierarchical,**kwargs_sample_hierarchical)#像以前一样预备输入。batches=prepare_chunks(query_points,encoding_fn,chunksize=chunksize)ifviewdirs_encoding_fnisnotNone:batches_viewdirs=prepare_viewdirs_chunks(query_points,rays_d,viewdirs_encoding_fn,chunksize=chunksize)else:batches_viewdirs=[None]*len(batches)#经过精细模型向前传递新样本。fine_model=fine_modeliffine_modelisnotNoneelsecoarse_modelpredictions=[]forbatch,batch_viewdirsinzip(batches,batches_viewdirs):predictions.append(fine_model(batch,viewdirs=batch_viewdirs))raw=torch.cat(predictions,dim=0)raw=raw.reshape(list(query_points.shape[:2])+[raw.shape[-1]])#口头可微分体积渲染,从新分解RGB图像。rgb_map,depth_map,acc_map,weights=raw2outputs(raw,z_vals_combined,rays_d)#存储输入outputs['z_vals_hierarchical']=z_hierarchoutputs['rgb_map_0']=rgb_map_0outputs['depth_map_0']=depth_map_0outputs['acc_map_0']=acc_map_0#存储输入outputs['rgb_map']=rgb_mapoutputs['depth_map']=depth_mapoutputs['acc_map']=acc_mapoutputs['weights']=weightsreturnoutputs


到这一步骤,就简直领有了训练模型所需的一切模块。如今为一个繁难的训练环节做一些设置,创立超参数和辅佐函数,然起初训练模型。


一切用于训练的超参数都在此设置,自动值取自原始论文中数据,除非计算上有限度。在计算受限状况下,本次讨论驳回的都是正当的自动值。

#编码器d_input=3#输入维度n_freqs=10#输入到编码函数中的样本点数量log_space=True#假设设置,频率按对数空间缩放use_viewdirs=True#假设设置,则经常使用视图方向作为输入n_freqs_views=4#视图编码配置的数量#采样战略n_samples=64#每条射线的空间样本数perturb=True#假设设置,则对采样位置运行噪声inverse_depth=False#假设设置,则按反深度线性采样点#模型d_filter=128#线性层滤波器的尺寸n_layers=2#bottleneck层数量skip=[]#运行输入残差的层级use_fine_model=True#假设设置,则创立一个精细模型d_filter_fine=128#精细网络线性层滤波器的尺寸n_layers_fine=6#精细网络瓶颈层数#分层采样n_samples_hierarchical=64#每条射线的样本数perturb_hierarchical=False#假设设置,则对采样位置运行噪声#优化器lr=5e-4#学习率#训练n_iters=10000batch_size=2**14#每个梯度步长的射线数量(2的幂次)one_image_per_step=True#每个梯度步骤一个图像(禁用批处置)chunksize=2**14#依据须要启动修正,以顺应GPU内存center_crop=True#裁剪图像的中心部分(每幅图像裁剪一次性)center_crop_iters=50#经过这么多epoch后,中止裁剪中心display_rate=25#每X个epoch显示一次性测试输入#早停warmup_iters=100#热身阶段的迭代次数warmup_min_fitness=10.0#在热身_iters处继续训练的最小PSNR值n_restarts=10#训练停滞时从新开局的次数#捆绑了各种函数的参数,以便一次性性传递。kwargs_sample_stratified={'n_samples':n_samples,'perturb':perturb,'inverse_depth':inverse_depth}kwargs_sample_hierarchical={'perturb':perturb}


这一环节会创立一些用于训练的辅佐函数。NeRF很容易发生部分最小值,在这种状况下,训练很快就会停滞并发生空白输入。必要时,会应用EarlyStopping从新启动训练。

#绘制采样函数defplot_samples(z_vals:torch.Tensor,z_hierarch:Optional[torch.Tensor]=None,ax:Optional[np.ndarray]=None):r"""绘制分层样本和(可选)分级样本。"""y_vals=1+np.zeros_like(z_vals)ifaxisNone:ax=plt.subplot()ax.plot(z_vals,y_vals,'b-o')ifz_hierarchisnotNone:y_hierarch=np.zeros_like(z_hierarch)ax.plot(z_hierarch,y_hierarch,'r-o')ax.set_ylim([-1,2])ax.set_title('StratifiedSamples(blue)andHierarchicalSamples(red)')ax.axes.yaxis.set_visible(False)ax.grid(True)returnaxdefcrop_center(img:torch.Tensor,frac:float=0.5)->torch.Tensor:r"""从图像中裁剪中心方形。"""h_offset=round(img.shape[0]*(frac/2))w_offset=round(img.shape[1]*(frac/2))returnimg[h_offset:-h_offset,w_offset:-w_offset]classEarlyStopping:r"""基于适配规范的早期中止辅佐器"""def__init__(self,patience:int=30,margin:float=1e-4):self.best_fitness=0.0self.best_iter=0self.margin=marginself.patience=patienceorfloat('inf')#在epoch中止提高后期待的中止期间def__call__(self,iter:int,fitness:float):r"""审核能否合乎中止规范。"""if(fitness-self.best_fitness)>self.margin:self.best_iter=iterself.best_fitness=fitnessdelta=iter-self.best_iterstop=delta>=self.patience#超越耐性则中止训练returnstopdefinit_models():r"""为NeRF训练初始化模型、编码器和优化器。"""#编码器encoder=PositionalEncoder(d_input,n_freqs,log_space=log_space)encode=lambdax:encoder(x)#视图方向编码ifuse_viewdirs:encoder_viewdirs=PositionalEncoder(d_input,n_freqs_views,log_space=log_space)encode_viewdirs=lambdax:encoder_viewdirs(x)d_viewdirs=encoder_viewdirs.d_outputelse:encode_viewdirs=Noned_viewdirs=None#模型model=NeRF(encoder.d_output,n_layers=n_layers,d_filter=d_filter,skip=skip,d_viewdirs=d_viewdirs)model.to(device)model_params=list(model.parameters())ifuse_fine_model:fine_model=NeRF(encoder.d_output,n_layers=n_layers,d_filter=d_filter,skip=skip,d_viewdirs=d_viewdirs)fine_model.to(device)model_params=model_params+list(fine_model.parameters())else:fine_model=None#优化器optimizer=torch.optim.Adam(model_params,lr=lr)#早停warmup_stopper=EarlyStopping(patience=50)returnmodel,fine_model,encode,encode_viewdirs,optimizer,warmup_stopper


上方就是详细的训练循环环节函数:

deftrain():r"""启动NeRF训练。"""#对一切图像启动射线洗牌。ifnotone_image_per_step:height,width=images.shape[1:3]all_rays=torch.stack([torch.stack(get_rays(height,width,focal,p),0)forpinposes[:n_training]],0)rays_rgb=torch.cat([all_rays,images[:,None]],1)rays_rgb=torch.permute(rays_rgb,[0,2,3,1,4])rays_rgb=rays_rgb.reshape([-1,3,3])rays_rgb=rays_rgb.type(torch.float32)rays_rgb=rays_rgb[torch.randperm(rays_rgb.shape[0])]i_batch=0train_psnrs=[]val_psnrs=[]iternums=[]foriintrange(n_iters):model.train()ifone_image_per_step:#随机选用一张图片作为目的。target_img_idx=np.random.randint(images.shape[0])target_img=images[target_img_idx].to(device)ifcenter_cropandi<center_crop_iters:target_img=crop_center(target_img)height,width=target_img.shape[:2]target_pose=poses[target_img_idx].to(device)rays_o,rays_d=get_rays(height,width,focal,target_pose)rays_o=rays_o.reshape([-1,3])rays_d=rays_d.reshape([-1,3])else:#在一切图像上随机显示。batch=rays_rgb[i_batch:i_batch+batch_size]batch=torch.transpose(batch,0,1)rays_o,rays_d,target_img=batchheight,width=target_img.shape[:2]i_batch+=batch_size#一个epoch后洗牌ifi_batch>=rays_rgb.shape[0]:rays_rgb=rays_rgb[torch.randperm(rays_rgb.shape[0])]i_batch=0target_img=target_img.reshape([-1,3])#运转TinyNeRF的一次性迭代,失掉渲染后的RGB图像。outputs=nerf_forward(rays_o,rays_d,near,far,encode,model,kwargs_sample_stratified=kwargs_sample_stratified,n_samples_hierarchical=n_samples_hierarchical,kwargs_sample_hierarchical=kwargs_sample_hierarchical,fine_model=fine_model,viewdirs_encoding_fn=encode_viewdirs,chunksize=chunksize)#审核任何数字疑问。fork,vinoutputs.items():iftorch.isnan(v).any():print(f"![NumericalAlert]{k}containsNaN.")iftorch.isinf(v).any():print(f"![NumericalAlert]{k}containsInf.")#反向流传rgb_predicted=outputs['rgb_map']loss=torch.nn.functional.mse_loss(rgb_predicted,target_img)loss.backward()optimizer.step()optimizer.zero_grad()psnr=-10.*torch.log10(loss)train_psnrs.append(psnr.item())#以给定的显示速率评价测试值。ifi%display_rate==0:model.eval()height,width=testimg.shape[:2]rays_o,rays_d=get_rays(height,width,focal,testpose)rays_o=rays_o.reshape([-1,3])rays_d=rays_d.reshape([-1,3])outputs=nerf_forward(rays_o,rays_d,near,far,encode,model,kwargs_sample_stratified=kwargs_sample_stratified,n_samples_hierarchical=n_samples_hierarchical,kwargs_sample_hierarchical=kwargs_sample_hierarchical,fine_model=fine_model,viewdirs_encoding_fn=encode_viewdirs,chunksize=chunksize)rgb_predicted=outputs['rgb_map']loss=torch.nn.functional.mse_loss(rgb_predicted,testimg.reshape(-1,3))print("Loss:",loss.item())val_psnr=-10.*torch.log10(loss)val_psnrs.append(val_psnr.item())iternums.append(i)#绘制输入示例fig,ax=plt.subplots(1,4,figsize=(24,4),gridspec_kw={'width_ratios':[1,1,1,3]})ax[0].imshow(rgb_predicted.reshape([height,width,3]).detach().cpu().numpy())ax[0].set_title(f'Iteration:{i}')ax[1].imshow(testimg.detach().cpu().numpy())ax[1].set_title(f'Target')ax[2].plot(range(0,i+1),train_psnrs,'r')ax[2].plot(iternums,val_psnrs,'b')ax[2].set_title('PSNR(train=red,val=blue')z_vals_strat=outputs['z_vals_stratified'].view((-1,n_samples))z_sample_strat=z_vals_strat[z_vals_strat.shape[0]//2].detach().cpu().numpy()if'z_vals_hierarchical'inoutputs:z_vals_hierarch=outputs['z_vals_hierarchical'].view((-1,n_samples_hierarchical))z_sample_hierarch=z_vals_hierarch[z_vals_hierarch.shape[0]//2].detach().cpu().numpy()else:z_sample_hierarch=None_=plot_samples(z_sample_strat,z_sample_hierarch,ax=ax[3])ax[3].margins(0)plt.show()#审核PSNR能否存在疑问,假设发现疑问,则中止运转。ifi==warmup_iters-1:ifval_psnr<warmup_min_fitness:print(f'ValPSNR{val_psnr}belowwarmup_min_fitness{warmup_min_fitness}.Stopping...')returnFalse,train_psnrs,val_psnrselifi<warmup_iters:ifwarmup_stopperisnotNoneandwarmup_stopper(i,psnr):print(f'TrainPSNRflatlinedat{psnr}for{warmup_stopper.patience}iters.Stopping...')returnFalse,train_psnrs,val_psnrsreturnTrue,train_psnrs,val_psnrs


最终的结果如下图所示:


▲图6|运转结果示用意©️【深蓝AI】

原文链接:

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...