Book Image

Hands-On Machine Learning with C++

By : Kirill Kolodiazhnyi
Book Image

Hands-On Machine Learning with C++

By: Kirill Kolodiazhnyi

Overview of this book

C++ can make your machine learning models run faster and more efficiently. This handy guide will help you learn the fundamentals of machine learning (ML), showing you how to use C++ libraries to get the most out of your data. This book makes machine learning with C++ for beginners easy with its example-based approach, demonstrating how to implement supervised and unsupervised ML algorithms through real-world examples. This book will get you hands-on with tuning and optimizing a model for different use cases, assisting you with model selection and the measurement of performance. You’ll cover techniques such as product recommendations, ensemble learning, and anomaly detection using modern C++ libraries such as PyTorch C++ API, Caffe2, Shogun, Shark-ML, mlpack, and dlib. Next, you’ll explore neural networks and deep learning using examples such as image classification and sentiment analysis, which will help you solve various problems. Later, you’ll learn how to handle production and deployment challenges on mobile and cloud platforms, before discovering how to export and import models using the ONNX format. By the end of this C++ book, you will have real-world machine learning and C++ knowledge, as well as the skills to use C++ to build powerful ML systems.
Table of Contents (19 chapters)
1
Section 1: Overview of Machine Learning
5
Section 2: Machine Learning Algorithms
12
Section 3: Advanced Examples
15
Section 4: Production and Deployment Challenges

To get the most out of this book

To be able to compile and run the examples included in this book, you will need to configure a particular development environment. All code examples have been tested with the Arch and Ubuntu 18.04 Linux distributions. The following list outlines the packages you'll need to install on the Ubuntu platform:

  • build-essential
  • unzip
  • git
  • cmake
  • cmake-curses-gui
  • python
  • python-pip
  • libblas-dev
  • libopenblas-dev
  • libatlas-base-dev
  • liblapack-dev
  • libboost-all-dev
  • libopencv-core3.2
  • libopencv-imgproc3.2
  • libopencv-dev
  • libopencv-highgui3.2
  • libopencv-highgui-dev
  • protobuf-compiler
  • libprotobuf-dev
  • libhdf5-dev
  • libjson-c-dev
  • libx11-dev
  • openjdk-8-jdk
  • wget
  • ninja-build

Also, you need to install the following additional packages for Python:

  • pyyaml
  • typing

Besides the development environment, you'll have to check out requisite third-party libraries' source code samples and build them. Most of these libraries are actively developed and don't have strict releases, so it's easier to check out a particular commit from the development tree and build it than downloading the latest official release. The following table shows you the libraries you have to check out, their repository URLs, and the hash number of the commit to check out:

Library repository

Branch name

Commit

https://github.com/shogun-toolbox/shogun

master

f7255cf2cc6b5116e50840816d70d21e7cc039bb

https://github.com/Shark-ML/Shark

master

221c1f2e8abfffadbf3c5ef7cf324bc6dc9b4315

https://gitlab.com/conradsnicta/armadillo-code

9.500.x

442d52ba052115b32035a6e7dc6587bb6a462dec

https://github.com/davisking/dlib

v19.15

929c630b381d444bbf5d7aa622e3decc7785ddb2

https://github.com/eigenteam/eigen-git-mirror

3.3.7

cf794d3b741a6278df169e58461f8529f43bce5d

https://github.com/mlpack/mlpack

master

e2f696cfd5b7ccda2d3af1c7c728483ea6591718

https://github.com/Kolkir/plotcpp

master

c86bd4f5d9029986f0d5f368450d79f0dd32c7e4

https://github.com/pytorch/pytorch

v1.2.0

8554416a199c4cec01c60c7015d8301d2bb39b64

https://github.com/xtensor-stack/xtensor

master

02d8039a58828db1ffdd2c60fb9b378131c295a2

https://github.com/xtensor-stack/xtensor-blas

master

89d9df93ff7306c32997e8bb8b1ff02534d7df2e

https://github.com/xtensor-stack/xtl

master

03a6827c9e402736506f3ded754e890b3ea28a98

https://github.com/opencv/opencv_contrib/releases/tag/3.3.0

3.3.0

https://github.com/ben-strasser/fast-cpp-csv-parser

master

3b439a664090681931c6ace78dcedac6d3a3907e

https://github.com/Tencent/rapidjson

master

73063f5002612c6bf64fe24f851cd5cc0d83eef9

Also, for the last chapter, you'll have to install the Android Studio IDE. You can download it from the official site at https://developer.android.com/studio. Besides the IDE, you'll also need to install and configure the Android SDK. The respective example in this book was developed and tested with this SDK, which can be downloaded from https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip. To configure this SDK, you have to unzip it and install particular packages. The following script shows how to do it:

mkdir /android 
cd /android

wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip

yes | ./tools/bin/sdkmanager --licenses
yes | ./tools/bin/sdkmanager "platform-tools"
yes | ./tools/bin/sdkmanager "platforms;android-25"
yes | ./tools/bin/sdkmanager "build-tools;25.0.2"
yes | ./tools/bin/sdkmanager "system-images;android-25;google_apis;armeabi-v7a"
yes | ./tools/bin/sdkmanager --install "ndk;20.0.5594570"

export ANDROID_NDK=/android/ndk/20.0.5594570
export ANDROID_ABI='armeabi-v7a'

Another way to configure the development environment is through the use of Docker. Docker allows you to configure a lightweight virtual machine with particular components. You can install Docker from the official Ubuntu package repository. Then, use the scripts provided with this book to automatically configure the environment. You will find the docker folder in the examples package. The following steps show how to use Docker configuration scripts:

  1. Run the following commands to create the image, run it, and configure the environment:
cd docker 
docker build -t buildenv:1.0 .
docker run -it buildenv:1.0 bash
cd /development
./install_env.sh
./install_android.sh
exit
  1. Use the following command to save our Docker container with the configured libraries and packages into a new Docker image:
docker commit [container id]
  1. Use the following command to rename the updated Docker image:
docker tag [image id] [new name] 
  1. Use the following command to start a new Docker container and share the book examples sources to it:
docker run -it -v [host_examples_path]:[container_examples_path] [tag name] bash

After running the preceding command, you will be in the command-line environment with the necessary configured packages, compiled third-party libraries, and with access to the programming examples package. You can use this environment to compile and run the code examples in this book. Each programming example is configured to use the CMake build system so you will be able to build them all in the same way. The following script shows a possible scenario of building a code example:

cd [example folder name]
mkdir build
cd build
cmake ..
cmake --build . --target all

Also, you can configure your local machine environment to share X Server with a Docker container to be able to run graphical UI applications from this container. It will allow you to use, for example, the Android Studio IDE or a C++ IDE (such as Qt Creator) from the Docker container, without local installation. The following script shows how to do this:

xhost +local:root 
docker run --net=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -it -v [host_examples_path]:[container_examples_path] [tag name] bash

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the following section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

To be more comfortable with understanding and building the code examples, we recommend you carefully read the documentation for each third-party library, and take some time to learn the basics of the Docker system and of development for the Android platform. Also, we assume that you have sufficient working knowledge of the C++ language and compilers, and that you are familiar with the CMake build system.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-Machine-Learning-with-CPP. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781789955330_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We downloaded a pre-trained model with the torch.hub.load() function."

A block of code is set as follows:

class Network {
public:
Network(const std::string& snapshot_path,
const std::string& synset_path,
torch::DeviceType device_type);
std::string Classify(const at::Tensor& image);
private:
torch::DeviceType device_type_;
Classes classes_;
torch::jit::script::Module model_;
};

Any command-line input or output is written as follows:

cd ~/[DEST_PATH]/server
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=~/dev/server/third-party/libtorch
cmake --build . --target all

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: " Start it by clicking the Start button at the top of the page. "

Warnings or important notes appear like this.
Tips and tricks appear like this.