1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 https://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 package org.tensorflow.ovic; 16 17 import android.graphics.Bitmap; 18 import android.os.SystemClock; 19 import android.util.Log; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.nio.ByteBuffer; 23 import java.nio.MappedByteBuffer; 24 25 /** 26 * Base class that benchmarks image models. 27 * 28 * <p>===================== General workflow ======================= 29 * 30 * <pre>{@code 31 * benchmarker = new OvicBenchmarker(); 32 * benchmarker.getReadyToTest(labelInputStream, model); 33 * while (!benchmarker.shouldStop()) { 34 * Bitmap bitmap = ... 35 * imgId = ... 36 * benchmarker.processBitmap(bitmap, imgId); 37 * } 38 * }</pre> 39 */ 40 public abstract class OvicBenchmarker { 41 /** Tag for the {@link Log}. */ 42 private static final String TAG = "OvicBenchmarker"; 43 44 /** Dimensions of inputs. */ 45 protected static final int DIM_BATCH_SIZE = 1; 46 protected static final int DIM_PIXEL_SIZE = 3; 47 protected int imgHeight = 224; 48 protected int imgWidth = 224; 49 50 /* Preallocated buffers for storing image data in. */ 51 protected int[] intValues = null; 52 53 /** A ByteBuffer to hold image data, to be feed into classifier as inputs. */ 54 protected ByteBuffer imgData = null; 55 56 /** Total runtime in ms. */ 57 protected double totalRuntime = 0.0; 58 /** Total allowed runtime in ms. */ 59 protected double wallTime = 20000 * 30.0; 60 /** Record whether benchmark has started (used to skip the first image). */ 61 protected boolean benchmarkStarted = false; 62 63 /** 64 * Initializes an {@link OvicBenchmarker} 65 * 66 * @param wallTime: a double number specifying the total amount of time to benchmark. 67 */ OvicBenchmarker(double wallTime)68 public OvicBenchmarker(double wallTime) { 69 benchmarkStarted = false; 70 totalRuntime = 0.0; 71 this.wallTime = wallTime; 72 } 73 74 /** Return the cumulative latency of all runs so far. */ getTotalRunTime()75 public double getTotalRunTime() { 76 return totalRuntime; 77 } 78 79 /** Check whether the benchmarker should stop. */ shouldStop()80 public Boolean shouldStop() { 81 if (totalRuntime >= wallTime) { 82 Log.e( 83 TAG, 84 "Total runtime " 85 + Double.toString(totalRuntime) 86 + " exceeded walltime " 87 + Double.toString(wallTime)); 88 return true; 89 } 90 return false; 91 } 92 93 /** Abstract class for checking whether the benchmarker is ready to start processing images */ readyToTest()94 public abstract boolean readyToTest(); 95 96 /** 97 * Abstract class for getting the benchmarker ready. 98 * 99 * @param labelInputStream: an {@link InputStream} specifying where the list of labels should be 100 * read from. 101 * @param model: a {@link MappedByteBuffer} model to benchmark. 102 */ getReadyToTest(InputStream labelInputStream, MappedByteBuffer model)103 public abstract void getReadyToTest(InputStream labelInputStream, MappedByteBuffer model); 104 105 /** 106 * Perform test on a single bitmap image. 107 * 108 * @param bitmap: a {@link Bitmap} image to process. 109 * @param imageId: an ID uniquely representing the image. 110 */ processBitmap(Bitmap bitmap, int imageId)111 public abstract boolean processBitmap(Bitmap bitmap, int imageId) 112 throws IOException, InterruptedException; 113 114 /** Perform test on a single bitmap image without an image ID. */ processBitmap(Bitmap bitmap)115 public boolean processBitmap(Bitmap bitmap) throws IOException, InterruptedException { 116 return processBitmap(bitmap, /* imageId = */ 0); 117 } 118 119 /** Returns the last inference results as string. */ getLastResultString()120 public abstract String getLastResultString(); 121 122 /** 123 * Loads input buffer from intValues into ByteBuffer for the interpreter. 124 * Input buffer must be loaded in intValues and output will be placed in imgData. 125 */ loadsInputToByteBuffer()126 protected void loadsInputToByteBuffer() { 127 if (imgData == null || intValues == null) { 128 throw new RuntimeException("Benchmarker is not yet ready to test."); 129 } 130 // Convert the image to ByteBuffer. 131 imgData.rewind(); 132 int pixel = 0; 133 long startTime = SystemClock.uptimeMillis(); 134 135 for (int i = 0; i < imgHeight; ++i) { 136 for (int j = 0; j < imgWidth; ++j) { 137 final int pixelValue = intValues[pixel++]; 138 imgData.put((byte) ((pixelValue >> 16) & 0xFF)); 139 imgData.put((byte) ((pixelValue >> 8) & 0xFF)); 140 imgData.put((byte) (pixelValue & 0xFF)); 141 } 142 } 143 long endTime = SystemClock.uptimeMillis(); 144 Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime)); 145 } 146 } 147