gpt4 book ai didi

java - 将自定义TensorFlowLite模型添加到就绪的Android应用程序时出现问题

转载 作者:行者123 更新时间:2023-11-30 12:04:20 25 4
gpt4 key购买 nike

我正在尝试使用以下仓库添加我的自定义tflite模型:https://github.com/amitshekhariitbhu/Android-TensorFlow-Lite-Example。我的.tflite模型参数是:

== Input details ==
name: x shape: [ 1 256 256 3]
type: <class 'numpy.float32'>
== Output details ==
name: Identity shape: [ 1 30]
type: <class 'numpy.float32'>


我以.tflite格式和labels.txt添加了模型。

这是我的错误:

java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 786432 bytes and a ByteBuffer with 196608 bytes.
at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:272)
at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:249)
at org.tensorflow.lite.Tensor.setTo(Tensor.java:110)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:145)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:275)
at org.tensorflow.lite.Interpreter.run(Interpreter.java:249)
at com.amitshekhar.tflite.TensorFlowImageClassifier.recognizeImage(TensorFlowImageClassifier.java:66)
at com.amitshekhar.tflite.MainActivity$1.onImage(MainActivity.java:70)
at com.wonderkiln.camerakit.EventDispatcher$1.run(EventDispatcher.java:42)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)


我需要为真正的工作应用程序更改哪些参数?

最佳答案

我在TensorFlowImageClassifier.java中更改了以下字符串:

private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer;

if(quant) {
byteBuffer = ByteBuffer.allocateDirect(BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE);
} else {
byteBuffer = ByteBuffer.allocateDirect(4 * BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE);
}

byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[inputSize * inputSize];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int pixel = 0;
for (int i = 0; i < inputSize; ++i) {
for (int j = 0; j < inputSize; ++j) {
final int val = intValues[pixel++];
if(quant){
byteBuffer.put((byte) ((val >> 16) & 0xFF));
byteBuffer.put((byte) ((val >> 8) & 0xFF));
byteBuffer.put((byte) (val & 0xFF));
} else {
byteBuffer.put((byte) ((((val >> 16) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
byteBuffer.put((byte) ((((val >> 8) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
byteBuffer.put((byte) ((((val) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
}

}
}
return byteBuffer;
}


@Override
public List<Recognition> recognizeImage(Bitmap bitmap) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
if(quant){
byte[][] result = new byte[1][labelList.size()];
interpreter.run(byteBuffer, result);
return getSortedResultByte(result);
} else {
byte [][] result = new byte[1][labelList.size()];
interpreter.run(byteBuffer, result);
return getSortedResultByte(result);


而MainActivity.java中的这个字符串:

private static final boolean QUANT = true;
private static final int INPUT_SIZE = 256;


但是它不能以正确的方式工作...
另外,我认为,其中一些参数必须更改:

private static final int MAX_RESULTS = 3;
private static final int BATCH_SIZE = 1;
private static final int PIXEL_SIZE = 3;
private static final float THRESHOLD = 0.1f;

private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128;


在我的情况下,哪些参数也很重要?

关于java - 将自定义TensorFlowLite模型添加到就绪的Android应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com