ncnn
ncnn copied to clipboard
计算结果异常
我这里一个文本检测模型, 同张图像缩放到不同大小,发现结果结果偏差很大 1601大小就正常,1600大小,错的离谱。 怀疑是某层计算结果不对,把每层计算结果保存下来跟caffe跑的进行对比 结果又对了(计算数值有差别,但最终文本行检测正常), 后来有设置set_light_mode(false);发现结果也对。 怀疑,有内存越界错误。
经一下午的调试发现是Crop层调用的代码copy_cut_border()存在问题 crop.cpp
int Crop::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;
int _outw = outw == -233 ? w - woffset : outw;
int _outh = outh == -233 ? h - hoffset : outh;
int _outc = outc == -233 ? channels - coffset : outc;
#这里bottom_blob_sliced用的是bottom的部分内存
const Mat bottom_blob_sliced(w, h, _outc, (void*)(const float*)bottom_blob.channel(coffset));
int top = hoffset;
int bottom = h - _outh - hoffset;
int left = woffset;
int right = w - _outw - woffset;
copy_cut_border(bottom_blob_sliced, top_blob, top, bottom, left, right, opt.blob_allocator, opt.num_threads);
mat.cpp
void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, Allocator* allocator, int num_threads)
{
int w = src.w - left - right;
int h = src.h - top - bottom;
size_t elemsize = src.elemsize;
if (w == src.w && h == src.h)
{
dst = src; ##这里直接赋值,是地址赋值
return;
}
在light_mode下,会对bottom进行释放。 net.cpp forward_layer()
if (opt.lightmode)
{
// delete after taken in light mode
## 这里进行了release,会释放内存##
blob_mats[bottom_blob_index].release();
// deep copy for inplace forward if data is shared
if (layer->support_inplace && *bottom_blob.refcount != 1)
{
bottom_blob = bottom_blob.clone();
}
}
修复: dst=src.clone()即可
改成这样更好
if(src.refcount)
{
dst = src;
}
else
{
dst = src.clone();
}
感谢详细的问题反馈~
搞了三天,我也发现这个问题,有越界现象,看源码调试,调出来。要是早点过来看这个,估计早解决了。