CoreML 2.0|Automate Article Classification with Custom-Trained Machine Learning Models
Discover how iOS developers leverage CoreML 2.0 to train and convert machine learning models, enabling automatic article classification and seamless integration into real-world apps for improved accuracy and efficiency.
点击这里查看本文章简体中文版本。
點擊這裡查看本文章正體中文版本。
This post was translated with AI assistance — let me know if anything sounds off!
Experience iOS 12 CoreML — Use Machine Learning to Automatically Predict Article Categories, and Train the Model Yourself!
Exploring CoreML 2.0: How to Convert or Train Models and Apply Them in Real Products
Continuing from the previous article on using machine learning on iOS, this article officially begins with using CoreML.
First, a brief history: Apple released the CoreML machine learning framework (including Vision introduced in the previous article) in 2017. In 2018, they followed up with CoreML 2.0, which not only improved performance but also supported custom CoreML models.
Preface
If you have only heard the term “machine learning” but don’t know what it means, here is a simple explanation in one sentence:
“Predicting the outcome of the same event based on your past experience”
For example: When I eat an egg pancake, I add ketchup. After buying it a few times, the breakfast shop owner remembers and asks, “Handsome, ketchup?” I answer, “Yes” — the owner predicts correctly. If I answer, “No, because it’s radish cake + egg pancake” — the owner remembers and corrects the question the next time the same situation occurs.
Input data: egg pancake, cheese egg pancake, egg pancake + radish cake, radish cake, egg
Output data: Add ketchup / Do not add ketchup
Model: The Landlady’s Memory and Judgment
Actually, my understanding of machine learning is mostly limited to knowing the basic concepts and theories, but I haven’t studied it in depth. Please kindly correct me if there are any mistakes.
Speaking of which, I have to pay tribute 🛐 to the Apple gods for productizing machine learning. As long as you know the basic concepts, you can operate it without a vast knowledge base, lowering the entry barrier. I myself only felt a real connection to machine learning after implementing this example, which sparked a strong interest in the field.
Start
The first step, and the most important, is of course the “model” mentioned earlier. Where does the model come from?
There are three methods:
- Find pre-trained models online and convert them to CoreML format
Awesome-CoreML-Models This GitHub project collects many pre-trained models from others.
Model conversion can refer to the official website or online resources.
At the bottom of Apple’s Machine Learning official website, under Download Core ML Models, you can download models trained by Apple (mainly for learning or testing purposes).
Train Your Own Model Using Tools 🏆
So, what can be done?
Image Recognition 🏆
Text Content Recognition and Classification 🏆
Word Segmentation
Text Language Detection
Named Entity Recognition
For word segmentation, please refer to Natural Language Processing in iOS Apps: An Introduction to NSLinguisticTagger
Today’s Key Points — Text Content Recognition and Classification + Training Your Own Model
Simply put, 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!” and “Get $1000 shopping credit now” => “Advertisement”; “Alan sent you a message” and “Your account is about to expire” => “Important matters”
Practical Applications: Spam Detection, Tag Generation, Classification Prediction
p.s Since I haven’t figured out what to train the image recognition for, I haven’t explored it yet; interested friends can check out this article, the official GUI training tool for images is very convenient!!
Required Tools: MacOS Mojave⬆ + Xcode 10
Training Tool: BlankSpace007/TextClassiferPlayground (The official version only provides a GUI training tool for images; text training requires coding yourself. This is a third-party tool offered by an expert online.)
Prepare Training Data:
The data structure is as shown in the above image, supporting .json and .csv files.
Prepare the data 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
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":
//Remove above
[
{
"label":"",
"text":""
}
]
//Remove below
}
]
Open the recently downloaded JSON file and keep only the content inside the DATA structure.
Training Tools Used:
After downloading the training tools, click TextClassifier.playground to open the Playground
Click the red box to execute -> Click the green box to switch the View display
Drag the JSON file into the GUI tool
Open the Console below to check the training progress. Seeing the line “Test Accuracy” means the model training is complete.
If there is too much data, it will test your computer’s processing power.
After filling in the basic information, click “Save”
Save the trained model file
CoreML Model File
Your model is now fully trained! Wasn’t that easy?
Specific Training Methods:
First, perform word segmentation on the input sentence (e.g., “我想知道婚禮需要準備什麼” => “我想”, “知道”, “婚禮”, “需要”, “準備”, “什麼”), then determine its category and carry out a series of machine learning computations.
Group the training data, for example: 80% is used for training and the other 20% is used for testing and validation.
Up to 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.
{: lqip=”data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNDAwIiBoZWlnaHQ9IjgzMSI+PHJlY3Qgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0iZ3JleSIvPjwvc3ZnPg==” }
Drag and drop or add the model file (*.mlmodel) into the project
Code Section:
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)")
}
Completed!
Questions to Explore:
Can it support further learning?
Can the mlmodel file be converted to other platforms?
Can you train models on iOS?
The above three points have all been found to be unfeasible based on current information.
Conclusion:
Currently, I apply it in practical apps to predict the category when posting articles.
I only have about 100 training data points, and the current prediction accuracy is around 35%. This is mainly for experimental purposes.
— — — — —
It’s that simple—completing the first machine learning project in life; there is still a long way to go to learn how the background works. I hope this project can inspire everyone!
Reference: WWDC2018 Create ML (Part 2)
If you have any questions or feedback, feel free to contact me.
This post was originally published on Medium (View original post), and automatically converted and synced by ZMediumToMarkdown.