[TF] TensorFlow & TFLite - C++ API

使用TensorFlow C++接口调用预训练模型

Valuable Resource: MLPerf Inference - Image Classification

Ref: Use TensorFlow C++ API with OpenCV3

 

一、Bazel

Ref: 安装和配置bazel

Download bazel: https://github.com/bazelbuild/bazel/releases/tag/0.18.0

注意,tf和bazel的版本匹配。

Notice: tf 1.15.0 prefer bazel 1.0.0

 

 

二、 Compile

Ref: Tensorflow ARM交叉编译错误集锦

Ref: tensorflow C++ 环境搭建及实战

编译完成后,在bazel-bin/tensorflow中会生成两个我们需要的库文件:libtensorflow_cc.solibtensorflow_framework.so

在后面我们用C++调用tensorflow时需要链接这两个库文件。

(a) Conda installation: https://anaconda.org/conda-forge/libtensorflow_cc

(b) Download directly: https://www.tensorflow.org/install/lang_c

下载后加压配置好。 

export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib

使用并链接动态链接库。

#include <stdio.h>
#include <tensorflow/c/c_api.h>

int main() {
  printf("Hello from TensorFlow C library version %s\n", TF_Version());
  return 0;
}

编译测试。

gcc hello_tf.c -ltensorflow -o hello_tf

gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf
jeff@unsw-ThinkPad-T490:Desktop$ ./hello_tf 
Hello from TensorFlow C library version 1.15.0

 

 

三、c++环境下调用pb文件

Ref: Are there any tutorials/books for TensorFlow in C++?

  • Install and test label_image: 

Download TensorFlow Master,

Follow: TensorFlow Lite C++ image classification demo

复制代码
$ bazel-bin/tensorflow/lite/examples/label_image/label_image --tflite_model ./mobilenet_v1_1.0_224.tflite --labels ./labels.txt --image ~/Pictures/test.bmp
Loaded model .
/mobilenet_v1_1.0_224.tflite resolved reporter invoked average time: 60.048 ms 0.0772664: 825 825:stole 0.0664576: 488 488:cellular telephone, cellular phone, cellphone, cell, mobile phone 0.0564614: 475 475:cardigan 0.0518701: 835 835:suit, suit of clothes 0.0516843: 585 585:hair slide
复制代码

 

Follow: TensorFlow C++ and Python Image Recognition Demo

 

How to retrain my own .pb & .tflite? 

 

  • Retraining MobileNet for Image Classification

Download TensorFlow Hub. [这个策略有问题]

(1) Creating the TensorFlow Hub pip package using Linux

(2) train by "retrain.py"

复制代码
python examples/image_retraining/retrain.py 
--bottleneck_dir=tf_files/bottlenecks
--model_dir=tf_files/models/
--summaries_dir=tf_files/training_summaries/"mobilenet_1.0_224"
--output_graph=tf_files/retrained_graph.pb
--output_labels=tf_files/retrained_labels.txt
--image_dir Images/
--architecture mobilenet_1.0_224
--image_dir ./Images
--learning_rate=0.0001
--testing_percentage=20
--validation_percentage=20
--train_batch_size=16
--validation_batch_size=-1
--flip_left_right True
--random_scale=30
--random_brightness=30
--eval_step_interval=100
--how_many_training_steps=100
复制代码

 

  • Convert to tflite

When ssd_mobilenet_v1, the command shows as following.

复制代码
tflite_convert 
--graph_def_file=tflite/tflite_graph.pb
--output_file=detect_float.tflite
--output_format=TFLITE
--input_shapes=1,300,300,3
--input_arrays=normalized_input_image_tensor
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'
--inference_type=${inference_type}
--mean_values=128
--std_dev_values=127
--change_concat_input_ranges=false
--allow_custom_ops toco --graph_def_file=${output_tflite_dir}/tflite_graph.pb \ --input_format=TENSORFLOW_GRAPHDEF \ --output_file=${output_tflite_dir}/detect_float.tflite \ --inference_type=FLOAT \ --input_type=FLOAT \ --input_arrays=normalized_input_image_tensor \ --input_shapes=1,300,300,3 \ --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1', 'TFLite_Detection_PostProcess:2', 'TFLite_Detection_PostProcess:3' \ --allow_custom_ops
复制代码

 

  • Retraining MobileNet By Keras

Download Keras. [这个策略比较好]

使用Keras训练 MobileNet for Classification 为好。

详见:mobileNet迁移学习训练猫狗分类生成kmodel模型,k210运行

tflite_convert --output_file=./OUTPUT/single.tflite --keras_model_file=./OUTPUT/my_model.h5

 

 

 

动态链接库

[windows tensorflow之路(一)] windows 下使用bazel编译libtensorflow_cc.so

[windows tensorflow之路(二)]windows下使用 bazel 编译一个dll ,并使用 python代码调用

[windows tensorflow之路(三)] windows下编写tensorflow c++代码和opencv代码并使用bazel编译教程

 

 

/* implement */

 

 

 

 

posted @   郝壹贰叁  阅读(2984)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示