Skip to content

Commit a6728e2

Browse files
authored
Added multi-threading support on the Android app (#48)
1 parent 15d3074 commit a6728e2

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

  • cifar/android/CifarETTrainingDemo/app/src/main/java/com/example/democifar10

cifar/android/CifarETTrainingDemo/app/src/main/java/com/example/democifar10/MainActivity.kt

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class MainActivity : ComponentActivity() {
250250

251251
override fun onCreate(savedInstanceState: Bundle?) {
252252
val batchSize = 4
253-
val numEpochs = 5
253+
val numEpochs = 10
254254
val width = 32
255255
val height = 32
256256
val channels = 3
@@ -405,15 +405,48 @@ class MainActivity : ComponentActivity() {
405405
}
406406
}
407407

408-
// Start fine-tuning
409-
trainModel(
410-
tModule!!, trnImgData!!, trnLblData!!, tstImgData!!, tstLblData!!, numEpochs, batchSize
411-
)
408+
// Start fine-tuning in a background thread to prevent ANR
409+
Thread {
410+
try {
411+
trainModel(
412+
tModule!!, trnImgData!!, trnLblData!!, tstImgData!!, tstLblData!!, numEpochs, batchSize
413+
)
414+
} catch (e: Exception) {
415+
Log.e(debugTag, "Error during training: ${e.message}", e)
416+
runOnUiThread {
417+
val statusText = findViewById<TextView>(R.id.statusText)
418+
statusText.text = "Training failed: ${e.message}"
419+
}
420+
}
421+
}.start()
412422
}
413423

414424
evaluateButton.setOnClickListener {
415-
// Evaluate the model
416-
evaluateModel(tModule!!, tstImgData!!, tstLblData!!, batchSize)
425+
Log.d("Button", "Evaluate button clicked")
426+
427+
// Show progress immediately on the UI thread
428+
runOnUiThread {
429+
try {
430+
val statusText = findViewById<TextView>(R.id.statusText)
431+
statusText.text = "Starting evaluation..."
432+
} catch (e: Exception) {
433+
Log.e("StatusText", "Error in evaluate button click: ${e.message}")
434+
e.printStackTrace()
435+
}
436+
}
437+
438+
// Evaluate the model in a background thread to prevent ANR
439+
Thread {
440+
try {
441+
evaluateModel(tModule!!, tstImgData!!, tstLblData!!, batchSize)
442+
} catch (e: Exception) {
443+
Log.e(debugTag, "Error during evaluation: ${e.message}", e)
444+
runOnUiThread {
445+
val statusText = findViewById<TextView>(R.id.statusText)
446+
statusText.text = "Evaluation failed: ${e.message}"
447+
}
448+
}
449+
}.start()
417450
}
418451
}
419452

0 commit comments

Comments
 (0)