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

Deep Learning on iOS #360iDev

shu223
August 16, 2017

Deep Learning on iOS #360iDev

2017年8月13日〜16日にアメリカ合衆国コロラド州デンバーにて開催された「360|iDev 2017」にて登壇して際の発表資料です。

ブログ: コロラド州デンバーで開催されたiOSカンファレンス「360|iDev 2017」に登壇した話

shu223

August 16, 2017
Tweet

More Decks by shu223

Other Decks in Technology

Transcript

  1. Overview • How to implement “Deep Learning” on iOS Metal

    Performance Shaders (MPSCNN) Accelerate (BNNS) Core ML Vision Your App
  2. !

  3. Your App Metal Performance Shaders (MPSCNN) Accelerate (BNNS) iOS 10

    • Optimized for GPU (by ) and CPU (by Accelerate) • Available for iOS 10, too • Basically any ML tools can be used to train the models Still works!
  4. Train Inference Trained Params -Which tools can be used for

    the training? -What kind of formats can be used to pass the pre-trained params?
  5. Which ML tools can be used? : ANY What model

    format can be used? : ANY Model (Trained Params) dat Train ML Tools
  6. Which ML tools can be used? : ANY What model

    format can be used? : ANY Model (Trained Params) dat Train ML Tools hdf5 •The “.dat” files are common binary files. - Not specific for iOS or MPS. •Contains the trained params - Weights / Biases •Any other format can be used as long as it can be read by the iOS app. - c.f. hdf5
  7. Which ML tools can be used? : ANY What model

    format can be used? : ANY Model (Trained Params) dat Train ML Tools hdf5 •Any tools which can train CNN, and export the params
  8. MPSCNNConvolution MPSCNNFullyConnected MPSCNNPooling MPSCNNConvolution MPSCNNConvolution MPSCNNPooling • Almost same name

    -> Easy to find • Complicated Maths or GPU optimization are encapsulated Classes corresponding to each CNN layers are provided
  9. Input image MPSImage Result (UInt, etc.) CNN • Implemented in

    Step 2 • Trained params (created in Step 1) are loaded. Do something
  10. • Step 1: Create the model - Any ML tools

    can be used - Any file format can be used for the trained params • Step 2: Implement the network - Classes corresponding to each CNN layer are provided • Step 3: Implement the inference - Input to the CNN, and output from the CNN
  11. Development Flow w/ MPSCNN ML Tools Train some format Trained

    Params dat Extract Parse dat MPSCNNConvolution MPSCNNFullyConnected Implement Network 2,000 lines& App
  12. Development Flow w/ Core ML ML Tools Train some format

    Trained Params dat Extract Parse dat MPSCNNConvolution MPSCNNFullyConnected Implement Network App 2,000 lines& 1) Convert w/ coremltools 2) Drag & Drop some format Generate
  13. Input image MPSImage Result (UInt, etc.) CNN Do something let

    size = MTLSize(width: inputWidth, height: inputHeight let region = MTLRegion(origin: MTLOrigin(x: 0, y: 0, z: 0 size: size) network.srcImage.texture.replace( region: region, mipmapLevel: 0, slice: 0, withBytes: context.data!, bytesPerRow: inputWidth, bytesPerImage: 0) Need to know Metal to use MPSCNN let origin = MTLOrigin(x: 0, y: 0, z: 0) let size = MTLSize(width: 1, height: 1, depth: 1) finalLayer.texture.getBytes(&(result_half_array[4*i]), bytesPerRow: MemoryLayout<UIn bytesPerImage: MemoryLayout<U from: MTLRegion(origin: origi size: size), mipmapLevel: 0, slice: i)
  14. Input image MPSImage Results CNN Do something let ciImage =

    CIImage(cvPixelBuffer: imageBuffer) let handler = VNImageRequestHandler(ciImage: ciImage) try! handler.perform([self.coremlRequest]) Don’t need to touch Metal to use Vision guard let results = request.results as? [VNClassificationObservation] else { return } guard let best = results.first?.identifier else { return
  15. • He added “watchOS might be a case on that

    you should use BNNS.” • Because watchOS doesn’t support MPSCNN, but supports BNNS. (I haven’t tried yet.)
  16. My current understanding: • The cost for passing data between

    CPU and GPU is not small • When the network is small, the CPU <-> GPU cost might be bigger than the benefit of parallel processing. BNNS might be better when the network is small.
  17. Recap • Why is “Deep Learning on iOS” exciting? •

    How to implement “Deep Learning” on iOS - w/ MPSCNN (iOS 10) - w/ Core ML & Vision (iOS 11) - When to choose BNNS MPSCNN BNNS Core ML Vision