網站首頁 編程語言 正文
驗證ncnn模型的精度
1、進行pth模型的驗證
得到ncnn模型的順序為:.pth–>.onnx–>ncnn
.pth的精度驗證如下:
如進行的是二分類:
model = init_model(model, data_cfg, device=device, mode='eval')
###.pth轉.onnx模型
# #---
# input_names = ["x"]
# output_names = ["y"]
# inp = torch.randn(1, 3, 256, 128) ##錯誤示例
inp = np.full((1, 3, 160, 320), 0.5).astype(np.float) #(160,320) = (h,w)
inp = torch.FloatTensor(inp)
out = model(inp)
print(out)
沒有經過softmax層,out輸出為±1的兩個值。
2、轉為onnx后的精度驗證
sess = onnxruntime.InferenceSession("G:\\pycharm_pytorch171\\pytorch_classification\\main\\sim.onnx", providers=["CUDAExecutionProvider"]) # use gpu
input_name = sess.get_inputs()[0].name
print("input_name: ", input_name)
output_name = sess.get_outputs()[0].name
print("output_name: ", output_name)
# test_images = torch.rand([1, 3, 256, 128])
test_images = np.full((1, 3, 160, 320), 0.5).astype(np.float) #(160,320) = (h,w)
test_images = torch.FloatTensor(test_images)
print("test_image", test_images)
prediction = sess.run([output_name], {input_name: test_images.numpy()})
print(prediction)
3、ncnn精度驗證
首先保證mean、norm輸出的值與onnx保持一致,因為onnx直接輸入值0.5,ncnn模型經過mean、norm計算后的結果與0.5一致就行。
然后就是ncnn模型的計算輸出
- 查看輸出結果是否是0.5,首先得將輸入值1給到img
```cpp
constexpr int w = 320;
constexpr int h = 160;
float cbuf[h][w];
cv::Mat img(h, w, CV_8UC3,(float *)cbuf);
//BYTE* iPtr = new BYTE[128 * 256 * 3];
BYTE* iPtr = new BYTE[h * w * 3];
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
for (int k = 0; k < 3; k++)
{
//iPtr[i * 256 * 3 + j * 3 + k] = img.at<cv::Vec3f>(i, j)[k];
img.at<cv::Vec3b>(i, j)[k] = 1;
}
}
}
```
- 經過上面的賦值,通過了mean、norm計算后,得到的結果進行查看,值為0.5則正確轉換。得到的結果送入下面的代碼進行輸出。
ncnn結果為mat,因此采用該方法進行遍歷查看。
```cpp
//輸出ncnn mat
void ncnn_mat_print(const ncnn::Mat& m)
{
for (int q = 0; q < m.c; q++)
{
const float* ptr = m.channel(q);
for (int y = 0; y < m.h; y++)
{
for (int x = 0; x < m.w; x++)
{
printf("%f ", ptr[x]);
}
ptr += m.w;
printf("\n");
}
printf("------------------------\n");
}
}
```
將mat給到模型進行推理得到結果。
4、結果確認
一般情況下,pth模型與onnx模型結果相差不大,ncnn會有點點損失,千分位上的損失,這樣精度基本上是一致的。
若不一致,看哪一步結果相差太大,如果是ncnn這一步相差太大,檢查是否是值輸入有問題,或者是輸入的(h,w)弄反了。
原文鏈接:https://blog.csdn.net/weixin_44753371/article/details/129022931
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-09-23 C#實現目錄跳轉(TreeView和SplitContainer)的示例代碼_C#教程
- 2022-05-08 C++類中隱藏的幾個默認函數你知道嗎_C 語言
- 2022-05-11 log4j2.xml 的標簽 loggers 中 root 的屬性 level 指的是什么
- 2022-05-31 使用python把Excel中的數據在頁面中可視化_python
- 2022-12-04 .NET?Core利用BsonDocumentProjectionDefinition和Lookup
- 2022-10-15 QT?UDP網絡編程實現簡單消息傳輸_C 語言
- 2022-10-28 React中使用react-file-viewer問題_React
- 2022-10-21 一文詳解?Compose?Navigation?的實現原理_Android
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支