xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/java/ovic/README.md (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# OVIC Benchmarker for LPCV 2020
2
3This folder contains the SDK for track one of the
4[Low Power Computer Vision workshop at CVPR 2020.](https://lpcv.ai/2020CVPR/ovic-track)
5
6## Pre-requisite
7
8Follow the steps [here](https://www.tensorflow.org/lite/demo_android) to install
9Tensorflow, Bazel, and the Android NDK and SDK.
10
11## Test the benchmarker:
12
13The testing utilities helps the developers (you) to make sure that your
14submissions in TfLite format will be processed as expected in the competition's
15benchmarking system.
16
17Note: for now the tests only provides correctness checks, i.e. classifier
18predicts the correct category on the test image, but no on-device latency
19measurements. To test the latency measurement functionality, the tests will
20print the latency running on a desktop computer, which is not indicative of the
21on-device run-time. We are releasing an benchmarker Apk that would allow
22developers to measure latency on their own devices.
23
24### Obtain the sample models
25
26The test data (models and images) should be downloaded automatically for you by
27Bazel. In case they are not, you can manually install them as below.
28
29Note: all commands should be called from your tensorflow installation folder
30(under this folder you should find `tensorflow/lite`).
31
32*   Download the
33    [testdata package](https://storage.googleapis.com/download.tensorflow.org/data/ovic_2019_04_30.zip):
34
35```sh
36curl -L https://storage.googleapis.com/download.tensorflow.org/data/ovic_2019_04_30.zip -o /tmp/ovic.zip
37```
38
39*   Unzip the package into the testdata folder:
40
41```sh
42unzip -j /tmp/ovic.zip -d tensorflow/lite/java/ovic/src/testdata/
43```
44
45### Run tests
46
47You can run test with Bazel as below. This helps to ensure that the installation
48is correct.
49
50```sh
51bazel test //tensorflow/lite/java/ovic:OvicClassifierTest --cxxopt=-Wno-all --test_output=all
52
53bazel test //tensorflow/lite/java/ovic:OvicDetectorTest --cxxopt=-Wno-all --test_output=all
54```
55
56### Test your submissions
57
58Once you have a submission that follows the instructions from the
59[competition site](https://lpcv.ai/2020CVPR/ovic-track), you can verify it in
60two ways:
61
62#### Validate using randomly generated images
63
64You can call the validator binary below to verify that your model fits the
65format requirements. This often helps you to catch size mismatches (e.g. output
66for classification should be [1, 1001] instead of [1,1,1,1001]). Let say the
67submission file is located at `/path/to/my_model.lite`, then call:
68
69```sh
70bazel build //tensorflow/lite/java/ovic:ovic_validator --cxxopt=-Wno-all
71bazel-bin/tensorflow/lite/java/ovic/ovic_validator /path/to/my_model.lite classify
72```
73
74Successful validation should print the following message to terminal:
75
76```
77Successfully validated /path/to/my_model.lite.
78
79```
80
81To validate detection models, use the same command but provide "detect" as the
82second argument instead of "classify".
83
84#### Test that the model produces sensible outcomes
85
86You can go a step further to verify that the model produces results as expected.
87This helps you catch bugs during TFLite conversion (e.g. using the wrong mean
88and std values).
89
90*   Move your submission to the testdata folder:
91
92```sh
93cp /path/to/my_model.lite tensorflow/lite/java/ovic/src/testdata/
94```
95
96*   Resize the test image to the resolutions that are expected by your
97    submission:
98
99The test images can be found at
100`tensorflow/lite/java/ovic/src/testdata/test_image_*.jpg`. You may reuse these
101images if your image resolutions are 128x128 or 224x224.
102
103*   Add your model and test image to the BUILD rule at
104    `tensorflow/lite/java/ovic/src/testdata/BUILD`:
105
106```JSON
107filegroup(
108    name = "ovic_testdata",
109    srcs = [
110        "@tflite_ovic_testdata//:detect.lite",
111        "@tflite_ovic_testdata//:float_model.lite",
112        "@tflite_ovic_testdata//:low_res_model.lite",
113        "@tflite_ovic_testdata//:quantized_model.lite",
114        "@tflite_ovic_testdata//:test_image_128.jpg",
115        "@tflite_ovic_testdata//:test_image_224.jpg"
116        "my_model.lite",        # <--- Your submission.
117        "my_test_image.jpg",    # <--- Your test image.
118    ],
119    ...
120```
121
122*   For classification models, modify `OvicClassifierTest.java`:
123
124    *   change `TEST_IMAGE_PATH` to `my_test_image.jpg`.
125
126    *   change either `FLOAT_MODEL_PATH` or `QUANTIZED_MODEL_PATH` to
127        `my_model.lite` depending on whether your model runs inference in float
128        or
129        [8-bit](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/quantize).
130
131    *   change `TEST_IMAGE_GROUNDTRUTH` (ImageNet class ID) to be consistent
132        with your test image.
133
134*   For detection models, modify `OvicDetectorTest.java`:
135
136    *   change `TEST_IMAGE_PATH` to `my_test_image.jpg`.
137    *   change `MODEL_PATH` to `my_model.lite`.
138    *   change `GROUNDTRUTH` (COCO class ID) to be consistent with your test
139        image.
140
141Now you can run the bazel tests to catch any runtime issues with the submission.
142
143Note: Please make sure that your submission passes the test. If a submission
144fails to pass the test it will not be processed by the submission server.
145
146## Measure on-device latency
147
148We provide two ways to measure the on-device latency of your submission. The
149first is through our competition server, which is reliable and repeatable, but
150is limited to a few trials per day. The second is through the benchmarker Apk,
151which requires a device and may not be as accurate as the server, but has a fast
152turn-around and no access limitations. We recommend that the participants use
153the benchmarker apk for early development, and reserve the competition server
154for evaluating promising submissions.
155
156### Running the benchmarker app
157
158Make sure that you have followed instructions in
159[Test your submissions](#test-your-submissions) to add your model to the
160testdata folder and to the corresponding build rules.
161
162Modify `tensorflow/lite/java/ovic/demo/app/OvicBenchmarkerActivity.java`:
163
164*   Add your model to the benchmarker apk by changing `modelPath` and
165    `testImagePath` to your submission and test image.
166
167```
168  if (benchmarkClassification) {
169    ...
170    testImagePath = "my_test_image.jpg";
171    modelPath = "my_model.lite";
172  } else {  // Benchmarking detection.
173  ...
174```
175
176If you are adding a detection model, simply modify `modelPath` and
177`testImagePath` in the else block above.
178
179*   Adjust the benchmark parameters when needed:
180
181You can change the length of each experiment, and the processor affinity below.
182`BIG_CORE_MASK` is an integer whose binary encoding represents the set of used
183cores. This number is phone-specific. For example, Pixel 4 has 8 cores: the 4
184little cores are represented by the 4 less significant bits, and the 4 big cores
185by the 4 more significant bits. Therefore a mask value of 16, or in binary
186`00010000`, represents using only the first big core. The mask 32, or in binary
187`00100000` uses the second big core and should deliver identical results as the
188mask 16 because the big cores are interchangeable.
189
190```
191  /** Wall time for each benchmarking experiment. */
192  private static final double WALL_TIME = 3000;
193  /** Maximum number of iterations in each benchmarking experiment. */
194  private static final int MAX_ITERATIONS = 100;
195  /** Mask for binding to a single big core. Pixel 1 (4), Pixel 4 (16). */
196  private static final int BIG_CORE_MASK = 16;
197```
198
199Note: You'll need ROOT access to the phone to change processor affinity.
200
201*   Build and install the app.
202
203```
204bazel build -c opt --cxxopt=-Wno-all //tensorflow/lite/java/ovic/demo/app:ovic_benchmarker_binary
205adb install -r bazel-bin/tensorflow/lite/java/ovic/demo/app/ovic_benchmarker_binary.apk
206```
207
208Start the app and pick a task by clicking either the `CLF` button for
209classification or the `DET` button for detection. The button should turn bright
210green, signaling that the experiment is running. The benchmarking results will
211be displayed after about the `WALL_TIME` you specified above. For example:
212
213```
214my_model.lite: Average latency=158.6ms after 20 runs.
215```
216
217### Sample latencies
218
219Note: the benchmarking results can be quite different depending on the
220background processes running on the phone. A few things that help stabilize the
221app's readings are placing the phone on a cooling plate, restarting the phone,
222and shutting down internet access.
223
224Classification Model | Pixel 1 | Pixel 2 | Pixel 4
225-------------------- | :-----: | ------: | :-----:
226float_model.lite     | 97      | 113     | 37
227quantized_model.lite | 73      | 61      | 13
228low_res_model.lite   | 3       | 3       | 1
229
230Detection Model        | Pixel 2 | Pixel 4
231---------------------- | :-----: | :-----:
232detect.lite            | 248     | 82
233quantized_detect.lite  | 59      | 17
234quantized_fpnlite.lite | 96      | 29
235
236All latency numbers are in milliseconds. The Pixel 1 and Pixel 2 latency numbers
237are measured on `Oct 17 2019` (Github commit hash
238[I05def66f58fa8f2161522f318e00c1b520cf0606](https://github.com/tensorflow/tensorflow/commit/4b02bc0e0ff7a0bc02264bc87528253291b7c949#diff-4e94df4d2961961ba5f69bbd666e0552))
239
240The Pixel 4 latency numbers are measured on `Apr 14 2020` (Github commit hash
241[4b2cb67756009dda843c6b56a8b320c8a54373e0](https://github.com/tensorflow/tensorflow/commit/4b2cb67756009dda843c6b56a8b320c8a54373e0)).
242
243Since Pixel 4 has excellent support for 8-bit quantized models, we strongly
244recommend you to check out the
245[Post-Training Quantization tutorial](https://www.tensorflow.org/lite/performance/post_training_quantization).
246
247The detection models above are both single-shot models (i.e. no object proposal
248generation) using TfLite's *fast* version of Non-Max-Suppression (NMS). The fast
249NMS is significant faster than the regular NMS (used by the ObjectDetectionAPI
250in training) at the expense of about 1% mAP for the listed models.
251
252### Latency table
253
254We have compiled a latency table for common neural network operators such as
255convolutions, separable convolutions, and matrix multiplications. The table of
256results is available here:
257
258*   https://storage.cloud.google.com/ovic-data/
259
260The results were generated by creating a small network containing a single
261operation, and running the op under the test harness. For more details see the
262NetAdapt paper<sup>1</sup>. We plan to expand table regularly as we test with
263newer OS releases and updates to Tensorflow Lite.
264
265### Sample benchmarks
266
267Below are the baseline models (MobileNetV2, MnasNet, and MobileNetV3) used to
268compute the reference accuracy for ImageNet classification. The naming
269convention of the models are `[precision]_[model
270class]_[resolution]_[multiplier]`. Pixel 2 Latency (ms) is measured on a single
271Pixel 2 big core using the competition server on `Oct 17 2019`, while Pixel 4
272latency (ms) is measured on a single Pixel 4 big core using the competition
273server on `Apr 14 2020`. You can find these models on TFLite's
274[hosted model page](https://www.tensorflow.org/lite/guide/hosted_models#image_classification).
275
276Model                               | Pixel 2 | Pixel 4 | Top-1 Accuracy
277:---------------------------------: | :-----: | :-----: | :------------:
278quant_mobilenetv2_96_35             | 4       | 1       | 0.420
279quant_mobilenetv2_96_50             | 5       | 1       | 0.478
280quant_mobilenetv2_128_35            | 6       | 2       | 0.474
281quant_mobilenetv2_128_50            | 8       | 2       | 0.546
282quant_mobilenetv2_160_35            | 9       | 2       | 0.534
283quant_mobilenetv2_96_75             | 8       | 2       | 0.560
284quant_mobilenetv2_96_100            | 10      | 3       | 0.579
285quant_mobilenetv2_160_50            | 12      | 3       | 0.583
286quant_mobilenetv2_192_35            | 12      | 3       | 0.557
287quant_mobilenetv2_128_75            | 13      | 3       | 0.611
288quant_mobilenetv2_192_50            | 16      | 4       | 0.616
289quant_mobilenetv2_128_100           | 16      | 4       | 0.629
290quant_mobilenetv2_224_35            | 17      | 5       | 0.581
291quant_mobilenetv2_160_75            | 20      | 5       | 0.646
292float_mnasnet_96_100                | 21      | 7       | 0.625
293quant_mobilenetv2_224_50            | 22      | 6       | 0.637
294quant_mobilenetv2_160_100           | 25      | 6       | 0.674
295quant_mobilenetv2_192_75            | 29      | 7       | 0.674
296quant_mobilenetv2_192_100           | 35      | 9       | 0.695
297float_mnasnet_224_50                | 35      | 12      | 0.679
298quant_mobilenetv2_224_75            | 39      | 10      | 0.684
299float_mnasnet_160_100               | 45      | 15      | 0.706
300quant_mobilenetv2_224_100           | 48      | 12      | 0.704
301float_mnasnet_224_75                | 55      | 18      | 0.718
302float_mnasnet_192_100               | 62      | 20      | 0.724
303float_mnasnet_224_100               | 84      | 27      | 0.742
304float_mnasnet_224_130               | 126     | 40      | 0.758
305float_v3-small-minimalistic_224_100 | -       | 5       | 0.620
306quant_v3-small_224_100              | -       | 5       | 0.641
307float_v3-small_224_75               | -       | 5       | 0.656
308float_v3-small_224_100              | -       | 7       | 0.677
309quant_v3-large_224_100              | -       | 12      | 0.728
310float_v3-large_224_75               | -       | 15      | 0.735
311float_v3-large-minimalistic_224_100 | -       | 17      | 0.722
312float_v3-large_224_100              | -       | 20      | 0.753
313
314### References
315
3161.  **NetAdapt: Platform-Aware Neural Network Adaptation for Mobile
317    Applications**<br />
318    Yang, Tien-Ju, Andrew Howard, Bo Chen, Xiao Zhang, Alec Go, Mark Sandler,
319    Vivienne Sze, and Hartwig Adam. In Proceedings of the European Conference
320    on Computer Vision (ECCV), pp. 285-300. 2018<br />
321    [[link]](https://arxiv.org/abs/1804.03230) arXiv:1804.03230, 2018.
322