Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ example to run object detection models #1741

Closed
terrydl opened this issue Jun 23, 2017 · 27 comments
Closed

C++ example to run object detection models #1741

terrydl opened this issue Jun 23, 2017 · 27 comments
Labels
stat:awaiting model gardener Waiting on input from TensorFlow model gardener

Comments

@terrydl
Copy link

terrydl commented Jun 23, 2017

Could you please help to add a C++ example to run the object detection models, similar to the python example object_detection_tutorial.ipynb? I am very interesting to run the object detection model Android platform.

@tatatodd
Copy link

Sounds like a reasonable request to me. Adding @jch1 @derekjchow for visibility.

Also marking as "contributions welcome", since someone from the community could help with this.

@yeephycho
Copy link

I tried to modify the image_label C++ code to run the detection.
The problem is that the output tensor data type is different from the image_label, I tried to fetch detection:0 something tensor, when I run the binary, there's a error saying that the output float is different from uint8 which I presume is a serial of chars.
Still waiting for any related documents or examples or any clue to manipulation on the tensors in C++.

@terrydl
Copy link
Author

terrydl commented Jun 26, 2017

Yes, I also found that there were no equivalent tensorflow C++ APIs to do the same as Python APIs as below:

  image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  # Each box represents a part of the image where a particular object was detected.
  boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  # Each score represent how level of confidence for each of the objects.
  # Score is shown on the result image, together with the class label.
  scores = detection_graph.get_tensor_by_name('detection_scores:0')
  classes = detection_graph.get_tensor_by_name('detection_classes:0')
  num_detections = detection_graph.get_tensor_by_name('num_detections:0')

@satok16 satok16 assigned satok16 and tombstone and unassigned satok16 Jun 26, 2017
@tombstone tombstone added stat:awaiting model gardener Waiting on input from TensorFlow model gardener and removed help wanted labels Jun 28, 2017
@khegelich360Fly
Copy link

I ported the object detection example over to C# using TensorFlowSharp https://github.com/migueldeicaza/TensorFlowSharp. Using the C++ API directly should be similar.
See Code below:

class MainClass
{
    static string ModelsFile = @"ssd_mobilenet_v1_coco_11_06_2017\frozen_inference_graph.pb";
    static string LabelsFile = @"data\mscoco_label_map.pbtxt";
    static string ImageFile1 = @"test_images\image1.jpg";
    static string ImageFile2 = @"test_images\image2.jpg";

    public static void Main(string[] args)
    {
        var files = new List<string>() { ImageFile1, ImageFile2 };

        var graph = new TFGraph();
        var model = File.ReadAllBytes(ModelsFile);

        graph.Import(model, "");
        using (var session = new TFSession(graph))
        {
            // read labels
            String[] labels = File.ReadAllLines(LabelsFile);
            String[] final_labels = new String[labels.Length];

            int itemId = 0;
            for(int i=0; i<labels.Length; i++)
            {
                if (labels[i].Contains("id:"))
                {
                    int startCut = labels[i].LastIndexOf(" ") + 1;
                    String subString = labels[i].Substring(startCut, labels[i].Length - startCut);
                    itemId = Int32.Parse(subString);
                }
                if (labels[i].Contains("display_name:"))
                {
                    int startCut = labels[i].IndexOf("\"") + 1;
                    String subString = labels[i].Substring(startCut, labels[i].Length - 1 - startCut);
                    final_labels[itemId] = subString;
                }
            }
            
            // Loop over test images
            foreach (var file in files)
            {
                var tensor = CreateTensorFromImageFile(file);

                var runner = session.GetRunner();
                if (runner == null || tensor == null)
                {
                    Console.WriteLine($"runner or tensor is null!?");
                    Environment.Exit(1);
                }

                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                String[] outputs = { "detection_boxes:0", "detection_scores:0", 
                                                "detection_classes:0", "num_detections:0" };
                runner.AddInput("image_tensor:0", tensor).Fetch(outputs);

                TFTensor[] output = null;
                try
                {
                    output = runner.Run();
                }
                catch(TFException e)
                {
                    Console.WriteLine(e.ToString());
                }

                long[] dimens = tensor.Shape;
                int image_width = (int)dimens[2];
                int image_height = (int)dimens[1];
                float numDetections = ((float[])output[3].GetValue(true))[0];
                var boxes = ((float[][][])output[0].GetValue(true))[0];
                float[] scores = ((float[][])output[1].GetValue(true))[0];
                float[] classes = ((float[][])output[2].GetValue(true))[0];

                if (numDetections > 0) {
                    for (int i = 0; i < Math.Min(20, numDetections); i++)
                    {
                        if (scores[i] > 0.5)
                        {
                            double y = boxes[i][0] * image_height;
                            double x = boxes[i][1] * image_width;
                            double height = (boxes[i][2]*image_height) - (boxes[i][0]*image_height);
                            double width = (boxes[i][3]*image_width) - (boxes[i][1]*image_width);
                            Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
                            String class_name = final_labels[(int)classes[i]];
                            Console.WriteLine("Class: {0}, Confidence: {1}, Rect: {2}", class_name, scores[i], rect.ToString());
                        }
                    }
                }
                    
                stopWatch.Stop();
                Console.WriteLine("RunTime: {0}", stopWatch.ElapsedMilliseconds);
            }
        }
    }

    static TFTensor CreateTensorFromImageFile(string file)
    {
        var contents = File.ReadAllBytes(file);

        // DecodeJpeg uses a scalar String-valued tensor as input.
        var tensor = TFTensor.CreateString(contents);

        TFGraph graph;
        TFOutput input, output;

        // Construct a graph to normalize the image
        ConstructGraphToNormalizeImage(out graph, out input, out output);

        // Execute that graph to normalize this one image
        using (var session = new TFSession(graph))
        {
            var normalized = session.Run(
                     inputs: new[] { input },
                     inputValues: new[] { tensor },
                     outputs: new[] { output });

            return normalized[0];
        }
    }

    static void ConstructGraphToNormalizeImage(out TFGraph graph, out TFOutput input, out TFOutput output)
    {
        graph = new TFGraph();
        input = graph.Placeholder(TFDataType.String);
        output = graph.DecodeJpeg(contents: input, channels: 3);
        output = graph.ExpandDims(input: graph.DecodeJpeg(contents: input, channels: 3), dim: graph.Const(0, "make_batch"));
    }
}

@shresthamalik
Copy link

I am also trying to modify the examples/label_image main.cc to export the trained model to C++. It seems the expected input type is uint8 (exact error: Running model failed: Invalid argument: Expects arg[0] to be uint8 but float is provided) . In the function ReadTensorFromImageFile() , I change the casting datatype to uint8, auto float_caster = Cast(root.WithOpName("uint8_caster"), image_reader, tensorflow::DataType::DT_UINT8); but still the tensor returned is of type float? Is this a casting issue?

As, this https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/ops/math_ops.cc says casting is supported for only few datatypes, is uint8 not supported? Am I looking at the right place for Cast() operator?

Any tips to convert C++ tensor to uint8?

@shresthamalik
Copy link

The uint8 happened because ResizeBillinear() converts the tensor back into float. We can get uint8 tensor by using Cast() at the end ,i.e. after scaling and normalizing. The C++ detection is running fine for me after this.

@AdamHeavens
Copy link

Would you mind sharing the code?

@daisyl0
Copy link

daisyl0 commented Jul 28, 2017

here is my code,I'd like to share it. I edit it from label_image.cc
it's not perfect,but it runs fine for me for now.

/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// A minimal but useful C++ example showing how to load an Imagenet-style object
// recognition TensorFlow model, prepare input images for it, run them through
// the graph, and interpret the results.
//
// It's designed to have as few dependencies and be as clear as possible, so
// it's more verbose than it could be in production code. In particular, using
// auto for the types of a lot of the returned values from TensorFlow calls can
// remove a lot of boilerplate, but I find the explicit types useful in sample
// code to make it simple to look up the classes involved.
//
// To use it, compile and then run in a working directory with the
// learning/brain/tutorials/label_image/data/ folder below it, and you should
// see the top five labels for the example Lena image output. You can then
// customize it to use your own models or images by changing the file names at
// the top of the main() function.
//
// The googlenet_graph.pb file included by default is created from Inception.
//
// Note that, for GIF inputs, to reuse existing code, only single-frame ones
// are supported.

#include <fstream>
#include <utility>
#include <vector>
#include <iostream>

#include "tensorflow/cc/ops/const_op.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/default_device.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/command_line_flags.h"

// These are all common classes it's handy to reference with no namespace.
using tensorflow::Flag;
using tensorflow::Tensor;
using tensorflow::Status;
using tensorflow::string;
using tensorflow::int32;

using namespace std;

// Takes a file name, and loads a list of labels from it, one per line, and
// returns a vector of the strings. It pads with empty strings so the length
// of the result is a multiple of 16, because our model expects that.
Status ReadLabelsFile(const string& file_name, std::vector<string>* result,
                      size_t* found_label_count) {
  std::ifstream file(file_name);
  if (!file) {
    return tensorflow::errors::NotFound("Labels file ", file_name,
                                        " not found.");
  }
  result->clear();
  string line;
  while (std::getline(file, line)) {
    result->push_back(line);
  }
  *found_label_count = result->size();
  const int padding = 16;
  while (result->size() % padding) {
    result->emplace_back();
  }
  return Status::OK();
}

static Status ReadEntireFile(tensorflow::Env* env, const string& filename,
                             Tensor* output) {
  tensorflow::uint64 file_size = 0;
  TF_RETURN_IF_ERROR(env->GetFileSize(filename, &file_size));

  string contents;
  contents.resize(file_size);

  std::unique_ptr<tensorflow::RandomAccessFile> file;
  TF_RETURN_IF_ERROR(env->NewRandomAccessFile(filename, &file));

  tensorflow::StringPiece data;
  TF_RETURN_IF_ERROR(file->Read(0, file_size, &data, &(contents)[0]));
  if (data.size() != file_size) {
    return tensorflow::errors::DataLoss("Truncated read of '", filename,
                                        "' expected ", file_size, " got ",
                                        data.size());
  }
  output->scalar<string>()() = data.ToString();
  return Status::OK();
}

// Given an image file name, read in the data, try to decode it as an image,
// resize it to the requested size, and then scale the values as desired.
Status ReadTensorFromImageFile(const string& file_name, const int input_height,
                               const int input_width, const float input_mean,
                               const float input_std,
                               std::vector<Tensor>* out_tensors) {
  auto root = tensorflow::Scope::NewRootScope();
  using namespace ::tensorflow::ops;  // NOLINT(build/namespaces)

  string input_name = "file_reader";
  string output_name = "normalized";

  // read file_name into a tensor named input
  Tensor input(tensorflow::DT_STRING, tensorflow::TensorShape());
  TF_RETURN_IF_ERROR(
      ReadEntireFile(tensorflow::Env::Default(), file_name, &input));

  // use a placeholder to read input data
  auto file_reader =
      Placeholder(root.WithOpName("input"), tensorflow::DataType::DT_STRING);

  std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
      {"input", input},
  };

  // Now try to figure out what kind of file it is and decode it.
  const int wanted_channels = 3;
  tensorflow::Output image_reader;
  if (tensorflow::StringPiece(file_name).ends_with(".png")) {
    image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
                             DecodePng::Channels(wanted_channels));
  } else if (tensorflow::StringPiece(file_name).ends_with(".gif")) {
    // gif decoder returns 4-D tensor, remove the first dim
    image_reader =
        Squeeze(root.WithOpName("squeeze_first_dim"),
                DecodeGif(root.WithOpName("gif_reader"), file_reader));
  } else {
    // Assume if it's neither a PNG nor a GIF then it must be a JPEG.
    image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
                              DecodeJpeg::Channels(wanted_channels));
  }
  // Now cast the image data to float so we can do normal math on it.
  // auto float_caster =
  //     Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT);

    auto uint8_caster =  Cast(root.WithOpName("uint8_caster"), image_reader, tensorflow::DT_UINT8);

  // The convention for image ops in TensorFlow is that all images are expected
  // to be in batches, so that they're four-dimensional arrays with indices of
  // [batch, height, width, channel]. Because we only have a single image, we
  // have to add a batch dimension of 1 to the start with ExpandDims().
  auto dims_expander = ExpandDims(root.WithOpName("dim"), uint8_caster, 0);

  // Bilinearly resize the image to fit the required dimensions.
  // auto resized = ResizeBilinear(
  //     root, dims_expander,
  //     Const(root.WithOpName("size"), {input_height, input_width}));


  // Subtract the mean and divide by the scale.
  // auto div =  Div(root.WithOpName(output_name), Sub(root, dims_expander, {input_mean}),
  //     {input_std});


  //cast to int
  //auto uint8_caster =  Cast(root.WithOpName("uint8_caster"), div, tensorflow::DT_UINT8);

  // This runs the GraphDef network definition that we've just constructed, and
  // returns the results in the output tensor.
  tensorflow::GraphDef graph;
  TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));

  std::unique_ptr<tensorflow::Session> session(
      tensorflow::NewSession(tensorflow::SessionOptions()));
  TF_RETURN_IF_ERROR(session->Create(graph));
  TF_RETURN_IF_ERROR(session->Run({inputs}, {"dim"}, {}, out_tensors));
  return Status::OK();
}

// Reads a model graph definition from disk, and creates a session object you
// can use to run it.
Status LoadGraph(const string& graph_file_name,
                 std::unique_ptr<tensorflow::Session>* session) {
  tensorflow::GraphDef graph_def;
  Status load_graph_status =
      ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &graph_def);
  if (!load_graph_status.ok()) {
    return tensorflow::errors::NotFound("Failed to load compute graph at '",
                                        graph_file_name, "'");
  }
  session->reset(tensorflow::NewSession(tensorflow::SessionOptions()));
  Status session_create_status = (*session)->Create(graph_def);
  if (!session_create_status.ok()) {
    return session_create_status;
  }
  return Status::OK();
}



int main(int argc, char* argv[]) {
  // These are the command-line flags the program can understand.
  // They define where the graph and input data is located, and what kind of
  // input the model expects. If you train your own model, or use something
  // other than inception_v3, then you'll need to update these.
  string image(argv[1]);
  string graph ="faster_rcnn_resnet101_coco_11_06_2017/frozen_inference_graph.pb";
  string labels ="labels/mscoco_label_map.pbtxt";
  int32 input_width = 299;
  int32 input_height = 299;
  float input_mean = 0;
  float input_std = 255;
  string input_layer = "image_tensor:0";
  vector<string> output_layer ={ "detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0" };

  bool self_test = false;
  string root_dir = "";

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

  // Get the image from disk as a float array of numbers, resized and normalized
  // to the specifications the main graph expects.
  std::vector<Tensor> resized_tensors;
  string image_path = tensorflow::io::JoinPath(root_dir, image);
  Status read_tensor_status =
      ReadTensorFromImageFile(image_path, input_height, input_width, input_mean,
                              input_std, &resized_tensors);
  if (!read_tensor_status.ok()) {
    LOG(ERROR) << read_tensor_status;
    return -1;
  }
  const Tensor& resized_tensor = resized_tensors[0];

  LOG(ERROR) <<"image shape:" << resized_tensor.shape().DebugString()<< ",len:" << resized_tensors.size() << ",tensor type:"<< resized_tensor.dtype();
  // << ",data:" << resized_tensor.flat<tensorflow::uint8>();
  // Actually run the image through the model.
  std::vector<Tensor> outputs;
  Status run_status = session->Run({{input_layer, resized_tensor}},
                                   output_layer, {}, &outputs);
  if (!run_status.ok()) {
    LOG(ERROR) << "Running model failed: " << run_status;
    return -1;
  }

  int image_width = resized_tensor.dims();
  int image_height = 0;
  //int image_height = resized_tensor.shape()[1];

  LOG(ERROR) << "size:" << outputs.size() << ",image_width:" << image_width << ",image_height:" << image_height << endl;

  //tensorflow::TTypes<float>::Flat iNum = outputs[0].flat<float>();
  tensorflow::TTypes<float>::Flat scores = outputs[1].flat<float>();
  tensorflow::TTypes<float>::Flat classes = outputs[2].flat<float>();
  tensorflow::TTypes<float>::Flat num_detections = outputs[3].flat<float>();
  auto boxes = outputs[0].flat_outer_dims<float,3>();

  LOG(ERROR) << "num_detections:" << num_detections(0) << "," << outputs[0].shape().DebugString();

  for(size_t i = 0; i < num_detections(0) && i < 20;++i)
  {
    if(scores(i) > 0.5)
    {
      LOG(ERROR) << i << ",score:" << scores(i) << ",class:" << classes(i)<< ",box:" << "," << boxes(0,i,0) << "," << boxes(0,i,1) << "," << boxes(0,i,2)<< "," << boxes(0,i,3);
    }
  }

  return 0;
}

@AdamHeavens
Copy link

I think some of the formatting must of been lost in the post as been trying to get the above code to run for the last couple of hours without success. Just comparing to label_image.cc to figure out what it is suppose to be doing. Will post when I have a working copy. Thanks

@AdamHeavens
Copy link

Thank you for editing.. Now compiles without issue!

@terrydl terrydl closed this as completed Aug 28, 2017
@terrydl
Copy link
Author

terrydl commented Aug 28, 2017

Close this issue as it's resolved.

@pzw520125
Copy link

I use c++ code above, but the efficiency is very low, excuse me why? How to improve efficiency @daisyl0

@smitshilu
Copy link
Contributor

smitshilu commented Feb 1, 2018

@daisyl0 or @pzw520125 how can I compile it? Its keep giving me error. I tried with g++ and bazel build. If you are using bazel then can you please tell me which deps I need to include?

@szm-R
Copy link

szm-R commented Mar 13, 2018

Hey guys, I'm also wondering how to compile the code. Following the example in C++ API guide I changed the BUILD to look like this:

`load("//tensorflow:tensorflow.bzl", "tf_cc_binary")

tf_cc_binary(
name = "OB_Inference",
srcs = ["OB_Inference.cc"],
deps = [
"//tensorflow/cc:cc_ops",
"//tensorflow/cc:client_session",
"//tensorflow/core:tensorflow",
],
)`

Where OB_Inference.cc in the code shared by @daisyl0. The file OB_Inference.cc is in TF_root/tensorflow/cc/example (as instructed in C++ API guide) and for compiling it I issued the following command:

bazel run -c opt //tensorflow/cc/example:example

But it gives me this error:

INFO: Running command line: bazel-bin/tensorflow/cc/example/OB_Inference terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid

I appreciate any help as I'm very new to TF. Thank you.

@KallerLong1
Copy link

@shresthamalik how do you to Cast the ouputtensors from to float to unit, can you share the way to me, I meet same issue, thanks very much.

@shresthamalik
Copy link

Hi, @KallerLong1 there is a C++ TF API , Cast() which can be used for this purpose.
https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/cast

I used this code for image classification as example:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/main.cc

In this example, line 157 , uses Cast() to convert tensor to float. We can use Cast() to convert tensor to tensorflow::DT_UINT8

@KallerLong1
Copy link

@shresthamalik Thanks very much for your help. I had to do the below steps, I don't know how to do next to return a tensor project. Please see below code.
// Now cast the image data to DT_FLOAT so we can do normal math on it.
auto float_caster =
Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT);
// The convention for image ops in TensorFlow is that all images are expected
// to be in batches, so that they're four-dimensional arrays with indices of
// [batch, height, width, channel]. Because we only have a single image, we
// have to add a batch dimension of 1 to the start with ExpandDims().
auto dims_expander = ExpandDims(root, float_caster, 0);
// Bilinearly resize the image to fit the required dimensions.
auto resized = ResizeBilinear(
root, dims_expander,
Const(root.WithOpName("size"), { input_height, input_width }));
// Subtract the mean and divide by the scale.
Div(root.WithOpName(output_name), Sub(root, resized, { input_mean }),{ input_std });

// This runs the GraphDef network definition that we've just constructed, and
// returns the results in the output tensor.
tensorflow::GraphDef graph;
TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));

std::unique_ptr<tensorflow::Session> session(
	tensorflow::NewSession(tensorflow::SessionOptions()));
TF_RETURN_IF_ERROR(session->Create(graph));
TF_RETURN_IF_ERROR(session->Run({ inputs }, { output_name }, {}, out_tensors));

auto uint_caster = tensorflow::ops::Cast(root.WithOpName("uint8_caster"), out_tensors->data()[0], tensorflow::DT_UINT8);

tensorflow::Operation o = uint_caster.y.op();
return Status::OK();

@KallerLong1
Copy link

@shresthamalik Thanks for your help, I had resolved it.

@cagrigider
Copy link

@daisyl0
How can I get input_mean and input_std for my own model ?

@japer21
Copy link

japer21 commented Jun 13, 2018

Caused by: java.lang.IllegalArgumentException: Expects arg[0] to be uint8 but float is provided

I am developping on Android Studio and the model input is : Found 1 possible inputs: (name=image_tensor, type=uint8(4), shape=[?,?,?,3])

In my android code what I am trying to get is the array float of the values of pixels of an image stored as Bitmap and then I am trying to feed the model. I am getting that error .
Please anyone could help me?

@cmbowyer13
Copy link

cmbowyer13 commented Jul 4, 2018

If we have a requirement to pass in raw data files say numpy arrays of shape [height, width, 1] of float32 data type. That is what my object detection FRCNN model was trained on and will expect for input tensors. For this setup how should one store this and read into the main.cc @daisyl0 for a c++ execution? Thanks in advance.

@rkachach
Copy link

@terrydl Thanks for sharing your code. I was struggling on the cast issue :)

@sevensix6
Copy link

sevensix6 commented Oct 16, 2018

The uint8 happened because ResizeBillinear() converts the tensor back into float. We can get uint8 tensor by using Cast() at the end ,i.e. after scaling and normalizing. The C++ detection is running fine for me after this.

I also have the problem same: "Invalid argument: Expects arg[0] to be bool but float is provided". I using tensorflow1.8 in C++ api, and load the *.pb model trained by python. When run the sesssion, error occurs, can you help me ? Thanks a lot @shresthamalik @tatatodd @yeephycho @khegelich360Fly

@idealboy
Copy link

The uint8 happened because ResizeBillinear() converts the tensor back into float. We can get uint8 tensor by using Cast() at the end ,i.e. after scaling and normalizing. The C++ detection is running fine for me after this.

I also have the problem same: "Invalid argument: Expects arg[0] to be bool but float is provided". I using tensorflow1.8 in C++ api, and load the *.pb model trained by python. When run the sesssion, error occurs, can you help me ? Thanks a lot @shresthamalik @tatatodd @yeephycho @khegelich360Fly

Have you solved this problem?thank you!

@ahoseinib110
Copy link

ahoseinib110 commented Oct 11, 2019

thank you very much for your code.

@naserpiltan
Copy link

naserpiltan commented Aug 22, 2020

@daisyl0 hi , how can convert uint8_caster to a Tensorflow::tensor object ?
i know that uint8_caster is a tensorflow::output object.
i wanna cast a tensor of int64 to a tensor of int32.
but i dont have an idea about that. can you help me ?

@Aksh-kumar
Copy link

can anyone told me how to use tensorflow c++ API. what I am doing just googling too much copy and pasting codes I don't understand even a single ++ API. I try to get any knowledge from official tensorflow web site but their only Input and output is mention, not detail documentation is available, How Internally It works. How to convert normal c++ data type to tensors etc. can anyone suggest me some good reference where I can grasp these knowledge??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting model gardener Waiting on input from TensorFlow model gardener
Projects
None yet
Development

No branches or pull requests