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

Attila Blenesi - TensorFlow For AndroidDevs

Attila Blenesi - TensorFlow For AndroidDevs

droidcon Berlin

July 17, 2018
Tweet

More Decks by droidcon Berlin

Other Decks in Programming

Transcript

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

    Learning APIs TensorFlow Cloud ML @ablenessy | @droidconBerlin | #DCBERLIN18
  2. Application Developer ML Practitioner Data Scientist Firebase ML Kit Machine

    Learning APIs TensorFlow Cloud ML @ablenessy | @droidconBerlin | #DCBERLIN18
  3. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  4. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  5. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  6. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  7. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  8. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionTextDetector() detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  9. val image = FirebaseVisionImage.fromBitmap(selectedImage) val detector = FirebaseVision.getInstance() .getVisionCloudDocumentTextDetector(options) detector.detectInImage(image)

    .addOnSuccessListener { texts -> processTextRecognitionResult(texts) }a .addOnFailureListener(...) @ablenessy | @droidconBerlin | #DCBERLIN18
  10. 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 | @droidconBerlin | #DCBERLIN18
  11. 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 | @droidconBerlin | #DCBERLIN18
  12. Do I even have to dig deeper ? @ablenessy |

    @droidconBerlin | #DCBERLIN18
  13. generic model = generic solution Do I even have to

    dig deeper ? @ablenessy | @droidconBerlin | #DCBERLIN18
  14. Local ~ 36 hours ~ 12 hours Cloud vs @ablenessy

    | @droidconBerlin | #DCBERLIN18
  15. 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
  16. 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
  17. trainingInput: scaleTier: CUSTOM masterType: standard_gpu # 1 GPU pythonVersion: "3.5"

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

    runtimeVersion: "1.8" Model > Training > Optimisation > Deployment Cloud ML
  19. 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
  20. 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
  21. 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 usefull nodes ) with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString())
  22. 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 usefull nodes ) with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString())
  23. 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 usefull nodes ) with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString())
  24. 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())
  25. Model > Training > Optimisation > Deployment // TensorFlow Mobile

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

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

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

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

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

    TensorFlowInferenceInterface( assetManager, "file:///android_asset/frozen_model.pb" ) inputValues = generateInput(inputDrawing) inferenceInterface.feed("input_node", inputValues, inputDimensions) inferenceInterface.run(arrayOf("output_node"), false) inferenceInterface.fetch("output_node", outputValues) val result = generateBitmapFromOutput(outputValues)