Tensorflow Lite - 输入形状必须是5维错误

问题描述 投票:0回答:1

我试图将一个tensorflow模型移植到tensorflow lite中,以便在一个android应用程序中使用它。转换是成功的,一切都在运行,除了 Internal error: Failed to run on the given Interpreter: input must be 5-dimensional. 原模型中的输入是 input_shape=(20, 320, 240, 1),是20张320×240的灰度图像(因此...,1)。这里是重要的代码。

List<Mat> preprocessedFrames = preprocFrames(buf);
//has length of 20 -> no problem there (shouldn't affect dimensionality either...)

        int[] output = new int[2];
        float[][][] inputMatrices = new float[preprocessedFrames.toArray().length][320][240];

        for(int i = 0; i < preprocessedFrames.toArray().length; i++) {
            Mat inpRaw = preprocessedFrames.get(i);

            Bitmap data = Bitmap.createBitmap(inpRaw.cols(), inpRaw.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(inpRaw, data);

            int[][] pixels = pixelsFromBitmap(data);
            float[][] inputMatrix = inputMatrixFromIntPixels(pixels);
            // returns float[][] with floats from 0 to 1

            inputMatrices[i] = inputMatrix;
        }

        try{

            detector.run(inputMatrices, output);
            Debug("results: " + output.toString());
        }

模型给我的输出是2个神经元转化为2个标签. 模型代码如下:

model = tf.keras.Sequential(name='detector')
    model.add(tf.keras.layers.Conv3D(filters=(56), input_shape=(20, 320, 240, 1), strides=(2,2,2), kernel_size=(3,11,11), padding='same', activation="relu"))
    model.add(tf.keras.layers.AveragePooling3D(pool_size=(1,4,4)))
    model.add(tf.keras.layers.Conv3D(filters=(72), kernel_size=(4,7,7), strides=(1,2,2), padding='same'))
    model.add(tf.keras.layers.Conv3D(filters=(81), kernel_size=(2,4,4), strides=(2,2,2), padding='same'))
    model.add(tf.keras.layers.Conv3D(filters=(100), kernel_size=(1,2,2), strides=(3,2,2), padding='same'))
    model.add(tf.keras.layers.Conv3D(filters=(128), kernel_size=(1,2,2), padding='same'))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(768, activation='tanh', kernel_regularizer=tf.keras.regularizers.l2(0.011)))
    model.add(tf.keras.layers.Dropout(rate=0.1))
    model.add(tf.keras.layers.Dense(256, activation='sigmoid', kernel_regularizer=tf.keras.regularizers.l2(0.012)))
    model.add(tf.keras.layers.Dense(2, activation='softmax'))

    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001), loss=tf.keras.losses.CategoricalCrossentropy(),
        metrics=['accuracy'])

EDIT: 我打印出了第一个输入张量,如下图所示

int[] shape = detector.getInputTensor(0).shape();
            for(int r = 0; r < shape.length; r++){
                Log.d("********" + r, "*******: " + r + " : " + shape[r]);
            }

我首先得到的输出是[1,20,320,240,1],然后我只得到[20,320,240]。我现在真的很绝望......

android tensorflow2.0 tensorflow-lite
1个回答
0
投票

所以,我自己琢磨了一下,似乎我真的只需要把内容放到一维,每一个像素放到五维,就可以把输入变成五维。我不知道为什么,但我愿意接受这个事实xD。

float[][] output = new float[1][2];
            float[][][][][] inputMatrices = new float[1][preprocessedFrames.toArray().length][320][240][1];

            for(int i = 0; i < preprocessedFrames.toArray().length; i++) {
                Mat inpRaw = preprocessedFrames.get(i);

                Bitmap data = Bitmap.createBitmap(inpRaw.cols(), inpRaw.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(inpRaw, data);

                int[][] pixels = pixelsFromBitmap(data);
                float[][] inputMatrix = inputMatrixFromIntPixels(pixels);

                for (int j = 0; j < inputMatrix.length - 1; j++) {
                    for(int k = 0; k < inputMatrix[0].length - 1; k++) {
                        inputMatrices[0][i][k][j][0] = inputMatrix[j][k];
                    }
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.