Skip to content

Using trained model in Android #4

@milsun

Description

@milsun

Hi
Can you please provide code for using the trained model in Android; I have been trying, but app crashes everytime.

Here is the code I am using:

   AssetManager assetManager = context.getResources().getAssets();
    sTFInterfaceSuper = new TensorFlowInferenceInterface(assetManager, MODEL_FILE_SUPER);

   public synchronized static Bitmap getSuperBitmap(final Bitmap bitmap) {
    if (sTFInterfaceSuper == null) {
        Log.d("dude","tf model is NOT initialized.");
        return null;
    }

    if (bitmap == null) {
        return null;
    }

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();

    final int newW = 4 * w;
    final int newH = 4 * h;

    int[] mIntValues = new int[w * h];
    float[] mFlatIntValues = new float[w * h * 3];
    float[] mOutputs = new float[newW * newH * 3];

    int[] input_shape = new int[]{w, h, 3};

    bitmap.getPixels(mIntValues, 0, w, 0, 0, w, h);

    for (int i = 0; i < mIntValues.length; ++i) {
        final int val = mIntValues[i];
        mFlatIntValues[i * 3 + 0] = (float) ((val >> 16) & 0xFF) / 255;
        mFlatIntValues[i * 3 + 1] = (float) ((val >> 8) & 0xFF) / 255;
        mFlatIntValues[i * 3 + 2] = (float) (val & 0xFF) / 255;
    }

    final long start = System.currentTimeMillis();

    sTFInterfaceSuper.feed("input_shape",input_shape, 3);
    sTFInterfaceSuper.feed(INPUT_NAME_SUPER, mFlatIntValues, w, h, 3 );

    sTFInterfaceSuper.run(new String[] { OUTPUT_NAME_SUPER }, true);

    sTFInterfaceSuper.fetch(OUTPUT_NAME_SUPER, mOutputs);

    final long end = System.currentTimeMillis();
    Log.d("dude","%d millis Super time. " + (end - start));

    Bitmap output = Bitmap.createBitmap(newW, newH, Bitmap.Config.ARGB_8888);

    for (int y = 0; y < newH; y++) {
        for (int x = 0; x < newW; x++) {
            output.setPixel(x, y, (int)mOutputs[y * newW + x]);
        }
    }

    return output;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions