Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- namespace{
- 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,
- std::vector<Tensor>* out_tensors) {
- auto root = tensorflow::Scope::NewRootScope();
- using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
- // 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 tensorflow_example(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 const image(argv[1]);
- std::string const model_folder("/home/ramsus/Qt/computer_vision_model/tensorflow/object_detection/");
- string const graph = model_folder + "ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb";
- int32 input_width = 300;
- int32 input_height = 300;
- 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" };
- // First we load and initialize the model.
- std::unique_ptr<tensorflow::Session> session;
- string graph_path = graph;
- std::cout<<"graph path:"<<graph_path<<std::endl;
- 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;
- }
- std::cout<<"load graph success"<<std::endl;
- // 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 const image_path = image;
- Status read_tensor_status = ReadTensorFromImageFile(image_path, &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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement