Since I was able to install coremltools as described in the previous post, it's time to try to convert an actual model and run it with MLKit.
I have chosen "VGG Face CNN descriptor" from Model Zoo as the very first model to tray -- for no particular reason. I just needed a model to try.
First, I downloaded the model from this link. It contains .prototxt, .affemodel, .txt files and a sample image.
Second, I converted them into .mlmodel file using coremltools
(p27)# python
>>> import coremltools
>>> caffe_model = ('VGG_FACE.caffemodel', 'VGG_FACE_deploy.prototxt')
>>> labels = 'names.txt'
>>> coreml_model = coremltools.converters.caffe.convert(caffe_model, class_labels=labels, image_input_names='data')
>>> coreml_model.save('DeepFace.mlmodel')
Third, I created a new Xcode project (a single view iOS app), and imported 'DeepFace.mlmodel' file along with 'ak.png' file as a test image and wrote a following piece of code.
As a property of ViewController class,
let model = DeepFace()
In viewDidLoad() function,
let files = [
"ak.png"
]
for file in files {
let image = UIImage(named: file)!
let buffer = image.pixelBuffer()!
if let output = try? model.prediction(data: buffer) {
print(file, output.classLabel)
} else {
print(file, "failed")
}
}
It correctly detected the test image as "Aamir Khan".
I have also tried several other images from the internet (you need to resize and crop them into 224x244 image), and most of them were identified correctly (as long as the name is in the list).
I have tried to upload the source code (along with .mlmodel file) to github, but the .mlmodel file is too big (580MB). You can easily create your own version by following steps above.