Book Image

Intelligent Mobile Projects with TensorFlow

By : Jeff Tang
Book Image

Intelligent Mobile Projects with TensorFlow

By: Jeff Tang

Overview of this book

As a developer, you always need to keep an eye out and be ready for what will be trending soon, while also focusing on what's trending currently. So, what's better than learning about the integration of the best of both worlds, the present and the future? Artificial Intelligence (AI) is widely regarded as the next big thing after mobile, and Google's TensorFlow is the leading open source machine learning framework, the hottest branch of AI. This book covers more than 10 complete iOS, Android, and Raspberry Pi apps powered by TensorFlow and built from scratch, running all kinds of cool TensorFlow models offline on-device: from computer vision, speech and language processing to generative adversarial networks and AlphaZero-like deep reinforcement learning. You’ll learn how to use or retrain existing TensorFlow models, build your own models, and develop intelligent mobile apps running those TensorFlow models. You'll learn how to quickly build such apps with step-by-step tutorials and how to avoid many pitfalls in the process with lots of hard-earned troubleshooting tips.
Table of Contents (14 chapters)

To get the most out of this book

We recommend that you start with reading the first four chapters in order, along with running the accompanying iOS and Android apps available from the book's source code repository at http://github.com/jeffxtang/mobiletfbook. That'll help you ensure that you have the development environments all set up for TensorFlow mobile app development and that you know how to integrate TensorFlow into your own iOS and/or Android apps. If you're an iOS developer, you'll also learn how to use Objective-C or Swift with TensorFlow, and when and how to use the TensorFlow pod or the manual TensorFlow iOS library.

Then, if you need to build a custom TensorFlow Android library, go to Chapter 7, Recognizing Drawing with CNN and LSTM, and if you want to learn how to use a Keras model in your mobile app, check out Chapter 8, Predicting Stock Price with RNN, and Chapter 10, Building an AlphaZero-like Mobile Game App.

If you're more interested in TensorFlow Lite or Core ML, read Chapter 11, Using TensorFlow Lite and Core ML on Mobile, and if you're most interested in TensorFlow on Raspberry Pi, or reinforcement learning in TensorFlow, jump to Chapter 12, Developing TensorFlow Apps on Raspberry Pi.

Other than that, you can go through chapters 5 to 10 in order to see how to train different kinds of CNN, RNN, LSTM, GAN, and AlphaZero models and how to use them on mobile, maybe running the iOS and/or Android apps for each chapter before looking into the detailed implementation. Alternatively, you can jump directly to any chapter with the model you're most interested in; just be aware that a later chapter may refer to an earlier chapter for some duplicated details, such as steps of adding a TensorFlow custom iOS library to your iOS app, or fixing some model loading or running errors by building a TensorFlow custom library. However, rest assured that you won't be lost, or at least we've done our best to provide user-friendly and step-by-step tutorials, with occasional references to some steps of previous tutorials, to help you avoid all possible pitfalls you may encounter when building mobile TensorFlow apps, while also avoiding repeating ourselves.

When to read the book

AI, or its hottest branch machine learning, or its hottest subbranch deep learning, has enjoyed rapid progress in recent years. New releases of TensorFlow, backed by Google and with the most popular developer community of all open source machine learning frameworks, has also been launched at a faster speed. When we started writing the book in December 2017, the latest TensorFlow release was 1.4.0, released on November 2, 2017, and after that 1.5.0 was released on January 26, 2018, 1.6.0 on February 28, 2018, 1.7.0 on March 29, 2018, and 1.8.0 on April 27, 2018. All the iOS, Android, and Python code in the book has been tested with all those TensorFlow versions. Still, by the time you read the book, the latest TensorFlow version likely will be later than 1.8.0.

It turns out you don't need to worry too much about new releases of TensorFlow; the code in the book will most likely run seamlessly on the latest TensorFlow releases. During our testing run the apps on TensorFlow 1.4, 1.5, 1.6, 1.7, and 1.8, we've made no code changes at all. It's likely that in a later version, more TensorFlow operations will be supported by default, so you won't need to build a custom TensorFlow library, or you'll be able to build the custom TensorFlow library in a simpler way.

Of course, there's no guarantee that all the code will run without any changes in all future TensorFlow versions, but with all the detailed tutorials and troubleshooting tips covered in the book, no matter when you read the book, now or months later, you should have a smooth time reading the book and running the apps in the book with TensorFlow 1.4-1.8 or later.

As we have to stop with a specific TensorFlow version at certain points to get the book published, we'll continue to test run all the code in the book with every new major TensorFlow release, and update the code and test results accordingly on the book's source code repository at http://github.com/jeffxtang/mobiletfbook. If you have any questions about the code or the book, you may also post an issue directly on the repository.

Another concern is about the choice between TensorFlow Mobile and TensorFlow Lite. The book covers TensorFlow Mobile in most chapters (1 to 10). TensorFlow Lite may be the future of running TensorFlow on mobile, it's still in developer preview as of Google I/O 2018—that's why Google expects you to "use TensorFlow Mobile to cover production cases." Even after TensorFlow Lite is officially released, according to Google, "TensorFlow Mobile isn't going away anytime soon"—in fact, with the latest TensorFlow 1.8.0 version we tested before the book's publication, we found that using TensorFlow Mobile gets even simpler.

If the day that TensorFlow Lite fully replaces TensorFlow Mobile in all use cases, with Lite's better performance and smaller size, does finally come, the skills you'll learn from the book will only better prepare you for that day. In the meantime, before that unforeseeable future arrives, you can read the book and get to know how to use the big brother such as TensorFlow Mobile to run all those amazing and powerful TensorFlow models in your mobile apps.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.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.packtpub.com.
  2. Select the SUPPORT tab.
  1. Click on Code Downloads & Errata.
  2. 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 the Packt GitHub account at https://github.com/PacktPublishing/Intelligent-Mobile-Projects-with-TensorFlow. 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!

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: "Install the matplotlib, pillow, lxml, and jupyter libraries. On Ubuntu or Mac, you can run."

A block of code is set as follows:

syntax = "proto2";
package object_detection.protos;
message StringIntLabelMapItem {
optional string name = 1;
optional int32 id = 2;
optional string display_name = 3;
};

message StringIntLabelMap {
repeated StringIntLabelMapItem item = 1;
};

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

sudo pip install pillow
sudo pip install lxml
sudo pip install jupyter
sudo pip install matplotlib

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: "Now, let's select the Enhance Image option, you'll see the result"

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