examples/osrt_cpp/dlr/classification.cpp
//===============================================================================
/*! \brief Do inference on acquired images
 */
\brief Do inference on acquired images
\ brief Do inference on acquired images
void ProcessFrames(void ) {
  int index_top_results[1]; 
  std::vector<int64_t> output_sizes;
  int frame_cnt = 0;
  char tmp_string[160];
  int num_outputs;
  const int batch_size = 1;
  float *input_data = (float *)malloc(batch_size * RES_X * RES_Y * NUM_CHANNELS * sizeof(float));
  int64_t image_shape[4] = { batch_size, RES_Y, RES_X, NUM_CHANNELS };
  double fp_ms_avg = 0.0; 
  DLRModelHandle model;
  if (CreateDLRModel(&model, dlr_model, 1, 0) != 0) 
  {
    throw std::runtime_error("Could not load DLR Model");
  }
    GetDLRNumOutputs(&model, &num_outputs);
    for (int i = 0; i < num_outputs; i++) 
    {
      int64_t cur_size = 0;
      int cur_dim = 0;
      GetDLROutputSizeDim(&model, i, &cur_size, &cur_dim);
      output_sizes.push_back(cur_size);
    }
    std::vector<std::vector<float>> outputs;
    for (auto i : output_sizes) {
      outputs.push_back(std::vector<float>(i, 0));
    }
    std::cout << "Inference call (initialization) started...\n";
    memset(input_data, 0, batch_size * RES_X * RES_Y * NUM_CHANNELS * sizeof(float));
    if (SetDLRInput(&model, input_node_name.c_str(), image_shape, input_data, 4) != 0) 
    {
        throw std::runtime_error("Could not set input '" + input_node_name + "'");
    }
    if (RunDLRModel(&model) != 0) 
    {
        throw std::runtime_error("Could not run");
    }
    for (int i = 0; i < num_outputs; i++) 
    {
      if (GetDLROutput(&model, i, outputs[i].data()) != 0) 
      {
          throw std::runtime_error("Could not get output" + std::to_string(i));
      }
    }
    uint8_t *in_data_resize = (uint8_t*)malloc(NUM_CHANNELS*(RES_X*RES_Y));
    std::vector<uint8_t> in;
    std::string last_label = "None";
    int wanted_height   = 224;
    int wanted_width    = 224;
    int wanted_channels = 3;
    std::vector<float> mean = {123.675, 116.28, 103.53};
    std::vector<float> scale = {0.017125, 0.017507, 0.017429};
    preprocImage<float>(input_image, (float*)input_data, wanted_height, wanted_width, wanted_channels, mean, scale);
      std::cout << "Classifying input:" << input_image << std::endl;
        if (SetDLRInput(&model, input_node_name.c_str(), image_shape, input_data, 4) != 0) 
        {
        throw std::runtime_error("Could not set input '" + input_node_name + "'");
        }
        if (RunDLRModel(&model) != 0) 
        {
        throw std::runtime_error("Could not run");
        }
        for (int i = 0; i < num_outputs; i++) 
        {
        if (GetDLROutput(&model, i, outputs[i].data()) != 0) 
        {
          throw std::runtime_error("Could not get output" + std::to_string(i));
        }
        }
      for(int bcnt = 0; bcnt < BATCH_SIZE; bcnt++)
      {
        int argmax = 0;
        std::cout << "[" << (frame_cnt + bcnt) << "]";
        argmax = getTop_n(outputs, bcnt, index_top_results);
        if(argmax < vecOfLabels.size()) {
        last_label = vecOfLabels[argmax];
      }
      }
      frame_cnt += BATCH_SIZE;
      std::cout  << std::endl;
}
void ProcessFrames ( void ) { int index_top_results [ 1 ] ; std :: vector < int64_t > output_sizes ; int frame_cnt = 0 ; char tmp_string [ 160 ] ; int num_outputs ; const int batch_size = 1 ; float * input_data = ( float * ) malloc ( batch_size * RES_X * RES_Y * NUM_CHANNELS * sizeof ( float ) ) ; int64_t image_shape [ 4 ] = { batch_size , RES_Y , RES_X , NUM_CHANNELS } ; double fp_ms_avg = 0.0 ; DLRModelHandle model ; if ( CreateDLRModel ( & model , dlr_model , 1 , 0 ) != 0 ) { throw std :: runtime_error ( " " ) ; } GetDLRNumOutputs ( & model , & num_outputs ) ; for ( int i = 0 ; i < num_outputs ; i ++ ) { int64_t cur_size = 0 ; int cur_dim = 0 ; GetDLROutputSizeDim ( & model , i , & cur_size , & cur_dim ) ; output_sizes . push_back ( cur_size ) ; } std :: vector < std :: vector < float > > outputs ; for ( auto i : output_sizes ) { outputs . push_back ( std :: vector < float > ( i , 0 ) ) ; } std :: cout << " \n " ; memset ( input_data , 0 , batch_size * RES_X * RES_Y * NUM_CHANNELS * sizeof ( float ) ) ; if ( SetDLRInput ( & model , input_node_name . c_str ( ) , image_shape , input_data , 4 ) != 0 ) { throw std :: runtime_error ( " " + input_node_name + " " ) ; } if ( RunDLRModel ( & model ) != 0 ) { throw std :: runtime_error ( " " ) ; } for ( int i = 0 ; i < num_outputs ; i ++ ) { if ( GetDLROutput ( & model , i , outputs [ i ] . data ( ) ) != 0 ) { throw std :: runtime_error ( " " + std :: to_string ( i ) ) ; } } uint8_t * in_data_resize = ( uint8_t * ) malloc ( NUM_CHANNELS * ( RES_X * RES_Y ) ) ; std :: vector < uint8_t > in ; std :: string last_label = " " ; int wanted_height = 224 ; int wanted_width = 224 ; int wanted_channels = 3 ; std :: vector < float > mean = { 123.675 , 116.28 , 103.53 } ; std :: vector < float > scale = { 0.017125 , 0.017507 , 0.017429 } ; preprocImage < float > ( input_image , ( float * ) input_data , wanted_height , wanted_width , wanted_channels , mean , scale ) ; std :: cout << " " << input_image << std :: endl ; if ( SetDLRInput ( & model , input_node_name . c_str ( ) , image_shape , input_data , 4 ) != 0 ) { throw std :: runtime_error ( " " + input_node_name + " " ) ; } if ( RunDLRModel ( & model ) != 0 ) { throw std :: runtime_error ( " " ) ; } for ( int i = 0 ; i < num_outputs ; i ++ ) { if ( GetDLROutput ( & model , i , outputs [ i ] . data ( ) ) != 0 ) { throw std :: runtime_error ( " " + std :: to_string ( i ) ) ; } } for ( int bcnt = 0 ; bcnt < BATCH_SIZE ; bcnt ++ ) { int argmax = 0 ; std :: cout << " " << ( frame_cnt + bcnt ) << " " ; argmax = getTop_n ( outputs , bcnt , index_top_results ) ; if ( argmax < vecOfLabels . size ( ) ) { last_label = vecOfLabels [ argmax ] ; } } frame_cnt += BATCH_SIZE ; std :: cout << std :: endl ; }
\brief Do inference on acquired images
\ brief Do inference on acquired images
//Only Top1 results for now.. but can be expanded to top5 results //Initial inference time //First inference is dummy, initialization call!! //---------------------------------------------------------------------------- // Single batch, runs BATCH_SIZE inferences //----------------------------------------------------------------------------