第一次集成使用 tensorflow ,内心还是有些激动的。开始时候,并不知道怎么进行,其实是一脸茫然的,然后就看了不少文章,关于集成的,大致有了个思路,然后就开始集成测试。这次就总结下具体集成思路和步骤。
方式: tensorflow android 而不是 tensorflow lite
基本思路
首先,在 android 上集成 tensorflow ,我们可以确定使用 TensorFlowInferenceInterface 类,不知道的自己去查资料。通过阅读官方给的源码,我们大概就可以知道所需要的内容和怎么使用。
TensorFlowInferenceInterface 构造函数
首先,在实例化此类的时候,我们需要提供 assets 和 modelName ,所以可以确定要将模型放在 assets 文件夹下,同时传入模型名字即可。其次,在初始化的时候首先执行的 prepareNativeRuntime() 函数,可以确定要加载相关的 so 库,并且在集成后初始化后,不需要在业务代码中去重复加载,因为这里已经加载过了。
模型
模型放在 assets 文件夹下
so 库
so 库 不需要去加载
public TensorFlowInferenceInterface(AssetManager assetManager, String model) {
prepareNativeRuntime();
this.modelName = model;
this.g = new Graph();
this.sess = new Session(g);
this.runner = sess.runner();
....
}
private void prepareNativeRuntime() {
····
try {