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

TensorFlow no Android

TensorFlow no Android

Palestra apresentada no DevFest Cerrado e Android Meetup

Douglas Kayama

September 30, 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 Como funciona TensorFlowImageClassifier carrega • MODEL file • LABEL

    file • TensorFlowInferenceInterface (uma classe que carrega a lib nativa) 30
  8. @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); 31
  9. @douglasdrumond Use as ferramentas do TF freeze_graph Graph Transformation Tool

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

    MB •Inception v3 quantized = 24 MB •Inception v1 quantized = 7MB 34
  11. @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 36