YunYang1994 / tensorflow-yolov3

🔥 TensorFlow Code for technical report: "YOLOv3: An Incremental Improvement"
https://yunyang1994.gitee.io/2018/12/28/YOLOv3-算法的一点理解/
MIT License
3.63k stars 1.36k forks source link

我用c++接口调用已经训练好的pb模型,为什么提示Not found: FetchOutputs node pred_lbbox/concat_2: not found #307

Open BlackDeal opened 5 years ago

BlackDeal commented 5 years ago

int main(int argc, char* argv[]) {

string dataset = "../2.jpg"; string graph = "../yolov3_coco.pb"; string input_layer = "input/input_data"; //input ops string final_out = " pred_sbbox/concat_2, pred_mbbox/concat_2, pred_lbbox/concat_2"; //output ops string root_dir = "";

string classesFile = "coco.names"; std::ifstream ifs(classesFile.c_str()); string line; while (getline(ifs, line)) classes.push_back(line);

// We need to call this to set up global state for TensorFlow. tensorflow::port::InitMain(argv[0], &argc, &argv); if (argc > 1) { LOG(ERROR) << "Unknown argument " << argv[1] << "\n"; return -1; }

// First we load and initialize the model. std::unique_ptr session; string graph_path = tensorflow::io::JoinPath(root_dir, graph); //std::cout<<graph_path<<endl; Status load_graph_status = LoadGraph(graph_path, &session); // std::cout<<load_graph_status<<endl; // return -1; if (!load_graph_status.ok()) { LOG(ERROR) << load_graph_status; return -1; } cv::VideoCapture cap; // std::cout<<cap.open(0)<<endl; //return 0; //if(!cap.open(0)) { // return 0; //}

//for(;;) {

cv::Mat srcImage, rgbImage;
srcImage = cv::imread(dataset,1);
//cap >> srcImage;
if(srcImage.empty()){
    //break;
    return -1;
}
cv::cvtColor(srcImage, rgbImage, CV_BGR2RGB);
cv::Mat rgbImage_new = rgbImage.reshape(4,0);
//rgbImage.convertTo(rgbImage,CV_32FC3);
//rgbImage = rgbImage/255;
//cv::Mat padImage = resizeKeepAspectRatio(rgbImage, YOLOV3_SIZE, YOLOV3_SIZE);

Tensor resized_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, YOLOV3_SIZE, YOLOV3_SIZE, IMG_CHANNELS}));
CVMat_to_Tensor(rgbImage_new,&resized_tensor,YOLOV3_SIZE, YOLOV3_SIZE);
//std::cout<<resized_tensor.shape().DebugString()<<endl;
Status read_tensor_status = readTensorFromMat(rgbImage, resized_tensor);
if (!read_tensor_status.ok()) {
  LOG(ERROR) << read_tensor_status;
  return -1;
}

// Actually run the image through the model.
const string tmp_l = final_out;
const string par = ",";
std::vector<string> node_out= split(tmp_l,par);
//std::cout<<node_out[0].size()<<endl;
//return -1;
std::vector<Tensor> outputs;
Status run_status = session->Run({{input_layer, resized_tensor}},
                                 {node_out}, {}, &outputs);

// std::cout<<outputs[0].shape().DebugString()<<endl; //std::cout<<outputs[1].shape().DebugString()<<endl; //std::cout<<outputs[2].shape().DebugString()<<endl; //return -1; if (!run_status.ok()) { LOG(ERROR) << "Running model failed: " << run_status; return -1; } tensorflow::Scope con_name = tensorflow::Scope::NewRootScope();

const int num_classes = 80;
tensorflow::Tensor image_features_reshaped1(tensorflow::DT_FLOAT,
                                           tensorflow::TensorShape({-1,5+num_classes}));
tensorflow::Tensor image_features_reshaped2(tensorflow::DT_FLOAT,
                                           tensorflow::TensorShape({-1,5+num_classes}));
tensorflow::Tensor image_features_reshaped3(tensorflow::DT_FLOAT,
                                           tensorflow::TensorShape({-1,5+num_classes}));
image_features_reshaped1.CopyFrom(outputs[0], tensorflow::TensorShape({-1,5+num_classes}));
image_features_reshaped2.CopyFrom(outputs[1], tensorflow::TensorShape({-1,5+num_classes}));
image_features_reshaped3.CopyFrom(outputs[2], tensorflow::TensorShape({-1,5+num_classes}));
std::vector<Tensor> run_con;
auto outputs_con = tensorflow::ops::Concat(con_name.WithOpName("concat_outs"),\
{image_features_reshaped1,image_features_reshaped2,image_features_reshaped3},0);

tensorflow::ClientSession session2(con_name);
TF_CHECK_OK(session2.Run({outputs_con}, &run_con));

float *p = run_con[0].flat<float>().data();
 cv::Mat result(run_con[0].dim_size(1), run_con[0].dim_size(2), CV_32FC(1), p);
 std::vector<cv::Mat> outs;
 outs.push_back (result);
//Tensor tmp = outputs_con.output();

// tfTensor2cvMat(run_con[0],outs); postprocess(rgbImage, outs);

cv::cvtColor(rgbImage, srcImage , CV_RGB2BGR);
cv::imshow( "Yolov3", srcImage );
while( cv::waitKey(10) != 27 )
{}//break;

// } return 0; }

BlackDeal commented 5 years ago

I got the reason!!

sainisanjay commented 5 years ago

Hi @BlackDeal, May i know how you get to know that output nodes are "pred_sbbox/concat_2, pred_mbbox/concat_2, pred_lbbox/concat_2"; //output ops" Because when i am trying to convert model into .dlc(which is Qualcomm SNPE format) its says that
ERROR_TF_NODE_NOT_FOUND_IN_GRAPH: Node not found in graph. Node name: pred_sbbox/concat_2

774219268 commented 4 years ago

问题解决了吗?我也遇到类似问题想请教下,yolo3一共有三个尺度输出,你调用的时候是三个一起调用进行筛选?还是只选取里面一个输出啊?我是要部署在tensorflow安卓 demo上,但是输出的配置output name只有一个值需要修改

Byronnar commented 4 years ago

c++调用 pb模型, 应该需要先将 模型转换成 saved model格式, concat一个输出output name.

774219268 commented 4 years ago

也就是说,您调用的时候,三个名字都进行了调用是吧,然后经过了一个筛选的过程。方便加下微信吗?或者QQ

------------------ 原始邮件 ------------------ 发件人: "Byronnar"<notifications@github.com>; 发送时间: 2019年11月20日(星期三) 上午9:36 收件人: "YunYang1994/tensorflow-yolov3"<tensorflow-yolov3@noreply.github.com>; 抄送: "烧火棍"<774219268@qq.com>; "Comment"<comment@noreply.github.com>; 主题: Re: [YunYang1994/tensorflow-yolov3] 我用c++接口调用已经训练好的pb模型,为什么提示Not found: FetchOutputs node pred_lbbox/concat_2: not found (#307)

c++调用 pb模型, 应该需要先将 模型转换成 saved model格式, concat一个输出output name.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

774219268 commented 4 years ago

好的,我像您说的大概进行了组合,对三个output-name进行了拼接 private static final String YOLO_OUTPUT_NAMES = "pred_sbbox/concat_2,pred_mbbox/concat_2,pred_lbbox/concat_2"; 我不知道这样在安卓里面是否正确,但是我做了这样的修改以后并没有报错

Byronnar commented 4 years ago

你用的代码就是上面那个? 我concat之后 ,可以在serving端部署.

leiup commented 4 years ago

yolov3 c++ tensorflow inference https://blog.csdn.net/helei001/article/details/109050576