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

Support all architectures using 'org.tensorflow:tensorflow-android' dependency #1

Merged
merged 3 commits into from
Sep 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.2.0'
testCompile 'junit:junit:4.12'
compile files('libs/libandroid_tensorflow_inference_java.jar')
compile 'org.tensorflow:tensorflow-android:1.2.0'
}
Binary file removed app/libs/libandroid_tensorflow_inference_java.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@

package com.mindorks.tensorflowexample;

import android.content.res.AssetManager;
import android.os.Trace;
import android.util.Log;

import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -31,6 +25,12 @@
import java.util.PriorityQueue;
import java.util.Vector;

import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

import android.content.res.AssetManager;
import android.support.v4.os.TraceCompat;
import android.util.Log;

/**
* Created by amitshekhar on 16/03/17.
*/
Expand All @@ -40,7 +40,7 @@
*/
public class TensorFlowImageClassifier implements Classifier {

private static final String TAG = "TensorFlowImageClassifier";
private static final String TAG = "TFImageClassifier";

// Only return this many results with at least this confidence.
private static final int MAX_RESULTS = 3;
Expand All @@ -58,6 +58,8 @@ public class TensorFlowImageClassifier implements Classifier {

private TensorFlowInferenceInterface inferenceInterface;

private boolean runStats = false;

private TensorFlowImageClassifier() {
}

Expand Down Expand Up @@ -96,10 +98,8 @@ 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);
Expand All @@ -120,23 +120,22 @@ public static Classifier create(
@Override
public List<Recognition> recognizeImage(final float[] pixels) {
// Log this method so that it can be analyzed with systrace.
Trace.beginSection("recognizeImage");
TraceCompat.beginSection("recognizeImage");

// Copy the input data into TensorFlow.
Trace.beginSection("fillNodeFloat");
inferenceInterface.fillNodeFloat(
inputName, new int[]{inputSize * inputSize}, pixels);
Trace.endSection();
TraceCompat.beginSection("feed");
inferenceInterface.feed(inputName, pixels, new long[]{inputSize * inputSize});
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<Recognition> pq =
Expand All @@ -161,13 +160,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
Expand Down
Binary file not shown.