Tensorflow .pb file in c++, opencv, placeholder tensor 'y_true' with dtype float and shape [?,3]











up vote
0
down vote

favorite












The python code runs fine, but the c++ code needs some fixing:
Tensorflow .pb file in c++, opencv, placeholder tensor 'y_true' with dtype float and shape [?,3]



I am having trouble getting the correct formats for c++. I can run the code in python.
One error message I receive in c++ is: You must feed a value for placeholder tensor 'y_true' with dtype float and shape [?,3]
[[Node: y_true = Placeholderdtype=DT_FLOAT, shape=[?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
The error message is because the result is not returned to the correct size array in c++, but I am having trouble understanding how to fix this.



The code is below: the lines I'm not understanding are where to put y_test_images in the c++ code:
pythyon:



feed_dict_testing = {x: x_batch, y_true: y_test_images}
result=sess.run(y_pred, feed_dict=feed_dict_testing)


c++:
I think I should put t somewhere, but not sure where:



tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));


This is the line that is not returning the correct result



   tensorflow::Status run_status  = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);


In python the following code works:



sess = tf.Session()
saver = tf.train.import_meta_graph('trained\break_up.meta')

sess.run(tf.global_variables_initializer())
saver.restore(sess, tf.train.latest_checkpoint('trained'))
graph = tf.get_default_graph()
y_pred = graph.get_tensor_by_name("y_pred:0")

x= graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, 3))

images2 = np.array(images_tf, dtype=np.uint8)
images2 = images2.astype('float32')
images2 = np.multiply(images2, 1.0/255.0)
x_batch = images2.reshape(1, 32,32,3)
feed_dict_testing = {x: x_batch, y_true: y_test_images}
result=sess.run(y_pred, feed_dict=feed_dict_testing)
print(result[0,0],result[0,1],result[0,2])


I am having trouble with the y_test_images numpy array on the c++ side.



  tensorflow::GraphDef graph_def;

std::string graphFile = "break_up2.pb";

tensorflow::Status graphLoadedStatus = ReadBinaryProto(tensorflow::Env::Default(),graphFile,&graph_def);
if (!graphLoadedStatus.ok()){
std::cout << graphLoadedStatus.ToString()<<std::endl;
return 1;
}
int node_count = graph_def.node_size();
for (int i = 0; i < node_count; i++)
{
auto n = graph_def.node(i);
std::cout<<"Names : "<< n.name() <<std::endl;
std::cout<<"OP : "<< n.op() <<std::endl;

}

std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
tensorflow::Status session_create_status = session_inception->Create(graph_def);
if (!session_create_status.ok()){
std::cout << session_create_status.ToString()<<std::endl;
return 1;
}

std::vector<tensorflow::Tensor> finalOutput;

std::string InputName = "x:0";
std::string OutputName = "y_true:0";
std::vector<float> data_y[3];
tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));
auto t_matrix = t.matrix<float>();
t_matrix(0, 0) = 1.0;
t_matrix(0, 1) = 0.5;
t_matrix(0, 2) = 0.0;

tensorflow::Status run_status = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);
if (!run_status.ok()){
std::cout << run_status.ToString() << "n";
return 1;
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    The python code runs fine, but the c++ code needs some fixing:
    Tensorflow .pb file in c++, opencv, placeholder tensor 'y_true' with dtype float and shape [?,3]



    I am having trouble getting the correct formats for c++. I can run the code in python.
    One error message I receive in c++ is: You must feed a value for placeholder tensor 'y_true' with dtype float and shape [?,3]
    [[Node: y_true = Placeholderdtype=DT_FLOAT, shape=[?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
    The error message is because the result is not returned to the correct size array in c++, but I am having trouble understanding how to fix this.



    The code is below: the lines I'm not understanding are where to put y_test_images in the c++ code:
    pythyon:



    feed_dict_testing = {x: x_batch, y_true: y_test_images}
    result=sess.run(y_pred, feed_dict=feed_dict_testing)


    c++:
    I think I should put t somewhere, but not sure where:



    tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));


    This is the line that is not returning the correct result



       tensorflow::Status run_status  = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);


    In python the following code works:



    sess = tf.Session()
    saver = tf.train.import_meta_graph('trained\break_up.meta')

    sess.run(tf.global_variables_initializer())
    saver.restore(sess, tf.train.latest_checkpoint('trained'))
    graph = tf.get_default_graph()
    y_pred = graph.get_tensor_by_name("y_pred:0")

    x= graph.get_tensor_by_name("x:0")
    y_true = graph.get_tensor_by_name("y_true:0")
    y_test_images = np.zeros((1, 3))

    images2 = np.array(images_tf, dtype=np.uint8)
    images2 = images2.astype('float32')
    images2 = np.multiply(images2, 1.0/255.0)
    x_batch = images2.reshape(1, 32,32,3)
    feed_dict_testing = {x: x_batch, y_true: y_test_images}
    result=sess.run(y_pred, feed_dict=feed_dict_testing)
    print(result[0,0],result[0,1],result[0,2])


    I am having trouble with the y_test_images numpy array on the c++ side.



      tensorflow::GraphDef graph_def;

    std::string graphFile = "break_up2.pb";

    tensorflow::Status graphLoadedStatus = ReadBinaryProto(tensorflow::Env::Default(),graphFile,&graph_def);
    if (!graphLoadedStatus.ok()){
    std::cout << graphLoadedStatus.ToString()<<std::endl;
    return 1;
    }
    int node_count = graph_def.node_size();
    for (int i = 0; i < node_count; i++)
    {
    auto n = graph_def.node(i);
    std::cout<<"Names : "<< n.name() <<std::endl;
    std::cout<<"OP : "<< n.op() <<std::endl;

    }

    std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
    tensorflow::Status session_create_status = session_inception->Create(graph_def);
    if (!session_create_status.ok()){
    std::cout << session_create_status.ToString()<<std::endl;
    return 1;
    }

    std::vector<tensorflow::Tensor> finalOutput;

    std::string InputName = "x:0";
    std::string OutputName = "y_true:0";
    std::vector<float> data_y[3];
    tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));
    auto t_matrix = t.matrix<float>();
    t_matrix(0, 0) = 1.0;
    t_matrix(0, 1) = 0.5;
    t_matrix(0, 2) = 0.0;

    tensorflow::Status run_status = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);
    if (!run_status.ok()){
    std::cout << run_status.ToString() << "n";
    return 1;
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The python code runs fine, but the c++ code needs some fixing:
      Tensorflow .pb file in c++, opencv, placeholder tensor 'y_true' with dtype float and shape [?,3]



      I am having trouble getting the correct formats for c++. I can run the code in python.
      One error message I receive in c++ is: You must feed a value for placeholder tensor 'y_true' with dtype float and shape [?,3]
      [[Node: y_true = Placeholderdtype=DT_FLOAT, shape=[?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
      The error message is because the result is not returned to the correct size array in c++, but I am having trouble understanding how to fix this.



      The code is below: the lines I'm not understanding are where to put y_test_images in the c++ code:
      pythyon:



      feed_dict_testing = {x: x_batch, y_true: y_test_images}
      result=sess.run(y_pred, feed_dict=feed_dict_testing)


      c++:
      I think I should put t somewhere, but not sure where:



      tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));


      This is the line that is not returning the correct result



         tensorflow::Status run_status  = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);


      In python the following code works:



      sess = tf.Session()
      saver = tf.train.import_meta_graph('trained\break_up.meta')

      sess.run(tf.global_variables_initializer())
      saver.restore(sess, tf.train.latest_checkpoint('trained'))
      graph = tf.get_default_graph()
      y_pred = graph.get_tensor_by_name("y_pred:0")

      x= graph.get_tensor_by_name("x:0")
      y_true = graph.get_tensor_by_name("y_true:0")
      y_test_images = np.zeros((1, 3))

      images2 = np.array(images_tf, dtype=np.uint8)
      images2 = images2.astype('float32')
      images2 = np.multiply(images2, 1.0/255.0)
      x_batch = images2.reshape(1, 32,32,3)
      feed_dict_testing = {x: x_batch, y_true: y_test_images}
      result=sess.run(y_pred, feed_dict=feed_dict_testing)
      print(result[0,0],result[0,1],result[0,2])


      I am having trouble with the y_test_images numpy array on the c++ side.



        tensorflow::GraphDef graph_def;

      std::string graphFile = "break_up2.pb";

      tensorflow::Status graphLoadedStatus = ReadBinaryProto(tensorflow::Env::Default(),graphFile,&graph_def);
      if (!graphLoadedStatus.ok()){
      std::cout << graphLoadedStatus.ToString()<<std::endl;
      return 1;
      }
      int node_count = graph_def.node_size();
      for (int i = 0; i < node_count; i++)
      {
      auto n = graph_def.node(i);
      std::cout<<"Names : "<< n.name() <<std::endl;
      std::cout<<"OP : "<< n.op() <<std::endl;

      }

      std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
      tensorflow::Status session_create_status = session_inception->Create(graph_def);
      if (!session_create_status.ok()){
      std::cout << session_create_status.ToString()<<std::endl;
      return 1;
      }

      std::vector<tensorflow::Tensor> finalOutput;

      std::string InputName = "x:0";
      std::string OutputName = "y_true:0";
      std::vector<float> data_y[3];
      tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));
      auto t_matrix = t.matrix<float>();
      t_matrix(0, 0) = 1.0;
      t_matrix(0, 1) = 0.5;
      t_matrix(0, 2) = 0.0;

      tensorflow::Status run_status = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);
      if (!run_status.ok()){
      std::cout << run_status.ToString() << "n";
      return 1;
      }









      share|improve this question













      The python code runs fine, but the c++ code needs some fixing:
      Tensorflow .pb file in c++, opencv, placeholder tensor 'y_true' with dtype float and shape [?,3]



      I am having trouble getting the correct formats for c++. I can run the code in python.
      One error message I receive in c++ is: You must feed a value for placeholder tensor 'y_true' with dtype float and shape [?,3]
      [[Node: y_true = Placeholderdtype=DT_FLOAT, shape=[?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
      The error message is because the result is not returned to the correct size array in c++, but I am having trouble understanding how to fix this.



      The code is below: the lines I'm not understanding are where to put y_test_images in the c++ code:
      pythyon:



      feed_dict_testing = {x: x_batch, y_true: y_test_images}
      result=sess.run(y_pred, feed_dict=feed_dict_testing)


      c++:
      I think I should put t somewhere, but not sure where:



      tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));


      This is the line that is not returning the correct result



         tensorflow::Status run_status  = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);


      In python the following code works:



      sess = tf.Session()
      saver = tf.train.import_meta_graph('trained\break_up.meta')

      sess.run(tf.global_variables_initializer())
      saver.restore(sess, tf.train.latest_checkpoint('trained'))
      graph = tf.get_default_graph()
      y_pred = graph.get_tensor_by_name("y_pred:0")

      x= graph.get_tensor_by_name("x:0")
      y_true = graph.get_tensor_by_name("y_true:0")
      y_test_images = np.zeros((1, 3))

      images2 = np.array(images_tf, dtype=np.uint8)
      images2 = images2.astype('float32')
      images2 = np.multiply(images2, 1.0/255.0)
      x_batch = images2.reshape(1, 32,32,3)
      feed_dict_testing = {x: x_batch, y_true: y_test_images}
      result=sess.run(y_pred, feed_dict=feed_dict_testing)
      print(result[0,0],result[0,1],result[0,2])


      I am having trouble with the y_test_images numpy array on the c++ side.



        tensorflow::GraphDef graph_def;

      std::string graphFile = "break_up2.pb";

      tensorflow::Status graphLoadedStatus = ReadBinaryProto(tensorflow::Env::Default(),graphFile,&graph_def);
      if (!graphLoadedStatus.ok()){
      std::cout << graphLoadedStatus.ToString()<<std::endl;
      return 1;
      }
      int node_count = graph_def.node_size();
      for (int i = 0; i < node_count; i++)
      {
      auto n = graph_def.node(i);
      std::cout<<"Names : "<< n.name() <<std::endl;
      std::cout<<"OP : "<< n.op() <<std::endl;

      }

      std::unique_ptr<tensorflow::Session> session_inception(tensorflow::NewSession(tensorflow::SessionOptions()));
      tensorflow::Status session_create_status = session_inception->Create(graph_def);
      if (!session_create_status.ok()){
      std::cout << session_create_status.ToString()<<std::endl;
      return 1;
      }

      std::vector<tensorflow::Tensor> finalOutput;

      std::string InputName = "x:0";
      std::string OutputName = "y_true:0";
      std::vector<float> data_y[3];
      tensorflow::Tensor t(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 3}));
      auto t_matrix = t.matrix<float>();
      t_matrix(0, 0) = 1.0;
      t_matrix(0, 1) = 0.5;
      t_matrix(0, 2) = 0.0;

      tensorflow::Status run_status = session_inception->Run({{InputName,input_tensor}},{{OutputName}},{},&finalOutput);
      if (!run_status.ok()){
      std::cout << run_status.ToString() << "n";
      return 1;
      }






      python c++ tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 5:35









      jester

      17218




      17218





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405805%2ftensorflow-pb-file-in-c-opencv-placeholder-tensor-y-true-with-dtype-float%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405805%2ftensorflow-pb-file-in-c-opencv-placeholder-tensor-y-true-with-dtype-float%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...