espcn有两处和前面不同
1。激励函数使用 tanh:
double tanh_naive(double x) {
if (x < -40) {
// 避免负溢出
return -1;
} else if (x > 40) {
// 避免正溢出
return 1;
} else {
// tanh的精确表达式,如果x太大是不稳定的
double exp2x = exp(2 * x);
return (exp2x - 1.0) / (exp2x + 1.0);
}
}
void vl_tanh(卷积层 * out)//激励函数
{
float * s=out->data, *s0;
float p;
int width=out->width;
int height=out->height;
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 = tanh_naive(p);
}
}
s+=width*height;//下一通道
}
}
2。像素组合(图像放大)
//9通道合成1通道,图像放大3倍
void 像素组合(卷积层*si,卷积层*di)//图像放大
{
int w=si->width;
int h=si->height;
int len=w*h;
int dw=di->width;
float *d=di->data;//3倍1通道
float *s=si->data;//9通道
float *ps,*pd;
int t1,t2;
float *ps0;
//通道排列方式
int kk[9]={0,3,6,
1,4,7,
2,5,8};
for(int k=0;k<si->depth;k++){
//计算位置
t1=k%3;//余数 0,1,2
t2=t1+(k-t1)/3*dw;//余数+跳行
pd=d+t2;
ps0=s+(kk[k])*len;
for(int j=0;j<h;j++){
for(int i=0;i<w;i++)
{
ps=ps0+j*w+i;
*pd = *ps;
pd+=3;//下一位置
}
pd+=2*dw;//过2行
}
}
}
这节完成