takeaway


A few months ago, I started to get into machine learning. Faced with a bunch of English videos and math basics, I was finally able to try machine learning on SideProject.

Looking back on the road ahead, I found that I was groping through a path of many pits and twists in the woods. But when I arrived here, I found that it was not so difficult and time-consuming. As a lesson from the past, I would like to share my experience of these months with the majority of programmers who have just started or are planning to start, as well as those who have given up from the beginning.

This series of articles for anyone interested in machine learning, without the mathematical basis, and from the perspective of no basis and practice, let everybody can understand the minimalist in a few days time use process of machine learning, and applied to the quality of the work or personal project, solve some simple but complex practical problems, Thus lay a foundation for further in-depth study.

directory


  • The basic theory
    • About Machine Learning
    • What is machine learning
    • Machine learning process
    • Machine learning classification
  • Hello World
    • Environment set up
    • Learning goals
    • Data preparation
    • Training model
    • Save the model
    • The point inspection
  • First App
    • Learning goals
    • Model transformation
    • Model import
    • Start coding
    • The results
  • conclusion
    • Review the main points
    • What I missed
    • So what happens next?





A dark PDF version can be downloaded here:

PDF (1.7 MB) step_by_step_machine_learning_v1. 0.0 _by_AntScript.





The basic theory


About Machine Learning

Looking back at history, when science and technology developed to a certain extent, it was likely to have a significant impact on the development and transformation of society.

  • In the first scientific and technological Revolution, with the development of steam engine, coal mining, steel and other technologies, human beings replaced human and animal production with machines on a large scale.
  • The second scientific and technological revolution, with the large-scale application of electricity, greatly promoted the development of productive forces;
  • The third scientific and technological revolution, with the development of information technology, mankind has entered the digital information age.

The current state of academic research and hardware capabilities may well make machine learning the beginning of the fourth technological revolution.

Once skilled craftsmen, in front of the quality and efficiency of automatic equipment can only be frustrated; AlphaGo can beat the best human in the world of Go in just three days.

So the essence of machine learning, like the steam engine, like electricity, is a technology, a technology that can extend human limitations.

For example, driving a car, you do not need to understand all the principles of the car, as long as you learn driving skills, with a driver’s license, you can use the car to break their own speed and physical limitations, in a short time to reach the distance before walking can not reach; Machine learning is the same, you don’t need to learn all the relevant theoretical knowledge, as long as you master certain concepts and processes, you can use it to break the limits of your brain, to produce solutions to problems that previously were beyond your own intelligence.

Of course, drivers who can repair cars can find problems and fix them when they go wrong, and these drivers who can repair cars later become experts in cars. To become an expert in machine learning, one must not only learn how to use it, but also have a deep understanding of how it works and be able to adapt and improve it.

This paper is committed to understanding how to use machine learning without considering the principle behind it. On the premise of not using any image recognition technology, through the development of a handwritten number recognition App, we can feel more possibilities brought by machine learning.

What is machine learning

In abstract terms, machine learning is the process of analyzing old data and automatically deriving methods that can predict outcomes under new conditions.

thinking

The differences between machine learning and human learning and their advantages and disadvantages.

Machine learning process

Let’s think about how we humans learn:

A photographer of natural scenery, photographing in nature all the year round. After years of observation and accumulation, he became a good weather forecaster.

How did computers become expert weather forecasters? Input a large number of historical data, through analysis and comparison, found the law, the weather prediction results are almost always close, accuracy is higher than the photographer.

thinking

What they have in common is the accumulation of experience, while the difference is that it takes years or even decades for a human to become an expert in a certain field, while a machine can reach or surpass a human in a very short period of time if it has enough data.

Machine learning classification

For now, there are only two types of machine learning to worry about: prediction and classification.

classification

The result of a classification is discrete, or enumeration type, for example, an image that identifies 1 cat, 2 dog, 3 neither, and the result has 3 enumerations, so it’s a classification

To predict

In contrast to categorization, prediction (or linear regression) results in a continuous prediction of the price of an airline ticket at some point in the range, such as a given date and flight, which results in a point on the open range from 0 to infinity (theoretically).

conclusion

Let’s review the process of machine learning

  • First, enough data needs to be collected;
  • Then, the computer needs some way to analyze and learn from the data;
  • Finally, machines have the ability to predict or classify.

Are the following questions classification or prediction?

  • Determine whether a user will buy a product based on their browsing history and purchase history.
  • Automatically tag pictures uploaded by users.
  • Forecast for tomorrow’s PM2.5 index

The next chapter will begin the practice of machine learning, which requires elementary programming skills.




Hello World


Environment set up

Anaconda

Anaconda is a very convenient data analysis Python integration tool/environment. It contains most of the data analysis tools/packages, supported by Mac, Windows, and Linux. We only need to install this one software.

If you’re already familiar with Python development, skip the next section.

Difference, download Anaconda (not clear version suggested Python3.6 version installed,) www.anaconda.com/download

For Mac, use the default Settings. For Windows, manually select environment variables.

When finished, run Anaconda Navigator.

Running Jupyter Notebook will automatically open a local server web page to the AnacondaProjects folder.

Create a new Python3 file:

The newly opened page is an interactive Python programming environment where you can conveniently run code and output results in real time.

Everything is ready to start coding, the following operations are Mac environment, the rest of the environment is similar, if there is a difference please Google.

Learning goals

In this section, we first train a model that can recognize handwritten numbers, and then save the model.

This is the first step in a simplified set of processes for applying machine learning to practical applications. It is also the most important step for applying machine learning to practical applications. It gives us a basic understanding of one of the most important concepts in machine learning: training.

Data preparation

You can type the following code block by block in an interactive environment, Shift + Enter to run.

In the previous chapter, the first step in machine learning was to collect data, and we used this set of data already collected.

Import function library

import matplotlib.pyplot as plt
from sklearn import datasets, svm
Copy the code
  • Matplotlib. pyplot: Used to display images
  • Sklearn. datasets: contains a large number of machine learning datasets of various types
  • SVM: Support Vector Machine (SVM), an algorithm in Machine learning
  • Sklearn. metrics: Used to evaluate the accuracy of the model

Load the data

digits = datasets.load_digits()
Copy the code
  • Load_digits () : A data set of handwritten digits contained in sklearn, which can be considered the Hello World of machine learning

Data and information

  • Images: an 8×8 matrix two-dimensional array that stores the image values of handwritten digits
  • Data: Amortizes the matrix in images into a one-dimensional array of 64 elements
  • Target: The number represented by each matrix in images, 0-9
  • Target_names: Label of target

You can familiarize yourself with the structure of the data by printing it line by line

len(digits.images)
digits.images[5]
digits.data[5]
digits.target[5]
digits.target_names[5]
Copy the code

Next we define a function that graphically displays the data

def show_image(image, label):
    plt.figure(1, figsize=(3, 3))
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title(label)
    plt.show()
Copy the code

Now we can use this function to make it more intuitive to display the data

show_image(digits.images[5], digits.target[5])
Copy the code

Training model

With the data ready, we can start training the model

# train_test_split # X_train # y_train # train_test_split # X_train # y_train Type of classified images for training # X_train: image data for testing # y_train: X = digits. Data y = digits_dataset. Target X_train, X_test, y_train, y_test = train_test_split(X, y, Test_size = 0.33, SVC(Support Vector Classification) classclassifier = SVM.SVC(gamma=0.001) classclassifier = SVM Classifier. Fit (X_train, y_train) classifier. Predicted = classifier. Predict (y_test[:20]) print(predicted) Print (testing_target[:20]) # 20 print(testing_target[:20]) # 20 print(testing_target[:20]Copy the code

  • Training data and test data cannot overlap, just like we have done a lot of real questions in the usual training, but the questions in the exam are already done, which cannot well reflect our real level

Save the model

The training data we used above is a very small data set, and the model can be trained in a short time. However, in the real environment, the training data is usually very large, and the training cannot be completed in a short time. Some of them need dozens of minutes, hours, or even days. So we need to save the trained model for future use.

Sklearn already includes the module Joblib for saving models

from sklearn.externals import joblib
Copy the code

save

joblib.dump(classifier, 'digits.pkl')
Copy the code

read

classifier = joblib.load('digits.pkl')
Copy the code

The point inspection

  • How do I use data sets in SkLearn
  • How to use data to train models
  • How to save the trained model




First App


Learning goals

In this section, the model exported in the previous section will be used in our iOS project to develop an App that can recognize handwritten numbers through camera photography.

The examples developed in this section are based on iOS, so if you’re a developer on another platform, check out the resources and apply the model to a platform you’re familiar with.

Model transformation

Starting with iOS11, Apple’s CoreML library supports machine learning, making it easy to integrate trained models into Xcode projects, but only after converting trained models into xcodo-supported formats (.mlmodel).

Using Python2

The model transformation uses coremlTools, a Python-based tool provided by Apple, but currently only supports Python2. If you use 3, you need to install a module that supports 2 and run it directly from the command line:

python2 -m pip install ipykernel
python2 -m ipykernel install --user
Copy the code
Refer to the link Stackoverflow.com/questions/3…

After the installation is complete, create a new Python2 file:

Since we used the Python3 exported model in the previous section, Python2 cannot read it directly, so we need to do it again in Python2 (don’t copy it directly, write it yourself) :

from sklearn import datasets, SVM from sklearn.model_selection import train_test_split digits = datasets.load_digits() # # X_train # y_train # X_train # y_train # X_train # y_train X = digits. Data y = digits. Target X_train, X_test, y_train, y_test = train_test_split(X, y, Test_size = 0.33, SVC(Support Vector Classification) classifiers = SVM.SVC(gamma=0.001 Score = classifier. Score (X_test, classifier. Print (score) print(classifier, 'digits.pkl')Copy the code

Why not use Python2 in the first place, you might ask?

  • Rewrite the code for review
  • Check your resources to learn more about Python2 and 3
  • Python3 is the future, and maybe coremltools already supports it by the time you see it

conversion

Start by installing CoremlTools on the command line

pip install -U coremltools
Copy the code

Now we can convert the saved model to a format supported by Xcode:

Externals import joblib import coremltools class = joblib.load('digits. PKL ') There are 64 parameters, so the parameter names we generated are # feature_0, feature_1... Feature_63 Feature_names = ["feature_"+ STR (I) for I, x in enumerate(X_train[0])] And preserved coreml_model = coremltools. Converters. Sklearn. Convert (classifier, feature_names, "digit") coreml_model.save("digits.mlmodel")Copy the code

You can now see the digits. mlModel File in the list. Click on it and it will open on a new page

  • Check the digits.mlmodel file, which is about 400Kb
  • Don’t right click on the list to save, it’s only 8Kb (I spent a lot of time on this pit)
  • Can also be coreml_model. Save when using a local path stored directly (for example, / Users / [Your Name] / Downloads/who mlmodel)

Model import

Create a new iOS project in Xcode and drag the files saved in the previous section into the project. Xcode will automatically parse and list the interface of the model:

Outputs a 64-bit integer of type Int64 (0 to 9).

The input is feture0-feature_63, a total of 64 Double arguments. You might think: a function with 64 arguments would take a long time to write parameter names.

Yes, so think about how to optimize the parameters to make it more convenient to use, for now we will use.

Start coding

What we need to predict is a picture, and the input parameter is 64 doubles. These 64 doubles are the gray value of each point in the 8*8 bitmap. Therefore, we need to convert the picture into 64 doubles first.

Here’s what I think:

  • First take a picture through the camera
  • Reduce the image to 64 pixels, 8 by 8
  • Process the photo into grayscale
  • Convert the gray value of each pixel to a Double
  • You end up with 64 doubles to input

Viewcontroller.swift: viewController.swift: viewController.swift: viewController.swift

Import SwifterSwift // Use SwifterSwift to simplify color operation // Pull gray value from a UIColor // 0.0 -> whitest 1.0 -> blackest extension UIColor {func getGray() -> Double {let RGB = self.rgbComponents; Var gray = (rgb.red. Double * 0.3 + rgb.green. Double * 0.59 + rgb.blue. Double * 0.59 + rgb.blue 0.11).int // Set with a threshold, If gray >= 200 {gray = 255} return (255-gray.double) / 255}} // Remove the UIColor extension from UIImage UIImage { func getPixelColor(pos: CGPoint) -> UIColor? { guard let pixelData = self.cgImage?.dataProvider?.data else { return nil } let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData) let pixelInfo: Int = ((Int(self.sie.width) * Int(pos.y)) + Int(pos.x)) * 4 let r = CGFloat(data[pixelInfo])/CGFloat(255.0) let g = CGFloat(data[pixelInfo+1])/CGFloat(255.0) let b = CGFloat(data[pixelInfo+2])/CGFloat(255.0) let a = CGFloat(data[pixelInfo+3])/CGFloat(255.0) return UIColor(red: r, green: g, Blue: b, alpha: a)}}Copy the code
// Encapsulate the model's prediction method so that it is convenient to call extension digits {// Encapsulate the model's prediction method with an 8*8 UIImage as the parameter, which is convenient to call func prediction(image: UIImage) -> Int64? { var data: [Double] = [] for y in 0.. <Int(image.size.height) { for x in 0.. <Int(image.size.width) { guard let c = image.getPixelColor(pos: CGPoint(x: x, y: y)) else { break } data.append(Double(15 - Int(c.getGray().double/16))) } } return prediction(data: Func prediction(data: [Double]) -> Int64? Func prediction(data: [Double]) -> Int64? { guard data.count == 64 else { return nil } if let result = try? prediction( feature_0: data[0], feature_1: data[1], feature_2: data[2], feature_3: data[3], feature_4: data[4], feature_5: data[5], feature_6: data[6], feature_7: data[7], feature_8: data[8], feature_9: data[9], feature_10: data[10], feature_11: data[11], feature_12: data[12], feature_13: data[13], feature_14: data[14], feature_15: data[15], feature_16: data[16], feature_17: data[17], feature_18: data[18], feature_19: data[19], feature_20: data[20], feature_21: data[21], feature_22: data[22], feature_23: data[23], feature_24: data[24], feature_25: data[25], feature_26: data[26], feature_27: data[27], feature_28: data[28], feature_29: data[29], feature_30: data[30], feature_31: data[31], feature_32: data[32], feature_33: data[33], feature_34: data[34], feature_35: data[35], feature_36: data[36], feature_37: data[37], feature_38: data[38], feature_39: data[39], feature_40: data[40], feature_41: data[41], feature_42: data[42], feature_43: data[43], feature_44: data[44], feature_45: data[45], feature_46: data[46], feature_47: data[47], feature_48: data[48], feature_49: data[49], feature_50: data[50], feature_51: data[51], feature_52: data[52], feature_53: data[53], feature_54: data[54], feature_55: data[55], feature_56: data[56], feature_57: data[57], feature_58: data[58], feature_59: data[59], feature_60: data[60], feature_61: data[61], feature_62: data[62], feature_63: data[63] ) { return result.digit } return nil } }Copy the code
// UI // Use a button to open the camera and take a photo. After the photo is taken, process the image into 8*8 pixels and display it. Then use a Label to display the predicted results. UIButtonType.roundedRect) let resultLabel = UILabel(text: "no result") let imageView = UIImageView() override func viewDidLoad() { super.viewDidLoad() pickImageButton.frame = CGRect(x: 0, y: 0, width: 100, height: 50) pickImageButton.setTitle("Pick Image", for: .normal) view.addSubview(pickImageButton) pickImageButton.addTarget(self, action: #selector(startPickImage(sender:)), for: UIControlEvents.touchUpInside) resultLabel.frame = CGRect(x: 300, y: 200, width: 100, height: 50) view.addSubview(resultLabel) imageView.frame = CGRect(x: 50, y: 300, width: 256, height: 256) imageView.contentMode = .scaleAspectFill view.addSubview(imageView) }Copy the code
// Extension ViewController: UIImagePickerControllerDelegate { // pickImageButton Action @objc func startPickImage(sender: Any) {takePicture ()} / / photo func takePicture () {guard UIImagePickerController. IsSourceTypeAvailable (. Camera) else { resultLabel.text = "camera not available" return } let cameraPicker = UIImagePickerController() cameraPicker.delegate = self cameraPicker.sourceType = .camera cameraPicker.allowsEditing = false present(cameraPicker, animated: True, completion: nil)} / / photo cancel agent method func imagePickerControllerDidCancel (_ picker: UIImagePickerController) {dismiss(animated: true, completion: nil)} func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { dismiss(animated: true, completion: nil) guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { resultLabel.text = "no image" Return} / / resizing images into 8 * 8 UIGraphicsBeginImageContext (CGSize (width: 8, height: 8)) image. The draw (in: CGRect (x: 0, y: 0, width: 8, height: 8)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() imageView.image = newImage guard let targetImage = newImage else { resultLabel.text = "new image is nil" return } let digitsModel = digits() guard let result = digitsModel.prediction(image: Else {resultLabel.text = "predict failed" return} resultLabel.text = "\(result)  in 0.. <Int(targetImage.size.height) { for x in 0.. <Int(targetImage.size.width) { guard let gray = targetImage.getPixelColor(pos: CGPoint(x: x, y: y))? .getGray() else { break } let color = UIColor( red: 255 - (gray * 255).int, green: 255 - (gray * 255).int, blue: 255 - (gray * 255).int ) images[y * 8 + x].backgroundColor = color } } } }Copy the code

The results

We can write a few numbers by hand and take photos to make predictions. But may be due to the picture processing is not good enough, the actual test down the effect is not ideal. So directly in Jupyter will test data displayed on the screen, and then use mobile phone photos for testing.

show_image(digits.images[1501], digits.target[1501])
show_image(digits.images[1502], digits.target[1502])
show_image(digits.images[1503], digits.target[1503])
Copy the code

Using App for photo prediction, it can be seen that although the restored image has some deviation, the result is still very accurate:








conclusion


After all this learning and practice, you may be tempted to use machine learning to do something. I hope you can keep your interest and thirst for knowledge in machine learning and continue your exploration and learning with confidence.

Review the main points

  • There are currently two categories of machine learning that we know of: prediction and classification, and classification is sometimes referred to as prediction. For example, in the App above, the so-called “predict the numbers on the picture” is actually to classify the numbers on the picture from 0 to 9. Being able to accurately identify the type of problem is very important for the following steps.
  • Python environment setup, and the differences between Python2 and Python3.
  • We used the Machine learning library SKKit-learn (SciKit-learn), which is a packaged library for model training with little code.
  • The algorithm used for training is SVC (Support Vector Classification) Support Vector machine classifier. The first 2/3 data are used for training, and the last 1/3 data are tested, and the test accuracy is output, so as to evaluate the model.
  • The training of the model takes time, and some training of massive data or intensive calculation may take a long time. Trained models can be saved for future use.
  • A trained model can be used on multiple platforms, but depending on the format supported by the platform, the model may need to be transformed.

What I missed

As a veteran of machine learning, you might say, “This article is clickbait. Machine learning is great. !” Lao Bird is right, this article is not enough to be an introduction, not only skips over a lot of principles, but also leaves out a lot of details:

  • In addition to Sklearn, there are other commonly used machine learning libraries, such as Keras, which is also relatively easy to use, TensorFlow from Google, Theano, PyTorch, etc.
  • The data we use to train the model is ready-made, but in practice there are data processing steps such as data collection, data cleansing, and data normalization to carry out the following training.
  • In addition to SVC, there are many other algorithms in machine learning, each with its own advantages and disadvantages and scope of application.
  • Parameter adjustment is an indispensable step in model training in practical application. By adjusting the input parameters and observing the accuracy of model output, a relatively more accurate model can be trained.

So what happens next?

  • There are also two data sets in Sklearn that are highly used for entry: Boston housing price forecast and Iris classification. By consulting data and completing App development, sklearn can predict housing price by inputting house information and classify flowers by inputting iris information.
  • Try solving a simple problem at work or on a personal project using the machine learning knowledge learned above.
  • Self research materials to understand more commonly used concepts in machine learning: overfitting, underfitting, supervised learning, unsupervised learning, loss function, optimization function, etc.
  • Learn some common machine learning algorithms. Resources: Introduction to mainstream machine learning algorithms and analysis of their strengths and weaknesses
  • Learn about deep learning and how it relates to machine learning.
  • There are likely to be new articles on the application of machine learning in real projects and introduction to deep learning. You can subscribe to antscript.com RSS feed or subscribe to updates via email via this link.
  • Share or retweet this post.
  • If you find any errors, please contact [email protected].