1. 程式人生 > >純C++超解析度重建DRRN --改編--(二)歸一化(BatchNorm) 和 縮放和平移(Scale)

純C++超解析度重建DRRN --改編--(二)歸一化(BatchNorm) 和 縮放和平移(Scale)

DRRN和前面相比增加了

1。歸一化(BatchNorm)

其中 均值(u) 和方差(std)需要除以一個約等於1000的比例因子,std 還要開平方 該部分已經放到載入模型中去了:

// 輸入歸一化 x_norm = (x-u)/std, 其中u和std是個累計計算的均值和方差。
// 其中 u,std需要除以一個約等於1000的比例因子,std 還要開平方 已經放到載入模型部分去了
// u =*data/scale_factor;std = sqrt(*data/scale_factor);
void vl_BatchNorm(卷積層 * out,float * u,float * std)//函式 均值 方差
{

	float * s=out->data, *s0;
	float p;
	int width=out->width; 
	int height=out->height; 

	float * u0=u, * std0=std;

	for (int c=0; c<out->depth; ++c)
	{
		for (int i=0; i<height; ++i)
		{
			for (int j=0; j<width; ++j)
			{
				s0 = s+i*width+j;
				p  = *s0;//
					*s0 = (p-(*u0))/(*std0);//均值 方差
			}
			
		}
		s+=width*height;//下一通道
		u0++;std0++;
	}

}

2。縮放和平移(Scale):

// y=alpha*x_norm + beta,對歸一化後的x進行比例縮放和位移。
void vl_Scale(卷積層 * out,float * alpha,float * beta)
{

	float * s=out->data, *s0;
	float p;
	int width=out->width; 
	int height=out->height; 

	float * a0=alpha, * b0=beta;

	for (int c=0; c<out->depth; ++c)
	{
		for (int i=0; i<height; ++i)
		{
			for (int j=0; j<width; ++j)
			{
				s0 = s+i*width+j;
				p  = *s0;//
					*s0 = p*(*a0)+(*b0);//縮放和平移
			}
			
		}
		s+=width*height;//下一通道
		a0++;b0++;
	}

}

該部分完成