Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add read npy for ncnn2table #5930

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

wxqwinner
Copy link

@wxqwinner wxqwinner commented Mar 6, 2025

  • 内容概述
    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
Copy link
Member

tencent-adm commented Mar 6, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@nihui nihui left a comment

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 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
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


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
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
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
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
Author

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

@github-actions github-actions bot added the doc label Mar 7, 2025
@nihui
Copy link
Member

nihui commented Mar 7, 2025

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

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

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.

3 participants