Coder Social home page Coder Social logo

heightweightcoremlkerastest's Introduction

HeightWeightCoreMLKerasTest

This is a super simple end to end test of Keras, TensorFlow and CoreML.

It create an incredible simple linear regression model from some height/weight statistics and export the model to CoreML in order to use it as a function inside a sample iOS App. Of course no real reason to use ML in a scenario like this. Just a simple test with documented step by step instructions.

https://medium.com/@JMangia/super-simple-end-to-end-test-of-keras-tensorflow-and-coreml-f247ab73fb42

Step by step instruction to create a very simple ML model using Keras/TensorFlow, import it on CoreML using CoreMLConversionTool and using it locally on a simple iOS App

Download and Install Anaconda Python

https://www.continuum.io/downloads

Clone this repository on your local computer

git clone https://github.com/JacopoMangiavacchi/HeightWeightCoreMLKerasTest.git

Create the Keras, TensorFlow, Python, CoreML environment

conda env create

This environment is created based on the environment.yml file for iinstalling Python 2.7, TensorFlow 1.1, Keras 2.0.4, CoreMLTools 0.6.3, Pandas and other Python usefull packages:

name: KerasTensorFlowCoreML
channels:
    - !!python/unicode
    'defaults'
dependencies:
    - python=2.7
    - pip==9.0.1
    - numpy==1.12.0
    - jupyter==1.0
    - matplotlib==2.0.0
    - scikit-learn==0.18.1
    - scipy==0.19.0
    - pandas==0.19.2
    - pillow==4.0.0
    - seaborn==0.7.1
    - h5py==2.7.0
    - pip:
        - tensorflow==1.1.0
        - keras==2.0.6
        - coremltools==0.6.3

Wait for the environment to create.

Activate the environment (Mac/Linux)

source activate KerasTensorFlowCoreML

Activate the environment (Windows)

activate KerasTensorFlowCoreML

Check that your prompt changed to

(KerasTensorFlowCoreML) $

Launch Jupyter Notebook

jupyter notebook

Open your browser to

http://localhost:8888

Create a basic Model with Keras/TensorFlow and export it with CoreMLTools

Open createModel.ipynb in your Jupyter browsing session

Execute any cells in order to create, save and export the Keras Model using CoreML Exporting Tools

The Basic CoreML Model will be saved in the current folder as HeightWeight_model.mlmodel

Build the iOS sample project

Open the iOS sample project HeightWeightCoreMLKerasTest.xcodeproj in XCode 9 and Build and Test the App on your iPhone or Simulator

The iOS sample project use a Swift wrapper class (HeightWeightModelWrapper.swift) to incupsulate all CoreML API and simplify the usage of CoreML Multi Array and implement some utility like convert from Centimeters to Inches and Pounds to Kilos.

Using this simple model from Swift to predict Height from a given Weight is as simple as executing this two line of code!

let modelWrapper = HeightWeightModelWrapper()
let resultInKilos = modelWrapper.predictHeight(cm: input)

Extend the basic Model adding Parameters for Male/Female

Open extendModel.ipynb in your Jupyter browsing session

Execute any cells in order to create, save and export the Keras Model using CoreML Exporting Tools

The Extended CoreML Model will be saved in the current folder as HeightWeightExtended_model.mlmodel

Build the iOS Extended sample project

Open the iOS Extended sample project HeightWeightCoreMLKerasTest.xcodeproj in XCode 9 and Build and Test the App on your iPhone or Simulator

The iOS sample project extend the Swift wrapper class (HeightWeightModelWrapper.swift) to incupsulate all CoreML API and simplify the usage of CoreML Multi Array and implement some utility like convert from Centimeters to Inches and Pounds to Kilos.

Using this simple model from Swift to predict Height from a given Weight is as simple as executing this two line of code!

let modelWrapper = HeightWeightModelWrapper()
let resultInKilos = modelWrapper.predictHeight(cm: input, sex: .Female)

Delete the Environment

Free your storage cleaning the Python, Keras, TensorFlow, CoreMLTools environment: source deactivate conda remove -y -n KerasTensorFlowCoreML --all

The CoreML models will not be deleted and they will remain in your folder

heightweightcoremlkerastest's People

Contributors

freemansion avatar jacopomangiavacchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

heightweightcoremlkerastest's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.