Caffe2 for iOS. A simple integration into existing projects.
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.
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
.
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
.
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"
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%
Prediciting the class in the example app examples/Caffe2Test
takes approx, 2ms on an iPhone 7 Plus and 6ms on an iPhone 6.
Deployment target of your App is >= iOS 10.3
Robert Biehl, robert@oksnap.me
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.