From f47d23a33fa2f7df8aeb053efa376fba0041e034 Mon Sep 17 00:00:00 2001 From: yqs Date: Tue, 10 Oct 2023 12:13:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8floatbuffer?= =?UTF-8?q?=E5=88=9B=E5=BB=BAOnnxTensor,=E8=AF=86=E5=88=AB=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3[/visual/search/do]=E7=9A=84QPS=EF=BC=8C=E6=80=A7?= =?UTF-8?q?=E8=83=BD(qps)=E5=A4=A7=E7=BA=A6=E6=8F=90=E5=8D=87=E4=BA=86100%?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../face/search/core/domain/ImageMat.java | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/face-search-core/src/main/java/com/visual/face/search/core/domain/ImageMat.java b/face-search-core/src/main/java/com/visual/face/search/core/domain/ImageMat.java index 7b7e002..c9e44df 100755 --- a/face-search-core/src/main/java/com/visual/face/search/core/domain/ImageMat.java +++ b/face-search-core/src/main/java/com/visual/face/search/core/domain/ImageMat.java @@ -18,6 +18,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.Base64; @@ -693,12 +694,92 @@ public class ImageMat implements Serializable { */ public OnnxTensor to4dFloatOnnxTensorAndDoReleaseMat(boolean firstChannel) { try { - return OnnxTensor.createTensor(env, this.to4dFloatArrayAndDoReleaseMat(firstChannel)); + // return OnnxTensor.createTensor(env, this.to4dFloatArrayAndDoReleaseMat(firstChannel)); + // 经过测试,直接使用floatbuffer创建OnnxTensor,识别接口[/visual/search/do]的QPS,性能(qps)大约提升了100%。 + long[] shape = new long[4]; + return OnnxTensor.createTensor(env, this.toFloatBuffer(shape,firstChannel,true),shape); }catch (Exception e){ throw new RuntimeException(e); } } + /** + * 获取指定行列和通道对应的值 + * @param rowIndex 行号 + * @param colIndex 列号 + * @param channelIndex 通道号 + * @param array 图像一维数组 + * @param cols 图像的列数 + * @param channels 图像的通道数 + * @return 指定行列和通道对应的值 + */ + public static float get(int rowIndex,int colIndex,int channelIndex,float[] array,int cols,int channels){ + return array[rowIndex*cols*channels+colIndex*channels+channelIndex]; + } + + /** + * 将mat转换成floatbuffer + * @param shape 用于接收数组的维度信息,传递参数为 new long[4] 的空数组 + * @param firstChannel + * @param release 是否释放mat + * @return mat对应的floatbuffer + */ + public FloatBuffer toFloatBuffer(long[] shape, boolean firstChannel, boolean release){ + try{ + return toFloatBuffer(shape,firstChannel,this.mat); + }finally { + if(release){ + this.release(); + } + } + } + + /** + * 将mat转换成floatbuffer + * @param shape 用于接收数组的维度信息,传递参数为 new long[4] 的空数组 + * @param firstChannel + * @param mat + * @return mat对应的floatbuffer + */ + public static FloatBuffer toFloatBuffer(long[] shape,boolean firstChannel,Mat mat) { + //为了提高性能将mat转换成1维数组,遍历一维数组的性能远高于频繁的mat.get + float[] data = new float[(int) (mat.total() * mat.channels())]; + mat.get(0, 0, data); + + FloatBuffer floatBuffer = FloatBuffer.allocate(data.length ); + int width = mat.cols(); + int height =mat.rows(); + int channel =mat.channels(); + if(firstChannel){ + for(int k=0; k< channel; k++){ + for(int i=0; i