โ˜•๏ธ Caffe2Kit

April 26, 2017 ยท View on GitHub

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.