Machine Learning Overview

From this article, we will introduce what is machine learning and how to complete a task of machine learning with Pipcook.

The Problem Setting

In general, a learning problem considers a set of n samples of data and then tries to returns the corresponding result by given input. The following example shows how we can teach a program about Node.js books and prices:

  1. const model = Record<string, number>;
  2. const learn = (book: string, price: number) => model[book] = price;
  3. const how_much = (book: string) => model[book];
  4. // prediction without learning.
  5. how_much('Node.js in Action'); // undefined, because the program don't know nothing
  6. // learn "Node.js in Action" and "Dive into Node.js".
  7. learn('Node.js in Action', 99.0);
  8. learn('Dive into Node.js', 199.0);
  9. // prediction after learning.
  10. how_much('Node.js in Action'); // 99.0
  11. how_much('Dive into Node.js'); // 199.0

The Machine Learning problem is similar, except that we can make the computer learning more intelligent through machine learning algorithms and can give real “predictions” from unknown data, for example, The following helps you get a more expensive book name:

  1. how_much('Pipcook in Action'); // 89.0
  2. how_much('Dive into Pipcook'); // 199.0

However machine learning is not a panacea, so let’s see what problems it can solve, the following is machine learning problems fall into a few categories in given sample types(Image and Text):

Sample TypeProblem CategoryDescription
ImageImage Classificationit does classify the image to specific classes.
Image Generationit generates an image automatically.
Object Detectionit detects the given objects and returns class and position for each one.
Image Segmentationit returns the outline of given objects in pixel.
Image Clusteringit returns clusters from images.
TextText Classificationit does classify the text to specific classes.
Named Entity Recognitionit recognizes the named entity from a sentence.
Relationship Extractionit extracts the relationships between sentences.
Coreference Resolutionit does the pronouns and other referring to be connected with the right individuals.
Writing Correctionit helps to do writing correction.
Machine Translationit translates a language to target language.
Question and Answeringit generates an answer by the given question.
Text Summaryit generates a summary for your given long text.
Text Creationit generates an artwork by a given portfolio.
Text Clusteringit returns clusters from words or sentences.

So how do we achieve the above prediction in our life? Let’s take a look at the stages involved in a machine learning project:

  1. Collect samples and process them into a format from which machine learning models can learn features.
  2. Choose a machine learning model for training, generally choose different models according to different task types and scenarios.
  3. Before training the model, we need to divide the above samples into a training set and a testing set in a ratio.
  4. Then during the training stage, we input the training set into the model, so that it will learn the features from that.
  5. After the model is trained, generally use the testing set to input the trained model again to determine how effective the model is.

Training set and Testing set

Machine learning is about learning some properties of a data set and then testing those properties against another data set. A common practice in machine learning is to evaluate an algorithm by splitting a data set into two. We call one of those sets the training set, on which we learn some properties, we call the other set the testing set, on which we test the learned properties.

Loading Dataset

MNIST(Modified National Institute of Standards and Technology database) is a large database of handwritten digits:

Machine Learning Overview - 图1

Next, we will use handwritten digit recognition as an example to introduce how to complete an image classification task completely through Pipcook.

In Pipcook, we use the pipeline to completely describe a machine learning task. We use different plugins in Pipeline to provide different nodes, and then connect the different plugins through Pipeline as a whole.

In the following, we start writing a Pipeline from loading the MNIST datasets:

  1. {
  2. "plugins": {
  3. "package": "@pipcook/plugins-mnist-data-collect",
  4. "params": {
  5. "trainCount": 8000,
  6. "testCount": 2000
  7. }
  8. }
  9. }

The “@pipcook/plugins-mnist-data-collect” plugin will download data from the standard MNIST dataset, and distribute the training set and the testing set according to the ratio of 8000:2000.

Where to save the pipeline file?

Run pipcook init to create a new project, it will download plugins and their dependencies, and you will use them later. See Introduction to Pipeline for more details.

Learning

In the case of the digits dataset, the task is to predict, given an image, which digit it represents. We are given samples of each of the 10 possible classes (the digits zero through nine) on which we fit a model to be able to predict the classes to which unseen samples belong.

In Pipcook, building a model for classification is also plugins configuration.

  1. {
  2. "plugins": {
  3. "dataAccess": {
  4. "package": "@pipcook/plugins-pascalvoc-data-access"
  5. }
  6. }
  7. }

We use PASCAL VOC as the dataset format in our pipeline, the “@pipcook/plugins-pascalvoc-data-access” plugin does transfer the minist dataset in PASCAL VOC format.

PASCAL VOC is to provide standardized image data sets for object class recognition.

  1. {
  2. "plugins": {
  3. "dataProcess": {
  4. "package": "@pipcook/plugins-image-data-process",
  5. "params": {
  6. "resize": [28, 28]
  7. }
  8. }
  9. }
  10. }

Next, we use the plugin “@pipcook/plugins-image-data-process” to resize the image to 28x28 in our dataset, which is required for the next step.

  1. {
  2. "plugins": {
  3. "modelDefine": {
  4. "package": "@pipcook/plugins-tfjs-simplecnn-model-define"
  5. },
  6. "modelTrain": {
  7. "package": "@pipcook/plugins-image-classification-tfjs-model-train",
  8. "params": {
  9. "epochs": 15
  10. }
  11. },
  12. "modelEvaluate": {
  13. "package": "@pipcook/plugins-image-classification-tfjs-model-evaluate"
  14. }
  15. }
  16. }

We choose a CNN for this task of Image Classification as using the following plugins:

  • “@pipcook/plugins-tfjs-simplecnn-model-define” is to define the model.
  • “@pipcook/plugins-image-classification-tfjs-model-train” is to train the defined model from given training set.
  • “@pipcook/plugins-image-classification-tfjs-model-evaluate” is to test the trained model with the testing set.

So far, our pipeline is defined completely, and then we can train.

  1. $ pipcook run pipeline.json

Predicting

After the training is completed, we can find an output directory under the current project directory, which is our trained model and a JavaScript file index.js to predict.

  1. 📂output
  2. 📂logs
  3. 📂model
  4. 📜package.json
  5. 📜metadata.json
  6. 📜index.js

As you can see, the deploy folder is an npm package. You can integrate it into any Node.js project and use the predict() method provided by it.

  1. const predict = require('/path/to/output');
  2. const app = express();
  3. app.post('/predict', async (req, res) => {
  4. const r = await predict(req.body.image);
  5. res.json(r);
  6. });
  7. app.listen(8080);

Then start your service:

  1. $ node app.js

And send a request for prediction:

  1. $ curl -XPOST http://localhost:8080/predict -f"/path/to/your/img.png"
  2. {
  3. "result": 7
  4. }