tensorflow训练出的模型识别的demo

    xiaoxiao2021-03-25  86

    //一个最小的但有用的C++的例子展示了如何加载一个ImageNet式目标识别tensorflow模型,准备输入图像,通过图运行和解释结果。 //它的目的是有尽可能少的依赖和尽可能清楚,所以它比它在产品代码中更详细。特别是,自动使用 //TensorFlow的大量返回值的类型能删除大量的样板,但我发现在样本代码中,显式类型是有用的,使其查找相关的类变得简单。 //要使用它,在工作目录中learning/brain/tutorials/label_image /data/文件夹下面编译并运行,你应该能看到Lena图像这个例子输出的前五个标签。然后你可以自定义它使用您自己的模型或图像,通过在main()函数中改变文件名。 //默认包括的googlenet_graph.pb文件是创建自Inception。 #include <fstream> #include <vector> #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/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" // 这些都是公共类,它很方便的引用没有命名空间。 using tensorflow::Flag; using tensorflow::Tensor; using tensorflow::Status; using tensorflow::string; using tensorflow::int32; // 取一个文件名,从它加载一个标签列表,每行一个,返回一个字符串的向量。它填充空字符串,所以结果的长度是16的倍数,因为我们的模型期望这样。 Status ReadLabelsFile(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(); } // 给定一个图像文件名,读入数据,尝试将其解码为图像,将它调整到所要求的大小,然后按需要缩放值。 Status ReadTensorFromImageFile(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"; auto file_reader = tensorflow::ops::ReadFile(root.WithOpName(input_name), file_name); // 现在尝试找出它是什么样的文件,并解码它。 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")) { image_reader = DecodeGif(root.WithOpName("gif_reader"), file_reader); } else { // 假设图像不是 PNG 或是 GIF 格式,就一定是JPEG格式. image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader, DecodeJpeg::Channels(wanted_channels)); } // 现在把图像数据cast为float类型,这样我们可以做常规的数学计算. auto float_caster = Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT); // 在TensorFlow里,图像ops的约定是所有图像都能批处理,以使它们是具有[批,高度,宽度,通道]索引的四维数组。因为我们只有一个图像,我们 //用expanddims()函数开始添加1维的批尺寸。 auto dims_expander = ExpandDims(root, float_caster, 0); // 双线调整图像大小以适合其要求的尺寸。 auto resized = ResizeBilinear( root, dims_expander, Const(root.WithOpName("size"), {input_height, input_width})); // 减去mean,除以scale Div(root.WithOpName(output_name), Sub(root, resized, {input_mean}), {input_std}); // 这个运行我们刚刚建的GraphDef网络模型的定义,返回输出张量的结果 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({}, {output_name}, {}, out_tensors)); return Status::OK(); } // 从磁盘读取一个模型图像的定义,创建session对象,你可以使用它来运行 Status LoadGraph(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(); } // 分析Inception图像的输出,以检索最高分数和它们在张量中的位置,这些位置对应于类别。 Status GetTopLabels(const std::vector<Tensor>& outputs, int how_many_labels, Tensor* indices, Tensor* scores) { auto root = tensorflow::Scope::NewRootScope(); using namespace ::tensorflow::ops; // NOLINT(build/namespaces) string output_name = "top_k"; TopK(root.WithOpName(output_name), outputs[0], how_many_labels); // 这个运行我们刚刚建的GraphDef网络模型的定义,返回输出张量的结果 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)); // TopK节点返回两个输出,分数和原来的索引,因此,我们必须追加:0和:1指定两者。 std::vector<Tensor> out_tensors; TF_RETURN_IF_ERROR(session->Run({}, {output_name + ":0", output_name + ":1"}, {}, &out_tensors)); *scores = out_tensors[0]; *indices = out_tensors[1]; return Status::OK(); } // 给定一个模型运行的输出,以及包含该标签的文件的名称打印出得分最高值的前五名。 Status PrintTopLabels(const std::vector<Tensor>& outputs, string labels_file_name) { std::vector<string> labels; size_t label_count; Status read_labels_status = ReadLabelsFile(labels_file_name, &labels, &label_count); if (!read_labels_status.ok()) { LOG(ERROR) << read_labels_status; return read_labels_status; } const int how_many_labels = std::min(5, static_cast<int>(label_count)); Tensor indices; Tensor scores; TF_RETURN_IF_ERROR(GetTopLabels(outputs, how_many_labels, &indices, &scores)); tensorflow::TTypes<float>::Flat scores_flat = scores.flat<float>(); tensorflow::TTypes<int32>::Flat indices_flat = indices.flat<int32>(); for (int pos = 0; pos < how_many_labels; ++pos) { const int label_index = indices_flat(pos); const float score = scores_flat(pos); LOG(INFO) << labels[label_index] << " (" << label_index << "): " << score; } return Status::OK(); } // 这是一个测试函数,返回最顶上的标签索引是否为预期的。 Status CheckTopLabel(const std::vector<Tensor>& outputs, int expected, bool* is_expected) { *is_expected = false; Tensor indices; Tensor scores; const int how_many_labels = 1; TF_RETURN_IF_ERROR(GetTopLabels(outputs, how_many_labels, &indices, &scores)); tensorflow::TTypes<int32>::Flat indices_flat = indices.flat<int32>(); if (indices_flat(0) != expected) { LOG(ERROR) << "Expected label #" << expected << " but got #" << indices_flat(0); *is_expected = false; } else { *is_expected = true; } return Status::OK(); } int main(int argc, char* argv[]) { // 他们定义图形和输入数据的位置,以及什么样的输入模型是期望的。 //如果你训练自己的模型,或使用GoogLeNet以外的模型,你需要更新这些。 string image = "tensorflow/examples/label_image/data/grace_hopper.jpg"; string graph = "tensorflow/examples/label_image/data/" "tensorflow_inception_graph.pb"; string labels = "tensorflow/examples/label_image/data/" "imagenet_comp_graph_label_strings.txt"; int32 input_width = 299; int32 input_height = 299; int32 input_mean = 128; int32 input_std = 128; string input_layer = "Mul"; string output_layer = "softmax"; bool self_test = false; string root_dir = ""; std::vector<Flag> flag_list = { Flag("image", &image, "image to be processed"), Flag("graph", &graph, "graph to be executed"), Flag("labels", &labels, "name of file containing labels"), Flag("input_width", &input_width, "resize image to this width in pixels"), Flag("input_height", &input_height, "resize image to this height in pixels"), Flag("input_mean", &input_mean, "scale pixel values to this mean"), Flag("input_std", &input_std, "scale pixel values to this std deviation"), Flag("input_layer", &input_layer, "name of input layer"), Flag("output_layer", &output_layer, "name of output layer"), Flag("self_test", &self_test, "run a self test"), Flag("root_dir", &root_dir, "interpret image and graph file names relative to this directory"), }; string usage = tensorflow::Flags::Usage(argv[0], flag_list); const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list); if (!parse_result) { LOG(ERROR) << usage; return -1; } // 我们需要调用这个函数建立Tensorflow的通用状态 tensorflow::port::InitMain(argv[0], &argc, &argv); if (argc > 1) { LOG(ERROR) << "Unknown argument " << argv[1] << "\n" << usage; return -1; } // 首先加载和初始化模型 std::unique_ptr<tensorflow::Session> session; string graph_path = tensorflow::io::JoinPath(root_dir, graph); Status load_graph_status = LoadGraph(graph_path, &session); if (!load_graph_status.ok()) { LOG(ERROR) << load_graph_status; return -1; } // 用float数组的个数从硬盘获取图像,调整大小和归一化到主图像期望的规格 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]; // 通过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; } // 这是用于自动化测试,以确保我们由默认设置得到预期的结果。我们知道标签866(军装)应该是Asmiral Hopper图像的置顶标签。 if (self_test) { bool expected_matches; Status check_status = CheckTopLabel(outputs, 866, &expected_matches); if (!check_status.ok()) { LOG(ERROR) << "Running check failed: " << check_status; return -1; } if (!expected_matches) { LOG(ERROR) << "Self-test failed!"; return -1; } } // 对我们产生的结果做一些有意思的事情 Status print_status = PrintTopLabels(outputs, labels); if (!print_status.ok()) { LOG(ERROR) << "Running print failed: " << print_status; return -1; } return 0; } 点击打开链接

    转载请注明原文地址: https://ju.6miu.com/read-20963.html

    最新回复(0)