title: How to increase BatchSize with Tensorflow's C++ API?
body: <p>I took the code in <a href="https://gist.github.com/kyrs/9adf86366e9e4f04addb" rel="nofollow noreferrer">https://gist.github.com/kyrs/9adf86366e9e4f04addb</a> (which takes an opencv cv::Mat image as input and converts it to tensor) and I use it to label images with the  model <strong>inception_v3_2016_08_28_frozen.pb</strong> stated in the Tensorflow tutorial (<a href="https://www.tensorflow.org/tutorials/image_recognition#usage_with_the_c_api" rel="nofollow noreferrer">https://www.tensorflow.org/tutorials/image_recognition#usage_with_the_c_api</a>). Everything worked fine when using a batchsize of 1. However, when I increase the batchsize to 2 (or greater), the size of 
  <strong>finalOutput</strong> (which is of type std::vector) is zero. </p>
  
  <p>Here's the code to reproduce the error:</p>
  
  <pre><code>// Only for VisualStudio
  #define COMPILER_MSVC
  #define NOMINMAX
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
  #include "tensorflow/core/public/session.h"
  #include "tensorflow/core/platform/env.h"
  #include "tensorflow/core/framework/tensor.h"
  
  int batchSize = 2;
  int height = 299;
  int width = 299;
  int depth = 3;
  
  int mean = 0;
  int stdev = 255;
  
  // Set image paths
  cv::String pathFilenameImg1 = "D:/IMGS/grace_hopper.jpg";
  cv::String pathFilenameImg2 = "D:/IMGS/lenna.jpg";
  
  // Set model paths
  std::string graphFile = "D:/Tensorflow/models/inception_v3_2016_08_28_frozen.pb";
  std::string labelfile = "D:/Tensorflow/models/imagenet_slim_labels.txt";
  std::string InputName = "input";
  std::string OutputName = "InceptionV3/Predictions/Reshape_1";
  
  
  void read_prepare_image(cv::String pathImg, cv::Mat &amp;imgPrepared) {
  
         // Read Color image:
         cv::Mat imgBGR = cv::imread(pathImg);
  
         // Now we resize the image to fit Model's expected sizes:
         cv::Size s(height, width);
         cv::Mat imgResized;
         cv::resize(imgBGR, imgResized, s, 0, 0, cv::INTER_CUBIC);
  
         // Convert the image to float and normalize data:
         imgResized.convertTo(imgPrepared, CV_32FC1);
         imgPrepared = imgPrepared - mean;
         imgPrepared = imgPrepared / stdev;
  
  }
  
  int main()
  {
         // Read and prepare images using OpenCV:
         cv::Mat img1, img2;
         read_prepare_image(pathFilenameImg1, img1);
         read_prepare_image(pathFilenameImg2, img2);
  
         // creating a Tensor for storing the data
         tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ batchSize, height, width, depth }));
         auto input_tensor_mapped = input_tensor.tensor&lt;float, 4&gt;();
  
         // Copy images data into the tensor:
         for (int b = 0; b &lt; batchSize; ++b) {
  
               const float * source_data;
  
               if (b == 0) 
                      source_data = (float*)img1.data;
               else 
                      source_data = (float*)img2.data;
  
               for (int y = 0; y &lt; height; ++y) {
  
                      const float* source_row = source_data + (y * width * depth);
                      for (int x = 0; x &lt; width; ++x) {
  
                             const float* source_pixel = source_row + (x * depth);
                             const float* source_B = source_pixel + 0;
                             const float* source_G = source_pixel + 1;
                             const float* source_R = source_pixel + 2;
  
                             input_tensor_mapped(b, y, x, 0) = *source_R;
                             input_tensor_mapped(b, y, x, 1) = *source_G;
                             input_tensor_mapped(b, y, x, 2) = *source_B;
  
                      }
               }
         }
  
         // Load the graph:
         tensorflow::GraphDef graph_def;
         ReadBinaryProto(tensorflow::Env::Default(), graphFile, &amp;graph_def);
  
         // create a session with the graph
         std::unique_ptr&lt;tensorflow::Session&gt; session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
         session_inception-&gt;Create(graph_def);
  
         // run the loaded graph 
         std::vector&lt;tensorflow::Tensor&gt; finalOutput;
         session_inception-&gt;Run({ { InputName,input_tensor } }, { OutputName }, {}, &amp;finalOutput);
  
         // Get Top 5 classes:
         std::cerr &lt;&lt; "final output size = " &lt;&lt; finalOutput.size() &lt;&lt; std::endl;
         tensorflow::Tensor output = std::move(finalOutput.at(0));
         auto scores = output.flat&lt;float&gt;();
         std::cerr &lt;&lt; "scores size=" &lt;&lt; scores.size() &lt;&lt; std::endl;
  
         std::ifstream label(labelfile);
         std::string line;
  
         std::vector&lt;std::pair&lt;float, std::string&gt;&gt; sorted;
  
         for (unsigned int i = 0; i &lt;= 1000; ++i) {
               std::getline(label, line);
               sorted.emplace_back(scores(i), line);
         }
  
         std::sort(sorted.begin(), sorted.end());
         std::reverse(sorted.begin(), sorted.end());
         std::cout &lt;&lt; "size of the sorted file is " &lt;&lt; sorted.size() &lt;&lt; std::endl;
         for (unsigned int i = 0; i&lt; 5; ++i)
               std::cout &lt;&lt; "The output of the current graph has category  " &lt;&lt; sorted[i].second &lt;&lt; " with probability " &lt;&lt; sorted[i].first &lt;&lt; std::endl;
  
  }
  </code></pre>
  
  <p>Do I miss anything? Any ideas?</p>
  
  <p>Thanks in advance!</p>