Swift for TensorFlow
Nagoya iOS Meetup vol.3
• (@k_ryuichirou)
• Nagoya.Swift+ / Nagoya.cloud.first
• iOS
• Bot
1. Nagoya.Swift+
• 3
• next 16. Jun.
2. Nagoya.cloud.first
• 1
• next 2. Jun.
Swift for Tensorflow
1.How to use TensorFlow on iOS
2.Swift for Tensorflow
3.Demo (from Tutorial)
How to use TensorFlow on iOS
1.TensorFlow Mobile
2.TensorFlow Light
3.tfcoreml
4.Tensorflow.js (Safari) <- new!
5.Ml kit <- new!
1. Tensorflow Mobile
•
!
pod 'TensorFlow-experimental'
•
"
pod 'TensorFlow'
• File format : model.pb
• See TensorFlow iOS Examples
2. TensorFlow Light
• convert .pb to .tflite at your macOS
• pod 'TensorFlowLite'
• File format : model.tflite
• See iOS Demo App
3. tfcoreml (tf-coreml)
• pip install -U tfcoreml
• Convert .pb or .tflite to .mlmodel
• Supports python 2.7 and 3.x
• See tf-coreml/inception_v3.ipynb
4. Tensorflow.js (Safari)
• npm install @tensorflow/tfjs
• inference and train
• might be only way to train model
• See TensorFlow.js
5. Ml kit
• Image labeling, Text recognition, Landmark
detection
• Face detection, barcode scanning
• CoreImage.framework (CIDetector)?
• Deliver .tflite model over Firebase
Swift for Tensorflow
1.How to use TensorFlow on iOS
2.Swift for Tensorflow
3.Demo (from Tutorial)
Swift for Tensorflow
is not
Tensorflow by Swift
How to use Swift for Tensorflow (1/2)
1.Install a toolchain from swift/Installation.md
2.Change toolchain to Swift for TensorFlow
3.import TensorFlow at Playground
2. Change toolchain to Swift for TensorFlow
4. import TensorFlow & Test Drive
Demo
Caution!
• You cannot use import TensorFlow in iOS
project.
• Why?
• This project is Swift for Tensorflow.
History of Swift for Tensorflow
1.Proposal about dynamically "callable" type
2.tensorflow/swift
3.apple/swift
1. Proposal about dynamically "callable" type
• [pitch] Introduce user-defined dynamically
"callable" types by Chris Lattner
// import DogModule.Dog as Dog // an alternate
let Dog = Python.import("DogModule.Dog")
// import pickle
let pickle = Python.import("pickle")
// file = open(filename)
let file = Python.open(filename)
// blob = file.read()
let blob = file.read()
// result = pickle.loads(blob)
let result = pickle.loads(blob)
!!!!!!!!!
2. tensorflow/swift
from Why Swift ForTensorFlow
The core graph program extraction algorithm,
automatic differentiation, and Python language
interoperability features of Swift for TensorFlow
can be implemented for other programming
languages, and we are occasionally asked why we
didn’t use some other one for this project.
Python Interoperability
import Python
let sys = Python.import("sys")
print(sys.version)
// 2.7.10 (default, Oct 6 2017, 22:29:07)
// [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
Demo #2
as is
// Python : a = np.arange(15).reshape(3, 5)
let a = np.arange.call(with: 15).reshape.call(with: 3, 5)
to be
// Python : a = np.arange(15).reshape(3, 5)
let a = np.arange(15).reshape(3, 5) // not work!
Requirement for Swift
• from Python Interoperability document:
We need to implement support for the
@dynamicCallable feature, improving the call-
side syntax, just like we improved member
lookup.
• followings are also important :
1.Graph Program Extraction <- core feature
2.Automatic Differentiation
3. apple/swift
• tensorflow/swift -> tensorflow at apple/swift
Summary, Swift for Tensorflow
• Chris Lattner's long journey to implement
@dynamicCallable feature
• He proved the importance of @dynamicCallable
with TensorFlow
• It will continue on tensorflow at apple/swift
tfcoreml
• TensorFlow Core ML - Qiita
• Google Developers Blog: Announcing Core ML
support in TensorFlow Lite
MlKit
• ML Kit | Google Developers
Tensorflow Lite
• Introduction to TensorFlow Lite | TensorFlow
Swift for Tensorflow (1/2)
• Swift for TensorFlow | TensorFlow*
tensorflow/swift: Swift for TensorFlow
documentation repository.
• swift/WhySwiftForTensorFlow.md at master ·
tensorflow/swift
• swift/GraphProgramExtraction.md at master ·
tensorflow/swift
• swift/AutomaticDifferentiation.md at master ·
tensorflow/swift
Swift for Tensorflow (2/2)
• swift/PythonInteroperability.md at master ·
tensorflow/swift
• Naoki Hiroshima

Swift for tensorflow

  • 1.
  • 2.
    • (@k_ryuichirou) • Nagoya.Swift+/ Nagoya.cloud.first • iOS • Bot
  • 3.
    1. Nagoya.Swift+ • 3 •next 16. Jun. 2. Nagoya.cloud.first • 1 • next 2. Jun.
  • 4.
    Swift for Tensorflow 1.Howto use TensorFlow on iOS 2.Swift for Tensorflow 3.Demo (from Tutorial)
  • 5.
    How to useTensorFlow on iOS 1.TensorFlow Mobile 2.TensorFlow Light 3.tfcoreml 4.Tensorflow.js (Safari) <- new! 5.Ml kit <- new!
  • 6.
    1. Tensorflow Mobile • ! pod'TensorFlow-experimental' • " pod 'TensorFlow' • File format : model.pb • See TensorFlow iOS Examples
  • 7.
    2. TensorFlow Light •convert .pb to .tflite at your macOS • pod 'TensorFlowLite' • File format : model.tflite • See iOS Demo App
  • 8.
    3. tfcoreml (tf-coreml) •pip install -U tfcoreml • Convert .pb or .tflite to .mlmodel • Supports python 2.7 and 3.x • See tf-coreml/inception_v3.ipynb
  • 9.
    4. Tensorflow.js (Safari) •npm install @tensorflow/tfjs • inference and train • might be only way to train model • See TensorFlow.js
  • 10.
    5. Ml kit •Image labeling, Text recognition, Landmark detection • Face detection, barcode scanning • CoreImage.framework (CIDetector)? • Deliver .tflite model over Firebase
  • 11.
    Swift for Tensorflow 1.Howto use TensorFlow on iOS 2.Swift for Tensorflow 3.Demo (from Tutorial)
  • 12.
    Swift for Tensorflow isnot Tensorflow by Swift
  • 13.
    How to useSwift for Tensorflow (1/2) 1.Install a toolchain from swift/Installation.md 2.Change toolchain to Swift for TensorFlow 3.import TensorFlow at Playground
  • 14.
    2. Change toolchainto Swift for TensorFlow
  • 15.
  • 16.
  • 17.
    Caution! • You cannotuse import TensorFlow in iOS project. • Why? • This project is Swift for Tensorflow.
  • 18.
    History of Swiftfor Tensorflow 1.Proposal about dynamically "callable" type 2.tensorflow/swift 3.apple/swift
  • 19.
    1. Proposal aboutdynamically "callable" type • [pitch] Introduce user-defined dynamically "callable" types by Chris Lattner // import DogModule.Dog as Dog // an alternate let Dog = Python.import("DogModule.Dog") // import pickle let pickle = Python.import("pickle") // file = open(filename) let file = Python.open(filename) // blob = file.read() let blob = file.read() // result = pickle.loads(blob) let result = pickle.loads(blob)
  • 20.
  • 22.
    2. tensorflow/swift from WhySwift ForTensorFlow The core graph program extraction algorithm, automatic differentiation, and Python language interoperability features of Swift for TensorFlow can be implemented for other programming languages, and we are occasionally asked why we didn’t use some other one for this project.
  • 23.
    Python Interoperability import Python letsys = Python.import("sys") print(sys.version) // 2.7.10 (default, Oct 6 2017, 22:29:07) // [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
  • 24.
  • 25.
    as is // Python: a = np.arange(15).reshape(3, 5) let a = np.arange.call(with: 15).reshape.call(with: 3, 5) to be // Python : a = np.arange(15).reshape(3, 5) let a = np.arange(15).reshape(3, 5) // not work!
  • 26.
    Requirement for Swift •from Python Interoperability document: We need to implement support for the @dynamicCallable feature, improving the call- side syntax, just like we improved member lookup. • followings are also important : 1.Graph Program Extraction <- core feature 2.Automatic Differentiation
  • 27.
    3. apple/swift • tensorflow/swift-> tensorflow at apple/swift
  • 28.
    Summary, Swift forTensorflow • Chris Lattner's long journey to implement @dynamicCallable feature • He proved the importance of @dynamicCallable with TensorFlow • It will continue on tensorflow at apple/swift
  • 30.
    tfcoreml • TensorFlow CoreML - Qiita • Google Developers Blog: Announcing Core ML support in TensorFlow Lite MlKit • ML Kit | Google Developers
  • 31.
    Tensorflow Lite • Introductionto TensorFlow Lite | TensorFlow
  • 32.
    Swift for Tensorflow(1/2) • Swift for TensorFlow | TensorFlow* tensorflow/swift: Swift for TensorFlow documentation repository. • swift/WhySwiftForTensorFlow.md at master · tensorflow/swift • swift/GraphProgramExtraction.md at master · tensorflow/swift • swift/AutomaticDifferentiation.md at master · tensorflow/swift
  • 33.
    Swift for Tensorflow(2/2) • swift/PythonInteroperability.md at master · tensorflow/swift • Naoki Hiroshima