Machine Learning on iOS: image classification šŸ“· MobileNetV2

Mohamed nouri
4 min readAug 14, 2019

--

Hello there In this short article, I will show you how to incorporate Appleā€™s Core ML ā€œMobileNetV2ā€ framework into your iOS app to create a quick image classification app.

So youā€™ve been working on iOS apps for a while now and you think youā€™re pretty slick. Think youā€™ve done it all? , actually, the iOS framework is much more powerful and complex than that. there is always something new to learn and in today's lesson, we will take a look at the amazing core ML framework to create a simple classification app.

The app is pretty simple first you load an image from the phone gallery and then using MoileNet V2 model the app will predict the name of the object in the image

the app UI should look like this: I will put the full code source down below oh, by the way, I donā€™t like storyboards šŸ˜…

From where to start?

as always First, we need a core Ml model that recognizes images we will use one of Appelā€™s pre-trained Models you can find the full official list of models here, as I mentioned before in this project we will be using the MobileNet version 2 model (32 bit) .its a powerful small size stander image recognition model and itā€™s able to recognize up to 1000 categories such as trees animals, food, people, vehicles and more ā€¦ very amazing

let's start by downloading this core ml model ā€œMobileNet 2ā€ Then simply the ml Model can be dragged directly into Xcode :

you can see here a lot of metadata such as name, author, and input/output description

so going back to the source code (our View Controller )we can start using the MobileNet model directly like so :

let mobilenet = MobileNetV2()

now go to the function in our ViewController where the image gets selected by the user (if you're using one code simply here where you're going to pass your image to be processed by the model it could be just a static image )

predict image function takes any UIImage

so in order to use our model first, we need to take this image and convert it to a cv pixel buffer so that it can be used by the model's prediction function

please check the input description in the model metadata (CvPixelBuffer 224*244)

we are using some low-level APIs you donā€™t need to know about so I will just provide a function for that

function Convert an image to cv Pixel

good so let's use our image: assuming we have a valid pixel buffer image we can use the mobilenet V2 prediction function using our image buffer result.

this needs to be wrapped in a try-catch statement because it can throw. after that will have a prediction we can use the class label which is the category with the highest confidence and we will set that to our label result

The full predicted image function

There you have it a prediction app using machine learning super easy to make thanks to the powerful Apple Core ml

the final app screenshots

Please find the full code source here

Thank you so muchšŸ“·

--

--