【问题标题】:Android TensorFlow - prevent resize ImageAndroid TensorFlow - 防止调整图像大小
【发布时间】:2018-10-11 04:59:26
【问题描述】:

我正在处理 TensorFlow 风格化图像。但是,我面临的问题是它会调整我的实际图像大小。我想在整个图像本身上应用样式。例如,如果我的图像分辨率是 1280x960,那么在我对其应用样式后它应该是相同的。

我没有使用默认的 INPUT_SIZE 值 256。使用默认值可以正常工作。这是我用来防止调整图像大小的代码。

private TensorFlowInferenceInterface inferenceInterface;
private void applyStyle(){
    inferenceInterface = new TensorFlowInferenceInterface(mActivity.getAssets(), "bossK_float.pb");
    Bitmap bitmap = getBitmapFromPath();
    bitmap=Bitmap.createBitmap(bitmap,0,bitmap.getWidth(),bitmap.getHeight(), matrix, true);
    INPUT_SIZE_WIDTH = bitmap.getWidth();
    INPUT_SIZE_HEIGHT = bitmap.getHeight();
    mStyledBitmap = stylizeImage(bitmap);
}

private Bitmap stylizeImage(Bitmap bitmap) {
            Bitmap scaledBitmap = scaleBitmap(bitmap, INPUT_SIZE_WIDTH, INPUT_SIZE_HEIGHT);

            intValues = new int[INPUT_SIZE_WIDTH * INPUT_SIZE_HEIGHT];
            floatValues = new float[INPUT_SIZE_WIDTH * INPUT_SIZE_HEIGHT * 3];

            scaledBitmap.getPixels(intValues, 0, scaledBitmap.getWidth(), 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());
            scaledBitmap = scaledBitmap.copy(Bitmap.Config.ARGB_8888, true);



            for (int i = 0; i < intValues.length; ++i) {
                final int val = intValues[i];
                floatValues[i * 3 + 0] = ((val >> 16) & 0xFF) * 1.0f;
                floatValues[i * 3 + 1] = ((val >> 8) & 0xFF) * 1.0f;
                floatValues[i * 3 + 2] = (val & 0xFF) * 1.0f;
            }
            Trace.beginSection("feed");
            inferenceInterface.feed(INPUT_NAME, floatValues, INPUT_SIZE_WIDTH, INPUT_SIZE_HEIGHT, 3);
            Trace.endSection();

            Trace.beginSection("run");
            inferenceInterface.run(new String[]{OUTPUT_NAME});
            Trace.endSection();

            Trace.beginSection("fetch");
            inferenceInterface.fetch(OUTPUT_NAME, floatValues);
            Trace.endSection();

            for (int i = 0; i < intValues.length; ++i) {
                intValues[i] =
                        0xFF000000
                                | (((int) (floatValues[i * 3 + 0])) << 16)
                                | (((int) (floatValues[i * 3 + 1])) << 8)
                                | ((int) (floatValues[i * 3 + 2]));
            }
            scaledBitmap.setPixels(intValues, 0, scaledBitmap.getWidth(), 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());

            return scaledBitmap;
        }

    private Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBitmap = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        return newBitmap;
    }

当我将 INPUT_SIZE 值更改为 INPUT_SIZE_WIDTH 和 INPUT_SIZE_HEIGHT 时,我的应用程序会停止并且没有错误消息。我调试了这段代码,但它卡在这段代码上并停止了我的应用程序:

Trace.beginSection("run");
inferenceInterface.run(new String[]{OUTPUT_NAME});
Trace.endSection();

请告诉我,如何使用 TensorFlow 设置整个图像的样式。

谢谢!

【问题讨论】:

  • 您必须使用模型所需的确切输入大小。如果您想使用其他输入大小,则必须使用另一个固定输入大小或可变输入大小(这更困难)重新训练您的模型。但是,我不建议使用如此高的输入大小,因为这会使您在移动设备上的性能非常差。
  • @sladomic 感谢您的回复!是的,在模型中具有可变输入大小更加困难。但是你能告诉我哪种尺寸最适合在每台移动设备上工作吗?我有输入大小为 2000 和 1024 的模型文件,但这两个都给 OOM。根据我的经验,我知道输入大小为 256 可以正常工作。我什至不能使用 512 作为输入大小,虽然我在 AsyncTask 中运行它,但它表示主线程上的进程太多。

标签: android image-processing tensorflow styles


【解决方案1】:

由于大小不同,您的代码停在那里。您可能一定遇到了 ArrayOutOfBound 异常。

该模型将被训练以接受特定尺寸的图像。因此,每当您进行分类时,图像都会缩小到该特定大小。

即使是您在创建 pb/lite/tflite 文件时的训练数据也将被转换为接受您在模型创建中提到的相同大小的图像。结果不会影响到更大的灭绝。你可以试试看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多