36,59 €
Leverage the power of machine learning on mobiles and build intelligent mobile applications with ease
Key Features
Book Description
Machine learning presents an entirely unique opportunity in software development. It allows smartphones to produce an enormous amount of useful data that can be mined, analyzed, and used to make predictions. This book will help you master machine learning for mobile devices with easy-to-follow, practical examples.
You will begin with an introduction to machine learning on mobiles and grasp the fundamentals so you become well-acquainted with the subject. You will master supervised and unsupervised learning algorithms, and then learn how to build a machine learning model using mobile-based libraries such as Core ML, TensorFlow Lite, ML Kit, and Fritz on Android and iOS platforms. In doing so, you will also tackle some common and not-so-common machine learning problems with regard to Computer Vision and other real-world domains.
By the end of this book, you will have explored machine learning in depth and implemented on-device machine learning with ease, thereby gaining a thorough understanding of how to run, create, and build real-time machine-learning applications on your mobile devices.
What you will learn
Who this book is for
If you are a mobile app developer or a machine learning enthusiast keen to use machine learning to build smart mobile applications, this book is for you. Some experience with mobile application development is all you need to get started with this book. Prior experience with machine learning will be an added bonus
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 270
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Pravin DhandreAcquisition Editor:Dayne CastelinoContent Development Editor:Karan ThakkarTechnical Editor: Sagar SawantCopy Editor: Safis EditingProject Coordinator:Namrata SwettaProofreader: Safis EditingIndexer:Rekha NairGraphics:Jisha ChirayilProduction Coordinator:Aparna Bhagat
First published: December 2018
Production reference: 1271218
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78862-935-5
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Revathi Gopalakrishnan is a software professional with more than 17 years of experience in the IT industry. She has worked extensively in mobile application development and has played various roles, including developer and architect, and has led various enterprise mobile enablement initiatives for large organizations. She has also worked on a host of consumer applications for various customers around the globe. She has an interest in emerging areas, and machine learning is one of them. Through this book, she has tried to bring out how machine learning can make mobile application development more interesting and super cool. Revathi resides in Chennai and enjoys her weekends with her husband and her two lovely daughters.
Avinash Venkateswarlu has more than 3 years' experience in IT and is currently exploring mobile machine learning. He has worked in enterprise mobile enablement projects and is interested in emerging technologies such as mobile machine learning and cryptocurrency. Venkateswarlu works in Chennai, but enjoys spending his weekends in his home town, Nellore. He likes to do farming or yoga when he is not in front of his laptop exploring emerging technologies.
KarthikeyanNG is the head of engineering and technology at an Indian lifestyle and fashion retail brand. He served as a software engineer at Symantec Corporation, and has worked with two US-based start-ups as an early employee and has built various products. He has more than 9 years of experience with various scalable products using web, mobile, ML, AR, and VR technologies. He is an aspiring entrepreneur and technology evangelist. His interests lie in using new technologies and innovative ideas to resolve problems. He has also bagged prizes from more than 15 hackathons and is a TEDx speaker and a speaker at technology conferences and meetups, as well as a guest lecturer at a Bengaluru University. When not at work, he is found trekking.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Machine Learning for Mobile
About Packt
Why subscribe?
Packt.com
Contributors
About the authors
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Introduction to Machine Learning on Mobile
Definition of machine learning
When is it appropriate to go for machine learning systems?
The machine learning process
Defining the machine learning problem
Preparing the data
Building the model
Selecting the right machine learning algorithm
Training the machine learning model
Testing the model
Evaluation of the model
Making predictions/Deploying in the field
Types of learning
Supervised learning
Unsupervised learning
Semi-supervised learning
Reinforcement learning
Challenges in machine learning
Why use machine learning on mobile devices?
Ways to implement machine learning in mobile applications
Utilizing machine learning service providers for a machine learning model
Ways to train the machine learning model
On a desktop (training in the cloud)
On a device
Ways to carry out the inference – making predictions
Inference on a server
Inference on a device
Popular mobile machine learning tools and SDKs
Skills needed to implement on-device machine learning
Summary
Supervised and Unsupervised Learning Algorithms
Introduction to supervised learning algorithms
Deep dive into supervised learning algorithms
Naive Bayes
Decision trees
Linear regression
Logistic regression
Support vector machines
Random forest
Introduction to unsupervised learning algorithms
Deep dive into unsupervised learning algorithms
Clustering algorithms
Clustering methods
Hierarchical agglomerative clustering methods
K-means clustering
Association rule learning algorithm
Summary
References
Random Forest on iOS
Introduction to algorithms
Decision tree 
Advantages of the decision tree algorithm
Disadvantages of decision trees
Advantages of decision trees
Random forests
Solving the problem using random forest in Core ML
Dataset
Naming the dataset
Technical requirements
Creating the model file using scikit-learn 
Converting the scikit model to the Core ML model
Creating an iOS mobile application using the Core ML model
Summary
Further reading
TensorFlow Mobile in Android
An introduction to TensorFlow
TensorFlow Lite components
Model-file format
Interpreter
Ops/Kernel
Interface to hardware acceleration
The architecture of a mobile machine learning application
Understanding the model concepts
Writing the mobile application using the TensorFlow model
Writing our first program
Creating and Saving the TF model
Freezing the graph
Optimizing the model file
Creating the Android app
Copying the TF Model
Creating an activity
Summary
Regression Using Core ML in iOS
Introduction to regression
Linear regression
Dataset
Dataset naming
Understanding the basics of Core ML
Solving the problem using regression in Core ML
Technical requirements
How to create the model file using scikit-learn
Running and testing the model
Importing the model into the iOS project
Writing the iOS application
Running the iOS application
Further reading
Summary
The ML Kit SDK
Understanding ML Kit
ML Kit APIs
Text recognition
Face detection
Barcode scanning
Image labeling
Landmark recognition
Custom model inference
Creating a text recognition app using Firebase on-device APIs
Creating a text recognition app using Firebase on-cloud APIs
Face detection using ML Kit
Face detection concepts
Sample solution for face detection using ML Kit
Running the app
Summary
Spam Message Detection
Understanding NLP
Introducing NLP
Text-preprocessing techniques
Removing noise
Normalization
Standardization
Feature engineering
Entity extraction
Topic modeling
Bag-of-words model
Statistical Engineering
TF–IDF
TF
Inverse Document Frequency (IDF)
TF-IDF
Classifying/clustering the text
Understanding linear SVM algorithm
Solving the problem using linear SVM in Core ML
About the data
Technical requirements
Creating the Model file using Scikit Learn 
Converting the scikit-learn model into the Core ML model
Writing the iOS application
Summary
Fritz
Introduction to Fritz
Prebuilt ML models
Ability to use custom models
Model management
Hand-on samples using Fritz
Using the existing TensorFlow for mobile model in an Android application using Fritz
Registering with Fritz
Uploading the model file (.pb or .tflite)
Setting up Android and registering the app
Adding Fritz's TFMobile library
Adding dependencies to the project
Registering the FritzJob service in your Android Manifest
Replacing the TensorFlowInferenceInterface class with Fritz Interpreter
Building and running the application
Deploying a new version of your model
Creating an android application using fritz pre-built models
Adding dependencies to the project
Registering the Fritz JobService in your Android Manifest
Creating the app layout and components
Coding the application
Using the existing Core ML model in an iOS application using Fritz
Registering with Fritz
Creating a new project in Fritz
Uploading the model file (.pb or .tflite)
Creating an Xcode project
Installing Fritz dependencies
Adding code
Building and running the iOS mobile application
Summary
Neural Networks on Mobile
Introduction to neural networks
Communication steps of  a neuron
The activation function
Arrangement of neurons
Types of neural networks
Image recognition solution
Creating a TensorFlow image recognition model
What does TensorFlow do?
Retraining the model
About bottlenecks
Converting the TensorFlow model into the Core ML model
Writing the iOS mobile application
Handwritten digit recognition solution
Introduction to Keras
Installing Keras
Solving the problem
Defining the problem statement
Problem solution
Preparing the data
Defining the model's architecture
Compiling and fitting the model
Converting the Keras model into the Core ML model
Creating the iOS mobile application
Summary
Mobile Application Using Google Vision
Features of Google Cloud Vision
Sample mobile application using Google Cloud Vision
How does label detection work?
Prerequisites
Preparations
Understanding the Application
Output
Summary
The Future of ML on Mobile Applications
Key ML mobile applications 
Google Maps
Snapchat
Tinder
Netflix
Oval Money
ImprompDo
Dango
Carat
Uber
GBoard
Key innovation areas
Personalization applications
Healthcare
Targeted promotions and marketing
Visual and audio recognition
E-commerce 
Finance management
Gaming and entertainment
Enterprise apps
Real estate
Agriculture
Energy
Mobile security
Opportunities for stakeholders
Hardware manufacturers
Mobile operating system vendors
Third-party mobile ML SDK providers
ML mobile application developers
Summary
Question and Answers
FAQs
Data science
What is data science?
Where is data science used?
What is big data?
What is data mining?
Relationship between data science and big data
What are artificial neural networks?
What is AI?
How are data science, AI, and machine learning interrelated?
Machine learning framework 
Caffe2
scikit-learn
TensorFlow
Core ML
Mobile machine learning project implementation
What are the high-level important items to be considered before starting the project?
What are the roles and skills required to implement a mobile machine learning project?
 What should you focus on when testing the mobile machine learning project?
What is the help that the domain expert will provide to the machine learning project?
What are the common pitfalls in machine learning projects?
Installation
Python
Python dependencies
Xcode
References 
Other Books You May Enjoy
Leave a review - let other readers know what you think
This book will help you perform machine learning on mobile with simple practical examples. You start from the basics of machine learning, and by the time you complete the book, you will have a good grasp of what mobile machine learning is and what tools/SDKs are available for implementing mobile machine learning, and will also be able to implement various machine learning algorithms in mobile applications that can be run in both iOS and Android.
You will learn what machine learning is and will understand what is driving mobile machine learning and how it is unique. You will be exposed to all the mobile machine learning tools and SDKs: TensorFlow Lite, Core ML, ML Kit, and Fritz on Android and iOS. This book will explore the high-level architecture and components of each toolkit. By the end of the book, you will have a broad understanding of machine learning models and will be able to perform on-device machine learning. You will get deep-dive insights into machine learning algorithms such as regression, classification, linear support vector machine (SVM), and random forest. You will learn how to do natural language processing and implement spam message detection. You will learn how to convert existing models created using Core ML and TensorFlow into Fritz models. You will also be exposed to neural networks. You will also get sneak peek into the future of machine learning, and the book also contains an FAQ section to answer all your queries on mobile machine learning. It will help you to build an interesting diet application that provides the calorie values of food items that are captured on a camera, which runs both in iOS and Android.
Machine Learning for Mobile is for you if you are a mobile developer or machine learning user who aspires to exploit machine learning and use it on mobiles and smart devices. Basic knowledge of machine learning and entry-level experience with mobile application development is preferred.
Chapter 1, Introduction to Machine Learning on Mobile, explains what machine learning is and why we should use it on mobile devices. It introduces different approaches to machine learning and their pro and cons.
Chapter 2, Supervised and Unsupervised Learning Algorithms, covers supervised and unsupervised approaches of machine learning algorithms. We will also learn about different algorithms, such as Naive Bayes, decision trees, SVM, clustering, associated mapping, and many more.
Chapter 3, Random Forest on iOS, covers random forests and decision trees in depth and explains how to apply them to solve machine learning problems. We will also create an application using a decision tree to diagnose breast cancer.
Chapter 4, TensorFlow Mobile in Android, introduces TensorFlow for mobile. We will also learn about the architecture of a mobile machine learning application and write an application using TensorFlow in Android.
Chapter 5, Regression Using Core ML in iOS, explores regression and Core ML and shows how to apply it to solve a machine learning problem. We will be creating an application using scikit-learn to predict house prices.
Chapter 6, ML Kit SDK, explores ML Kit and its benefits. We will be creating some image labeling applications using ML Kit and device and cloud APIs.
Chapter 7, Spam Message Detection in iOS - Core ML, introduces natural language processing and the SVM algorithm. We will solve a problem of bulk SMS, that is, whether messages are spam or not.
Chapter 8, Fritz, introduces the Fritz mobile machine learning platform. We will create an application using Fritz and Core ML in iOS. We will also see how Fritz can be used with the sample dataset we create earlier in the book.
Chapter 9, Neural Networks on Mobile, covers the concepts of neural networks, Keras, and their applications in the field of mobile machine learning. We will be creating an application to recognize handwritten digits and also the TensorFlow image recognition model.
Chapter 10, Mobile Application Using Google Cloud Vision, introduces the Google Cloud Vision label-detection technique in an Android application to determine what is in pictures taken by a camera.
Chapter 11, Future of ML on Mobile Applications, covers the key features of mobile applications and the opportunities they provide for stakeholders.
Appendix, Question and Answers, contains questions that may be on your mind and tries to provide answers to those questions.
Readers need to have prior knowledge of machine learning, Android Studio, and Xcode.
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.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
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/Machine-Learning-for-Mobile. 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!
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/9781788629355_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
We're living in a world of mobile applications. They've become such a part and parcel of our everyday lives that we rarely look into the numbers behind them. (These include the revenue they make, the actual market size of the business, and the quantitative figures that would fuel the growth of mobile applications.) Let's take a peek at the numbers:
Forbes predicts that mobile application revenue is slated to hit $189 billion by the year 2020
We are also seeing that the
global smartphone installation base is increasing exponentially. Therefore, the revenue from applications getting installed on them is also increasing at an unimaginable rate
Mobile devices and services are now the hubs for people's entertainment and business lives, as well as for communication. The smartphone has replaced the PC as the most important smart connected device. Mobile innovations, new business models, and mobile technologies are transforming every walk of human life.
Now, we come to machine learning. Why has machine learning been booming recently? Machine learning is not a new subject. It existed over 10-20 years ago, so why is it in focus now and why is everyone talking about it? The reason is simple: data explosion. Social networking and mobile devices have enabled the generation of user data like never before. Ten years ago, you didn't have images uploaded to the cloud like you do today because mobile phone penetration then cannot be compared to what it is today. The 4G connection makes it possible even to live stream video data on-demand (VDO) now, so it means more data is running all around the world like never before. The next era is predicted to be the era of the internet of things (IOT), where there is going to be more data-sensor-based data.
All this data is valuable only when we can put it to proper use, derive insights that bring value to us, and bring about unseen data patterns that provide new business opportunities. So, for this to happen, machine learning is the right tool to unlock the stored value in these piles and piles of data that are being accumulated each day.
So, it has become obvious that it is a great time to be a mobile application developer and a great time to be a machine learning data scientist. But how cool would it be if we were able to bring the power of machine learning to mobile devices and develop really cool mobile applications that leverage the power of machine learning? That's what we are trying to do through this book: give insights to mobile application developers on the basics of machine learning, expose them to various machine learning algorithms and mobile machine learning SDKs/tools, and go over developing mobile machine learning applications using these SDKs/tools.
Machine learning in the mobile space is a key innovation area that must be properly understood by mobile developers as it is transforming the way users can visualize and utilize mobile applications. So, how can machine learning transform mobile applications and convert them into applications that are any user's dream? Let me give you some examples to give a bird's eye view of what machine learning can do for mobile applications:
Facebook and YouTube mobile applications use machine learning—
Recommendations
or
People you might know
are nothing but machine learning in action.
Apple and Google read the behavior or wording of each user behavior and recommend the next word that is suitable for your style of typing. They have already implemented this in both iOS and Android devices.
Oval Money analyzes a user's previous transactions and offers them different ways to avoid extra spending.
Google Maps is using machine learning to make your life easier.
Django uses machine learning to solve the problem to find a perfect emoji. It is a floating assistant that can be integrated into different messengers.
Machine learning can be applied to mobile applications belonging to any domain—healthcare, finance, games, communication, or anything under the sun. So, let's understand what machine learning is all about.
In this chapter, we will cover the following topics:
What is machine learning?
When is it appropriate to go for solutions that get implemented using machine learning?
Categories of machine learning
Key algorithms in machine learning
The process that needs to be followed for implementing machine learning
Some of the key concepts of machine learning that are good to know
Challenges in implementing machine learning
Why use machine learning in mobile applications?
Ways to implement machine learning in mobile applications
Machine learning is focused on writing software that can learn from past experience. One of the standard definitions of machine learning, as given by Tom Mitchell, a professor at the Carnegie Mellon University (CMU), is the following:
For example, a computer program that learns to play chess might improve its performance as measured by its ability to win at the class of tasks involving playing chess, through experience obtained by playing chess against itself. In general, to have a well-defined learning problem, we must identify the class of tasks, the measure of performance to be improved, and the source of experience. Consider that a chess-learning problem consists of the following: task, performance measure, and training experience, where:
Task T
is playing chess
Performance measure P
is the percentage of games won against opponents
Training experience E
is the program playing practice chess games against itself
To put it in simple terms, if a computer program is able to improve the way it performs a task with the help of previous experience, this way you will know the computer has learned. This scenario is very different from one where a program can perform a particular task because its programmers have already defined all the parameters and have provided the data required to do so. A normal program can perform the task of playing chess because the programmers have written the code to play chess with a built-in winning strategy. However, a machine learning program does not possess a built-in strategy; in fact, it only has a set of rules of the legal moves in the game, and what a winning scenario is. In such a case, the program needs to learn by repeatedly playing the game until it can win.
Is machine learning applicable to all scenarios? When exactly should wehave the machine learn rather than directly programming the machine with instructions to carry out the task?
Machine learning systems are not knowledge-based systems. In knowledge-based systems, we can directly use the knowledge to codify all possible rules to infer a solution. We go for machine learning when such codification of instructions is not straightforward. Machine learning programs will be more applicable in the following scenarios:
Very complex tasks that are difficult to program
: There are regular tasks humans perform, such as speaking, driving, seeing and recognizing things, tasting, and classifying things by looking at them, which seem so simple to us. But, we do not know how our brains are wired or programmed or what rules need to be defined to perform all this seamlessly, for which we could create a program to replicate these actions. It is possible through machine learning to perform some of them, not to the extent that humans do, but machine learning has great potential here.
Very complex tasks that deal with a huge volume of data
: There are tasks that include analyzing huge volumes of data and finding hidden patterns, or coming up with new correlations in the data, that are not humanly possible. Machine learning is helpful for tasks for which we do not humanly know the steps to arrive at a solution and which are so complex in nature due to the various solution possibilities that it is not humanly possible to determine solutions.
Adapting to changes in environment and data
: A program hardcoded with a set of instructions cannot adapt itself to the changing environment and is not capable of scaling up to new environments. Both of these can be achieved using machine learning programs.
The machine learning process is an iterative process. It cannot be completed in one go. The most important activities to be performed for a machine learning solution are as follows:
Define the machine learning problem (it must be well-defined).
Gather, prepare, and enhance the data that is required.
Use that data to build a model. This step goes in a loop and covers the following substeps. At times, it may also lead to revisiting
Step 2
on data or even require the redefinition of the problem statement:
Select the appropriate model/machine learning algorithm
Train the
machine learning
algorithm on the training data and build the model
Test the model
Evaluate the results
Continue this phase until the evaluation result is satisfactory and finalize the model
Use the finalized model to make future predictions for the problem statement.
There are four major steps involved in the whole process, which is iterative and repetitive, till the objective is met. Let's get into the details of each step in the following sections. The following diagram will give a quick overview of the entire process, so it is easy to go into the details:
As defined by Tom Mitchell, the problem must be a well-defined machine learning problem. The three important questions to be solved at this stage include the following:
Do we have the right problem?
Do we have the right data?
