Skip to content
Permalink
Browse files

Merge pull request #9 from the-super-toys/va/thread-safe-interpreter

Make interpreter global and thread safe
  • Loading branch information...
VictorAlbertos committed Mar 8, 2019
2 parents 8df6b69 + 843ff43 commit a757e2b3f1ec58512959530bf7320e964a2790fe
@@ -66,11 +66,8 @@ fun Bitmap.debugHeatMap(
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 16 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 8 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel and 0xFF) / 255f) }
val intpr = Interpreter(rawModel, Interpreter.Options().apply {
setNumThreads(1)
})
intpr.run(inputBuffer, output)
intpr.close()

intpreter.runThreadSafe(inputBuffer, output)

// calculate tempered softmax
val flattened = output[0][0].flattened()
@@ -116,6 +113,14 @@ fun Bitmap.debugHeatMap(
}
}



@Synchronized
fun Interpreter.runThreadSafe(inputBuffer: ByteBuffer, output: Array<Array<Array<FloatArray>>>) {
run(inputBuffer, output)
}


@JvmOverloads
fun Bitmap.findCenter(
temperature: Float = 0.2f,
@@ -135,11 +140,8 @@ fun Bitmap.findCenter(
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 16 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel shr 8 and 0xFF) / 255f) }
pixels.forEach { pixel -> inputBuffer.putFloat((pixel and 0xFF) / 255f) }
val intpr = Interpreter(rawModel, Interpreter.Options().apply {
setNumThreads(1)
})
intpr.run(inputBuffer, output)
intpr.close()

intpreter.runThreadSafe(inputBuffer, output)

// calculate tempered softmax
val flattened = output[0][0].flattened()
@@ -148,4 +150,5 @@ fun Bitmap.findCenter(

// get averaged center
return MathUtils.getLargestFocusArea(reshaped[0][0], lowerBound = lowerBound)
}
}

@@ -1,3 +1,9 @@
package glimpse.core

internal val rawModel by lazy { IOUtils.loadModel(Glimpse.client.applicationContext, "model.tflite") }
import org.tensorflow.lite.Interpreter

private val rawModel by lazy { IOUtils.loadModel(Glimpse.client.applicationContext, "model.tflite") }

internal val intpreter by lazy {
Interpreter(rawModel, Interpreter.Options())
}

0 comments on commit a757e2b

Please sign in to comment.
You can’t perform that action at this time.