Skip to content

Commit be08d3d

Browse files
committed
Support all architectures by using pre-packaged dependency
I’ve copied the approach used by the Android Things sample app: https://github.com/androidthings/sample-tensorflow-imageclassifier This dependency downloads an AAR file which includes native binaries for all architectures, along with a Java interface (TensorFlowInferenceInterface) than can be used to interface with TensorFlow model without needing to write any JNI code! Documentation: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android TESTING Before these changes, I could not install the app on my Android 7.0 x86 emulator. It failed with this message: `Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]` After the change, I was successfully able to launch the app and Detect digits. :-)
1 parent 0128fef commit be08d3d

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ dependencies {
4242
})
4343
compile 'com.android.support:appcompat-v7:25.2.0'
4444
testCompile 'junit:junit:4.12'
45-
compile files('libs/libandroid_tensorflow_inference_java.jar')
45+
compile 'org.tensorflow:tensorflow-android:1.2.0'
4646
}
-26.2 KB
Binary file not shown.

app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java

+17-19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616

1717
package com.mindorks.tensorflowexample;
1818

19-
import android.content.res.AssetManager;
20-
import android.os.Trace;
21-
import android.support.v4.os.TraceCompat;
22-
import android.util.Log;
23-
24-
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;
25-
2619
import java.io.BufferedReader;
2720
import java.io.IOException;
2821
import java.io.InputStreamReader;
@@ -32,6 +25,12 @@
3225
import java.util.PriorityQueue;
3326
import java.util.Vector;
3427

28+
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;
29+
30+
import android.content.res.AssetManager;
31+
import android.support.v4.os.TraceCompat;
32+
import android.util.Log;
33+
3534
/**
3635
* Created by amitshekhar on 16/03/17.
3736
*/
@@ -59,6 +58,8 @@ public class TensorFlowImageClassifier implements Classifier {
5958

6059
private TensorFlowInferenceInterface inferenceInterface;
6160

61+
private boolean runStats = false;
62+
6263
private TensorFlowImageClassifier() {
6364
}
6465

@@ -97,10 +98,8 @@ public static Classifier create(
9798
}
9899
br.close();
99100

100-
c.inferenceInterface = new TensorFlowInferenceInterface();
101-
if (c.inferenceInterface.initializeTensorFlow(assetManager, modelFilename) != 0) {
102-
throw new RuntimeException("TF initialization failed");
103-
}
101+
c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelFilename);
102+
104103
// The shape of the output is [N, NUM_CLASSES], where N is the batch size.
105104
int numClasses =
106105
(int) c.inferenceInterface.graph().operation(outputName).output(0).shape().size(1);
@@ -124,19 +123,18 @@ public List<Recognition> recognizeImage(final float[] pixels) {
124123
TraceCompat.beginSection("recognizeImage");
125124

126125
// Copy the input data into TensorFlow.
127-
TraceCompat.beginSection("fillNodeFloat");
128-
inferenceInterface.fillNodeFloat(
129-
inputName, new int[]{inputSize * inputSize}, pixels);
126+
TraceCompat.beginSection("feed");
127+
inferenceInterface.feed(inputName, pixels, new long[]{inputSize * inputSize});
130128
TraceCompat.endSection();
131129

132130
// Run the inference call.
133-
TraceCompat.beginSection("runInference");
134-
inferenceInterface.runInference(outputNames);
131+
TraceCompat.beginSection("run");
132+
inferenceInterface.run(outputNames, runStats);
135133
TraceCompat.endSection();
136134

137135
// Copy the output Tensor back into the output array.
138-
TraceCompat.beginSection("readNodeFloat");
139-
inferenceInterface.readNodeFloat(outputName, outputs);
136+
TraceCompat.beginSection("fetch");
137+
inferenceInterface.fetch(outputName, outputs);
140138
TraceCompat.endSection();
141139

142140
// Find the best classifications.
@@ -168,7 +166,7 @@ public int compare(Recognition lhs, Recognition rhs) {
168166

169167
@Override
170168
public void enableStatLogging(boolean debug) {
171-
inferenceInterface.enableStatLogging(debug);
169+
runStats = debug;
172170
}
173171

174172
@Override
Binary file not shown.

0 commit comments

Comments
 (0)