五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

大成帶你用TFL數(shù)字分類識別

2020-05-31 13:42 作者:嗨大成  | 我要投稿

1.TFL數(shù)字分類識別

通過Android在手機屏幕上寫數(shù)字0,1,3,--9的數(shù)字,進行數(shù)值判斷,通過機器學習的mnist.tflite進行數(shù)字識別,數(shù)字識別根據(jù)輸入的手寫生成圖片。進行識別,識別率有的可以到99%,model加載mnist.tflite的庫

2.源碼

package org.tensorflow.lite.examples.digitclassifier

import android.content.Context
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.util.Log
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks.call
import java.io.FileInputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.channels.FileChannel
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import org.tensorflow.lite.Interpreter

class DigitClassifier(private val context: Context) {
?private var interpreter: Interpreter? = null
?var isInitialized = false
? ?private set

?/** Executor to run inference task in the background */
?private val executorService: ExecutorService = Executors.newCachedThreadPool()

?private var inputImageWidth: Int = 0 // will be inferred from TF Lite model
?private var inputImageHeight: Int = 0 // will be inferred from TF Lite model
?private var modelInputSize: Int = 0 // will be inferred from TF Lite model

?fun initialize(): Task<Void> {
? ?return call(
? ? ?executorService,
? ? ?Callable<Void> {
? ? ? ?initializeInterpreter()
? ? ? ?null
? ? ?}
? ?)
?}

?@Throws(IOException::class)
?private fun initializeInterpreter() {
? ?// Load the TF Lite model
? ?val assetManager = context.assets
? ?val model = loadModelFile(assetManager)

? ?// Initialize TF Lite Interpreter with NNAPI enabled
? ?val options = Interpreter.Options()
? ?options.setUseNNAPI(true)
? ?val interpreter = Interpreter(model, options)

? ?// Read input shape from model file
? ?val inputShape = interpreter.getInputTensor(0).shape()
? ?inputImageWidth = inputShape[1]
? ?inputImageHeight = inputShape[2]
? ?modelInputSize = FLOAT_TYPE_SIZE * inputImageWidth * inputImageHeight * PIXEL_SIZE

? ?// Finish interpreter initialization
? ?this.interpreter = interpreter
? ?isInitialized = true
? ?Log.d(TAG, "初始化TFLite推斷人.")
?}

?@Throws(IOException::class)
?private fun loadModelFile(assetManager: AssetManager): ByteBuffer {
? ?val fileDescriptor = assetManager.openFd(MODEL_FILE)
? ?val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
? ?val fileChannel = inputStream.channel
? ?val startOffset = fileDescriptor.startOffset
? ?val declaredLength = fileDescriptor.declaredLength
? ?return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
?}

?private fun classify(bitmap: Bitmap): String {
? ?if (!isInitialized) {
? ? ?throw IllegalStateException("TF Lite Interpreter is not initialized yet.")
? ?}

? ?var startTime: Long
? ?var elapsedTime: Long

? ?// Preprocessing: resize the input
? ?startTime = System.nanoTime()
? ?val resizedImage = Bitmap.createScaledBitmap(bitmap, inputImageWidth, inputImageHeight, true)
? ?val byteBuffer = convertBitmapToByteBuffer(resizedImage)
? ?elapsedTime = (System.nanoTime() - startTime) / 1000000
? ?Log.d(TAG, "預處理時間 = " + elapsedTime + "ms")

? ?startTime = System.nanoTime()
? ?val result = Array(1) { FloatArray(OUTPUT_CLASSES_COUNT) }
? ?interpreter?.run(byteBuffer, result)
? ?elapsedTime = (System.nanoTime() - startTime) / 1000000
? ?Log.d(TAG, "推斷的時間 = " + elapsedTime + "ms")

? ?return getOutputString(result[0])
?}

?fun classifyAsync(bitmap: Bitmap): Task<String> {
? ?return call(executorService, Callable<String> { classify(bitmap) })
?}

?fun close() {
? ?call(
? ? ?executorService,
? ? ?Callable<String> {
? ? ? ?interpreter?.close()
? ? ? ?Log.d(TAG, "關閉TF推斷人.")
? ? ? ?null
? ? ?}
? ?)
?}

?private fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer {
? ?val byteBuffer = ByteBuffer.allocateDirect(modelInputSize)
? ?byteBuffer.order(ByteOrder.nativeOrder())

? ?val pixels = IntArray(inputImageWidth * inputImageHeight)
? ?bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)

? ?for (pixelValue in pixels) {
? ? ?val r = (pixelValue shr 16 and 0xFF)
? ? ?val g = (pixelValue shr 8 and 0xFF)
? ? ?val b = (pixelValue and 0xFF)

? ? ?// Convert RGB to grayscale and normalize pixel value to [0..1]
? ? ?val normalizedPixelValue = (r + g + b) / 3.0f / 255.0f
? ? ?byteBuffer.putFloat(normalizedPixelValue)
? ?}

? ?return byteBuffer
?}

?private fun getOutputString(output: FloatArray): String {
? ?val maxIndex = output.indices.maxBy { output[it] } ?: -1
? ?return "推斷結果: %d\nConfidence: %2f".format(maxIndex, output[maxIndex])
?}

?companion object {
? ?private const val TAG = "DigitClassifier"
? ?private const val MODEL_FILE = "mnist.tflite"
? ?private const val FLOAT_TYPE_SIZE = 4
? ?private const val PIXEL_SIZE = 1
? ?private const val OUTPUT_CLASSES_COUNT = 10
?}
}

主要任務是實現(xiàn)數(shù)字分類model的新建,加載,通過調(diào)用mnist.tflite的模型,進行數(shù)字的預測,預測默認給出10個預測值,獲取最高的預測結果。

3.手機畫個手機輸入的面板,輸入0-9的數(shù)字,進行判斷

package org.tensorflow.lite.examples.digitclassifier

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import android.widget.TextView
import com.divyanshu.draw.widget.DrawView

class MainActivity : AppCompatActivity() {

?private var drawView: DrawView? = null
?private var clearButton: Button? = null
?private var predictedTextView: TextView? = null
?private var digitClassifier = DigitClassifier(this)

?@SuppressLint("ClickableViewAccessibility")
?override fun onCreate(savedInstanceState: Bundle?) {
? ?super.onCreate(savedInstanceState)
? ?setContentView(R.layout.tfe_dc_activity_main)

? ?// Setup view instances
? ?drawView = findViewById(R.id.draw_view)
? ?drawView?.setStrokeWidth(50.0f)
? ?drawView?.setColor(Color.WHITE)
? ?drawView?.setBackgroundColor(Color.BLACK)
? ?clearButton = findViewById(R.id.clear_button)
? ?predictedTextView = findViewById(R.id.predicted_text)

? ?// Setup clear drawing button
? ?clearButton?.setOnClickListener {
? ? ?drawView?.clearCanvas()
? ? ?predictedTextView?.text = getString(R.string.tfe_dc_prediction_text_placeholder)
? ?}

? ?// Setup classification trigger so that it classify after every stroke drew
? ?drawView?.setOnTouchListener { _, event ->
? ? ?// As we have interrupted DrawView's touch event,
? ? ?// we first need to pass touch events through to the instance for the drawing to show up
? ? ?drawView?.onTouchEvent(event)

? ? ?// Then if user finished a touch event, run classification
? ? ?if (event.action == MotionEvent.ACTION_UP) {
? ? ? ?classifyDrawing()
? ? ?}
? ? ?true
? ?}

? ?// Setup digit classifier
? ?digitClassifier
? ? ?.initialize()
? ? ?.addOnFailureListener { e -> Log.e(TAG, "Error to setting up digit classifier.", e) }
?}

?override fun onDestroy() {
? ?digitClassifier.close()
? ?super.onDestroy()
?}

?private fun classifyDrawing() {
? ?val bitmap = drawView?.getBitmap()
? ?if ((bitmap != null) && (digitClassifier.isInitialized)) {
? ? ?digitClassifier
? ? ? ?.classifyAsync(bitmap)
? ? ? ?.addOnSuccessListener { resultText -> predictedTextView?.text = resultText }
? ? ? ?.addOnFailureListener { e ->
? ? ? ? ?predictedTextView?.text = getString(
? ? ? ? ? ?R.string.tfe_dc_classification_error_message,
? ? ? ? ? ?e.localizedMessage
? ? ? ? ?)
? ? ? ? ?Log.e(TAG, "Error classifying drawing.", e)
? ? ? ?}
? ?}
?}

?companion object {
? ?private const val TAG = "MainActivity"
?}
}

4.運行效果:




大成帶你用TFL數(shù)字分類識別的評論 (共 條)

分享到微博請遵守國家法律
凉城县| 泗洪县| 颍上县| 玉环县| 罗江县| 秦安县| 新平| 永昌县| 台东县| 连山| 阆中市| 株洲县| 合阳县| 连平县| 板桥市| 长宁县| 木里| 子洲县| 三台县| 池州市| 青川县| 百色市| 锡林浩特市| 河津市| 平昌县| 仁布县| 余江县| 扎鲁特旗| 嘉定区| 唐海县| 巴彦县| 兴和县| 延寿县| 永康市| 阜平县| 永定县| 柳林县| 舞钢市| 威信县| 达尔| 乌兰浩特市|