The MobileNet neural network using Apple's new CoreML framework

Overview

MobileNet with CoreML

This is the MobileNet neural network architecture from the paper MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications implemented using Apple's shiny new CoreML framework.

This uses the pretrained weights from shicai/MobileNet-Caffe.

There are two demo apps included:

  • Cat Demo. Shows the prediction for a cat picture. Open the project in Xcode 9 and run it on a device with iOS 11 or on the simulator.

  • Camera Demo. Runs from a live video feed and performs a prediction as often as it can manage. (You'll need to run this app on a device, it won't work in the simulator.)

The cat demo app

Note: Also check out Forge, my neural net library for iOS 10 that comes with a version of MobileNet implemented in Metal.

Converting the weights

The repo already includes a fully-baked MobileNet.mlmodel, so you don't have to follow the steps in this section. However, in case you're curious, here's how I converted the original Caffe model into this .mlmodel file:

  1. Download the caffemodel file from shicai/MobileNet-Caffe into the top-level folder for this project.

Note: You don't have to download mobilenet_deploy.prototxt. There's already one included in this repo. (I added a Softmax layer at the end, which is missing from the original.)

  1. From a Terminal, do the following:
$ virtualenv -p /usr/bin/python2.7 env
$ source env/bin/activate
$ pip install tensorflow
$ pip install keras==1.2.2
$ pip install coremltools

It's important that you set up the virtual environment using /usr/bin/python2.7. If you use another version of Python, the conversion script will crash with Fatal Python error: PyThreadState_Get: no current thread. You also need to use Keras 1.2.2 and not the newer 2.0.

  1. Run the coreml.py script to do the conversion:
$ python coreml.py

This creates the MobileNet.mlmodel file.

  1. Clean up by deactivating the virtualenv:
$ deactivate

Done!

Comments
  • How long does it take to predict an image

    How long does it take to predict an image

    Thanks for your work.I think I run your demo successfully now.But I want to know how long does it take to predict an image so I add some code to test the time. And when I loop for 100 times for predict the cat image, it cost about 20s to do this on my iPhone6, But I think it should be faster.So am I wrong?

    opened by liu6381810 4
  • Model not working for me

    Model not working for me

    In both the Cat and Camera demos, the model only predicts "candle, taper, wax light", and at 100% or very close to 100% confidence. I've tried both the provided model and ran the trainer myself with the same results. Any idea what I could be doing wrong?

    opened by kevinpk 3
  • Error: When trying to convert Caffe model to CoreML model

    Error: When trying to convert Caffe model to CoreML model

    Hi, Thanks for this excellent repo. But when I am getting the following error when trying to convert the caffe model to .mlmodel.

    Traceback (most recent call last):
      File "coreml.py", line 23, in <module>
        class_labels='synset_words.txt')
      File "//anaconda/envs/coreml/lib/python2.7/site-packages/coremltools/converters/caffe/_caffe_converter.py", line 131, in convert
        return MLModel(model_path)
      File "//anaconda/envs/coreml/lib/python2.7/site-packages/coremltools/models/model.py", line 126, in __init__
        self.__proxy__ = _get_proxy_from_spec(model)
      File "//anaconda/envs/coreml/lib/python2.7/site-packages/coremltools/models/model.py", line 55, in _get_proxy_from_spec
        return _MLModelProxy.fromSpec(filename)
    RuntimeError: Got non-zero exit code 72 from xcrun. Output was: xcrun: error: unable to find utility "coremlcompiler", not a developer tool or in PATH
    

    I tried checking online but no help. Can you please guide me related to this problem?

    opened by r4ghu 3
  • [Question] Where are the conversion parameters come from?

    [Question] Where are the conversion parameters come from?

    Hi! This project helped me a lot, thank you!

    I'm new to machine learning and I have a question about coreml.py. It specifies some parameters to the convert function. Where are these parameters come from? What do these actual values (like 0.017) mean? Especially, scale, is_bgr, red_bias, green_bias and blue_bias. It will be very helpful if anyone could give me some information about it. Thank you✨

    scale = 0.017
    
    coreml_model = coremltools.converters.caffe.convert(
        ('mobilenet.caffemodel', 'mobilenet_deploy.prototxt'),
        image_input_names='data',
        is_bgr=True, image_scale=scale,
        red_bias=-123.68*scale, green_bias=-116.78*scale, blue_bias=-103.94*scale,
        class_labels='synset_words.txt')
    
    opened by yoching 2
  • Why is Keras necessary?

    Why is Keras necessary?

    Could you explain why it's necessary to download Keras? It looks like all you are doing is using coreml's caffe converter, which I'm under the impression doesn't rely on Keras in any way.

    opened by micahprice 1
  • Getting the output from intermediate layers in the mobilenetv2 network

    Getting the output from intermediate layers in the mobilenetv2 network

    @hollance

    I am trying to get the intermediate layer(add_node) output and merge it into the existing model outputs (confidences and coordinates)

    Experiments :

    • Was able to get the add output from the ssd model, also passed this output as an input to decode model stage where i just add a dummy permute node.
    • In the NMS model, it has a fixed set of inputs and outputs set by (confidenceInputFeatureName, confidenceOutputFeatureName ..etc) didn't find anything to forcefully create a new input as its set by the NMS_suppression.proto file
    • So thought of creating a new model part that takes input as the NMS outputs(confidences and coordinates) and the previous decode layer add_output.

    with this the coreml model is created as attached below. Screenshot 2021-05-21 at 10 40 11 AM

    But while loading it on through python it gives

    an error saying: RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Error reading protobuf spec. validator error: Pipeline: Input 'confidence' of model 'CoreML.Specification.ModelDescription' does not match the type previously specified by the pipeline input or the output of a previous model.". predict_error

    I am assuming this error is prompted bcoz the new model created is taking the input, not from the intermediate layer. Can we add a dummy node in the NMS model to bypass this ... any thoughts on this will be helpful or even is this the correct way of doing it.

    I have attached the coreml and convert pythod code used

    Archive.zip

    opened by letdivedeep 2
  • Model looks unoptimised

    Model looks unoptimised

    Screenshot 2020-03-18 at 18 33 00
    1. BN can be fused
    2. batchnorm and scale are separated layers here this is because of bad Caffe implementation.

    BTW is there any app where we just can benchmarks some other pertained classification models on iPhone?

    opened by mrgloom 1
  • Get wrong result after converting the model to coreML

    Get wrong result after converting the model to coreML

    Hello! I have used the caffe model you provided to get a semantic segmentation(two classes) model (just change to model into a FCN model). Then I convert the caffe model to coreML model. I can get reasonable result on iphone X but the result is not so good that is different from the result I get from caffe model on Ubuntu. Have you met problems like this? Is there any difference between caffe and coreML that matters? I am confused with it and thanks for you reply.

    opened by huangxf14 6
  • [Question] How does MobileNet.mlmodel compare to VGG16.mlmodel

    [Question] How does MobileNet.mlmodel compare to VGG16.mlmodel

    Hi! Nice work! I'm interested in how this MobileNet.mlmodel compares to the ones provided by Apple on their download page. Specifically how it compares to the VGG16 model which I've been using.

    Generally I'm on a quest to find the best (biggest) object classification model to use in my app. Maybe you have some useful suggestions?

    Anyway, thanks for providing this sample project!

    opened by 0xPr0xy 5
Owner
Matthijs Hollemans
Matthijs Hollemans
TextDetection-CoreML - This project is Text Detection on iOS using Vision built-in model

This project is Text Detection on iOS using Vision built-in model. If you are interested in iOS + Machine Learning, visit here yo

tucan9389 61 Nov 17, 2022
Real-Time image recognition for iOS with Vision(CoreML) and InceptionV3

Kesan-ML-iOS Real-Time image recognition for iOS with CoreML and InceptionV3  Test Flight Description Real-Time image recognition Integrating app wit

Ikmal Azman 3 Jun 26, 2022
PyTorch to CoreML: Writing custom layers with Metal

pytorch-coreml-custom-layer-example PyTorch to CoreML: Writing custom layers with Metal Convert PyTorch model cd Convert python3 -m pip install -r req

Aleksandr Ovechko 7 Sep 29, 2022
Converted CoreML Model Zoo.

CoreML-Models Converted CoreML Model Zoo. CoreML is a machine learning framework by Apple. If you are iOS developer, you can easly use machine learnin

MLBoy 483 Jan 5, 2023
Convert Torch7 models into Apple CoreML format

Convert Torch7 models into Apple CoreML format. Short tutorial This tool helps convert Torch7 models into Apple CoreML format which can then be run on

Prisma Labs 377 Nov 11, 2022
A CoreML models that detects nudity in a picture

Nudity Detection for CoreML Description This is the OpenNSFW dataset implemented in Apple's new framework called CoreML. The OpenNSFW dataset can pred

Philipp Gabriel 99 Oct 13, 2022
A CoreML model which classifies images of food

Food101 for CoreML Description This is the Food101 dataset implemented in Apple's new framework called CoreML. The Food101 dataset can predict foods f

Philipp Gabriel 133 Nov 17, 2022
Predict handwritten digits with CoreML

MNIST for CoreML (CNN) Description This is the MNIST dataset implemented in Apple's new framework CoreML. The MNIST dataset can predict handwritten (d

Philipp Gabriel 63 Nov 17, 2022
ImageClassification - Usage The MobileNetv2 CoreML Model with StoryBoard

Image Classification with The MobileNetV2 MobileNetV2: Inverted Residuals and Li

Hamit SEYREK 0 Jun 25, 2022
Gloth - Generates a CoreML Word Tagger Model trained on interactive fiction commands

Gloth Gloth: Fold dough 83 times Generates a CoreML Word Tagger Model trained on

Chris Sessions 0 Feb 12, 2022
Swift framework for document classification using a Core ML model.

DocumentClassifier Overview DocumentClassifier is a Swift framework for classifying documents into one of five categories (Business, Entertainment, Po

Todd Kramer 40 Nov 15, 2022
Text Classifier App for iOS, powered by Apple Vision & CreateML framework

Text Classifier App for iOS, powered by Apple Vision & CreateML framework

Ikmal Azman 2 Sep 15, 2022
A demo for iOS machine learning framework : Core ML

CoreMLDemo A demo for iOS machine learning framework : Core ML Only Xcode9 and above are supported. Model Places205-GoogLeNet comes from [Apple Machin

null 32 Sep 16, 2022
Demo of using TensorFlow Lite on iOS

TensorFlowLiteiOS Demo of using TensorFlow Lite on iOS Use the image classification model mobilenet_quant_v1_224. This is an excerpt and arrangement o

MLBoy 4 Jan 27, 2022
The example of running Depth Prediction using Core ML

DepthPrediction-CoreML This project is Depth Prediction on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you can see v

tucan9389 113 Nov 17, 2022
The example project of inferencing Semantic Segementation using Core ML

SemanticSegmentation-CoreML This project is Object Segmentation on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you c

tucan9389 248 Dec 17, 2022
This project is Text Recognition using Firebase built-in model on iOS

TextRecognition-MLKit This project is Text Recognition using Firebase built-in model on iOS. If you are interested in iOS + Machine Learning, visit he

tucan9389 80 Nov 17, 2022
The example project of inferencing Pose Estimation using Core ML

This project is Pose Estimation on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you can see various DEMOs. 한국어 README

tucan9389 636 Dec 19, 2022
Photo Assessment using Core ML and Metal.

PhotoAssessment Photo Assessment (i.e. quality score) using Core ML and Metal. ?? Article 使用 Metal 和 Core ML 评价照片质量 Parallel Computation using MPS ??

杨萧玉 59 Dec 26, 2022