diff --git a/README.md b/README.md index cc8a9ec..173df2d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,4 @@ # Android TensorFlow Machine Learning Example -[![Mindorks](https://img.shields.io/badge/mindorks-opensource-blue.svg)](https://mindorks.com/open-source-projects) -[![Mindorks Community](https://img.shields.io/badge/join-community-blue.svg)](https://mindorks.com/join-community) -[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102)](https://opensource.org/licenses/Apache-2.0) -[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/amitshekhariitbhu/AndroidTensorFlowMachineLearningExample/blob/master/LICENSE) - - ## About Android TensorFlow Machine Learning Example * This is an example project for integrating [TensorFlow](https://github.com/tensorflow/tensorflow) into Android application @@ -12,14 +6,16 @@ * How to build TensorFlow library(.so file and jar file) to use with Android Application. * This project include an example for object detection for an image taken from camera using TensorFlow library. -# [Read this article. It describes everything about building TensorFlow for Android.](https://blog.mindorks.com/android-tensorflow-machine-learning-example-ff0e9b2654cc) +## Prepare for Machine Learning Interview: [Machine Learning Interview Questions](https://github.com/amitshekhariitbhu/machine-learning-interview-questions) + +# [Check the Android TensorFlow Lite Machine Learning Example.](https://github.com/amitshekhariitbhu/Android-TensorFlow-Lite-Example)

- - - + + +

- +

@@ -29,11 +25,9 @@ ### Credits * The classifier example has been taken from Google TensorFlow example. -[Check out Mindorks awesome open source projects here](https://mindorks.com/open-source-projects) - ### License ``` - Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED + Copyright (C) 2022 Amit Shekhar Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/app/build.gradle b/app/build.gradle index fdf34b1..e73348b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,6 +42,6 @@ dependencies { }) compile 'com.android.support:appcompat-v7:27.0.2' testCompile 'junit:junit:4.12' - compile files('libs/libandroid_tensorflow_inference_java.jar') + compile 'org.tensorflow:tensorflow-android:1.2.0' compile 'com.wonderkiln:camerakit:0.13.1' } diff --git a/app/libs/libandroid_tensorflow_inference_java.jar b/app/libs/libandroid_tensorflow_inference_java.jar deleted file mode 100755 index 3b8d93b..0000000 Binary files a/app/libs/libandroid_tensorflow_inference_java.jar and /dev/null differ diff --git a/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java b/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java index b53a9fb..78b6971 100644 --- a/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java +++ b/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java @@ -18,7 +18,7 @@ import android.content.res.AssetManager; import android.graphics.Bitmap; -import android.os.Trace; +import android.support.v4.os.TraceCompat; import android.util.Log; import org.tensorflow.contrib.android.TensorFlowInferenceInterface; @@ -41,7 +41,7 @@ */ public class TensorFlowImageClassifier implements Classifier { - private static final String TAG = "TensorFlowImageClassifier"; + private static final String TAG = "ImageClassifier"; // Only return this many results with at least this confidence. private static final int MAX_RESULTS = 3; @@ -63,6 +63,8 @@ public class TensorFlowImageClassifier implements Classifier { private TensorFlowInferenceInterface inferenceInterface; + private boolean runStats = false; + private TensorFlowImageClassifier() { } @@ -105,10 +107,7 @@ public static Classifier create( } br.close(); - c.inferenceInterface = new TensorFlowInferenceInterface(); - if (c.inferenceInterface.initializeTensorFlow(assetManager, modelFilename) != 0) { - throw new RuntimeException("TF initialization failed"); - } + c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelFilename); // The shape of the output is [N, NUM_CLASSES], where N is the batch size. int numClasses = (int) c.inferenceInterface.graph().operation(outputName).output(0).shape().size(1); @@ -133,9 +132,9 @@ public static Classifier create( @Override public List recognizeImage(final Bitmap bitmap) { // Log this method so that it can be analyzed with systrace. - Trace.beginSection("recognizeImage"); + TraceCompat.beginSection("recognizeImage"); - Trace.beginSection("preprocessBitmap"); + TraceCompat.beginSection("preprocessBitmap"); // Preprocess the image data from 0-255 int to normalized float based // on the provided parameters. bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); @@ -145,23 +144,23 @@ public List recognizeImage(final Bitmap bitmap) { floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd; floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd; } - Trace.endSection(); + TraceCompat.endSection(); // Copy the input data into TensorFlow. - Trace.beginSection("fillNodeFloat"); - inferenceInterface.fillNodeFloat( - inputName, new int[]{1, inputSize, inputSize, 3}, floatValues); - Trace.endSection(); + TraceCompat.beginSection("feed"); + inferenceInterface.feed( + inputName, floatValues, new long[]{1, inputSize, inputSize, 3}); + TraceCompat.endSection(); // Run the inference call. - Trace.beginSection("runInference"); - inferenceInterface.runInference(outputNames); - Trace.endSection(); + TraceCompat.beginSection("run"); + inferenceInterface.run(outputNames, runStats); + TraceCompat.endSection(); // Copy the output Tensor back into the output array. - Trace.beginSection("readNodeFloat"); - inferenceInterface.readNodeFloat(outputName, outputs); - Trace.endSection(); + TraceCompat.beginSection("fetch"); + inferenceInterface.fetch(outputName, outputs); + TraceCompat.endSection(); // Find the best classifications. PriorityQueue pq = @@ -186,13 +185,13 @@ public int compare(Recognition lhs, Recognition rhs) { for (int i = 0; i < recognitionsSize; ++i) { recognitions.add(pq.poll()); } - Trace.endSection(); // "recognizeImage" + TraceCompat.endSection(); // "recognizeImage" return recognitions; } @Override public void enableStatLogging(boolean debug) { - inferenceInterface.enableStatLogging(debug); + runStats = debug; } @Override diff --git a/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so b/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so deleted file mode 100755 index 9390465..0000000 Binary files a/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so and /dev/null differ diff --git a/assets/ml_android.png b/assets/ml_android.png deleted file mode 100644 index fcdf9f5..0000000 Binary files a/assets/ml_android.png and /dev/null differ