Created
October 28, 2020 06:55
-
-
Save farmaker47/7aba9fd0a22c80b67d2d7218e7de218f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function for ML Binding | |
fun executeWithMLBinding( | |
contentImagePath: Bitmap, | |
styleImageName: String, | |
context: Context | |
): ModelExecutionResult { | |
try { | |
Log.i(TAG, "running models") | |
fullExecutionTime = SystemClock.uptimeMillis() | |
preProcessTime = SystemClock.uptimeMillis() | |
// Creates inputs for reference. | |
val styleBitmap = ImageUtils.loadBitmapFromResources(context, "thumbnails/$styleImageName") | |
val styleImage = TensorImage.fromBitmap(styleBitmap) | |
val contentImage = TensorImage.fromBitmap(contentImagePath) | |
preProcessTime = SystemClock.uptimeMillis() - preProcessTime | |
stylePredictTime = SystemClock.uptimeMillis() | |
// The results of this inference could be reused given the style does not change | |
// That would be a good practice in case this was applied to a video stream. | |
// Runs model inference and gets result. | |
val outputsPredict = modelMlBindingPredict.process(styleImage) | |
val styleBottleneckPredict = outputsPredict.styleBottleneckAsTensorBuffer | |
stylePredictTime = SystemClock.uptimeMillis() - stylePredictTime | |
Log.d(TAG, "Style Predict Time to run: $stylePredictTime") | |
styleTransferTime = SystemClock.uptimeMillis() | |
// Runs model inference and gets result. | |
val outputs = modelMlBindingTransfer.process(contentImage, styleBottleneckPredict) | |
styleTransferTime = SystemClock.uptimeMillis() - styleTransferTime | |
Log.d(TAG, "Style apply Time to run: $styleTransferTime") | |
postProcessTime = SystemClock.uptimeMillis() | |
val styledImage = outputs.styledImageAsTensorImage | |
val styledImageBitmap = styledImage.bitmap | |
postProcessTime = SystemClock.uptimeMillis() - postProcessTime | |
fullExecutionTime = SystemClock.uptimeMillis() - fullExecutionTime | |
Log.d(TAG, "Time to run everything: $fullExecutionTime") | |
return ModelExecutionResult( | |
styledImageBitmap, | |
preProcessTime, | |
stylePredictTime, | |
styleTransferTime, | |
postProcessTime, | |
fullExecutionTime, | |
formatExecutionLog() | |
) | |
} catch (e: Exception) { | |
val exceptionLog = "something went wrong: ${e.message}" | |
Log.d(TAG, exceptionLog) | |
val emptyBitmap = | |
ImageUtils.createEmptyBitmap( | |
CONTENT_IMAGE_SIZE, | |
CONTENT_IMAGE_SIZE | |
) | |
return ModelExecutionResult( | |
emptyBitmap, errorMessage = e.message!! | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment