有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的Tensorflow:java。尼奥。BufferOverFlowException

早上好。我是一名试图将tensorflow模型植入Android的开发者。 我在尝试修复多个错误时遇到了一个以前从未见过的错误。 我现在面临的java.nio.BufferOverFlowException错误是它以前没有发生过,但它突然发生了。 我的代码使用字节数组,但我无法指定问题出在哪一部分

此源接受一个浮点数组作为输入,并在通过模型后返回一个包含10个类的数组。 返回的值具有softmax值

public float[] hypothesis(float[] inputFloats, int nFeatures, int nClasses, Context context)
{
    try {
        int nInstance = inputFloats.length / nFeatures;
       // FloatBuffer.wrap(inputFloats);
        Toast.makeText(context, "", Toast.LENGTH_LONG).show();
        inferenceInterface.feed(INPUT_NODE, FloatBuffer.wrap(inputFloats), INPUT_SIZE);
        inferenceInterface.run(OUTPUT_NODES_HYPO);
        float[] result = new float[nInstance * nClasses];
        inferenceInterface.fetch(OUTPUT_NODE_HYPO, result);
        return result;
    }
    catch(Exception e){
        Toast.makeText(context, e+" ...", Toast.LENGTH_LONG).show();
        return null;
    }
}

inputfloats的长度是720,nFeatures的长度是720nClasses是10。 虽然该值不正确,但它以前工作过。 catch语句中的e打印java.nio.BufferOverFlowException

在将字节数组转换成浮点数组的过程中会出现问题吗? 相关来源

public float[] bytetofloat(byte[] array){
    int[] returnArr = new int[array.length/4];
    float[] returnArr1 = new float[array.length/4];
    for(int i = 0 ; i < returnArr.length; i++){
        //array[i] = 0;
        returnArr[i] = array[i*4] & 0xFF;
        if(returnArr[i] < 0 || returnArr[i]>255)
            Log.d("ARRAY", returnArr[i]+" ");
        returnArr1[i] = (float)returnArr[i];
    }
    return returnArr1;
}

 public Bitmap RGB2GRAY(Bitmap image){
    int width = image.getWidth();
    int height = image.getHeight();
    Bitmap bmOut;
    bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    for(int x = 0; x < width; x++){
        for(int y = 0 ; y < height; y++){
            int pixel = image.getPixel(x, y);
            int A = Color.alpha(pixel);
            int R = Color.red(pixel);
            int G = Color.green(pixel);
            int B = Color.blue(pixel);
            R = G = B = (int)(0.2126 * R + 0.7152 * G + 0.0722 * B);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));

        }
    }
    return bmOut;
}

private void activityPrediction(float[] inputArray){
    try {

        float[] result = activityInference.hypothesis(inputArray, 20*36, 10, getApplicationContext());
        predictionView.setText(Arrays.toString(result));
    }
    catch (Exception e){
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }


}

 private byte[] bitmapToByteArray(Bitmap bitmap)
{
    int chunkNumbers = 10;
    int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
    byte[] imageBytes = new byte[bitmapSize];
    int rows, cols;
    int chunkHeight, chunkWidth;
    rows = cols = (int) Math.sqrt(chunkNumbers);
    chunkHeight = bitmap.getHeight() / rows;
    chunkWidth = bitmap.getWidth() / cols;

    int yCoord = 0;
    int bitmapsSizes = 0;

    for (int x = 0; x < rows; x++)
    {
        int xCoord = 0;
        for (int y = 0; y < cols; y++)
        {
            Bitmap bitmapChunk = Bitmap.createBitmap(bitmap, xCoord, yCoord, chunkWidth, chunkHeight);
            byte[] bitmapArray = getBytesFromBitmapChunk(bitmapChunk);
            System.arraycopy(bitmapArray, 0, imageBytes, bitmapsSizes, bitmapArray.length);
            bitmapsSizes = bitmapsSizes + bitmapArray.length;
            xCoord += chunkWidth;

            bitmapChunk.recycle();
            bitmapChunk = null;
        }
        yCoord += chunkHeight;
    }

    return imageBytes;
}
private byte[] getBytesFromBitmapChunk(Bitmap bitmap)
{
    int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmapSize);
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}

“e.printStackTrace()”结果

at com.example.leehanbeen.platerecognize.ActivityInference.hypothesis(ActivityInference.java:58)
    at com.example.leehanbeen.platerecognize.MainActivity.activityPrediction(MainActivity.java:148)
    at com.example.leehanbeen.platerecognize.MainActivity.access$100(MainActivity.java:28)
    at com.example.leehanbeen.platerecognize.MainActivity$2.onClick(MainActivity.java:69)

围绕主要活动。爪哇:69

byte[] byteArrayRes = bitmapToByteArray(image_bitmap);
float[] inputArray = bytetofloat(byteArrayRes);

activityPrediction(inputArray);

主要活动。爪哇:28

public class MainActivity extends AppCompatActivity {

主要活动。爪哇:148

float[] result = activityInference.hypothesis(inputArray, 20*36, 10, getApplicationContext());

围绕活动推理。爪哇:58

float[] result = new float[nInstance * nClasses];
inferenceInterface.fetch(OUTPUT_NODE_HYPO, result);

共 (0) 个答案