Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using trained model in Android #4

Open
milsun opened this issue Nov 6, 2018 · 2 comments
Open

Using trained model in Android #4

milsun opened this issue Nov 6, 2018 · 2 comments

Comments

@milsun
Copy link

milsun commented Nov 6, 2018

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;
}
@gitman88
Copy link

Did you solve this problem?

@ando0689
Copy link

I think the app crashes because the trained models are .pb not .tflite, I don't know how the author uses .pb directly in Android, but as far as I researched, it needs to be converted to .tflite first.
So if the author could provide a way to convert it to .tflite, everything else will be easy..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants