Home Exploring iOS 12 CoreML — Automatically Predict Article Categories Using Machine Learning, Even Train the Model Yourself!
Post
Cancel

Exploring iOS 12 CoreML — Automatically Predict Article Categories Using Machine Learning, Even Train the Model Yourself!

Exploring iOS 12 CoreML — Automatically Predict Article Categories Using Machine Learning, Even Train the Model Yourself!

Explore CoreML 2.0, how to convert or train models and apply them in real products

Following the previous article on researching machine learning on iOS, this article officially delves into using CoreML.

First, a brief history: Apple released CoreML (including Vision introduced in the previous article) machine learning framework in 2017; in 2018, they followed up with CoreML 2.0, which not only improved performance but also supports custom CoreML models.

Introduction

If you’ve only heard the term “machine learning” but don’t understand what it means, here’s a simple explanation in one sentence:

“Predict the outcome of similar future events based on your past experiences.”

For example: I like to add ketchup to my egg pancake. After buying it a few times, the breakfast shop owner remembers, “Sir, ketchup?” I reply, “Yes” — the owner predicts correctly; if I reply, “No, because it’s radish cake + egg pancake” — the owner remembers and adjusts the question next time.

Input data: egg pancake, cheese egg pancake, egg pancake + radish cake, radish cake, egg

Output data: add ketchup / no ketchup

Model: the owner’s memory and judgment

My understanding of machine learning is also purely theoretical, without in-depth practical knowledge. If there are any mistakes, please correct me.

This is where I must thank Apple for productizing machine learning, making it accessible with just basic concepts and lowering the entry barrier. It was only after implementing this example that I felt a tangible connection to machine learning, sparking a great interest in this field.

Getting Started

The first and most important step is the “model” mentioned earlier. Where do models come from?

There are three ways:

  • Find pre-trained models online and convert them to CoreML format.

Awesome-CoreML-Models is a GitHub project that collects many pre-trained models.

For model conversion, refer to the official website or online resources.

  • Download pre-trained models from Apple’s Machine Learning website at the bottom of the page, mainly for learning or testing purposes.
  • Use tools to train your own model🏆

So, what can you do?

  • Image recognition 🏆
  • Text content recognition and classification🏆
  • Text segmentation
  • Language detection
  • Named entity recognition

For text segmentation, refer to Natural Language Processing in iOS Apps: An Introduction to NSLinguisticTagger

Today’s Main Focus — Text Content Recognition and Classification + Training Your Own Model

In simple terms, we provide the machine with “text content” and “categories” to train the computer to classify future data. For example: “Click to see the latest offers!”, “Get $1000 shopping credit now” => “Advertisement”; “Alan sent you a message”, “Your account is about to expire” => “Important matters”

Practical applications: spam detection, tag generation, classification prediction

p.s. I haven’t thought of any practical uses for image recognition yet, so I haven’t researched it; interested friends can check this article, the official site provides a convenient GUI training tool for images!!

Required Tools: MacOS Mojave⬆ + Xcode 10

Training Tool: BlankSpace007/TextClassiferPlayground (The official tool only provides GUI training tools for images, for text you need to write your own; this is a third-party tool provided by an expert online)

Preparing Training Data:

Data structure as shown above, supports .json, .csv files

Data structure as shown above, supports .json, .csv files

Prepare the data to be used for training, here we use Phpmyadmin (Mysql) to export the training data

1
SELECT `title` AS `text`,`type` AS `label` FROM `posts` WHERE `status` = '1'

Change the export format to JSON

Change the export format to JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
  {"type":"header","version":"4.7.5","comment":"Export to JSON plugin for PHPMyAdmin"},
  {"type":"database","name":"db"},
  {"type":"table","name":"posts","database":"db","data":
    // Delete above
    [
      {
         "label":"",
         "text":""
      }
    ]
    // Delete below
  }
]

Open the downloaded JSON file and keep only the content within the DATA structure

Using the Training Tool:

After downloading the training tool, click TextClassifer.playground to open Playground

Click the red box to execute -> click the green box to switch View display

Click the red box to execute -> click the green box to switch View display

Drag the JSON file into the GUI tool

Drag the JSON file into the GUI tool

Open the Console below to check the training progress, seeing "Test Accuracy" means the model training is complete

Open the Console below to check the training progress, seeing “Test Accuracy” means the model training is complete

If there is too much data, it will test your computer’s processing power.

Fill in the basic information and click "Save"

Fill in the basic information and click “Save”

Save the trained model file

CoreML model file

CoreML model file

At this point, your model is already trained! Isn’t it easy?

Specific Training Method:

  1. First, segment the input sentence (I want to know what needs to be prepared for the wedding => I want, to know, wedding, needs, to prepare, what), then see what its classification is and perform a series of machine learning calculations.
  2. Divide the training data into groups, for example: 80% for training and 20% for testing and validation

At this point, most of the work is done. Next, just add the model file to the iOS project and write a few lines of code.

![Drag/drop the model file (.mlmodel) into the project](/assets/793bf2cdda0f/14Uc1elBmhEnQ-J8z_RIQHQ.png)

Drag/drop the model file (*.mlmodel) into the project

Code Part:

1
2
3
4
5
6
7
import CoreML

//
if #available(iOS 12.0, *),let prediction = try? textClassifier().prediction(text: "Text content to predict") {
    let type = prediction.label
    print("I think it is...\(type)")
}

Done!

Questions to Explore:

  1. Can it support further learning?
  2. Can the mlmodel model file be converted to other platforms?
  3. Can the model be trained on iOS?

The above three points, based on the information currently available, are not feasible.

Conclusion:

Currently, I am applying it in a practical APP to predict the classification when posting articles.

[Wedding App](https://itunes.apple.com/tw/app/%E7%B5%90%E5%A9%9A%E5%90%A7-%E4%B8%8D%E6%89%BE%E6%9C%80%E8%B2%B4-%E5%8F%AA%E6%89%BE%E6%9C%80%E5%B0%8D/id1356057329?ls=1&mt=8){:target="_blank"}

Wedding App

I used about 100 pieces of training data, and the current prediction accuracy is about 35%, mainly for experimental purposes.

— — — — —

It’s that simple to complete the first machine learning project in your life; there is still a long way to go to learn how the background works. I hope this project can give everyone some inspiration!

References: WWDC2018 Create ML (Part 2)

If you have any questions or comments, feel free to contact me.

===

本文中文版本

===

This article was first published in Traditional Chinese on Medium ➡️ View Here



This post is licensed under CC BY 4.0 by the author.

Exploring Vision — Automatic Face Detection and Cropping for Profile Pictures (Swift)

Enhance User Experience by Adding 3D TOUCH to Your iOS APP (Swift)