logo资料库

TinyML Machine Learning with TensorFlow Lite on Arduino and Micr....pdf

第1页 / 共504页
第2页 / 共504页
第3页 / 共504页
第4页 / 共504页
第5页 / 共504页
第6页 / 共504页
第7页 / 共504页
第8页 / 共504页
资料共504页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Preface
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Introduction
Embedded Devices
Changing Landscape
Chapter 2. Getting Started
Who Is This Book Aimed At?
What Hardware Do You Need?
What Software Do You Need?
What Do We Hope You’ll Learn?
Chapter 3. Getting Up to Speed on Machine Learning
What Machine Learning Actually Is
The Deep Learning Workflow
Decide on a Goal
Collect a Dataset
Design a Model Architecture
Train the Model
Convert the Model
Run Inference
Evaluate and Troubleshoot
Wrapping Up
Chapter 4. The “Hello World” of TinyML: Building and Training a Model
What We’re Building
Our Machine Learning Toolchain
Python and Jupyter Notebooks
Google Colaboratory
TensorFlow and Keras
Building Our Model
Importing Dependencies
Generating Data
Splitting the Data
Defining a Basic Model
Training Our Model
Training Metrics
Graphing the History
Improving Our Model
Testing
Converting the Model for TensorFlow Lite
Converting to a C File
Wrapping Up
Chapter 5. The “Hello World” of TinyML: Building an Application
Walking Through the Tests
Including Dependencies
Setting Up the Test
Getting Ready to Log Data
Mapping Our Model
Creating an AllOpsResolver
Defining a Tensor Arena
Creating an Interpreter
Inspecting the Input Tensor
Running Inference on an Input
Reading the Output
Running the Tests
Project File Structure
Walking Through the Source
Starting with main_functions.cc
Handling Output with output_handler.cc
Wrapping Up main_functions.cc
Understanding main.cc
Running Our Application
Wrapping Up
Chapter 6. The “Hello World” of TinyML: Deploying to Microcontrollers
What Exactly Is a Microcontroller?
Arduino
Handling Output on Arduino
Running the Example
Making Your Own Changes
SparkFun Edge
Handling Output on SparkFun Edge
Running the Example
Testing the Program
Viewing Debug Data
Making Your Own Changes
ST Microelectronics STM32F746G Discovery Kit
Handling Output on STM32F746G
Running the Example
Making Your Own Changes
Wrapping Up
Chapter 7. Wake-Word Detection: Building an Application
What We’re Building
Application Architecture
Introducing Our Model
All the Moving Parts
Walking Through the Tests
The Basic Flow
The Audio Provider
The Feature Provider
The Command Recognizer
The Command Responder
Listening for Wake Words
Running Our Application
Deploying to Microcontrollers
Arduino
SparkFun Edge
ST Microelectronics STM32F746G Discovery Kit
Wrapping Up
Chapter 8. Wake-Word Detection: Training a Model
Training Our New Model
Training in Colab
Using the Model in Our Project
Replacing the Model
Updating the Labels
Updating command_responder.cc
Other Ways to Run the Scripts
How the Model Works
Visualizing the Inputs
How Does Feature Generation Work?
Understanding the Model Architecture
Understanding the Model Output
Training with Your Own Data
The Speech Commands Dataset
Training on Your Own Dataset
How to Record Your Own Audio
Data Augmentation
Model Architectures
Wrapping Up
Chapter 9. Person Detection: Building an Application
What We’re Building
Application Architecture
Introducing Our Model
All the Moving Parts
Walking Through the Tests
The Basic Flow
The Image Provider
The Detection Responder
Detecting People
Deploying to Microcontrollers
Arduino
SparkFun Edge
Wrapping Up
Chapter 10. Person Detection: Training a Model
Picking a Machine
Setting Up a Google Cloud Platform Instance
Training Framework Choice
Building the Dataset
Training the Model
TensorBoard
Evaluating the Model
Exporting the Model to TensorFlow Lite
Exporting to a GraphDef Protobuf File
Freezing the Weights
Quantizing and Converting to TensorFlow Lite
Converting to a C Source File
Training for Other Categories
Understanding the Architecture
Wrapping Up
Chapter 11. Magic Wand: Building an Application
What We’re Building
Application Architecture
Introducing Our Model
All the Moving Parts
Walking Through the Tests
The Basic Flow
The Accelerometer Handler
The Gesture Predictor
The Output Handler
Detecting Gestures
Deploying to Microcontrollers
Arduino
SparkFun Edge
Wrapping Up
Chapter 12. Magic Wand: Training a Model
Training a Model
Training in Colab
Other Ways to Run the Scripts
How the Model Works
Visualizing the Input
Understanding the Model Architecture
Training with Your Own Data
Capturing Data
Modifying the Training Scripts
Training
Using the New Model
Wrapping Up
Learning Machine Learning
What’s Next
Chapter 13. TensorFlow Lite for Microcontrollers
What Is TensorFlow Lite for Microcontrollers?
TensorFlow
TensorFlow Lite
TensorFlow Lite for Microcontrollers
Requirements
Why Is the Model Interpreted?
Project Generation
Build Systems
Specializing Code
Makefiles
Writing Tests
Supporting a New Hardware Platform
Printing to a Log
Implementing DebugLog()
Running All the Targets
Integrating with the Makefile Build
Supporting a New IDE or Build System
Integrating Code Changes Between Projects and Repositories
Contributing Back to Open Source
Supporting New Hardware Accelerators
Understanding the File Format
FlatBuffers
Porting TensorFlow Lite Mobile Ops to Micro
Separate the Reference Code
Create a Micro Copy of the Operator
Port the Test to the Micro Framework
Build a Bazel Test
Add Your Op to AllOpsResolver
Build a Makefile Test
Wrapping Up
Chapter 14. Designing Your Own TinyML Applications
The Design Process
Do You Need a Microcontroller, or Would a Larger Device Work?
Understanding What’s Possible
Follow in Someone Else’s Footsteps
Find Some Similar Models to Train
Look at the Data
Wizard of Oz-ing
Get It Working on the Desktop First
Chapter 15. Optimizing Latency
First Make Sure It Matters
Hardware Changes
Model Improvements
Estimating Model Latency
How to Speed Up Your Model
Quantization
Product Design
Code Optimizations
Performance Profiling
Optimizing Operations
Look for Implementations That Are Already Optimized
Write Your Own Optimized Implementation
Taking Advantage of Hardware Features
Accelerators and Coprocessors
Contributing Back to Open Source
Wrapping Up
Chapter 16. Optimizing Energy Usage
Developing Intuition
Typical Component Power Usage
Hardware Choice
Measuring Real Power Usage
Estimating Power Usage for a Model
Improving Power Usage
Duty Cycling
Cascading Design
Wrapping Up
Chapter 17. Optimizing Model and Binary Size
Understanding Your System’s Limits
Estimating Memory Usage
Flash Usage
RAM Usage
Ballpark Figures for Model Accuracy and Size on Different Problems
Speech Wake-Word Model
Accelerometer Predictive Maintenance Model
Person Presence Detection
Model Choice
Reducing the Size of Your Executable
Measuring Code Size
How Much Space Is Tensorflow Lite for Microcontrollers Taking?
OpResolver
Understanding the Size of Individual Functions
Framework Constants
Truly Tiny Models
Wrapping Up
Chapter 18. Debugging
Accuracy Loss Between Training and Deployment
Preprocessing Differences
Debugging Preprocessing
On-Device Evaluation
Numerical Differences
Are the Differences a Problem?
Establish a Metric
Compare Against a Baseline
Swap Out Implementations
Mysterious Crashes and Hangs
Desktop Debugging
Log Tracing
Shotgun Debugging
Memory Corruption
Wrapping Up
Chapter 19. Porting Models from TensorFlow to TensorFlow Lite
Understand What Ops Are Needed
Look at Existing Op Coverage in Tensorflow Lite
Move Preprocessing and Postprocessing into Application Code
Implement Required Ops if Necessary
Optimize Ops
Wrapping Up
Chapter 20. Privacy, Security, and Deployment
Privacy
The Privacy Design Document
Using a PDD
Security
Protecting Models
Deployment
Moving from a Development Board to a Product
Wrapping Up
Chapter 21. Learning More
The TinyML Foundation
SIG Micro
The TensorFlow Website
Other Frameworks
Twitter
Friends of TinyML
Wrapping Up
Appendix A. Using and Generating an Arduino Library Zip
Appendix B. Capturing Audio on Arduino
Index
About the Authors
Colophon
TinyML Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers Pete Warden & Daniel Situnayake
TinyML Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers Pete Warden and Daniel Situnayake Beijing Beijing Boston Boston Farnham Sebastopol Farnham Sebastopol Tokyo Tokyo
TinyML by Pete Warden and Daniel Situnayake Copyright © 2020 Pete Warden and Daniel Situnayake. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Mike Loukides Development Editor: Nicole Taché Production Editor: Beth Kelly Copyeditor: Octal Publishing, Inc. December 2019: First Edition Revision History for the First Edition 2019-12-13: First Release Proofreader: Rachel Head Indexer: WordCo, Inc. Interior Designer: David Futato Illustrator: Rebecca Demarest See http://oreilly.com/catalog/errata.csp?isbn=9781492052043 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. TinyML, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. TinyML is a trademark of the tinyML Foundation and is used with permission. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-05204-3 [LSI]
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Embedded Devices 3 Changing Landscape 4 2. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Who Is This Book Aimed At? 5 What Hardware Do You Need? 6 What Software Do You Need? 7 What Do We Hope You’ll Learn? 8 3. Getting Up to Speed on Machine Learning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 What Machine Learning Actually Is 12 The Deep Learning Workflow 13 Decide on a Goal 14 Collect a Dataset 14 Design a Model Architecture 16 Train the Model 21 Convert the Model 26 Run Inference 26 Evaluate and Troubleshoot 27 Wrapping Up 28 4. The “Hello World” of TinyML: Building and Training a Model. . . . . . . . . . . . . . . . . . . . . . 29 What We’re Building 30 Our Machine Learning Toolchain 32 Python and Jupyter Notebooks 32 iii
Google Colaboratory 33 TensorFlow and Keras 33 Building Our Model 34 Importing Dependencies 35 Generating Data 38 Splitting the Data 41 Defining a Basic Model 42 Training Our Model 46 Training Metrics 48 Graphing the History 49 Improving Our Model 54 Testing 58 Converting the Model for TensorFlow Lite 60 Converting to a C File 64 Wrapping Up 65 5. The “Hello World” of TinyML: Building an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Walking Through the Tests 68 Including Dependencies 69 Setting Up the Test 70 Getting Ready to Log Data 70 Mapping Our Model 72 Creating an AllOpsResolver 74 Defining a Tensor Arena 74 Creating an Interpreter 75 Inspecting the Input Tensor 75 Running Inference on an Input 78 Reading the Output 80 Running the Tests 82 Project File Structure 85 Walking Through the Source 86 Starting with main_functions.cc 87 Handling Output with output_handler.cc 90 Wrapping Up main_functions.cc 91 Understanding main.cc 91 Running Our Application 92 Wrapping Up 93 6. The “Hello World” of TinyML: Deploying to Microcontrollers. . . . . . . . . . . . . . . . . . . . . . 95 What Exactly Is a Microcontroller? 96 Arduino 97 Handling Output on Arduino 98 iv | Table of Contents
Running the Example 101 Making Your Own Changes 106 SparkFun Edge 106 Handling Output on SparkFun Edge 107 Running the Example 110 Testing the Program 117 Viewing Debug Data 118 Making Your Own Changes 118 ST Microelectronics STM32F746G Discovery Kit 119 Handling Output on STM32F746G 119 Running the Example 124 Making Your Own Changes 126 Wrapping Up 126 7. Wake-Word Detection: Building an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 What We’re Building 128 Application Architecture 129 Introducing Our Model 130 All the Moving Parts 132 Walking Through the Tests 133 The Basic Flow 134 The Audio Provider 138 The Feature Provider 139 The Command Recognizer 145 The Command Responder 151 Listening for Wake Words 152 Running Our Application 156 Deploying to Microcontrollers 156 Arduino 157 SparkFun Edge 165 ST Microelectronics STM32F746G Discovery Kit 175 Wrapping Up 180 8. Wake-Word Detection: Training a Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Training Our New Model 182 Training in Colab 182 Using the Model in Our Project 197 Replacing the Model 197 Updating the Labels 198 Updating command_responder.cc 198 Other Ways to Run the Scripts 201 How the Model Works 202 Table of Contents | v
Visualizing the Inputs 202 How Does Feature Generation Work? 206 Understanding the Model Architecture 208 Understanding the Model Output 213 Training with Your Own Data 214 The Speech Commands Dataset 215 Training on Your Own Dataset 216 How to Record Your Own Audio 216 Data Augmentation 218 Model Architectures 218 Wrapping Up 219 9. Person Detection: Building an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 What We’re Building 222 Application Architecture 224 Introducing Our Model 224 All the Moving Parts 225 Walking Through the Tests 227 The Basic Flow 227 The Image Provider 231 The Detection Responder 232 Detecting People 233 Deploying to Microcontrollers 235 Arduino 236 SparkFun Edge 246 Wrapping Up 257 10. Person Detection: Training a Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Picking a Machine 259 Setting Up a Google Cloud Platform Instance 260 Training Framework Choice 268 Building the Dataset 269 Training the Model 270 TensorBoard 272 Evaluating the Model 274 Exporting the Model to TensorFlow Lite 274 Exporting to a GraphDef Protobuf File 274 Freezing the Weights 275 Quantizing and Converting to TensorFlow Lite 275 Converting to a C Source File 276 Training for Other Categories 277 Understanding the Architecture 277 vi | Table of Contents
分享到:
收藏