1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.nn.benchmark.app;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Parcelable;
23 import android.text.method.ScrollingMovementMethod;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.widget.ArrayAdapter;
29 import android.widget.ListView;
30 import android.widget.TextView;
31 
32 import com.android.nn.benchmark.core.BenchmarkResult;
33 import com.android.nn.benchmark.core.TestModels;
34 import com.android.nn.benchmark.util.TestExternalStorageActivity;
35 
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 
39 public class NNControls extends Activity {
40     private static final String TAG = NNControls.class.getSimpleName();
41 
42     private ListView mTestListView;
43     private TextView mResultView;
44 
45     private ArrayAdapter<String> mTestListAdapter;
46     private ArrayList<String> mTestList = new ArrayList<String>();
47 
48     private boolean mSettings[] = {false, false, false};
49     private static final int SETTING_LONG_RUN = 0;
50     private static final int SETTING_PAUSE = 1;
51     private static final int SETTING_DISABLE_NNAPI = 2;
52 
53     private float mResults[];
54     private String mInfo[];
55 
56     private static final String[] DOGFOOD_MODEL_NAMES = new String[]{
57             "tts_float",
58             "asr_float",
59             "mobilenet_v1_1.0_224_quant_topk_aosp",
60             "mobilenet_v1_1.0_224_topk_aosp",
61             "mobilenet_v1_0.75_192_quant_topk_aosp",
62             "mobilenet_v1_0.75_192_topk_aosp",
63             "mobilenet_v1_0.5_160_quant_topk_aosp",
64             "mobilenet_v1_0.5_160_topk_aosp",
65             "mobilenet_v1_0.25_128_quant_topk_aosp",
66             "mobilenet_v1_0.25_128_topk_aosp",
67             "mobilenet_v2_0.35_128_topk_aosp",
68             "mobilenet_v2_0.5_160_topk_aosp",
69             "mobilenet_v2_0.75_192_topk_aosp",
70             "mobilenet_v2_1.0_224_topk_aosp",
71             "mobilenet_v2_1.0_224_quant_topk_aosp",
72     };
73 
74     @Override
onCreateOptionsMenu(Menu menu)75     public boolean onCreateOptionsMenu(Menu menu) {
76         // Inflate the menu items for use in the action bar
77         MenuInflater inflater = getMenuInflater();
78         inflater.inflate(R.menu.main_activity_actions, menu);
79 
80         return super.onCreateOptionsMenu(menu);
81     }
82 
init()83     void init() {
84         TestExternalStorageActivity.testWriteExternalStorage(this, true);
85 
86         for (TestModels.TestModelEntry testModel : TestModels.modelsList()) {
87             mTestList.add(testModel.toString());
88         }
89 
90         mTestListView = findViewById(R.id.test_list);
91         mTestListAdapter = new ArrayAdapter(this,
92                 android.R.layout.simple_list_item_activated_1,
93                 mTestList);
94 
95         mTestListView.setAdapter(mTestListAdapter);
96         mTestListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
97         mTestListAdapter.notifyDataSetChanged();
98 
99         mResultView = findViewById(R.id.results);
100         mResultView.setMovementMethod(new ScrollingMovementMethod());
101     }
102 
103     @Override
onCreate(Bundle savedInstanceState)104     protected void onCreate(Bundle savedInstanceState) {
105         super.onCreate(savedInstanceState);
106         setContentView(R.layout.controls);
107         init();
108     }
109 
110     @Override
onPause()111     protected void onPause() {
112         super.onPause();
113     }
114 
115     @Override
onResume()116     protected void onResume() {
117         super.onResume();
118     }
119 
makeBasicLaunchIntent()120     Intent makeBasicLaunchIntent() {
121         Intent intent = new Intent(this, NNBenchmark.class);
122         intent.putExtra(NNBenchmark.EXTRA_ENABLE_LONG, mSettings[SETTING_LONG_RUN]);
123         intent.putExtra(NNBenchmark.EXTRA_ENABLE_PAUSE, mSettings[SETTING_PAUSE]);
124         intent.putExtra(NNBenchmark.EXTRA_DISABLE_NNAPI, mSettings[SETTING_DISABLE_NNAPI]);
125         return intent;
126     }
127 
btnRun(View v)128     public void btnRun(View v) {
129         int count = 0;
130         int modelsCount = TestModels.modelsList().size();
131         for (int i = 0; i < modelsCount; i++) {
132             if (mTestListView.isItemChecked(i)) {
133                 count++;
134             }
135         }
136         if (count == 0) {
137             return;
138         }
139 
140         int testList[] = new int[count];
141         count = 0;
142         for (int i = 0; i < modelsCount; i++) {
143             if (mTestListView.isItemChecked(i)) {
144                 testList[count++] = i;
145             }
146         }
147 
148         Intent intent = makeBasicLaunchIntent();
149         intent.putExtra(NNBenchmark.EXTRA_TESTS, testList);
150         startActivityForResult(intent, 0);
151     }
152 
getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t)153     String getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t) {
154         return br.getSummary(t.mBaselineSec);
155     }
156 
onActivityResult(int requestCode, int resultCode, Intent data)157     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
158         if (requestCode == 0) {
159             if (resultCode == RESULT_OK) {
160                 int size = TestModels.modelsList().size();
161                 mResults = new float[size];
162                 mInfo = new String[size];
163 
164                 Parcelable r[] = data.getParcelableArrayExtra(NNBenchmark.EXTRA_RESULTS_RESULTS);
165                 int id[] = data.getIntArrayExtra(NNBenchmark.EXTRA_RESULTS_TESTS);
166 
167                 String mOutResult = "";
168                 for (int ct = 0; ct < id.length; ct++) {
169                     TestModels.TestModelEntry t = TestModels.modelsList().get(id[ct]);
170                     BenchmarkResult br = (BenchmarkResult) r[ct];
171 
172                     String s = t.toString() + " " + getResultShortSummary(br, t);
173                     mTestList.set(id[ct], s);
174                     mTestListAdapter.notifyDataSetChanged();
175                     mOutResult += s + '\n';
176                     mResults[id[ct]] = ((BenchmarkResult) r[ct]).getMeanTimeSec();
177                 }
178 
179                 mResultView.setText(mOutResult);
180             }
181         }
182     }
183 
btnSelAll(View v)184     public void btnSelAll(View v) {
185         for (int i = 0; i < TestModels.modelsList().size(); i++) {
186             mTestListView.setItemChecked(i, true);
187         }
188     }
189 
btnSelNone(View v)190     public void btnSelNone(View v) {
191         for (int i = 0; i < TestModels.modelsList().size(); i++) {
192             mTestListView.setItemChecked(i, false);
193         }
194     }
195 
onOptionsItemSelected(MenuItem item)196     public boolean onOptionsItemSelected(MenuItem item) {
197         // Handle presses on the action bar items
198         switch (item.getItemId()) {
199             case R.id.action_settings:
200                 NNSettings newFragment = new NNSettings(mSettings);
201                 newFragment.show(getFragmentManager(), "settings");
202                 return true;
203             default:
204                 return super.onOptionsItemSelected(item);
205         }
206     }
207 
btnSettings(View v)208     public void btnSettings(View v) {
209         NNSettings newFragment = new NNSettings(mSettings);
210         newFragment.show(getFragmentManager(), "settings");
211     }
212 
btnRunDogfood(View v)213     public void btnRunDogfood(View v) {
214         // Update settings for dogfood.
215         mSettings[SETTING_LONG_RUN] = true;
216         mSettings[SETTING_PAUSE] = false;
217         mSettings[SETTING_DISABLE_NNAPI] = false;
218 
219         // Select dogfood models.
220         for (int i = 0; i < mTestList.size(); i++) {
221             String modelName = mTestList.get(i);
222             boolean isDogfoodModel = Arrays.asList(DOGFOOD_MODEL_NAMES).contains(modelName);
223             mTestListView.setItemChecked(i, isDogfoodModel);
224         }
225 
226         // Run benchmark.
227         btnRun(v);
228     }
229 }
230