Pytorch網路訓練模型轉成C 推理執行

2021-10-10 18:30:16 字數 3267 閱讀 9913

顯著性檢測網路模型是基於pytorch深度學習框架進行搭建並參與訓練的,在實際應用和生產中,由於要滿足低延遲和嚴格部署要求,通常選用c++來進行推理過程。這裡的顯著圖推理模型c++轉換過程主要由以下幾個步驟組成:將pytorch網路模型轉換為torch script,並序列化為乙個檔案;配置編譯執行環境,在c++中載入模型引數,完成推理任務。

系統環境(windows)

電腦系統版本:windows 10

顯示卡版本:geforce rtx 2070(視訊記憶體8192mb)

編譯器:visual studio 2019 community

gcc版本:8.1.0

cmake版本:3.18.2

cuda版本:10.1,v10.1.243

cudnn版本:cudnn v8.0.2 for cuda 10.1

pytorch版本:stable(1.6.0)

libtorch-gpu版本:

libtorch-cpu版本:

opencv版本:

系統環境(linux)

linux系統版本:ubuntu 19.10

顯示卡版本:titan rtx(視訊記憶體24217mb)

gcc版本:gcc (ubuntu 7.5.0-3ubuntu1~19.10) 7.5.0

g++版本:g++ (ubuntu 7.5.0-3ubuntu1~19.10) 7.5.0

cmake版本:3.13.4-1build1

cuda版本:10.2, v10.2.89

cudnn版本:cudnn v8.0.1.13 for cuda 10.2

libtorch-gpu版本:

libtorch-cpu版本:

opencv版本:

網路模型

**說明

所有函式都在inference_cfile.cpp檔案中進行了說明:

calculate_sum(): 對單個區域tile塊內所有的顯著影象素值進行求和運算;

get_normalized_map(): 對區域級別的顯著圖中所有顯著值進行正則化,將每個顯著值除以所有的顯著值累加和;

get_saliency_map(): 給定saliency map的width和height,對畫素級別的顯著圖進行處理,獲取得到區域級別的顯著圖結果;

get_inference_result(): 給定輸入值和網路模型,經過推理過程得到相關結果;

inference_saliency_map(): 結合3、4的結果,獲取得到相對應的saliency map;

listtotensor(): 將影象資料集列表轉換成為模型推理所需要的資料型別;

print_map(): 輸出顯示單個saliency map結果;

print_maps(): 輸出顯示所有saliency map結果;

showusage(): 顯示使用說明;

commit_command(): 讀取外部傳入引數並進行解釋說明,執行相關推理任務。

cmakelists.txt 為環境編譯的檔案,具體內容見下面的**使用說明。

訓練網路模型權重引數為 convert_weights.pt。

可執行檔案為build/release中的inference_cfile.exe。

補充改進

**使用

環境編譯(windows):在windows x64平台下,執行cmd,進入工程專案資料夾下,執行以下命令:

mkdir build

cd build

cmake -dcmake_prefix_path=d:\\configures\\libtorch-cpu(\\libtorch-gpu);d:\\configures\\opencv\\build\\x64\\vc15\\lib -dcmake_build_type=release .. -g "visual studio 16 2019"

cmake --build . --config release

.\release\inference_cfile.exe --images_root=../images --weights_file=../convert_weights.pt --tile_width=6 --tile_height=4 --verbose=true
環境編譯(linux):

opencv編譯:進入opencv原始碼目錄,執行以下命令:

mkdir release

cd release

cmake -d cmake_build_type=release -d cmake_install_prefix=/usr/local ..

make

libtorch編譯:首先要進入conda環境中,再進入工程專案資料夾下,執行以下命令:

mkdir build

cd build

cmake -dcmake_prefix_path=/home/librah/workspace/libtorch-cpu(/libtorch-gpu) -dcmake_build_type=release ..

make

./inference_cfile --images_root=../images --weights_file=../convert_weights.pt --tile_width=6 --tile_height=4 --verbose=true
結果展示

windows系統:

linux系統:

參考文獻

[1] min k, corso j j. tased-net: temporally-aggregating spatial encoder-decoder network for video saliency detection[c]//proceedings of the ieee international conference on computer vision. 2019: 2394-2403.

[2]

[3] 在c++中載入torchscript模型:

說明

Pytorch 四 訓練模型

1.在前三部分已經分別完成了資料的處理 神經網路的定義,還有優化器 損失函式的選擇,這次我們將開始學習如何訓練模型。2.模型的訓練其實也是套路,大致分為一下五個部分。1 載入trainloader中的資料,並將其轉換為variable 形式的資料,作為神經網路的輸入。2 每次迴圈開始之前,確保梯度的...

pytorch模型訓練步驟

講解 textcnn模型 1.載入conf檔案 x import module models.model name config x.config dataset,embedding 2.載入model,初始化conf裡面值 model x.model config to config.device...

pytorch 修改預訓練模型

torchvision中提供了很多訓練好的模型,這些模型是在1000類,224 224的imagenet中訓練得到的,很多時候不適合我們自己的資料,可以根據需要進行修改。1 類別不同 coding utf 8 import torchvision.models as models 呼叫模型 mode...