Machine Learning for OpenCV 4 - Second Edition

Book description

A practical guide to understanding the core machine learning and deep learning algorithms, and implementing them to create intelligent image processing systems using OpenCV 4

Key Features

  • Gain insights into machine learning algorithms, and implement them using OpenCV 4 and scikit-learn
  • Get up to speed with Intel OpenVINO and its integration with OpenCV 4
  • Implement high-performance machine learning models with helpful tips and best practices

Book Description

OpenCV is an opensource library for building computer vision apps. The latest release, OpenCV 4, offers a plethora of features and platform improvements that are covered comprehensively in this up-to-date second edition.

You'll start by understanding the new features and setting up OpenCV 4 to build your computer vision applications. You will explore the fundamentals of machine learning and even learn to design different algorithms that can be used for image processing. Gradually, the book will take you through supervised and unsupervised machine learning. You will gain hands-on experience using scikit-learn in Python for a variety of machine learning applications. Later chapters will focus on different machine learning algorithms, such as a decision tree, support vector machines (SVM), and Bayesian learning, and how they can be used for object detection computer vision operations. You will then delve into deep learning and ensemble learning, and discover their real-world applications, such as handwritten digit classification and gesture recognition. Finally, you'll get to grips with the latest Intel OpenVINO for building an image processing system.

By the end of this book, you will have developed the skills you need to use machine learning for building intelligent computer vision applications with OpenCV 4.

What you will learn

  • Understand the core machine learning concepts for image processing
  • Explore the theory behind machine learning and deep learning algorithm design
  • Discover effective techniques to train your deep learning models
  • Evaluate machine learning models to improve the performance of your models
  • Integrate algorithms such as support vector machines and Bayes classifier in your computer vision applications
  • Use OpenVINO with OpenCV 4 to speed up model inference

Who this book is for

This book is for Computer Vision professionals, machine learning developers, or anyone who wants to learn machine learning algorithms and implement them using OpenCV 4. If you want to build real-world Computer Vision and image processing applications powered by machine learning, then this book is for you. Working knowledge of Python programming is required to get the most out of this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Machine Learning for OpenCV 4 Second Edition
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: Fundamentals of Machine Learning and OpenCV
  7. A Taste of Machine Learning
    1. Technical requirements
    2. Getting started with machine learning
    3. Problems that machine learning can solve
    4. Getting started with Python
    5. Getting started with OpenCV
    6. Installation
      1. Getting the latest code for this book
      2. Getting to grips with Python's Anaconda distribution
      3. Installing OpenCV in a conda environment
      4. Verifying the installation
      5. Getting a glimpse of OpenCV's ml module
    7. Applications of machine learning
    8. What's new in OpenCV 4.0?
    9. Summary
  8. Working with Data in OpenCV
    1. Technical requirements
    2. Understanding the machine learning workflow
    3. Dealing with data using OpenCV and Python
      1. Starting a new IPython or Jupyter session
      2. Dealing with data using Python's NumPy package
        1. Importing NumPy
        2. Understanding NumPy arrays
        3. Accessing single array elements by indexing
        4. Creating multidimensional arrays
      3. Loading external datasets in Python
      4. Visualizing the data using Matplotlib
        1. Importing Matplotlib
        2. Producing a simple plot
        3. Visualizing data from an external dataset
      5. Dealing with data using OpenCV's TrainData container in C++
    4. Summary
  9. First Steps in Supervised Learning
    1. Technical requirements
    2. Understanding supervised learning
      1. Having a look at supervised learning in OpenCV
      2. Measuring model performance with scoring functions
        1. Scoring classifiers using accuracy, precision, and recall
        2. Scoring regressors using mean squared error, explained variance, and R squared
    3. Using classification models to predict class labels
      1. Understanding the k-NN algorithm
      2. Implementing k-NN in OpenCV
        1. Generating the training data
        2. Training the classifier
        3. Predicting the label of a new data point
    4. Using regression models to predict continuous outcomes
      1. Understanding linear regression
      2. Linear regression in OpenCV
      3. Using linear regression to predict Boston housing prices
        1. Loading the dataset
        2. Training the model
        3. Testing the model
      4. Applying Lasso and ridge regression
    5. Classifying iris species using logistic regression
      1. Understanding logistic regression
        1. Loading the training data
        2. Making it a binary classification problem
        3. Inspecting the data
        4. Splitting data into training and test sets
        5. Training the classifier
        6. Testing the classifier
    6. Summary
  10. Representing Data and Engineering Features
    1. Technical requirements
    2. Understanding feature engineering
    3. Preprocessing data
      1. Standardizing features
      2. Normalizing features
      3. Scaling features to a range
      4. Binarizing features
      5. Handling the missing data
    4. Understanding dimensionality reduction
      1. Implementing Principal Component Analysis (PCA) in OpenCV
      2. Implementing independent component analysis (ICA)
      3. Implementing non-negative matrix factorization (NMF)
      4. Visualizing the dimensionality reduction using t-Distributed Stochastic Neighbor Embedding (t-SNE)
    5. Representing categorical variables
    6. Representing text features
    7. Representing images
      1. Using color spaces
        1. Encoding images in the RGB space
        2. Encoding images in the HSV and HLS space
      2. Detecting corners in images
      3. Using the star detector and BRIEF descriptor
      4. Using Oriented FAST and Rotated BRIEF (ORB)
    8. Summary
  11. Section 2: Operations with OpenCV
  12. Using Decision Trees to Make a Medical Diagnosis
    1. Technical requirements
    2. Understanding decision trees
      1. Building our first decision tree
        1. Generating new data
        2. Understanding the task by understanding the data
        3. Preprocessing the data
        4. Constructing the tree
      2. Visualizing a trained decision tree
      3. Investigating the inner workings of a decision tree
      4. Rating the importance of features
      5. Understanding the decision rules
      6. Controlling the complexity of decision trees
    3. Using decision trees to diagnose breast cancer
      1. Loading the dataset
      2. Building the decision tree
    4. Using decision trees for regression
    5. Summary
  13. Detecting Pedestrians with Support Vector Machines
    1. Technical requirement
    2. Understanding linear SVMs
      1. Learning optimal decision boundaries
      2. Implementing our first SVM
        1. Generating the dataset
        2. Visualizing the dataset
        3. Preprocessing the dataset
        4. Building the support vector machine
        5. Visualizing the decision boundary
    3. Dealing with nonlinear decision boundaries
      1. Understanding the kernel trick
      2. Knowing our kernels
      3. Implementing nonlinear SVMs
    4. Detecting pedestrians in the wild
      1. Obtaining the dataset
      2. Taking a glimpse at the histogram of oriented gradients (HOG)
      3. Generating negatives
      4. Implementing the SVM
      5. Bootstrapping the model
      6. Detecting pedestrians in a larger image
      7. Further improving the model
      8. Multiclass classification using SVMs
        1. About the data
        2. Attribute information
    5. Summary
  14. Implementing a Spam Filter with Bayesian Learning
    1. Technical requirements
    2. Understanding Bayesian inference
      1. Taking a short detour through probability theory
      2. Understanding Bayes' theorem
      3. Understanding the Naive Bayes classifier
    3. Implementing your first Bayesian classifier
      1. Creating a toy dataset
      2. Classifying the data with a normal Bayes classifier
      3. Classifying the data with a Naive Bayes classifier
      4. Visualizing conditional probabilities
    4. Classifying emails using the Naive Bayes classifier
      1. Loading the dataset
      2. Building a data matrix using pandas
      3. Preprocessing the data
      4. Training a normal Bayes classifier
      5. Training on the full dataset
      6. Using n-grams to improve the result
      7. Using TF-IDF to improve the result
    5. Summary
  15. Discovering Hidden Structures with Unsupervised Learning
    1. Technical requirements
    2. Understanding unsupervised learning
    3. Understanding k-means clustering
      1. Implementing our first k-means example
    4. Understanding expectation-maximization
      1. Implementing our expectation-maximization solution
      2. Knowing the limitations of expectation-maximization
        1. The first caveat – no guarantee of finding the global optimum
        2. The second caveat – we must select the number of clusters beforehand
        3. The third caveat – cluster boundaries are linear
        4. The fourth caveat – k-means is slow for a large number of samples
    5. Compressing color spaces using k-means
      1. Visualizing the true-color palette
      2. Reducing the color palette using k-means
    6. Classifying handwritten digits using k-means
      1. Loading the dataset
      2. Running k-means
    7. Organizing clusters as a hierarchical tree
      1. Understanding hierarchical clustering
      2. Implementing agglomerative hierarchical clustering
      3. Comparing clustering algorithms
    8. Summary
  16. Section 3: Advanced Machine Learning with OpenCV
  17. Using Deep Learning to Classify Handwritten Digits
    1. Technical requirements
    2. Understanding the McCulloch-Pitts neuron
    3. Understanding the perceptron
    4. Implementing your first perceptron
      1. Generating a toy dataset
      2. Fitting the perceptron to data
      3. Evaluating the perceptron classifier
      4. Applying the perceptron to data that is not linearly separable
    5. Understanding multilayer perceptrons
      1. Understanding gradient descent
      2. Training MLPs with backpropagation
      3. Implementing a MLP in OpenCV
        1. Preprocessing the data
        2. Creating an MLP classifier in OpenCV
        3. Customizing the MLP classifier
        4. Training and testing the MLP classifier
    6. Getting acquainted with deep learning
      1. Getting acquainted with Keras
    7. Classifying handwritten digits
      1. Loading the MNIST dataset
      2. Preprocessing the MNIST dataset
      3. Training an MLP using OpenCV
      4. Training a deep neural network using Keras
        1. Preprocessing the MNIST dataset
        2. Creating a convolutional neural network
        3. Model summary
        4. Fitting the model
    8. Summary
  18. Ensemble Methods for Classification
    1. Technical requirements
    2. Understanding ensemble methods
      1. Understanding averaging ensembles
        1. Implementing a bagging classifier
        2. Implementing a bagging regressor
      2. Understanding boosting ensembles
        1. Weak learners
        2. Implementing a boosting classifier
        3. Implementing a boosting regressor
      3. Understanding stacking ensembles
    3. Combining decision trees into a random forest
      1. Understanding the shortcomings of decision trees
      2. Implementing our first random forest
      3. Implementing a random forest with scikit-learn
      4. Implementing extremely randomized trees
    4. Using random forests for face recognition
      1. Loading the dataset
      2. Preprocessing the dataset
      3. Training and testing the random forest
    5. Implementing AdaBoost
      1. Implementing AdaBoost in OpenCV
      2. Implementing AdaBoost in scikit-learn
    6. Combining different models into a voting classifier
      1. Understanding different voting schemes
      2. Implementing a voting classifier
      3. Plurality
    7. Summary
  19. Selecting the Right Model with Hyperparameter Tuning
    1. Technical requirements
    2. Evaluating a model
      1. Evaluating a model the wrong way
      2. Evaluating a model in the right way
      3. Selecting the best model
    3. Understanding cross-validation
      1. Manually implementing cross-validation in OpenCV
      2. Using scikit-learn for k-fold cross-validation
      3. Implementing leave-one-out cross-validation
    4. Estimating robustness using bootstrapping
      1. Manually implementing bootstrapping in OpenCV
    5. Assessing the significance of our results
      1. Implementing Student's t-test
      2. Implementing McNemar's test
    6. Tuning hyperparameters with grid search
      1. Implementing a simple grid search
      2. Understanding the value of a validation set
      3. Combining grid search with cross-validation
      4. Combining grid search with nested cross-validation
    7. Scoring models using different evaluation metrics
      1. Choosing the right classification metric
      2. Choosing the right regression metric
    8. Chaining algorithms together to form a pipeline
      1. Implementing pipelines in scikit-learn
      2. Using pipelines in grid searches
    9. Summary
  20. Using OpenVINO with OpenCV
    1. Technical requirements
    2. Introduction to OpenVINO
    3. OpenVINO toolkit installation
      1. OpenVINO components
    4. Interactive face detection demo
    5. Using OpenVINO Inference Engine with OpenCV
    6. Using OpenVINO Model Zoo with OpenCV
    7. Image classification using OpenCV with OpenVINO Inference Engine
      1. Image classification using OpenVINO
      2. Image classification using OpenCV with OpenVINO
    8. Summary
  21. Conclusion
    1. Technical requirements
    2. Approaching a machine learning problem
    3. Building your own estimator
      1. Writing your own OpenCV-based classifier in C++
      2. Writing your own scikit-learn-based classifier in Python
    4. Where to go from here
    5. Summary
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Machine Learning for OpenCV 4 - Second Edition
  • Author(s): Aditya Sharma, Vishwesh Ravi Shrimali, Michael Beyeler
  • Release date: September 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789536300