TensorFlow2.8.0代码分析之例子Lable_Image_main.cc_函数main

函数:

int main(int argc, char* argv[])

函数作用:这些是程序可以理解的命令行标志。它们定义了图形和输入数据的位置,以及模型期望的输入类型。如果您训练自己的模型,或者使用除inception_v3之外的其他工具,那么您需要更新这些工具。

函数声明情况如下:

 

函数声明文件如下:

 

函数流程图如下:

 

函数逻辑顺序图如下:

 

函数原始代码如下:

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 = "tensorflow/examples/label_image/data/grace_hopper.jpg";

  string graph =

      "tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb";

  string labels =

      "tensorflow/examples/label_image/data/imagenet_slim_labels.txt";

  int32_t input_width = 299;

  int32_t input_height = 299;

  float input_mean = 0;

  float input_std = 255;

  string input_layer = "input";

  string output_layer = "InceptionV3/Predictions/Reshape_1";

  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;

  }

  //以浮点数数组的形式从磁盘获取图像,并根据主图形所需的规格调整大小和规格。

  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];

  //在模型中实际运行图像。

  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;

  }

  //这是用于自动测试,以确保我们使用默认设置获得预期结果。我们知道653号标签(军服)应该是霍珀上将形象的顶级标签。

  if (self_test) {

    bool expected_matches;

    Status check_status = CheckTopLabel(outputs, 653, &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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qqq9668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值