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

TensorFlow no Android – v2

TensorFlow no Android – v2

Palestra apresentada no DevFest Campinas

Douglas Kayama

November 04, 2017
Tweet

More Decks by Douglas Kayama

Other Decks in Technology

Transcript

  1. @douglasdrumond Como eu me sinto Axiomas: • Existe 1 •

    Existe uma operação s(n) que obtém o sucessor Prove: • Todo número par maior que 2 é soma de dois primos 13
  2. @douglasdrumond O que é? “An open-source software library for Machine

    Intelligence” – https://www.tensorflow.org/ 16
  3. @douglasdrumond Task do bazel task buildNativeBazel(type: Exec) { workingDir '../../..'

    commandLine bazelLocation, 'build', '-c', 'opt', \ 'tensorflow/examples/android:tensorflow_native_libs', \ '--crosstool_top=//external:android/crosstool', \ '--cpu=' + cpuType, \ '--host_crosstool_top=@bazel_tools//tools/cpp:toolchain' } 20 Não se preocupe em entender agora, pode copiar dos exemplos
  4. @douglasdrumond NDK Configurar WORKSPACE 23 # Uncomment and update the

    paths in these entries to build the Android demo. android_sdk_repository( name = "androidsdk", api_level = 23, # Ensure that you have the build_tools_version below installed in the # SDK manager as it updates periodically. build_tools_version = "26.0.1", # Replace with path to Android SDK on your system path = "<PATH_TO_SDK>", )
  5. @douglasdrumond NDK Pelo menos versão 14 24 android_ndk_repository( name="androidndk", path="<PATH_TO_NDK>",

    # This needs to be 14 or higher to compile TensorFlow. # Please specify API level to >= 21 to build for 64-bit # architectures or the Android NDK will automatically select biggest # API level that it supports without notice. # Note that the NDK version is not the API level. api_level=14)
  6. @douglasdrumond Android Inference Library Prebuilt binaries at https://ci.tensorflow.org/view/Nightly/job/ nightly-android/ Inference

    docs at https://github.com/tensorflow/tensorflow/ blob/master/tensorflow/contrib/android/java/org/tensorflow/ contrib/android/TensorFlowInferenceInterface.java Full TensorFlow Java API bridges with C++
 https://github.com/tensorflow/tensorflow/tree/master/java
 29
  7. @douglasdrumond Antes do Android Treine seu modelo 30 import tensorflow

    as tf from tensorflow.examples.tutorials.mnist import input_data ... optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(cost)
  8. @douglasdrumond Gere o protobuf from tensorflow.python.tools import freeze_graph ... freeze_graph.freeze_graph(input_graph_path,

    input_saver_def_path, input_binary, input_checkpoint_path, output_node_names, restore_op_name, filename_tensor_name, output_graph_path, clear_devices) 31
  9. @douglasdrumond Como funciona TensorFlowImageClassifier carrega • MODEL file • LABEL

    file • TensorFlowInferenceInterface (uma classe que carrega a lib nativa) 32
  10. @douglasdrumond Como funciona? List<Classifier.Recognition> results = classifier.recognizeImage(bitmap); …
 inferenceInterface.feed(inputName, floatValues,

    1, inputSize, inputSize, 3); …
 inferenceInterface.run(outputNames, logStats); … inferenceInterface.fetch(outputName, outputs); 33
  11. @douglasdrumond Use as ferramentas do TF freeze_graph Graph Transformation Tool

    •strip_unused_nodes •remove_nodes •fold_batch_norms 35 Quantize weights 75% de melhora no tamanho Memory mapping
  12. @douglasdrumond Outras preocupações Tamanho do modelo •Inception v3 = 90

    MB •Inception v3 quantized = 24 MB •Inception v1 quantized = 7MB 36
  13. @douglasdrumond Tuning Inclua apenas as operações necessárias Rode python/tools/ print_selective_registration_header.py

    
 Ponha ops_to_register.h na raiz do TensorFlow
 Compile com -DSELECTIVE_REGISTRATION 38