Skip to content

Commit 01066df

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Clean up UI (#660)
Summary: Pull Request resolved: #660 For segmentation model, clean up the UI. Need to polish classification model later. Reviewed By: kimishpatel Differential Revision: D49999041 fbshipit-source-id: 1b76445e2c118dbff9f7bf108a2f0760fb961eef
1 parent 8c728db commit 01066df

File tree

3 files changed

+90
-105
lines changed

3 files changed

+90
-105
lines changed

examples/demo-apps/android/ExecuTorchDemo/app/src/main/java/com/example/executorchdemo/MainActivity.java

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import android.app.Activity;
1212
import android.content.Context;
13-
import android.content.Intent;
1413
import android.graphics.Bitmap;
1514
import android.graphics.BitmapFactory;
1615
import android.os.Bundle;
@@ -22,7 +21,6 @@
2221
import android.widget.Button;
2322
import android.widget.ImageView;
2423
import android.widget.ProgressBar;
25-
import android.widget.Toast;
2624
import com.example.executorchdemo.executor.EValue;
2725
import com.example.executorchdemo.executor.Module;
2826
import com.example.executorchdemo.executor.Tensor;
@@ -36,11 +34,11 @@
3634

3735
public class MainActivity extends Activity implements Runnable {
3836
private ImageView mImageView;
39-
private Button mButtonSegment;
37+
private Button mButtonXnnpack;
38+
private Button mButtonHtp;
4039
private ProgressBar mProgressBar;
4140
private Bitmap mBitmap = null;
4241
private Module mModule = null;
43-
private String mBackend = "xnnpack";
4442
private String mImagename = "corgi.jpeg";
4543

4644
// see http://host.robots.ox.ac.uk:8080/pascal/VOC/voc2007/segexamples/index.html for the list of
@@ -50,11 +48,6 @@ public class MainActivity extends Activity implements Runnable {
5048
private static final int PERSON = 15;
5149
private static final int SHEEP = 17;
5250

53-
private void openClassificationActivity() {
54-
Intent intent = new Intent(this, ClassificationActivity.class);
55-
startActivity(intent);
56-
}
57-
5851
public static String assetFilePath(Context context, String assetName) throws IOException {
5952
File file = new File(context.getFilesDir(), assetName);
6053
if (file.exists() && file.length() > 0) {
@@ -74,6 +67,17 @@ public static String assetFilePath(Context context, String assetName) throws IOE
7467
}
7568
}
7669

70+
private void populateImage() {
71+
try {
72+
mBitmap = BitmapFactory.decodeStream(getAssets().open(mImagename));
73+
mBitmap = Bitmap.createScaledBitmap(mBitmap, 224, 224, true);
74+
mImageView.setImageBitmap(mBitmap);
75+
} catch (IOException e) {
76+
Log.e("ImageSegmentation", "Error reading assets", e);
77+
finish();
78+
}
79+
}
80+
7781
@Override
7882
protected void onCreate(Bundle savedInstanceState) {
7983
super.onCreate(savedInstanceState);
@@ -94,11 +98,20 @@ protected void onCreate(Bundle savedInstanceState) {
9498
finish();
9599
}
96100

101+
try {
102+
mModule =
103+
Module.load(MainActivity.assetFilePath(getApplicationContext(), "dl3_xnnpack_fp32.pte"));
104+
105+
} catch (IOException e) {
106+
Log.e("ImageSegmentation", "Error reading assets", e);
107+
finish();
108+
}
109+
97110
mImageView = findViewById(R.id.imageView);
98111
mImageView.setImageBitmap(mBitmap);
99112

100-
final Button buttonRestart = findViewById(R.id.restartButton);
101-
buttonRestart.setOnClickListener(
113+
final Button buttonNext = findViewById(R.id.nextButton);
114+
buttonNext.setOnClickListener(
102115
new View.OnClickListener() {
103116
public void onClick(View v) {
104117
if (Objects.equals(mImagename, "corgi.jpeg")) {
@@ -108,79 +121,62 @@ public void onClick(View v) {
108121
} else {
109122
mImagename = "corgi.jpeg";
110123
}
111-
try {
112-
mBitmap = BitmapFactory.decodeStream(getAssets().open(mImagename));
113-
mBitmap = Bitmap.createScaledBitmap(mBitmap, 224, 224, true);
114-
mImageView.setImageBitmap(mBitmap);
115-
} catch (IOException e) {
116-
Log.e("ImageSegmentation", "Error reading assets", e);
117-
finish();
118-
}
124+
populateImage();
119125
}
120126
});
121127

122-
final Button buttonSwitchBackend = findViewById(R.id.switchBackendButton);
123-
buttonSwitchBackend.setText("Use Qualcomm backend");
124-
buttonSwitchBackend.setOnClickListener(
128+
mButtonXnnpack = findViewById(R.id.xnnpackButton);
129+
mButtonHtp = findViewById(R.id.htpButton);
130+
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
131+
mButtonXnnpack.setOnClickListener(
125132
new View.OnClickListener() {
126133
public void onClick(View v) {
127134
try {
128-
if (Objects.equals(mBackend, "xnnpack")) {
129-
mBackend = "qnn";
130-
mModule.destroy();
131-
mModule =
132-
Module.load(
133-
MainActivity.assetFilePath(getApplicationContext(), "dlv3_qnn.pte"));
134-
buttonSwitchBackend.setText("Use XNNPACK backend");
135-
} else {
136-
mBackend = "xnnpack";
137-
mModule.destroy();
138-
mModule =
139-
Module.load(
140-
MainActivity.assetFilePath(
141-
getApplicationContext(), "dl3_xnnpack_fp32.pte"));
142-
buttonSwitchBackend.setText("Use Qualcomm backend");
143-
}
135+
mModule.destroy();
136+
mModule =
137+
Module.load(
138+
MainActivity.assetFilePath(getApplicationContext(), "dl3_xnnpack_fp32.pte"));
144139
} catch (IOException e) {
145140
Log.e("ImageSegmentation", "Error reading assets", e);
146141
finish();
147142
}
148-
Toast toast =
149-
Toast.makeText(getApplicationContext(), "Using: " + mBackend, Toast.LENGTH_SHORT);
150-
toast.setMargin(50, 50);
151-
toast.show();
152-
}
153-
});
154143

155-
final Button classificationDemoButton = findViewById(R.id.classificationDemoButton);
156-
classificationDemoButton.setOnClickListener(
157-
new View.OnClickListener() {
158-
public void onClick(View v) {
159-
openClassificationActivity();
144+
mButtonXnnpack.setEnabled(false);
145+
mProgressBar.setVisibility(ProgressBar.VISIBLE);
146+
mButtonXnnpack.setText(getString(R.string.run_model));
147+
148+
Thread thread = new Thread(MainActivity.this);
149+
thread.start();
160150
}
161151
});
162152

163-
mButtonSegment = findViewById(R.id.segmentButton);
164-
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
165-
mButtonSegment.setOnClickListener(
153+
mButtonHtp.setOnClickListener(
166154
new View.OnClickListener() {
167155
public void onClick(View v) {
168-
mButtonSegment.setEnabled(false);
156+
try {
157+
mModule.destroy();
158+
mModule =
159+
Module.load(MainActivity.assetFilePath(getApplicationContext(), "dlv3_qnn.pte"));
160+
} catch (IOException e) {
161+
Log.e("ImageSegmentation", "Error reading assets", e);
162+
finish();
163+
}
164+
mButtonHtp.setEnabled(false);
169165
mProgressBar.setVisibility(ProgressBar.VISIBLE);
170-
mButtonSegment.setText(getString(R.string.run_model));
166+
mButtonHtp.setText(getString(R.string.run_model));
171167

172168
Thread thread = new Thread(MainActivity.this);
173169
thread.start();
174170
}
175171
});
176172

177-
try {
178-
mModule =
179-
Module.load(MainActivity.assetFilePath(getApplicationContext(), "dl3_xnnpack_fp32.pte"));
180-
} catch (IOException e) {
181-
Log.e("ImageSegmentation", "Error reading assets", e);
182-
finish();
183-
}
173+
final Button resetImage = findViewById(R.id.resetImage);
174+
resetImage.setOnClickListener(
175+
new View.OnClickListener() {
176+
public void onClick(View v) {
177+
populateImage();
178+
}
179+
});
184180
}
185181

186182
@Override
@@ -240,17 +236,11 @@ public void run() {
240236
@Override
241237
public void run() {
242238
mImageView.setImageBitmap(transferredBitmap);
243-
mButtonSegment.setEnabled(true);
244-
mButtonSegment.setText("segment");
239+
mButtonXnnpack.setEnabled(true);
240+
mButtonXnnpack.setText(R.string.run_xnnpack);
241+
mButtonHtp.setEnabled(true);
242+
mButtonHtp.setText(R.string.run_htp);
245243
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
246-
247-
Toast toast =
248-
Toast.makeText(
249-
getApplicationContext(),
250-
"Inference time (ms): " + inferenceTime,
251-
Toast.LENGTH_SHORT);
252-
toast.setMargin(50, 50);
253-
toast.show();
254244
}
255245
});
256246
}

examples/demo-apps/android/ExecuTorchDemo/app/src/main/res/layout/activity_main.xml

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,57 @@
1919
app:layout_constraintTop_toTopOf="parent" />
2020

2121
<Button
22-
android:id="@+id/segmentButton"
22+
android:id="@+id/nextButton"
2323
android:layout_width="wrap_content"
2424
android:layout_height="wrap_content"
25-
android:layout_marginTop="20dp"
26-
android:text="segment"
25+
android:layout_marginTop="10dp"
26+
android:text="@string/next"
2727
android:textAllCaps="false"
2828
app:layout_constraintEnd_toEndOf="parent"
29-
app:layout_constraintHorizontal_bias="0.498"
29+
app:layout_constraintHorizontal_bias="0.45"
3030
app:layout_constraintStart_toStartOf="parent"
3131
app:layout_constraintTop_toBottomOf="@+id/imageView" />
3232

33-
34-
<ProgressBar
35-
android:id="@+id/progressBar"
36-
android:visibility="invisible"
37-
android:layout_marginTop="10dp"
33+
<Button
34+
android:id="@+id/resetImage"
3835
android:layout_width="wrap_content"
3936
android:layout_height="wrap_content"
40-
app:layout_constraintEnd_toEndOf="parent"
41-
app:layout_constraintHorizontal_bias="0.498"
42-
app:layout_constraintStart_toStartOf="parent"
43-
app:layout_constraintTop_toBottomOf="@+id/imageView" />
37+
android:text="@string/reset"
38+
android:textAllCaps="false"
39+
app:layout_constraintRight_toRightOf="@+id/nextButton"
40+
app:layout_constraintStart_toEndOf="@+id/nextButton"
41+
app:layout_constraintTop_toTopOf="@+id/nextButton" />
4442

4543
<Button
46-
android:id="@+id/restartButton"
44+
android:id="@+id/xnnpackButton"
4745
android:layout_width="wrap_content"
4846
android:layout_height="wrap_content"
49-
android:layout_marginTop="10dp"
50-
android:text="@string/restart"
47+
android:layout_marginTop="20dp"
48+
android:text="@string/run_xnnpack"
5149
android:textAllCaps="false"
5250
app:layout_constraintEnd_toEndOf="parent"
53-
app:layout_constraintHorizontal_bias="0.498"
51+
app:layout_constraintHorizontal_bias="0.45"
5452
app:layout_constraintStart_toStartOf="parent"
55-
app:layout_constraintTop_toBottomOf="@+id/segmentButton" />
53+
app:layout_constraintTop_toBottomOf="@+id/nextButton" />
5654

57-
<Button
58-
android:id="@+id/switchBackendButton"
55+
<ProgressBar
56+
android:id="@+id/progressBar"
5957
android:layout_width="wrap_content"
6058
android:layout_height="wrap_content"
6159
android:layout_marginTop="10dp"
62-
android:text="@string/switch_backend"
63-
android:textAllCaps="false"
60+
android:visibility="invisible"
6461
app:layout_constraintEnd_toEndOf="parent"
65-
app:layout_constraintHorizontal_bias="0.498"
62+
app:layout_constraintHorizontal_bias="0.45"
6663
app:layout_constraintStart_toStartOf="parent"
67-
app:layout_constraintTop_toBottomOf="@+id/restartButton" />
64+
app:layout_constraintTop_toBottomOf="@+id/nextButton" />
6865

6966
<Button
70-
android:id="@+id/classificationDemoButton"
67+
android:id="@+id/htpButton"
7168
android:layout_width="wrap_content"
7269
android:layout_height="wrap_content"
73-
android:layout_marginTop="10dp"
74-
android:text="@string/classification_demo"
70+
android:text="@string/run_htp"
7571
android:textAllCaps="false"
76-
app:layout_constraintEnd_toEndOf="parent"
77-
app:layout_constraintHorizontal_bias="0.498"
78-
app:layout_constraintStart_toStartOf="parent"
79-
app:layout_constraintTop_toBottomOf="@+id/switchBackendButton" />
80-
72+
app:layout_constraintStart_toEndOf="@+id/xnnpackButton"
73+
app:layout_constraintTop_toTopOf="@+id/xnnpackButton" />
8174

8275
</androidx.constraintlayout.widget.ConstraintLayout>

examples/demo-apps/android/ExecuTorchDemo/app/src/main/res/values/strings.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
<string name="image_view">Image View</string>
44
<string name="detect">Detect</string>
55
<string name="run_model">Running the model...</string>
6-
<string name="restart">Restart</string>
6+
<string name="next">Next</string>
77
<string name="select">Select</string>
88
<string name="live">Live</string>
99
<string name="classification_demo">Classification demo</string>
1010
<string name="segmentation_demo">Segmentation Demo</string>
11-
<string name="switch_backend">Switch backend</string>
11+
<string name="run_htp">HTP</string>
12+
<string name="run_xnnpack">XNNPACK</string>
13+
<string name="reset">Reset</string>
1214
</resources>

0 commit comments

Comments
 (0)