故事是这样的:
有一个手撑检测的tflite模型,需要在开发板上跑起来。手机版本的已成熟,要移植到开发板上。现在要验证tflite模型文件在板子上的运行结果要和手机上一致。
前提:为了多次重复测试,在Android端使用了同一帧数据(从一个录制的mp4中固定取一张图)测试代码如下图
文章图片
下面是测试过程
记录下Android版API运行推理前的图片数据文件(经过了规一化处理,所以都是-1~1之间的float数据)
文章图片
这一步卡在了写float数据到二进制文件中,C++读出来有问题
换了个方案,直接存储float字符串
private void saveFile(float[] pfImageData) {try {File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/tfimg");
StringBuilder sb = new StringBuilder();
for (float val : pfImageData) {//保留4位小数,这里可以改为其他值sb.append(String.format("%.4f", val));
sb.append("\r\n");
} FileWriter out = new FileWriter(file);
//文件写入流out.write(sb.toString());
out.close();
} catch (Exception e) {e.printStackTrace();
Log.e("Melon", "存储文件异常," + e.getMessage());
}}
拿着这个文件在板子上输入到Tflite模型中
测试代码,主要是RunInference()和read_file()
/* Copyright 2017 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.==============================================================================*/ #include "tensorflow/lite/examples/label_image/label_image.h" #include // NOLINT(build/include_order)#include // NOLINT(build/include_order)#include// NOLINT(build/include_order)#include// NOLINT(build/include_order)#include// NOLINT(build/include_order)#include // NOLINT(build/include_order) #include #include #include #include #include #include #include
运行指令 ./ws_app --tflite_model libnewpalm_detection.tflite --image tfimg对比推理前的输入一致
Android端
文章图片
开发板上
文章图片
对比推理后的输出一致 Android端
文章图片
开发板端
文章图片
【C++|C++ TensorflowLite模型验证的过程详解】到此这篇关于C++ TensorflowLite模型验证的文章就介绍到这了,更多相关C++ TensorflowLite模型验证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读