GithubHelp home page GithubHelp logo

amitshekhariitbhu / android-tensorflow-lite-example Goto Github PK

View Code? Open in Web Editor NEW
742.0 25.0 231.0 19.31 MB

Android TensorFlow Lite Machine Learning Example

Home Page: https://amitshekhar.me

License: Apache License 2.0

Java 100.00%
tensorflow tensorflow-tutorials machine-learning tensorflow-lite tensorflow-examples deep-learning deep-neural-networks android-example machine-learning-algorithms tflite

android-tensorflow-lite-example's Introduction

About me

Hi, I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Learn from my blogs: amitshekhar.me/blog

Learn from my videos: youtube.com/@amitshekhar

Get High Paying Tech Job: amitshekhar.me

You can connect with me on:

android-tensorflow-lite-example's People

Contributors

amitshekhariitbhu avatar soum-io avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-tensorflow-lite-example's Issues

FATAL EXCEPTION - pool-1-thread-1

I keep getting this error when i replaced the tflite model and the label to my own tflite model.
java.lang.RuntimeException: Error initializing TensorFlow!
Caused by: java.io.FileNotFoundException: optimized_output_graph.tflite
Is there anything im doing wrong. Please put me through

screenshot 81

Enabling GPU support on Android for Yolov2

How to enable GPU support on Android for YOLOv2 and YOLOv3. I have already converted YOLOv2.pb and YOLOv3.pb to respective .tflite. Can u please tell me what modifications I need to do to enable that?

The common object detection accuracy is poor when deployed a default tflite in mobile

Installed the default tflite apk file in the mobile device using the android studio environment. By default, the installed app has to detect the 90 common objects. The detecting accuracy is very poor.

System information

I didn't make any changes to the TFLIte framework. I cloned the GitHub repo (https://github.com/tensorflow/examples.git) and opened the object detection folder in the android studio. I generated a build and installed that generated Apk file.
Used windows 10 with android studio
Mobile device (Samsung A30)
TensorFlow installed from (https://github.com/tensorflow/examples.git
TensorFlow version 1.15
Describe the current behavior

After installing the tflite default(90 classes) apk file into the mobile. Checked with various object what mentioned in the labelmap file. But the prediction outcome is not correct most of the time.

Why this poor accuracy with the default TFLite Apk file.

Code is not working with inceptionV3 model [cannot convert an tensorflowlite tensor with type float32 to a java object of type [[b (which is compatible with the tensorflowlite type uint8)]

I have changed the required things for inceptionV3 as mentioned below but still its not working. its giving me an error :
cannot convert an tensorflowlite tensor with type float32 to a java object of type [[b (which is compatible with the tensorflowlite type uint8)

Changes that I did :

private static final int INPUT_SIZE = 299;
/**
* The inception net requires additional normalization of the used input.
*/
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128.0f;
private ByteBuffer convertBitmapToByteBufferForInceptionV3(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE * 4);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[inputSize * inputSize];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int pixel = 0;
for (int i = 0; i < inputSize; ++i) {
for (int j = 0; j < inputSize; ++j) {
final int val = intValues[pixel++];

                byteBuffer.putFloat((((val >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
                byteBuffer.putFloat((((val >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
                byteBuffer.putFloat(((val & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
            
        }
    }
    return byteBuffer;
}

image size with new model

I re-retrain my model and once I put inside it said:

Cannot copy to a TensorFlowLite tensor (Image) with 602112 bytes from a Java Buffer with 150528 bytes.

1 x 224 x 224 x 3 x 4 will get correct result but now it misses x 4

how can I fix it?

Reading model from internal storage

Hi there.

I wanted to know if it is possible to store and read the trained .tflite model from the Android device's internal storage instead of the assets folder?

The issue I am having is with the startOffset value in the loadModelFile(AssetManager assetManager, String modelPath) function. currently, it comes from AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath).
This file descriptor is used to get the start offset and declared length. Is there another way to read it from internal memory instead and still get the start offset and declaredLength? If not, is there a way to calculate the startOffset of a new model and its declared length when reading the raw binary from internal storage?

CreateProcess error=2, The system cannot find the file specified

image

hello sir, i need to learn about machine learning and i start with your amazing project but when i run, this error showed and i have no idea, can you help me found the solution ?

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformNativeLibsWithStripDebugSymbolForDebug'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:63)
at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88)
at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:124)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:80)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:105)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:99)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:99)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'E:\Ade\old\sdk\ndk-bundle\toolchains\mips64el-linux-android-4.9\prebuilt\windows-x86_64\bin\mips64el-linux-android-strip''
at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:220)
at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:204)
at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:340)
at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:86)
at org.gradle.internal.operations.BuildOperationIdentifierPreservingRunnable.run(BuildOperationIdentifierPreservingRunnable.java:39)
... 6 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'E:\Ade\old\sdk\ndk-bundle\toolchains\mips64el-linux-android-4.9\prebuilt\windows-x86_64\bin\mips64el-linux-android-strip'
at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
at net.rubygrapefruit.platform.internal.WindowsProcessLauncher.start(WindowsProcessLauncher.java:22)
at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:68)
... 7 more
Caused by: java.io.IOException: Cannot run program "E:\Ade\old\sdk\ndk-bundle\toolchains\mips64el-linux-android-4.9\prebuilt\windows-x86_64\bin\mips64el-linux-android-strip" (in directory "E:\Ade\new\tutor\MLearning\Android-TensorFlow-Lite-Example-master\app"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
... 10 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 11 more

change model to .tflite file

I tried to convert the model I made into tflite and execute it.
I don't know what the problem is, but if i press the detection button, the app is shut down.
The model I made is sort of cat type, and I think there was a problem in the process of modifying the model to tflite.
Is there a way to find out if the model is .tflite file?

Replaced the model and the label but it Crashes!

OS: Ubuntu 16.0.4 64bit
Python version: 3.6.5 on virtual env
Tensorflow version: 1.11.0

I retrained a model with model architecture (mobilenet_v1_224) . cant freeze it because it says it is already frozen after retraining. then I convert it using toco. I import it to assets folder ,the graph.tflite and labels.txt, edit the model and label variables in MainActivity,
but when I launched it in my device and hit "Detect Object" button, it crashes.Am I missing something? How can I fix this. Please help me, Thank you very much!!

Image Scanner activity not close on finish() and onBackPressed() not working

Hi @amitshekhariitbhu this work has been great so far, But when i tried to call the Image Scanner activity from another activity and get the result it is not working(activity not close) also onBackPressed() working and finally how can we train the model to recognize more object.

Below is my code:

Intent sendCapturedCode = new Intent();
sendCapturedCode.putExtra(getString(R.string.captured_image_tflow_result), imageAnalysesResult);
sendCapturedCode.putExtra(getString(R.string.captured_image_bitmap), capturedBitmap);
setResult(RESULT_OK, sendCapturedCode);
finish();

Cannot convert between a TensorFlowLite buffer with XXX bytes and a ByteBuffer with XXX bytes

Hi, many thanks for the article and the sample-code.

It works fine with the model mentioned in your project. However, I trained my own model (using the tensorflow-for-poets 1 and 2 tutorials) but I get an error using my model with your code:

"Cannot convert between a TensorFlowLite buffer with XXX bytes and a ByteBuffer with XXX bytes."

This happens when running the following statement:
interpreter.run(byteBuffer, result);

My model works fine with the sample-project in "the tensorflow-for-poets2 tutorial.

Just wondering what can be the issue. Any ideas?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.