1818
1919import android .content .res .AssetManager ;
2020import android .graphics .Bitmap ;
21- import android .os .Trace ;
21+ import android .support . v4 . os .TraceCompat ;
2222import android .util .Log ;
2323
2424import org .tensorflow .contrib .android .TensorFlowInferenceInterface ;
4141 */
4242public class TensorFlowImageClassifier implements Classifier {
4343
44- private static final String TAG = "TensorFlowImageClassifier " ;
44+ private static final String TAG = "ImageClassifier " ;
4545
4646 // Only return this many results with at least this confidence.
4747 private static final int MAX_RESULTS = 3 ;
@@ -63,6 +63,8 @@ public class TensorFlowImageClassifier implements Classifier {
6363
6464 private TensorFlowInferenceInterface inferenceInterface ;
6565
66+ private boolean runStats = false ;
67+ 6668 private TensorFlowImageClassifier () {
6769 }
6870
@@ -105,10 +107,7 @@ public static Classifier create(
105107 }
106108 br .close ();
107109
108- c .inferenceInterface = new TensorFlowInferenceInterface ();
109- if (c .inferenceInterface .initializeTensorFlow (assetManager , modelFilename ) != 0 ) {
110- throw new RuntimeException ("TF initialization failed" );
111- }
110+ c .inferenceInterface = new TensorFlowInferenceInterface (assetManager , modelFilename );
112111 // The shape of the output is [N, NUM_CLASSES], where N is the batch size.
113112 int numClasses =
114113 (int ) c .inferenceInterface .graph ().operation (outputName ).output (0 ).shape ().size (1 );
@@ -133,9 +132,9 @@ public static Classifier create(
133132 @ Override
134133 public List <Recognition > recognizeImage (final Bitmap bitmap ) {
135134 // Log this method so that it can be analyzed with systrace.
136- Trace .beginSection ("recognizeImage" );
135+ TraceCompat .beginSection ("recognizeImage" );
137136
138- Trace .beginSection ("preprocessBitmap" );
137+ TraceCompat .beginSection ("preprocessBitmap" );
139138 // Preprocess the image data from 0-255 int to normalized float based
140139 // on the provided parameters.
141140 bitmap .getPixels (intValues , 0 , bitmap .getWidth (), 0 , 0 , bitmap .getWidth (), bitmap .getHeight ());
@@ -145,23 +144,23 @@ public List<Recognition> recognizeImage(final Bitmap bitmap) {
145144 floatValues [i * 3 + 1 ] = (((val >> 8 ) & 0xFF ) - imageMean ) / imageStd ;
146145 floatValues [i * 3 + 2 ] = ((val & 0xFF ) - imageMean ) / imageStd ;
147146 }
148- Trace .endSection ();
147+ TraceCompat .endSection ();
149148
150149 // Copy the input data into TensorFlow.
151- Trace .beginSection ("fillNodeFloat " );
152- inferenceInterface .fillNodeFloat (
153- inputName , new int []{1 , inputSize , inputSize , 3 }, floatValues );
154- Trace .endSection ();
150+ TraceCompat .beginSection ("feed " );
151+ inferenceInterface .feed (
152+ inputName , floatValues , new long []{1 , inputSize , inputSize , 3 });
153+ TraceCompat .endSection ();
155154
156155 // Run the inference call.
157- Trace .beginSection ("runInference " );
158- inferenceInterface .runInference (outputNames );
159- Trace .endSection ();
156+ TraceCompat .beginSection ("run " );
157+ inferenceInterface .run (outputNames , runStats );
158+ TraceCompat .endSection ();
160159
161160 // Copy the output Tensor back into the output array.
162- Trace .beginSection ("readNodeFloat " );
163- inferenceInterface .readNodeFloat (outputName , outputs );
164- Trace .endSection ();
161+ TraceCompat .beginSection ("fetch " );
162+ inferenceInterface .fetch (outputName , outputs );
163+ TraceCompat .endSection ();
165164
166165 // Find the best classifications.
167166 PriorityQueue <Recognition > pq =
@@ -186,13 +185,13 @@ public int compare(Recognition lhs, Recognition rhs) {
186185 for (int i = 0 ; i < recognitionsSize ; ++i ) {
187186 recognitions .add (pq .poll ());
188187 }
189- Trace .endSection (); // "recognizeImage"
188+ TraceCompat .endSection (); // "recognizeImage"
190189 return recognitions ;
191190 }
192191
193192 @ Override
194193 public void enableStatLogging (boolean debug ) {
195- inferenceInterface . enableStatLogging ( debug ) ;
194+ runStats = debug ;
196195 }
197196
198197 @ Override
0 commit comments