Skip to content

Instantly share code, notes, and snippets.

@melito
Last active April 19, 2022 03:09
Show Gist options
  • Save melito/da9c408d8703b326226255d30e32d3c5 to your computer and use it in GitHub Desktop.
Save melito/da9c408d8703b326226255d30e32d3c5 to your computer and use it in GitHub Desktop.

Install python env

On macOS the system python crashes with weird bugs.

You get around that using virtualenv and python 2.7

Here's what I did

virtualenv -p /usr/bin/python2.7 env
source env/bin/activate
pip install tensorflow
pip install keras==1.2.2
pip install h5py
pip install coremltools

Once you've done that you need to edit the image dimension inputs on lines 4 & 5 in pose_deploy.prototxt (otherwise the generated CoreML model thinks you want to input an image that's 1x1)

Then just run: python convert.py

You'll end up with a Face.mlmodel that you can import into Xcode.

Using with Swift

If you attempt to use this with the Vision framework, nothing will happen. No errors will be thrown you will just get empty results. As of this writing the Vision framework seems to only support classifier models

You can use CoreML directly to make predictions like so:

let pixelBuffer = self.sourceImage.resize(to: CGSize(width: 368, height: 368)).pixelBuffer()
let m           = Face()
let output      = try? m.prediction(image: pixelBuffer!)

It's pretty slow and I get an output matrix. That's as far as I've gotten. Have not investigated the output data too closely

import coremltools
proto_file = 'pose_deploy.prototxt'
caffe_model = 'pose_iter_116000.caffemodel'
coreml_model = coremltools.converters.caffe.convert((caffe_model, proto_file), image_input_names='image')
coreml_model.save('Face.mlmodel')
@tangert
Copy link

tangert commented Aug 27, 2017

I got it to work and also am just left with an output matrix…did you make any progress on gathering meaning from it?

@melito
Copy link
Author

melito commented Jan 12, 2019

I never did.

Hopefully I find myself in a spot where I can and do in the near future. How about you a year on :)?

@Rana-Muhammad-Amir
Copy link

Hey...! @melito
i am working on it and i generate coreml and then give an image for prediction and i am getting output as MLMutilArray 1 x 1 x 22 x 40 x 40 array
now i am trying to convert it back to image and i get only few lines sometimes and sometime not, and i don't know how to convert it to UIImage or cv::Mat
i follow the below link:
https://gist.github.com/otmb/7b2e1caf3330b97c82dc217af5844ad5
can you help..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment