最近在学习TensorRT,遇到一个问题,就是自己写的层中有些参数通过函数没法传入,如Renfindet SSD层需要的NMS阈值和置信度阈值都是写死在里面,无法又外界传入。所以想了个傻办法:将界面传入的参数保存得文本文件中,然后通过读取文本文件的字符串,将阈值使用extern 参数录入detect_out层,进行结果检测;
下面的代码是对文本文件进行解析,获取NMS阈值和置信度阈值;
float find_str(string str_src,string str_dest)
{
if (!str_src.empty())
{
int index = 0;
while( (index = str_src.find(' ',index)) != string::npos)
{
str_src.erase(index,1);
}
int end = str_src.find_first_not_of(str_dest);
if (end != -1){
// cout<<"3:"<<T.substr(end+1,-1)<<endl;
return atof(str_src.substr(end+1,-1).c_str());
}
else{
return 0.;
}
}
}
void Get_score()
{
// 定义输入文件流类对象infile
ifstream infile("/home/ubuntu/exsdcard/move_dection/config/configure.ini",ios::in);
if(!infile)
{ // 判断文件是否存在
cerr<<"open error."<<endl;
// exit(); // 退出程序
}
char str[255]; // 定义字符数组用来接受读取一行的数据
float nms_score = 0.;
float conf_score = 0.;
while(infile)
{
infile.getline(str,255); // getline函数可以读取整行并保存在str数组里
string src_string = str;
cout<<str<<endl;
cout<<src_string<<endl;
if(nms_score == 0.){
nms_score = find_str(src_string,"NmsThresh");
cout<<nms_score<<endl;
}
if(conf_score == 0.){
conf_score = find_str(src_string,"ClassesThresh");
cout<<conf_score<<endl;
}
}
thresh.conf_score = conf_score;
thresh.nms_score = nms_score;
}
补充:定义的结构体
typedef struct Obj_Score_
{
float conf_score;
float nms_score;
}Obj_score;
对结构体实例化:
Obj_score thresh;