Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TENSORFLOW FOR ANDROID DEVS

TENSORFLOW FOR ANDROID DEVS

Introduction to the Tensorflow from an Android developers Point of view. We will go through a simple example, explaining the steps, hurdles of integration a TensorFlow model into a mobile application. In the end, you will understand the process of working with TensorFlow on Android devices. Hopefully, this will motivate you to implement your crazy AI ideas in the mobile world.

Updated version from Droidcon Transylvania

Blénesi Attila

June 26, 2018
Tweet

More Decks by Blénesi Attila

Other Decks in Programming

Transcript

  1. Application Developer ML Practitioner Data Scientist Firebase ML Kit Machine

    Learning APIs TensorFlow Cloud ML @ablenessy | @droidconRO | #DroidconT
  2. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconRO | #DroidconT
  3. val image = FirebaseVisionImage.fromBitmap(selectedImage) val options = FirebaseVisionCloudDetectorOptions.Builder() .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL) .setMaxResults(15)

    .build() val detector = FirebaseVision.getInstance() .getVisionCloudDocumentTextDetector(options) detector.detectInImage(image) .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconRO | #DroidconT
  4. generic model = generic solution Do I even have to

    dig deeper ? @ablenessy | @droidconRO | #DroidconT
  5. Model > Training > Optimisation > Deployment gcloud ml-engine jobs

    submit training $JOB_NAME \ --job-dir $JOB_DIR \ --module-name trainer.pix2pix \ --package-path ./trainer \ --region $REGION \ --config=trainer/cloudml-gpu.yaml \ -- \ --mode train \ --input-dir gs://$BUCKET_NAME/train Cloud ML
  6. Model > Training > Optimisation > Deployment gcloud ml-engine jobs

    submit training $JOB_NAME \ --job-dir $JOB_DIR \ --module-name trainer.pix2pix \ --package-path ./trainer \ --region $REGION \ --config=trainer/cloudml-gpu.yaml \ -- \ --mode train \ --input-dir gs://$BUCKET_NAME/train Cloud ML
  7. trainingInput: scaleTier: CUSTOM masterType: standard_gpu # 1 GPU pythonVersion: "3.5"

    runtimeVersion: "1.8" Model > Training > Optimisation > Deployment Cloud ML
  8. trainingInput: scaleTier: CUSTOM masterType: complex_model_m_gpu # 4 GPUs pythonVersion: "3.5"

    runtimeVersion: "1.8" Model > Training > Optimisation > Deployment Cloud ML
  9. Model > Training > Optimisation > Deployment gcloud ml-engine jobs

    submit training $JOB_NAME \ --job-dir $JOB_DIR \ --module-name trainer.pix2pix \ --package-path ./trainer \ --region $REGION \ --config=trainer/cloudml-gpu.yaml \ -- \ --mode train \ --input-dir gs://$BUCKET_NAME/train Cloud ML
  10. Model > Training > Optimisation > Deployment gcloud ml-engine jobs

    submit training $JOB_NAME \ --job-dir $JOB_DIR \ --module-name trainer.pix2pix \ --package-path ./trainer \ --region $REGION \ --config=trainer/cloudml-gpu.yaml \ -- \ --mode train \ --input-dir gs://$BUCKET_NAME/train Cloud ML
  11. Model > Training > Optimisation > Deployment # We start

    a session using a temporary fresh Graph with tf.Session(graph=tf.Graph()) as sess: # We import the meta graph in the current default Graph saver = tf.train.import_meta_graph(input_checkpoint + ‘.meta’,…) saver.restore(sess, input_checkpoint) # We restore the weights output_graph_def = tf.graph_util.convert_variables_to_constants( sess, # The session is used to retrieve the weights tf.get_default_graph().as_graph_def(), # retrieve the nodes output_node_names # select the useful nodes ) with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString())
  12. Model > Training > Optimisation > Deployment // TensorFlow Android

    implementation ‘org.tensorflow:tensorflow-android:1.10.0’
  13. Model > Training > Optimisation > Deployment output_tensor Generated image

    jalammar.github.io/Supercharging-android-apps-using-tensorflow/ Generator
  14. Model > Training > Optimisation > Deployment val interpreter =

    TensorFlowInferenceInterface( assetManager, "file:///android_asset/frozen_model.pb" ) inputValues = generateInput(inputDrawing) interpreter.feed("input_node", inputValues, inputDimensions) interpreter.run(arrayOf("output_node"), false) interpreter.fetch(“output_node", outputValues) val result = generateBitmapFromOutput(outputValues)
  15. Model > Training > Optimisation > Deployment // TensorFlow Lite

    implementation ‘org.tensorflow:tensorflow-lite:1.10.0’
  16. Model > Training > Optimisation > Deployment val interpreter =

    Interpreter( File(URI.create("file://android_asset/frozen_model.tflite")) )a inputValues = generateInput(inputDrawing) interpreter.run(inputValues, outputValues) val result = generateBitmapFromOutput(outputValues)