tensorflow lite example label_image 分析【二】

2023-05-16

接上文

3.代码分析

main函数首先将入参写入参数结构体

Settings s;

struct Settings {
  bool verbose = false;
  bool accel = false;
  bool input_floating = false;
  bool profiling = false;
  int loop_count = 1;
  float input_mean = 127.5f;
  float input_std = 127.5f;
  string model_name = "./mobilenet_quant_v1_224.tflite";
  string input_bmp_name = "./grace_hopper.bmp";
  string labels_file_name = "./labels.txt";
  string input_layer_type = "uint8_t";
  int number_of_threads = 4;

};

运行执行函数 RunInference(&s);


【背景知识:https://blog.csdn.net/jILRvRTrc/article/details/80553561】

RunInference();

1)建立模型model = tflite::FlatBufferModel::BuildFromFile(s->model_name.c_str());

2)建立OpResolver 用于指向每个node的操作函数 tflite::ops::builtin::BuiltinOpResolver resolver;

3)建立解释器 tflite::InterpreterBuilder(*model, resolver)(&interpreter);

4)对解释器进行参数设置包括

interpreter->UseNNAPI(s->accel); 是否使用NNAPI加速

interpreter->SetNumThreads(s->number_of_threads);设置运行线程数

bmp文件读入并进行必要的resize

int image_width = 224;
  int image_height = 224;
  int image_channels = 3;
  std::vector<uint8_t> in = read_bmp(s->input_bmp_name, &image_width,

                                     &image_height, &image_channels, s);

resize<float>(interpreter->typed_tensor<float>(input), in.data(),
                    image_height, image_width, image_channels, wanted_height,

                    wanted_width, wanted_channels, s);


打印运行参数相关信息

if (s->verbose) PrintInterpreterState(interpreter.get());

  // output something like
  // time (ms) , Node xxx, OpCode xxx, symblic name

  //      5.352, Node   5, OpCode   4, DEPTHWISE_CONV_2D

运行模型及获得运行时间

struct timeval start_time, stop_time;
  gettimeofday(&start_time, nullptr);
  for (int i = 0; i < s->loop_count; i++) {
    if (interpreter->Invoke() != kTfLiteOk) {
      LOG(FATAL) << "Failed to invoke tflite!\n";
    }
  }

  gettimeofday(&stop_time, nullptr);

打印profiling

  if (s->profiling) {
    profiler->StopProfiling();
    auto profile_events = profiler->GetProfileEvents();
    for (int i = 0; i < profile_events.size(); i++) {
      auto op_index = profile_events[i]->event_metadata;
      const auto node_and_registration =
          interpreter->node_and_registration(op_index);
      const TfLiteRegistration registration = node_and_registration->second;
      PrintProfilingInfo(profile_events[i], op_index, registration);
    }

  }

获取输出

int output = interpreter->outputs()[0];
  switch (interpreter->tensor(output)->type) {
    case kTfLiteFloat32:
      get_top_n<float>(interpreter->typed_output_tensor<float>(0), output_size,

                       num_results, threshold, &top_results, true);

加载label并显示对应输出结果

 std::vector<string> labels;
  size_t label_count;


  if (ReadLabelsFile(s->labels_file_name, &labels, &label_count) != kTfLiteOk)
    exit(-1);


  for (const auto& result : top_results) {
    const float confidence = result.first;
    const int index = result.second;
    LOG(INFO) << confidence << ": " << index << " " << labels[index] << "\n";

  }


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

tensorflow lite example label_image 分析【二】 的相关文章

随机推荐

  • 树莓派Linux固定多USB设备号相同厂家设备和不同设备(包括一个USB设备两个设备名称号的情况)

    目录 一 问题说明 二 解决思路 三 开干 一 问题说明 在使用USB摄像头时 xff0c 由于使用了两个摄像头 xff0c 故此在上电启动后 xff0c 设备号会发生变化 xff0c 比如设备1之前时video0 xff0c 有的时候上电
  • FreeRTOS学习笔记-流缓冲区(FreeRTOS中文官网)

    https www freertos org zh cn cmn s RTOS stream buffer API html RTOS 流缓冲区 API RTOS 流缓冲区 API 函数 xff1a xStreamBufferCreate
  • FreeRTOS学习笔记-消息缓冲区(FreeRTOS中文官网)

    RTOS消息缓冲区 API RTOS 消息缓冲区 API 函数 xff1a xMessageBufferCreate xMessageBufferCreateStatic xMessageBufferSend xMessageBufferS
  • ubuntu系统X86环境下配置TX2的ARM环境的交叉编译链

    总步骤 安装qtcreator的IDE安装aarch64 linux gnu g 43 43 交叉编译链下载Qt库的源码 xff0c 配置编译选项 xff0c 编译生成针对TX2的ARM环境的qmake工具配置qtcreator xff0c
  • ROS中Eigen库的引用

    在CmakeList txt中添加两个地方 find package Eigen REQUIRED include directories Eigen INCLUDE DIR 如果找不到Eigen xff0c 我们将第一句改成 find p
  • ROS tf使用

    1 静态tf发布 lt node pkg 61 34 tf 34 type 61 34 static transform publisher 34 name 61 34 link1 link2 broadcaster 34 args 61
  • SLAM算法配置——使用Realsense D435i结合ROS跑通ORB-SLAM2的RGB-D节点

    ORB SLAM2源地址 配置环境依赖 Pangolin xff0c OpenCV xff0c Eigen3 xff0c DBoW2 and g2o xff08 源代码里有 xff0c 不用自己装 xff09 xff0c ROS xff08
  • 代码编写及阅读规范

    阅读常识 1 C语言中在函数名或关键字前加下划线 一般情况是标识该函数或关键字是自己内部使用的 xff0c 与提供给外部的接口函数或关键字加以区分 规范 综述 C 43 43 是一门十分复杂并且威力强大的语言 xff0c 使用这门语言的时候
  • 流媒体开发之路

    其实很早之前 xff0c 就想写属于自己的博客 xff0c 大二就有了CSDN账号 xff0c 很讽刺的是 xff0c 工作几年了 xff0c 账号里面的内容竟然和小鲜肉脸一样干净 干净的让人尴尬 回顾自己的这几年的开发之路 xff0c 接
  • matlab图像处理实例详解---note

    1 直方图均衡及直方图规定化 可以优化图像的亮度及gamma效果 2 图像的标准差 当图像越清晰的时候 xff0c 图像的标准差就越大 是否可以用来做af的判定标准 作为fv的值 另外是否可以用图像的相关系数作为caf的一个trigger
  • promise限制并发请求数量

    所谓并发请求 xff0c 就是指在一个时间点多个请求同时执行 当并发的请求超过一定数量时 xff0c 会造成网络堵塞 xff0c 服务器压力大崩溃或者其他高并发问题 xff0c 此时需要限制并发请求的数量 假如等待请求接口1000个 xff
  • 一个跨平台的 C++ 内存泄漏检测器(转载)

    一个跨平台的 C 43 43 内存泄漏检测器 吴咏炜 adah 64 netstd com 2004 年 3 月 内存泄漏对于C C 43 43 程序员来说也可以算作是个永恒的话题了吧 在Windows下 xff0c MFC的一个很有用的功
  • printf和wprintf、printf输出结束标识符、c++按值返回临时对象是否是const的实验

    ifndef TEST H define TEST H include lt iostream gt include lt string gt using namespace std int x 61 5 struct s public s
  • 自己搭深度学习环境踩坑血泪史

    自己搭深度学习环境踩坑的血泪史 从一个沮丧的事情开始问题1 强行更新了一次win10后 双系统里的ubuntu的启动项就没了 xff0c 直接进入win10系统问题2 sudo apt get update 总是超时问题3 conda in
  • 电脑串口延迟/缓冲设置方法

    使用串口做精确信号发送的时候会经常出现不能时间不精确的问题 xff0c 使用两个u口转串口串连之后一个接收一个发送的情况下 收到的时间延迟数据如下 xff1a 注意 xff1a 这里的因为有一个接收缓冲区和一个发送缓冲区 xff0c 所以这
  • apt-get install 连同诸多依赖包一并安装的指令

    apt get install 连同诸多依赖包一并安装 如题 xff0c apt get安装某个包的时候 xff0c 经常会碰到很多依赖包 xff0c 需要一一安装了才行 xff0c 非常麻烦 当然 xff0c 可以使用以下指令一步到位 a
  • git 分支操作记录

    查看分支 xff1a 查看本地分支 xff1a git branch 查看远程分支 xff1a git branch r 查看全部分支 xff08 本地和远程 xff09 git branch a 新建分支 xff1a 创建新分支 xff1
  • C++ 简析容器Vector

    向量 xff08 Vector xff09 是一个封装了动态大小数组的顺序容器 xff08 Sequence Container xff09 跟任意其它类型容器一样 xff0c 它能够存放各种类型的对象 可以简单的认为 xff0c 向量是一
  • SLAM初始化

    本节的学习要点 xff1a 初始化的目的 单目 双目 初始化的两种方法初始化过程 初始化的目的 单目SLAM初始化的目的是 61 61 构建初始的三维点云地图 xff08 空间点 xff09 并为之后的计算提供初始值 61 61 由于仅从单
  • tensorflow lite example label_image 分析【二】

    接上文 3 代码分析 main函数首先将入参写入参数结构体 Settings s struct Settings bool verbose 61 false bool accel 61 false bool input floating 6