Table of Contents
Foreword xv
Preface xvii
1 Alls Magic 1
The Path of AI in JavaScript 2
What Is Intelligence? 3
The History of AI 4
The Neural Network 6
Today's AI 8
Why TensorFlow.js? 9
Significant Support 9
Online Ready 10
Offline Ready 10
Privacy 10
Diversity 10
Types of Machine Learning 11
Quick Definition: Supervised Learning 11
Quick Definition: Unsupervised Learning 12
Quick Definition: Semisupervised Learning 12
Quick Definition: Reinforcement Learning 13
Information Overload 13
AI Is Everywhere 14
A Tour of What Frameworks Provide 14
What Is a Model? 16
In This Book 17
Associated Code 18
Chapter Sections 20
Common AI/ML Terminology 20
Chapter Review 24
Review Questions 24
2 Introducing TensorFlow.js 25
Hello, TensorFlow.js 25
Leveraging TensorFlow.js 27
Let's Get TensorFlow.js Ready 28
Getting Set Up with TensorFlow.js in the Browser 29
Using NPM 29
Including a Script Tag 29
Getting Set Up with TensorFlow.js Node 30
Verifying TensorFlow.js Is Working 32
Download and Run These Examples 32
Let's Use Some Real TensorFlow.js 34
The Toxicity Classifier 35
Loading the Model 40
Classifying 42
Try It Yourself 42
Chapter Review 42
Chapter Challenge: Truck Alert! 43
Review Questions 44
3 Introducing Tensors 45
Why Tensors? 45
Hello, Tensors 46
Creating Tensors 47
Tensors for Data Exercises 50
Tensors on Tour 53
Tensors Provide Speed 53
Tensors Provide Direct Access 53
Tensors Batch Data 54
Tensors in Memory 54
Deallocating Tensors 54
Automatic Tensor Cleanup 55
Tensors Come Home 57
Retrieving Tensor Data 58
Tensor Manipulation 60
Tensors and Mathematics 60
Recommending Tensors 61
Chapter Review 66
Chapter Challenge: What Makes You So Special? 66
Review Questions 67
4 Image Tensors 69
Visual Tensors 70
Quick Image Tensors 72
JPGs and PNGs and GIFs, Oh My! 76
Browser: Tensor to Image 76
Browser: Image to Tensor 77
Node: Tensor to Image 80
Node: Image to Tensor 83
Common Image Modifications 85
Mirroring Image Tensors 85
Resizing Image Tensors 89
Cropping Image Tensors 91
New Image Tools 93
Chapter Review 93
Chapter Challenge: Sorting Chaos 93
Review Questions 94
5 Introducing Models 97
Loading Models 98
Loading Models Via Public URL 98
Loading Models from Other Locations 101
Our First Consumed Model 101
Loading, Encoding, and Asking a Model 102
Interpreting the Results 105
Cleaning the Board After 107
Our First TensorFlow Hub Model 107
Exploring TFHub 107
Wiring Up Inception v3 108
Our First Overlayed Model 110
The Localization Model 111
Labeling the Detection 113
Chapter Review 116
Chapter Challenge: Cute Faces 116
Review Questions 117
6 Advanced Models and UI 119
MobileNet Again 120
SSD MobileNet 122
Bounding Outputs 124
Reading Model Outputs 124
Displaying All Outputs 126
Detection Cleanup 127
Quality Checking 128
IoUs and NMS 129
Adding Text Overlays 134
Solving Low Contrast 134
Solving Draw Order 136
Connecting to a Webcam 138
Moving from Image to Video 139
Activating a Webcam 140
Drawing Detections 141
Chapter Review 142
Chapter Challenge: Top Detective 142
Review Questions 143
7 Model-Making Resources 145
Out-of-Network Model Shopping 146
Model Zoos 146
Converting Models 146
Your First Customized Model 149
Meet Teachable Machine 150
Use Teachable Machine 151
Gathering Data and Training 152
Verifying the Model 155
Machine Learning Gotchas 157
Small Amounts of Data 157
Poor Data 157
Data Bias 158
Overfitting 158
Underfitting 158
Datasets Shopping 159
The Popular Datasets 161
Chapter Review 162
Chapter Challenge: R.I.P. You Will Be MNIST 162
Review Questions 163
8 Training Models 165
Training 101 166
Data Prep 167
Design a Model 167
Identify Learning Metrics 169
Task the Model with Training 171
Put It All Together 171
Nonlinear Training 101 174
Gathering the Data 175
Adding Activations to Neurons 175
Watching Training 178
Improving Training 180
Chapter Review 185
Chapter Challenge: The Model Architect 185
Review Questions 186
9 Classification Models and Data Analysis 187
Classification Models 188
The Titanic 190
Titanic Dataset 190
Danfo.js 191
Preparing for the Titanic 192
Training on Titanic Data 197
Feature Engineering 199
Dnotebook 200
Titanic Visuals 201
Creating Features (aka Preprocessing) 204
Feature Engineered Training Results 207
Reviewing Results 207
Chapter Review 207
Chapter Challenge: Ship Happens 208
Review Questions 209
10 Image Training 211
Understanding Convolutions 212
Convolutions Quick Summary 213
Adding Convolution Layers 215
Understanding Max Pooling 216
Max Pooling Quick Summary 216
Adding Max Pooling Layers 218
Training Image Classification 218
Handling Image Data 220
The Sorting Hat 220
Getting Started 222
Converting Folders of Images 224
The CNN Model 227
Training and Saving 231
Testing the Model 232
Building a Sketchpad 232
Reading the Sketchpad 233
Chapter Review 236
Chapter Challenge: Saving the Magic 236
Review Questions 237
11 Transfer Learning 239
How Does Transfer Learning Work? 240
Transfer Learning Neural Networks 241
Easy MobileNet Transfer Learning 242
TensorFlow Hub Check, Mate! 244
Utilizing Layers Models for Transfer Learning 248
Shaving Layers on MobileNet 249
Layers Feature Model 250
A Unified Model 251
No Training Needed 251
Easy KNN: Bunnies Versus Sports Cars 253
Chapter Review 256
Chapter Challenge: Warp-Speed Learning 256
Review Questions 257
12 Dicify: Capstone Project 259
A Dicey Challenge 260
The Plan 261
The Data 261
The Training 263
The Website 263
Generating Training Data 263
Training 268
The Site Interface 269
Cut into Dice 270
Reconstruct the Image 272
Chapter Review 274
Chapter Challenge: Easy as 01, 10, 11 275
Review Questions 276
Afterword 277
A Chapter Review Answers 281
B Chapter Challenge Answers 289
C Rights and Licenses 299
Index 303