Open molyswu opened 4 months ago
使用 trtexec 转换模型
trtexec --onnx=model.onnx --saveEngine=model.engine
代码:
int main() {
// patchcore模型训练配置文件调整center_crop为 center_crop: null
// trtexec --onnx=model.onnx --saveEngine=model.engine 转换模型
string model_path = "D:/2011/anomalib/results/patchcore/tube/run/weights/onnx/model.engine";//"D:/ml/code/anomalib/results/efficient_ad/mvtec/bottle/run/weights/openvino/model.engine";
string meta_path = "D:/2011/anomalib/results/patchcore/tube/run/weights/onnx/metadata.json";
string image_path = "D:/2011/anomalib/datasets/D3.png";
string image_dir = "D:/2011/anomalib/datasets/img_192/abnormal/abnormal";
string save_dir = "D:/2011/python_work/NagatoYuki093/anomalib-tensorrt-cpp-main/anomalib-tensorrt-cpp/result"; // 注意目录不会自动创建,要手动创建才会保存
bool efficient_ad = true; // 是否使用efficient_ad模型
// 创建推理器
auto inference = Inference(model_path, meta_path, efficient_ad);
// 单张图片推理
cv::Mat image = readImage(image_path);
Result result = inference.single(image);
saveScoreAndImages(result.score, result.anomaly_map, image_path, save_dir);
cv::resize(result.anomaly_map, result.anomaly_map, { 1500, 500 });
cv::imshow("result", result.anomaly_map);
cv::waitKey(0);
// 多张图片推理
inference.multi(image_dir, save_dir);
return 0;
}
请问可以给我模型的onnx文件让我测试吗?
上传不了
压缩一下应该可以上传
这是上次文件提示:File size too big: 25 MB are allowed, 145 MB were attempted to upload.
是patchcore的模型
模型是patchcore:wide_resnet50_2
我将模型的onnx上传到谷歌云盘:https://drive.google.com/drive/my-drive
如下测试VS2022在2个配置(tensorrt、openvino)环境调试错误: 1.tensorrt加载模型报错:0x00007FF888044B2C 处(位于 anomalib-tensorrt-cpp.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x000000B7C3D4EE00 处。 2.openvino加载模型报错:0x00007FF888044B2C 处(位于 openvino.exe 中)有未经处理的异常: Microsoft C++ 异常: ov::Exception,位于内存位置 0x000000588815CD80 处。
我将模型的onnx上传到谷歌云盘:https://drive.google.com/drive/my-drive
这个链接是登录的,你应该分享文件,把分享链接给我,从你开始的错误发现似乎有 SogouInput 的dll,这很奇怪
“anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Program Files (x86)\SogouInput\14.2.0.8968\Resource.dll”。
“anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Program Files (x86)\SogouInput\Components\PicFace\1.1.0.2129\PicFace64.dll”。
“anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\psapi.dll”。
“anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\dwmapi.dll”。
线程 13220 已退出,返回值为 0 (0x0)。
“anomalib-tensorrt-cpp.exe”(Win32): 已卸载“C:\Windows\System32\SogouTSF.ime”
0x00007FF818C8829C (ucrtbased.dll) (anomalib-tensorrt-cpp.exe 中)处有未经处理的异常: 将一个无效参数传递给了将无效参数视为严重错误的函数。
你试试下面2个链接:
是下载不了,试试 https://easychuan.cn/ 或者 https://cowtransfer.com/ ,图片找不到会不会是opencv版本的问题,我用的4.8.0和4.9.0版本的
我的opencvv版本是4.9.0版本的。
#
https://easychuan.cn/r/n3mmv?t=nf 取件码:n3mmv ![Uploading 3B.png…]()
请帮忙分析一下是什么原因?
找到原因了,因为类的2个int变量 min_dim max_dim 没有初始化造成的,你试试新代码好不好用。 当时支持动态batch的时候没有做固定batch的测试,实在不好意思。
好的
程序单步跟踪到main.cpp的“ Result result = inference.single(image);”,再进入函数跟踪utils.cpp的“cv::Mat superimposeAnomalyMap(cv::Mat& anomaly_map, cv::Mat& origin_image) {
auto anomaly = anomaly_map.clone();” 报错:0x00007FF888044B2C 处(位于 anomalib-tensorrt-cpp.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x000000F8972FECD0 处。是什么原因?
谢谢!
调试成功了,有多一个符号。
请教你一个问题anomalib 的训练阈值和推理的阈值分别设置多少?
默认是0.5吧,我没有动过,这个项目是我练手的,没在实际生产中用过,我现在已经不做异常检测了
还有一个问题请教你,是我测试出来的图片,检测缺陷的结果没有分割缺陷区域的,是什么原因?是训练模型时候没事设置好参数,还是你的这一TensorrT推理程序没有写这一个分割缺陷区域代码,如何修改程序?如下是我测试的图片没有分割缺陷区域:
谢谢!
anomalib环境下运行:python tools/benchmarking/benchmark.py如错误如下是什么原因:
(anomalib) F:\work\anomalib>python tools/benchmarking/benchmark.py --config ./src/configs/model/padim.yaml
Benchmarking started 🏃♂️. This will take a while ⏲ depending on your configuration.
Traceback (most recent call last):
File "F:\work\anomalib\tools\benchmarking\benchmark.py", line 17, in
anomalib环境下运行:python tools/benchmarking/benchmark.py如错误如下是什么原因:
(anomalib) F:\work\anomalib>python tools/benchmarking/benchmark.py --config ./src/configs/model/padim.yaml
Benchmarking started 🏃♂️. This will take a while ⏲ depending on your configuration.
Traceback (most recent call last):
File "F:\work\anomalib\tools\benchmarking\benchmark.py", line 17, in
还有一个问题请教你,是我测试出来的图片,检测缺陷的结果没有分割缺陷区域的,是什么原因?是训练模型时候没事设置好参数,还是你的这一TensorrT推理程序没有写这一个分割缺陷区域代码,如何修改程序?如下是我测试的图片没有分割缺陷区域:
![]()
谢谢!
如果你说的是如下分割的话,如第二张图片,这个刚刚实现了,实现代码在 https://github.com/NagatoYuki0943/anomalib-tensorrt-cpp/blob/main/utils.cpp#L206 ,保存的位置在 https://github.com/NagatoYuki0943/anomalib-tensorrt-cpp/blob/main/inference.hpp#L301 ,里面第二张图片就是分割的
我的意思是像mvtec上图的第5个图会画出来。
如下错误是什么原因?anomalib环境下运行:python tools/benchmarking/benchmark.py如错误如下是什么原因: (anomalib) F:\work\anomalib>python tools/benchmarking/benchmark.py --config ./src/configs/model/padim.yaml Benchmarking started 🏃♂️. This will take a while ⏲ depending on your configuration. Traceback (most recent call last): File "F:\work\anomalib\tools\benchmarking\benchmark.py", line 17, in distribute(_args.config) File "F:\work\anomalib\src\anomalib\pipelines\benchmarking\benchmark.py", line 242, in distribute devices = config.hardware File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\dictconfig.py", line 355, in getattr self._format_and_raise( File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\base.py", line 231, in _format_and_raise format_and_raise( File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf_utils.py", line 899, in format_and_raise _raise(ex, cause) File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf_utils.py", line 797, in _raise raise ex.with_traceback(sys.exc_info()[2]) # set env var OC_CAUSE=1 for full trace File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\dictconfig.py", line 351, in getattr return self._get_impl( File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\dictconfig.py", line 442, in _get_impl node = self._get_child( File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\basecontainer.py", line 73, in _get_child child = self._get_node( File "C:\ProgramData\anaconda3\envs\anomalib\lib\site-packages\omegaconf\dictconfig.py", line 480, in _get_node raise ConfigKeyError(f"Missing key {key!s}") omegaconf.errors.ConfigAttributeError: Missing key hardware full_key: hardware object_type=dict
这个不清楚,我最近没用anomalib库
我的意思是像mvtec上图的第5个图会画出来。
支持了,其实原来第3张图就是,只不过是线条只有1个像素,看不清楚,我用了dilate操作让线条加粗了,代码在 https://github.com/NagatoYuki0943/anomalib-tensorrt-cpp/blob/main/utils.cpp#L184 , 通过调整 kernel 大小调整线条粗细, 下面叠加部分调整权重能调整背景图和红色线条的亮度 https://github.com/NagatoYuki0943/anomalib-tensorrt-cpp/blob/main/utils.cpp#L194
测试是我自己的数据集,不是公开数据集我按照你方法修改程序,还是没有红色线条的亮度。
可能图片形状和分辨率的关系?你可以给我一张图片吗,我拿你之前给我的模型测试一下
请查收图片:
如何修改程序缺陷小于等于5*5个像素,图片不保存。
请查收图片:
![]()
我实现了,原因是阈值0.5太低了,我添加了一个参数,可以动态调整阈值
float threshold = 0.9;
Result result = inference.single(image, threshold);
如何修改程序缺陷小于等于5*5个像素,图片不保存。 不保存图片把这一行注释就行
saveScoreAndImages(result.score, result.anomaly_map, image_path, save_dir);
多张图片推理也把这个注释就可以了
5x5像素可以试试opencv的连通域和分水岭算法,这个我也不太懂,你自己试试吧
调整阈值,可以将一部分5x5像素缺陷图片不保全。
将你的工程改为VS2022 加载model.engine模型,出现溢出, Expression: vector subscript out of range for information on how your program can cause an asserti。
“anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\windows.storage.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\SogouPY.ime”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\oleacc.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\winhttp.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\ntmarta.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\profapi.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Program Files (x86)\SogouInput\14.2.0.8968\Resource.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Program Files (x86)\SogouInput\Components\PicFace\1.1.0.2129\PicFace64.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\psapi.dll”。 “anomalib-tensorrt-cpp.exe”(Win32): 已加载“C:\Windows\System32\dwmapi.dll”。 线程 13220 已退出,返回值为 0 (0x0)。 “anomalib-tensorrt-cpp.exe”(Win32): 已卸载“C:\Windows\System32\SogouTSF.ime” 0x00007FF818C8829C (ucrtbased.dll) (anomalib-tensorrt-cpp.exe 中)处有未经处理的异常: 将一个无效参数传递给了将无效参数视为严重错误的函数。
请教如何处理?
谢谢! molyswu