Skip to content

add read npy for ncnn2table - #5930

Merged
nihui merged 9 commits into
Tencent:masterfrom
wxqwinner:add-ncnn2table-npy
Mar 18, 2025
Merged

add read npy for ncnn2table#5930
nihui merged 9 commits into
Tencent:masterfrom
wxqwinner:add-ncnn2table-npy

Conversation

@wxqwinner

@wxqwinner wxqwinner commented Mar 6, 2025

Copy link
Copy Markdown
Contributor
  • 内容概述
    ncnn2table增加读npy功能
  • 实现方式
    命令增加一个参数type,type为0时和原来一样,可省略
    type为1时,以npy格式读文件,读npy引用(https://github.com/llohse/libnpy)实现。
    ncnn2table test.param test.bin filelist.txt squeezenet.table shape=[227,227,3] method=kl type=1
  • 测试结果
    单个input blob测试通过,多个未测试(没资源)

@github-actions github-actions Bot added the tool label Mar 6, 2025
@tencent-adm

tencent-adm commented Mar 6, 2025

Copy link
Copy Markdown
Member

CLA assistant check
All committers have signed the CLA.

@nihui nihui left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/how-to-use-and-FAQ/quantized-int8-inference.md 文档中添加使用 npy 的内容

Comment thread tools/quantize/ncnn2table.cpp Outdated
Comment on lines +226 to +233
return ncnn::Mat(shape[0], (void*)(d.data.data())).clone();
case 2:
return ncnn::Mat(shape[0], shape[1], (void*)(d.data.data())).clone();
case 3:
return ncnn::Mat(shape[0], shape[1], shape[2], (void*)(d.data.data())).clone();
case 4:
return ncnn::Mat(shape[0], shape[1], shape[2], shape[3], (void*)(d.data.data())).clone();
default:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3d 4d 可能存在 channel gap
先从1d的构造,然后reshape到3d,再clone

Comment thread tools/quantize/ncnn2table.cpp Outdated

inline ncnn::Mat read_npy(const std::vector<int>& shape, const std::string& npypath)
{
npy::npy_data<float> d = npy::read_npy<float>(npypath);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_npy 可能会抛出异常,需要 catch 处理下,提示错误的文件是哪个,然后跳过当前文件?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议处理异常,尤其是为了跳过错误文件,因为用npy的情况下,使用的数据一般是训练的数据,基本都是经过清洗和前处理的,几乎不可能出现个别错误的npy文件,只有可能全部出错,这个和自己收集的image文件不一样。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议处理异常,尤其是为了跳过错误文件,因为用npy的情况下,使用的数据一般是训练的数据,基本都是经过清洗和前处理的,几乎不可能出现个别错误的npy文件,只有可能全部出错,这个和自己收集的image文件不一样。

那么 fprintf(stderr 输出npy错误信息,然后直接 exit ?

@wxqwinner

Copy link
Copy Markdown
Contributor Author

判断传入shape和npy的shape是否匹配还是直接判断两个的total size,如果是判断shape匹配,就稍微有点严格,要不要check这个?

@github-actions github-actions Bot added the doc label Mar 7, 2025
@nihui

nihui commented Mar 7, 2025

Copy link
Copy Markdown
Member

判断传入shape和npy的shape是否匹配还是直接判断两个的total size,如果是判断shape匹配,就稍微有点严格,要不要check这个?

严格判断shape匹配,可变shape没法统计的...

@nihui nihui closed this Mar 11, 2025
@nihui nihui reopened this Mar 11, 2025
Comment thread tools/quantize/ncnn2table.cpp Fixed
Comment thread tools/quantize/ncnn2table.cpp Fixed
Comment thread tools/quantize/ncnn2table.cpp Fixed
Comment thread tools/quantize/ncnn2table.cpp Fixed
@nihui

nihui commented Mar 11, 2025

Copy link
Copy Markdown
Member
ncnn/tools/quantize/ncnn2table.cpp:235:105: error: cannot pass non-trivial object of type 'const std::string' (aka 'const basic_string<char>') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]
  235 |         fprintf(stderr, "expect %d dims, but got: %d, npy file:%s\n", (int)dims, (int)npy_shape.size(), npypath);
      |                                                                ~~                                       ^~~~~~~
ncnn/tools/quantize/ncnn2table.cpp:235:105: note: did you mean to call the c_str() method?
  235 |         fprintf(stderr, "expect %d dims, but got: %d, npy file:%s\n", (int)dims, (int)npy_shape.size(), npypath);
      |                                                                                                         ^      
      |                                                                                                                .c_str()

@wxqwinner

Copy link
Copy Markdown
Contributor Author
ncnn/tools/quantize/ncnn2table.cpp:235:105: error: cannot pass non-trivial object of type 'const std::string' (aka 'const basic_string<char>') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]
  235 |         fprintf(stderr, "expect %d dims, but got: %d, npy file:%s\n", (int)dims, (int)npy_shape.size(), npypath);
      |                                                                ~~                                       ^~~~~~~
ncnn/tools/quantize/ncnn2table.cpp:235:105: note: did you mean to call the c_str() method?
  235 |         fprintf(stderr, "expect %d dims, but got: %d, npy file:%s\n", (int)dims, (int)npy_shape.size(), npypath);
      |                                                                                                         ^      
      |                                                                                                                .c_str()

是的,我准备把这个npypath打印消息移除

@wxqwinner
wxqwinner requested a review from nihui March 14, 2025 02:58
@nihui
nihui merged commit 701f537 into Tencent:master Mar 18, 2025
@nihui

nihui commented Mar 18, 2025

Copy link
Copy Markdown
Member

Thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants