背景介绍:生成engine ,模型crnn,ubantu16.04
工程代码截图:
具体流程:
1. generate crnn.wts from pytorch
git clone https://github.com/wang-xinyu/tensorrtx.git
git clone https://github.com/meijieru/crnn.pytorch.git
// download its weights 'crnn.pth'
// copy tensorrtx/crnn/genwts.py into crnn.pytorch/
// go to crnn.pytorch/
python genwts.py
// a file 'crnn.wts' will be generated.
2. build tensorrtx/crnn and run
// put crnn.wts into tensorrtx/crnn
// go to tensorrtx/crnn
mkdir build
cd build
cmake ..
make
sudo ./crnn -s // serialize model to plan file i.e. 'crnn.engine'
// copy crnn.pytorch/data/demo.png here
sudo ./crnn -d // deserialize plan file and run inference
3. check the output as follows:
raw: a-----v--a-i-l-a-bb-l-e---
sim: available
sudo ./crnn -s时:
1. [E] [TRT] Parameter check failed at: ../builder/Network.cpp::addConvolutionNd::488, condition: kernelWeights.values != nullptr
问题在这里: IConvolutionLayer* conv = network->addConvolutionNd(input, nOut, DimsHW{ks_w[i], ks_h[i]}, weightMap["cnn.conv" + std::to_string(i) + ".weight"], weightMap["cnn.conv" + std::to_string(i) + ".bias"]);
在加载权重参数的时候,wts文件里网络层名字和代码里不一样。
解决方式:将名字修改成一致即可。
wts文件里网络层
crnn.cpp里网络层名字
问题原因:代码不熟悉,搬砖。
2.splitLstmWeights(weightMap, lname + ".weight_ih_l0");
lstm->setWeightsForGate(0, RNNGateType::kINPUT, true, weightMap[lname + ".weight_ih_l00"]);
这俩句代码网上讲解很少,个人理解是拆分权重并加载。
3.[E] [TRT] (Unnamed Layer* 23) [Fully Connected]: kernel weights has count 262144 but 131072 was expected
解决方式:如提示所说,实际的参数量与网络所需要的参数不同,说明网络构建的有问题。