RobertBiehl / caffe2-ios

β˜•οΈ Caffe2Kit. A simple one step integration of Caffe2 for iOS.
Apache License 2.0
71 stars 13 forks source link
ai artificial-intelligence caffe2 cocoapods-specs deep-learning deep-neural-networks

β˜•οΈ Caffe2Kit

Caffe2 for iOS. A simple integration into existing projects.

Caffe2Kit - Simple integration of Caffe2 on iOS.

Twitter Caffe2 Req

🚨 Attention

Please note that this pod is in a very early stage and currently has multiple shortcomings:

  • Not officially on CocoaPods yet!
  • Only runs on iOS 10.3! -- should be fixed soon by udating the build_ios_pod.sh build script
  • Wrapper currently only supports classification tasks.

πŸ“² Installation

Caffe2Kit is will soon be available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Caffe2Kit', :git => 'git://github.com/RobertBiehl/caffe2-ios'

and run pod install.

Disable Bitcode

Since caffe2 is not yet built with bitcode support you need to add this to your Podfile

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end

and disable bitcode for your Target by setting Build Settings -> Enable Bitcode to No.

Additional steps:

These steps will hopefully be removed in later versions. 1) in Build Phases -> Your Target -> Link Binary with Libraries add libstdc++.tdb. 2) in Build Settings -> Other Linker Flags remove $(inherited)and -force_load "$(PODS_ROOT)/Caffe2Kit/install/lib/libCaffe2_CPU.a"

πŸš€ Using Caffe2Kit

import Caffe2Kit

do {
  let caffe = try Caffe2(initNetNamed: "squeeze_init_net", predictNetNamed:"squeeze_predict_net")
  let πŸŒ… = #imageLiteral(resourceName: "lion.png")

  if let res = self.caffe?.prediction(regarding:πŸŒ…) {
  // find top 5 classes
  let sorted = res
    .map{$0.floatValue}
    .enumerated()
    .sorted(by: {$0.element > $1.element})[0...5]

  // generate output
  let text = sorted
    .map{"\($0.offset): \(classes[$0.offset]) \($0.element*100)%"}
    .joined(separator: "\n")

  print("Result\n \(text)")
} catch _ {
  // model could not be loaded
}

Result:

291: 🦁 lion, king of beasts, Panthera leo 100.0%
373: πŸ’ macaque 3.59472e-08%
231: πŸ• collie 4.77662e-09%
374: πŸ’ langur 1.63787e-09%
371: πŸ’ patas, hussar monkey, Erythrocebus patas 5.34424e-10%
259: 🐢 Pomeranian 2.12385e-10%

⏱ Performance

Prediciting the class in the example app examples/Caffe2Test takes approx, 2ms on an iPhone 7 Plus and 6ms on an iPhone 6.

βœ… Requirements

Deployment target of your App is >= iOS 10.3

πŸ€– Author(s)

Robert Biehl, robert@oksnap.me

πŸ“„ License

Caffe2Kit is available under the Apache License 2.0. See the LICENSE file for more info.

Caffe2 is released under the BSD 2-Clause license.