TFLite JNI 接口实现

2024-06-03 14:38
文章标签 实现 接口 jni tflite

本文主要是介绍TFLite JNI 接口实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JNI is a C interface, which is not object-oriented. It does not really pass the objects.

要实现Mace JNI 接口,先研究下TFLite的 JNI接口实现

Java Code Examples for org.tensorflow.lite.Tensor

Java Code Examples for org.tensorflow.lite.Tensorhttps://www.programcreek.com/java-api-examples/?api=org.tensorflow.lite.Tensor学习 java-api的网址 如ByteBuffer

Java Code Examples for java.nio.ByteBufferhttps://www.programcreek.com/java-api-examples/?api=java.nio.ByteBuffer

Tensor数据类型判断

org.tensorflow.lite.Tensor.dataTypeOf java code examples | Tabninehttps://www.tabnine.com/code/java/methods/org.tensorflow.lite.Tensor/dataTypeOfjava/org/tensorflow/lite/Tensor.java

TFLite基础知识 - VitoYeah - 博客园tensorflow lite基本知识https://www.cnblogs.com/vitoyeah/p/10273299.html

How to run a Tensorflow-Lite inference in (Android Studio) NDK (C / C++ API)? - Stack Overflowhttps://stackoverflow.com/questions/61523713/how-to-run-a-tensorflow-lite-inference-in-android-studio-ndk-c-c-api

How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java? - Stack Overflowhttps://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java

int[] myIntArray = new int[3];
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: 
// https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 

For classes, for example String, it's the same:

String[] myStringArray = new String[3];
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

// String对象创建后是不可变的如需改变使用StringBuilder

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};
Or (less preferred) 但这是C/C++的用法
int num[] = {1, 2, 3, 4, 5};

java 基本数据类型和引用

        float f1;                                                                                                                                       
        boolean b1 = f1.getClass().isPrimitive();
Test.java:12: error: float cannot be dereferenced
        boolean b1 = f1.getClass().isPrimitive();

如果使用float的wrapper class,就可使用函数getClass了, 这样说明了基本类型和对象的区别

Float Arr = new Float(1.0); // java 里创建对象和C++还是不同的, class A a(xx) 这应该是在栈里创建而java 没有这种写法

基本类型的数组是个对象

        int[] Arr = new int[5]; 
        Class arrClass = Arr.getClass(); 
        System.out.println("name myClass isPrimitive: "
                           + arrClass.isPrimitive()); 
        System.out.println("name myClass isArray: "
                           + arrClass.isArray()); 
        System.out.println("name myClass: "
                           + arrClass.getName()); 
  
        // Get the ComponentType of arrClass 
        // using getComponentType() method 
        System.out.println("ComponentType of myClass: "
                           + arrClass.getComponentType()); 

java 对象类型判断

java判断对象是什么数据类型的方法 - 编程语言 - 亿速云这篇文章将为大家详细讲解有关java判断对象是什么数据类型的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1、insta...https://www.yisu.com/zixun/130629.html

java 字节序和native字节序

Java中的大端和小端 - 阿提说说 - OSCHINA - 中文开源技术交流社区Java整型的字节序是() A.Little-Endian(小端) B.Big-Endian(大端) C.由运行程序的CPU决定 D.由编译程序的CPU决定 对于大小端,我估计肯定有很多开发人员跟我一样都没听过 由于Java是跨平台的,JVM为我们屏蔽了...https://my.oschina.net/itsaysay/blog/4299422import java.nio.ByteBuffer;                                                                                                                                            
import java.nio.ByteOrder;  
import java.util.Arrays;  
  
public class JVMEndianTest {   
        
    public static void main(String[] args) {   
        ByteOrder byteOrder = ByteOrder.nativeOrder();
        //LITTLE_ENDIAN,CPU是Intel的
        System.out.println(byteOrder);
        int x = 0x01020304;  
            
        ByteBuffer bb = ByteBuffer.wrap(new byte[4]);  
        bb.asIntBuffer().put(x);  
        String ss_before = Arrays.toString(bb.array());  
            
        System.out.println("默认字节序 " +  bb.order().toString() +  ","  +  " 内存数据 " +  ss_before);  
            
        bb.order(ByteOrder.LITTLE_ENDIAN);  
        bb.asIntBuffer().put(x);  
        String ss_after = Arrays.toString(bb.array());  
            
        System.out.println("修改字节序 " + bb.order().toString() +  ","  +  " 内存数据 " +  ss_after);  
    }   
}

native order 也就是CPU决定的 order和 java oder的区别

上面代码打印出的log:

LITTLE_ENDIAN (ByteOrder.nativeOrder())
默认字节序 BIG_ENDIAN, 内存数据 [1, 2, 3, 4] (0x01020304 大端存储: 低位0x04, 低地址0x01)
修改字节序 LITTLE_ENDIAN, 内存数据 [4, 3, 2, 1] (0x01020304 小端存储: 低位0x04, 低地址0x04)

java中大小端问题研究 - 掘金一篇简单明了的工具类生成文章https://juejin.cn/post/6844903591963000846关于大小端,java提供了对应的函数

public static short littleByteToShort(byte[] data) {
    if (data.length != 2) {
        throw new UnsupportedOperationException("the byte length is not 2");
    }
    return ByteBuffer.allocate(data.length).order(ByteOrder.LITTLE_ENDIAN).put(data).getShort(0);
}
 

public static byte[] shortToLittleByte(short data) {
    return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(data).array();
}
 

如果不做处理,通过JNI传输的值有问题吗?

写代码验证了下, 基本数据类型 int/float, int array是没有问题的

但如果是

ByteBuffer buf = ByteBuffer.allocate(4);
Log.i(TAG, "Default java endian: "+buf.order().toString());
buf.putInt(0x2);
byte[] result = buf.array();
Log.i(TAG, "bytes: " + result[0]);
printBytes(result);


ByteBuffer buf1 = ByteBuffer.allocate(4);
buf1.order(ByteOrder.LITTLE_ENDIAN);
buf1.putInt(0x2);
Log.i(TAG, "now java endian: "+buf1.order().toString());
result = buf1.array();
Log.i(TAG, "bytes: " + result[0]);
printBytes(result);
01-01 01:15:44.734  8954  8954 I little_big_edian: Default java endian: BIG_ENDIAN
01-01 01:15:44.734  8954  8954 I little_big_edian: bytes: 0
01-01 01:15:44.734  8954  8954 I little_big_edian: 0 0 0 2 


01-01 01:15:44.734  8954  8954 I little_big_edian: now java endian: LITTLE_ENDIAN
01-01 01:15:44.734  8954  8954 I little_big_edian: bytes: 2
01-01 01:15:44.734  8954  8954 I little_big_edian: 2 0 0 0 
 

为什么要使用FloatBuffer替代float[]

The main reason is performance: ByteBuffers and the other NIO classes enable accelerated operations when interfacing with native code (typically by avoiding the need to copy data into a temporary buffer).

This is pretty important if you are doing a lot of OpenGL rendering calls for example.

The reason for creating a ByteBuffer first is that you want to use the allocateDirect call to create a direct byte buffer, which benefits from the accelerated operations. You then create a FloatBuffer from this that shares the same memory. The FloatBuffer doesn't itself have an allocateDirect method for some reason, which is why you have to go via ByteBuffer.

Java Code Examples of java.nio.FloatBufferThis page provides Java code examples for java.nio.FloatBuffer. The examples are extracted from open source Java projects from GitHub.http://www.javased.com/?api=java.nio.FloatBuffer

colorValues = new int[FINAL_SIZE * FINAL_SIZE];
float[] floatValues = new float[FINAL_SIZE * FINAL_SIZE * 3];
floatBuffer = FloatBuffer.wrap(floatValues, 0, FINAL_SIZE * FINAL_SIZE * 3);
floatBuffer.rewind(); // The position is set to zero and the mark is discarded
floatBuffer.put(xx);
floatBuffer.array();

FloatBuffer (Java SE 9 & JDK 9 )https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#rewind--

157     // set input tensors
158     for (int i = 0; i < inputs.length; ++i) {
159       getInputTensor(i).setTo(inputs[i]);
160     }

295    * Gets the input {@link Tensor} for the provided input index.
296    *
297    * @throws IllegalArgumentException if the input index is invalid.
298    */
299   Tensor getInputTensor(int index) {
300     if (index < 0 || index >= inputTensors.length) {
301       throw new IllegalArgumentException("Invalid input Tensor index: " + index);
302     }
303     Tensor inputTensor = inputTensors[index];
304     if (inputTensor == null) {
305       inputTensor =
306           inputTensors[index] =
307               Tensor.fromIndex(interpreterHandle, getInputTensorIndex(interpreterHandle, index));
308     }
309     return inputTensor;
310   }

170   /**
171    * Copies the contents of the provided {@code src} object to the Tensor.
172    *
173    * <p>The {@code src} should either be a (multi-dimensional) array with a shape matching that of
174    * this tensor, a {@link ByteByffer} of compatible primitive type with a matching flat size, or
175    * {@code null} iff the tensor has an underlying delegate buffer handle.
176    *
177    * @throws IllegalArgumentException if the tensor is a scalar or if {@code src} is not compatible
178    *     with the tensor (for example, mismatched data types or shapes).
179    */
180   void setTo(Object src) {
181     if (src == null) {
182       if (hasDelegateBufferHandle(nativeHandle)) {
183         return;
184       }
185       throw new IllegalArgumentException(
186           "Null inputs are allowed only if the Tensor is bound to a buffer handle.");
187     }
188     throwIfTypeIsIncompatible(src);
189     throwIfSrcShapeIsIncompatible(src);
190     if (isBuffer(src)) {
191       setTo((Buffer) src);
192     } else if (src.getClass().isArray()) {
193       writeMultiDimensionalArray(nativeHandle, src);
194     } else {
195       writeScalar(nativeHandle, src);
196     }
197   } 

  164 size_t WriteOneDimensionalArray(JNIEnv* env, TfLiteType data_type,
  165                                const void* src, size_t src_size, jarray dst) {
  166   const int len = env->GetArrayLength(dst);
  167   const size_t size = len * ElementByteSize(data_type);
  168   if (size > src_size) {
  169     ThrowException(
  170         env, kIllegalStateException,
  171         "Internal error: cannot fill a Java array of %d bytes with a Tensor of "
  172         "%d bytes",
  173         size, src_size);
  174     return 0;
  175   }
  176   switch (data_type) {
  177     case kTfLiteFloat32: {
  178       jfloatArray float_array = static_cast<jfloatArray>(dst);
  179       env->SetFloatArrayRegion(float_array, 0, len,
  180                                static_cast<const jfloat*>(src));
  181       return size;
  182     }
  183     case kTfLiteInt32: {
  184       jintArray int_array = static_cast<jintArray>(dst);
  185       env->SetIntArrayRegion(int_array, 0, len, static_cast<const jint*>(src));
  186       return size;
  187     }
  188     case kTfLiteInt64: {
  189       jlongArray long_array = static_cast<jlongArray>(dst);
  190       env->SetLongArrayRegion(long_array, 0, len,
  191                               static_cast<const jlong*>(src));
  192       return size;
  193     }

322   Tensor getOutputTensor(int index) {
323     if (index < 0 || index >= outputTensors.length) {
324       throw new IllegalArgumentException("Invalid output Tensor index: " + index);
325     }
326     Tensor outputTensor = outputTensors[index];
327     if (outputTensor == null) {
328       outputTensor =
329           outputTensors[index] =
330               Tensor.fromIndex(interpreterHandle, getOutputTensorIndex(interpreterHandle, index));
331     }
332     return outputTensor;
333   }
 

235   /**
236    * Copies the contents of the tensor to {@code dst} and returns {@code dst}.
237    *
238    * @param dst the destination buffer, either an explicitly-typed array, a {@link ByteBuffer} or
239    *     {@code null} iff the tensor has an underlying delegate buffer handle.
240    * @throws IllegalArgumentException if {@code dst} is not compatible with the tensor (for example,
241    *     mismatched data types or shapes).
242    */
243   Object copyTo(Object dst) {
244     if (dst == null) {
245       if (hasDelegateBufferHandle(nativeHandle)) {
246         return dst;
247       }
248       throw new IllegalArgumentException(
249           "Null outputs are allowed only if the Tensor is bound to a buffer handle.");
250     }
251     throwIfTypeIsIncompatible(dst);
252     throwIfDstShapeIsIncompatible(dst);
253     if (isBuffer(dst)) {
254       copyTo((Buffer) dst);
255     } else {
256       readMultiDimensionalArray(nativeHandle, dst);
257     }
258     return dst;
259   }
260 
 

JNI Functionshttps://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

https://jefflin1982.medium.com/android-jni%E5%A6%82%E4%BD%95%E5%8F%96%E5%BE%97java-object%E7%9A%84%E5%80%BC-96b10631f0ahttps://jefflin1982.medium.com/android-jni%E5%A6%82%E4%BD%95%E5%8F%96%E5%BE%97java-object%E7%9A%84%E5%80%BC-96b10631f0a

TFLite Java Interface implement

 

JNI 函数对应关系和命名方法

The naming convention for the C function is Java_{package_and_classname}_{function_name}(JNI_arguments). The dot in package name is replaced by underscore.

含packageName

package com.xiaomi.mace;

public class JniMaceUtils {

public static native float[] maceMobilenetClassify(Object[] input);

}

JNIEXPORT jfloatArray JNICALL
Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetClassify(
    JNIEnv *env, jclass thisObj, jobjectArray input_array) {

}

Java_packageName_className_functionName 这个命名格式

不含packageName

public class HelloJNI {// A native method that receives nothing and returns voidprivate native void sayHello();
}
对应的Native function name
Java_HelloJNI_sayHello: Java_className_functionName这个命名格式

JNI 怎样传参一个object and return an object 

 类似java 中的反射方法,通过访问class 对象得到methodID 对创建对象而言就是构造函数的 methodID, 调用构造函数,得到对象

Java Native Interface (JNI) - Java Programming Tutorial

这篇关于TFLite JNI 接口实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027271

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja