SHARE
TWEET

Untitled

a guest Oct 17th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sample::Sample(){
  2.     LOG(INFO) << "Initializing model";
  3.     struct timeval begint, endt;
  4.     gettimeofday(&begint, NULL);                    
  5.     TF_Buffer* graph_def = read_file("./models.pb");                      
  6.     tf_graph_ = TF_NewGraph();
  7.     // Import graph_def into graph                                                          
  8.     TF_Status* status = TF_NewStatus();                                                    
  9.     TF_ImportGraphDefOptions* opts = TF_NewImportGraphDefOptions();                        
  10.     TF_GraphImportGraphDef(tf_graph_, graph_def, opts, status);
  11.     TF_DeleteImportGraphDefOptions(opts);
  12.     if (TF_GetCode(status) != TF_OK) {
  13.         LOG(INFO) << "ERROR: Unable to import graph " << TF_Message(status);
  14.         TF_DeleteStatus(status);
  15.         TF_DeleteBuffer(graph_def);
  16.         TF_DeleteGraph(tf_graph_);
  17.         return;
  18.     }      
  19.     LOG(INFO) << "Successfully imported graph";  
  20.     gettimeofday(&endt, NULL);
  21.     float interval = (endt.tv_sec - begint.tv_sec) + ((float)endt.tv_usec - begint.tv_usec) / 1000000;
  22.     LOG(INFO) << "Imported graph time is "<< interval << " secs";
  23.  
  24.     inputs_.resize(3);
  25.     inputs_[0].oper = TF_GraphOperationByName(tf_graph_, "input0");
  26.     inputs_[0].index = 0;
  27.     inputs_[1].oper = TF_GraphOperationByName(tf_graph_, "input1");
  28.     inputs_[1].index = 0;
  29.     inputs_[2].oper = TF_GraphOperationByName(tf_graph_, "input2");
  30.     inputs_[2].index = 0;
  31.     output_.oper = TF_GraphOperationByName(tf_graph_, "output");
  32.     output_.index = 0;
  33.  
  34.     LOG(INFO) << "Creating session...";  
  35.     gettimeofday(&begint, NULL);
  36.     TF_SessionOptions* options = TF_NewSessionOptions();
  37.     TF_SetConfig(options, session_config, strlen(session_config), status);
  38.     if (TF_GetCode(status) != TF_OK) {
  39.         LOG(INFO) << "ERROR: Unable to read session config: " << TF_Message(status);
  40.         TF_DeleteBuffer(graph_def);
  41.         TF_DeleteGraph(tf_graph_);
  42.         TF_DeleteStatus(status);
  43.         TF_DeleteSessionOptions(options);
  44.         return;
  45.     }
  46.     tf_session_ = TF_NewSession(tf_graph_, options, status);
  47.     TF_DeleteSessionOptions(options);
  48.     if (TF_GetCode(status) != TF_OK) {
  49.         LOG(INFO) << "ERROR: Unable to create session: " << TF_Message(status);
  50.         TF_DeleteBuffer(graph_def);
  51.         TF_DeleteGraph(tf_graph_);
  52.         TF_CloseSession(tf_session_, status);
  53.         TF_DeleteSession(tf_session_, status);
  54.         TF_DeleteStatus(status);
  55.         return;
  56.     }
  57.     TF_DeleteBuffer(graph_def);
  58.     gettimeofday(&endt, NULL);
  59.     interval = (endt.tv_sec - begint.tv_sec) + ((float)endt.tv_usec - begint.tv_usec) / 1000000;
  60.     LOG(INFO) << "Create sessioin time is "<< interval << " secs";
  61.     // warm-up session
  62.     TF_Tensor* input_values[3];
  63.     std::int64_t local_feature_shape[3] = {1, 1000, 64};
  64.     std::int64_t region_feature_shape[4] = {1, 125, 125, 256};
  65.     std::int64_t kpts_shape[3] = {1, 1000, 2};
  66.     input_values[0] = create_tensor(TF_FLOAT, local_feature_shape, 3,
  67.                                     nullptr, 1000 * 64 * sizeof(float));
  68.     input_values[1] = create_tensor(TF_FLOAT, region_feature_shape, 4,
  69.                                     nullptr, 125 * 125 * 256 * sizeof(float));
  70.     input_values[2] = create_tensor(TF_FLOAT, kpts_shape, 3,
  71.                                     nullptr, 1000 * 2 * sizeof(float));
  72.     TF_Tensor* output_value;
  73.     LOG(INFO) << "tf session warmming up...";
  74.     gettimeofday(&begint, NULL);
  75.     TF_SessionRun(tf_session_,
  76.                   nullptr, // Run options.
  77.                   &inputs_[0], &input_values[0], 3, // Input tensors, input tensor values, number of inputs.
  78.                   &output_, &output_value, 1, // Output tensors, output tensor values, number of outputs.
  79.                   nullptr, 0, // Target operations, number of targets.
  80.                   nullptr, // Run metadata.
  81.                   status // Output status.
  82.     );
  83.     gettimeofday(&endt, NULL);
  84.     interval = (endt.tv_sec - begint.tv_sec) + ((float)endt.tv_usec - begint.tv_usec) / 1000000;
  85.     LOG(INFO) << "tf session run time is "<< interval << " secs";
  86.     delete_tensor(input_values[0]);
  87.     delete_tensor(input_values[1]);
  88.     delete_tensor(input_values[2]);
  89.     if (TF_GetCode(status) != TF_OK) {
  90.         TF_DeleteStatus(status);
  91.         delete_tensor(output_value);
  92.         LOG(INFO) << "tf session warm up failed";
  93.         return;
  94.     }
  95.     TF_DeleteStatus(status);
  96.     delete_tensor(output_value);
  97.     LOG(INFO) << "tf session warm up success";
  98. };
  99. Sample::~Sample()
  100. {
  101.     if (tf_graph_)
  102.         TF_DeleteGraph(tf_graph_);
  103.     TF_Status* status = TF_NewStatus();                                                    
  104.     TF_CloseSession(tf_session_, status);
  105.     if (TF_GetCode(status) != TF_OK)
  106.         LOG(INFO) << "ERROR: Unable to close session: " << TF_Message(status);
  107.     else
  108.         LOG(INFO) << "close session successfully";
  109.     TF_DeleteSession(tf_session_, status);
  110.     if (TF_GetCode(status) != TF_OK)
  111.         LOG(INFO) << "ERROR: Unable to delete session: " << TF_Message(status);
  112.     else
  113.         LOG(INFO) << "delete session successfully";
  114.     TF_DeleteStatus(status);
  115. }
  116. void Sample::compute(const cv::Mat& cv_img,
  117.                                  const std::vector<float>& keypoints,
  118.                                  UMat& descriptors)
  119. {
  120.     // code prepare input data
  121.     ...
  122.     TF_Status* status = TF_NewStatus();
  123.     size_t kpts_num = keypoints.size() / 2;
  124.     TF_Tensor* input_values[3];
  125.     std::int64_t local_feature_shape[3] = {1, 1000, 64};
  126.     std::int64_t region_feature_shape[4] = {1, 125, 125, 256};
  127.     std::int64_t kpts_shape[3] = {1, 1000, 2};
  128.     gettimeofday(&begint, NULL);
  129.     input_values[0] = create_tensor(TF_FLOAT, local_feature_shape, 3,
  130.                                     data_ptr1, kpts_num * 64 * sizeof(float));
  131.     input_values[1] = create_tensor(TF_FLOAT, region_feature_shape, 4,
  132.                                     data_ptr2, output_shape.count() * sizeof(float));
  133.     input_values[2] = create_tensor(TF_FLOAT, kpts_shape, 3,
  134.                                     data_ptr3, keypoints.size() * sizeof(float));
  135.     gettimeofday(&endt, NULL);
  136.     interval = (endt.tv_sec - begint.tv_sec) + ((float)endt.tv_usec - begint.tv_usec) / 1000000;
  137.     LOG(INFO) << "prepare input time is "<< interval << " secs";
  138.  
  139.     TF_Tensor* output_value;
  140.     LOG(INFO) << "tf session running...";
  141.     gettimeofday(&begint, NULL);
  142.     TF_SessionRun(tf_session_,
  143.                   nullptr, // Run options.
  144.                   &inputs_[0], &input_values[0], 3, // Input tensors, input tensor values, number of inputs.
  145.                   &output_, &output_value, 1, // Output tensors, output tensor values, number of outputs.
  146.                   nullptr, 0, // Target operations, number of targets.
  147.                   nullptr, // Run metadata.
  148.                   status // Output status.
  149.     );
  150.     gettimeofday(&endt, NULL);
  151.     interval = (endt.tv_sec - begint.tv_sec) + ((float)endt.tv_usec - begint.tv_usec) / 1000000;
  152.     LOG(INFO) << "tf session run time is "<< interval << " secs";
  153.     LOG(INFO) << "tf session finish...";
  154.     delete_tensor(input_values[0]);
  155.     delete_tensor(input_values[1]);
  156.     delete_tensor(input_values[2]);
  157.     if (TF_GetCode(status) != TF_OK) {
  158.         TF_DeleteStatus(status);
  159.         delete_tensor(output_value);
  160.         return;
  161.     }
  162.     CHECK_EQ(TF_TensorType(output_value), TF_FLOAT) << "output type error";
  163.     LOG(INFO) << "result num dims:" << TF_NumDims(output_value);
  164. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top