Share E-Book

Edge AI Engineering. Hands-on with the Raspberry Pi 2025 (Marcelo Rovai) (z-library.sk, 1lib.sk, z-lib.sk)

Author Marcelo Rovai

ai
Language English

No Description

Format PDF
Size 12.8 MB
4
Views
0
Downloads
0.00
Total Donations
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
Edge AI Engineering Hands-on with the Raspberry Pi Marcelo Rovai 2025-04-16
Page 3
Table of contents Preface 11 Acknowledgments 13 Introduction 14 Edge AI Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Why Edge AI Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The Raspberry Pi Advantage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 What You’ll Learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 About this Book 16 Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Structure and Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Classification of AI Applications 19 Fixed Function AI vs. Generative AI . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Fixed Function AI (Reactive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Generative AI (Proactive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 The Edge AI Advantage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Setup 23 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Raspberry Pi Models (covered in this book) . . . . . . . . . . . . . . . . . . . . 25 Engineering Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Hardware Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Raspberry Pi Zero 2W . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Raspberry Pi 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Installing the Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The Operating System (OS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Initial Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2
Page 4
Remote Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 SSH Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 To shut down the Raspi via terminal: . . . . . . . . . . . . . . . . . . . . . . . . 33 Transfer Files between the Raspi and a computer . . . . . . . . . . . . . . . . . 33 Increasing SWAP Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Installing a Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Installing a USB WebCam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Installing a Camera Module on the CSI port . . . . . . . . . . . . . . . . . . . . 45 Running the Raspi Desktop remotely . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Updating and Installing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Model-Specific Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Raspberry Pi Zero (Raspi-Zero) . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Raspberry Pi 4 or 5 (Raspi-4 or Raspi-5) . . . . . . . . . . . . . . . . . . . . . . 56 Image Classification 58 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Applications in Real-World Scenarios . . . . . . . . . . . . . . . . . . . . . . . . 59 Advantages of Running Classification on Edge Devices like Raspberry Pi . . . . 59 Setting Up the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Updating the Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Installing Required Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Setting up a Virtual Environment (Optional but Recommended) . . . . . . . . 60 Installing TensorFlow Lite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Installing Additional Python Libraries . . . . . . . . . . . . . . . . . . . . . . . 61 Creating a working directory: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Setting up Jupyter Notebook (Optional) . . . . . . . . . . . . . . . . . . . . . . 63 Verifying the Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Making inferences with Mobilenet V2 . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Define a general Image Classification function . . . . . . . . . . . . . . . . . . . 73 Testing with a model trained from scratch . . . . . . . . . . . . . . . . . . . . . 75 Installing Picamera2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Image Classification Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Data Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Training the model with Edge Impulse Studio . . . . . . . . . . . . . . . . . . . . . . 90 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 The Impulse Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Image Pre-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Model Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Model Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Trading off: Accuracy versus speed . . . . . . . . . . . . . . . . . . . . . . . . . 98 Model Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Deploying the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 3
Page 5
Live Image Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Conclusion: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Object Detection 116 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Object Detection Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Pre-Trained Object Detection Models Overview . . . . . . . . . . . . . . . . . . . . . 120 Setting Up the TFLite Environment . . . . . . . . . . . . . . . . . . . . . . . . 121 Creating a Working Directory: . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Inference and Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 EfficientDet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Object Detection Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Raw Data Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Labeling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Training an SSD MobileNet Model on Edge Impulse Studio . . . . . . . . . . . . . . 141 Uploading the annotated data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 The Impulse Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Preprocessing all dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Model Design, Training, and Test . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Deploying the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Inference and Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Training a FOMO Model at Edge Impulse Studio . . . . . . . . . . . . . . . . . . . . 158 How FOMO works? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Impulse Design, new Training and Testing . . . . . . . . . . . . . . . . . . . . . 161 Deploying the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Inference and Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Exploring a YOLO Model using Ultralitics . . . . . . . . . . . . . . . . . . . . . . . 171 Talking about the YOLO Model . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Testing the YOLO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Export Model to NCNN format . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Exploring YOLO with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Training YOLOv8 on a Customized Dataset . . . . . . . . . . . . . . . . . . . . 181 Inference with the trained model, using the Raspi . . . . . . . . . . . . . . . . . 185 Object Detection on a live stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Counting objects with YOLO 194 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 4
Page 6
Installing and using Ultralytics YOLOv8 . . . . . . . . . . . . . . . . . . . . . . . . . 196 Testing the YOLO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Export Model to NCNN format . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exploring YOLO with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Estimating the number of Bees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Pre-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Training YOLOv8 on a Customized Dataset . . . . . . . . . . . . . . . . . . . . 211 Inference with the trained model, using the Rasp-Zero . . . . . . . . . . . . . . . . . 215 Considerations about the Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . 219 Script For Reading the SQLite Database . . . . . . . . . . . . . . . . . . . . . . 224 Adding Environment data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Small Language Models (SLM) 229 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Raspberry Pi Active Cooler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Generative AI (GenAI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Large Language Models (LLMs) . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Closed vs Open Models: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Small Language Models (SLMs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Ollama . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Installing Ollama . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Meta Llama 3.2 1B/3B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Google Gemma 2 2B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Microsoft Phi3.5 3.8B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Multimodal Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Inspecting local resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Ollama Python Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Function Calling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 1. Importing Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 2. Defining Input and Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 3. Defining the Response Data Structure . . . . . . . . . . . . . . . . . . . . . . 262 4. Setting Up the OpenAI Client . . . . . . . . . . . . . . . . . . . . . . . . . . 262 5. Generating the Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 6. Calculating the Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Adding images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 SLMs: Optimization Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 RAG Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 A simple RAG project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 5
Page 7
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Vision-Language Models at the Edge 282 Why Florence-2 at the Edge? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Florence-2 Model Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Technical Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Training Dataset (FLD-5B) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Key Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Practical Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Comparing Florence-2 with other VLMs . . . . . . . . . . . . . . . . . . . . . . 288 Setup and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Environment configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Testing the installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 4. Defining the Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 7. Generating the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Florence-2 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Exploring computer vision and vision-language tasks . . . . . . . . . . . . . . . . . . 303 Caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 DETAILED_CAPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 MORE_DETAILED_CAPTION . . . . . . . . . . . . . . . . . . . . . . . . . . 306 OD - Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 DENSE_REGION_CAPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 CAPTION_TO_PHRASE_GROUNDING . . . . . . . . . . . . . . . . . . . . 310 Cascade Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 OPEN_VOCABULARY_DETECTION . . . . . . . . . . . . . . . . . . . . . . 312 Referring expression segmentation . . . . . . . . . . . . . . . . . . . . . . . . . 313 Region to Segmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Region to Texts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 OCR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Latency Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Fine-Tunning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Key Advantages of Florence-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Trade-offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Best Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Future Implications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Physical Computing with Raspberry Pi 327 From Sensors to Smart Analysis with Small Language Models . . . . . . . . . . 327 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 6
Page 8
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Install the Raspi Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Interacting with the Raspi via SSH . . . . . . . . . . . . . . . . . . . . . . . . . 331 Accessing the GPIOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Pin Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 “Hello World”: Blinking an LED . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Installing all LEDs (the “actuators”) . . . . . . . . . . . . . . . . . . . . . . . . 335 Sensors Installation and setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Installing Adafruit CircuitPython . . . . . . . . . . . . . . . . . . . . . . . . . . 339 DHT22 - Temperature & Humidity Sensor . . . . . . . . . . . . . . . . . . . . . 341 Installing the BMP280: Barometric Pressure & Altitude Sensor . . . . . . . . . 344 Measuring Weather and Altitude With BMP280 . . . . . . . . . . . . . . . . . 350 Playing with Sensors and Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Installing Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Testing the Notebook setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 GPIO Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Getting and displaying Sensor Data . . . . . . . . . . . . . . . . . . . . . . . . 361 Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Interacting an SLM with the Physical world . . . . . . . . . . . . . . . . . . . . . . . 364 Other Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Key Achievements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Technical Insights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Practical Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Challenges and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Future Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Experimenting with SLMs for IoT Control 376 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Software Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Basic Sensor Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 SLM Basic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Active Control Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Natural Language Interaction (User Command) . . . . . . . . . . . . . . . . . . . . . 392 Key Components and Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 System Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Example Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 7
Page 9
Data Logging and Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Evolution to Structured Command Processing . . . . . . . . . . . . . . . . . . . . . . 405 Structured Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 Improved Command Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 Benefits of the New Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 Handling Different Model Capabilities . . . . . . . . . . . . . . . . . . . . . . . 409 Example Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 Conclusion 412 Resourses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Advancing EdgeAI: Beyond Basic SLMs 414 Understanding SLM Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 1. Knowledge Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 2. Reasoning Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 3. Inconsistent Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 4. Domain Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 Techniques for Enhancing SLM at the Edge . . . . . . . . . . . . . . . . . . . . . . . 417 Optimizing Prompting Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Chain-of-Thought Prompting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Few-Shot Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Task Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Building Agents with SLMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 General Knowledge Router . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Improving Agent Reliability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 1. Function Calling with Pydantic . . . . . . . . . . . . . . . . . . . . . . . . . 434 2. Response Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Retrieval-Augmented Generation (RAG) . . . . . . . . . . . . . . . . . . . . . . . . . 438 Understanding RAG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 Implementing a Basic RAG System . . . . . . . . . . . . . . . . . . . . . . . . . 439 Key Components of Our Edge RAG System . . . . . . . . . . . . . . . . . . . . 440 Advantages of RAG for Edge AI . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Optimizing RAG for Edge Devices . . . . . . . . . . . . . . . . . . . . . . . . . 442 Application: Enhanced Weather Station with RAG . . . . . . . . . . . . . . . . 442 Using the RAG System for Edge AI Engineering . . . . . . . . . . . . . . . . . 444 Testing Different Models and Chunk Sizes . . . . . . . . . . . . . . . . . . . . . 448 Advanced Agentic RAG System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Key Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Important Code Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Detailed Workflow Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 8
Page 10
Fine-Tuning SLMs for Edge Deployment . . . . . . . . . . . . . . . . . . . . . . . . . 460 Preparing for Fine-Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Setting Up a Fine-Tuning Process . . . . . . . . . . . . . . . . . . . . . . . . . 461 Real implementation: Supervised Fine-Tuning (SFT) . . . . . . . . . . . . . . . 462 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Edge AI Engineering - Weekly Labs 466 Week 1: Introduction and Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 Lab 1: Raspberry Pi Configuration . . . . . . . . . . . . . . . . . . . . . . . . . 466 Lab 2: Development Environment Setup . . . . . . . . . . . . . . . . . . . . . . 466 Week 2: Image Classification Fundamentals . . . . . . . . . . . . . . . . . . . . . . . 467 Lab 3: Working with Pre-trained Models . . . . . . . . . . . . . . . . . . . . . . 467 Lab 4: Custom Dataset Creation . . . . . . . . . . . . . . . . . . . . . . . . . . 468 Week 3: Custom Image Classification . . . . . . . . . . . . . . . . . . . . . . . . . . 468 Lab 5: Edge Impulse Model Training . . . . . . . . . . . . . . . . . . . . . . . . 468 Lab 6: Model Deployment to Raspberry Pi . . . . . . . . . . . . . . . . . . . . 469 Week 4: Object Detection Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . 469 Lab 7: Pre-trained Object Detection . . . . . . . . . . . . . . . . . . . . . . . . 469 Lab 8: EfficientDet and FOMO Models . . . . . . . . . . . . . . . . . . . . . . 470 Week 5: Custom Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 Lab 9: Dataset Creation and Annotation . . . . . . . . . . . . . . . . . . . . . . 471 Lab 10: Training Models in Edge Impulse . . . . . . . . . . . . . . . . . . . . . 471 Week 6: Advanced Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Lab 11: FOMO Model Training . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Lab 12: YOLO Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Week 7: Object Counting Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Lab 13: Custom YOLO Training . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Lab 14: Fixed-Function AI Integration (Optional) . . . . . . . . . . . . . . . . 473 Week 8: Introduction to Generative AI . . . . . . . . . . . . . . . . . . . . . . . . . . 474 Lab 15: Raspberry Pi Configuration for SLMs . . . . . . . . . . . . . . . . . . . 474 Lab 16: Ollama Installation and Testing . . . . . . . . . . . . . . . . . . . . . . 474 Week 9: SLM Python Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Lab 17: Ollama Python Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Lab 18: Function Calling and Structured Outputs . . . . . . . . . . . . . . . . 476 Week 10: Retrieval-Augmented Generation . . . . . . . . . . . . . . . . . . . . . . . 477 Lab 19: RAG Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Lab 20: Advanced RAG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Week 11: Vision-Language Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Lab 21: Florence-2 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Lab 22: Vision Tasks with Florence-2 . . . . . . . . . . . . . . . . . . . . . . . 478 Week 12: Physical Computing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Lab 23: Sensor and Actuator Integration . . . . . . . . . . . . . . . . . . . . . . 479 9
Page 11
Lab 24: Jupyter Notebook Integration . . . . . . . . . . . . . . . . . . . . . . . 480 Week 13: SLM-Physical Computing Integration . . . . . . . . . . . . . . . . . . . . . 480 Lab 25: Basic SLM Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 Lab 26: SLM-IoT Control System . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Week 14: Advanced Edge AI Techniques . . . . . . . . . . . . . . . . . . . . . . . . . 482 Lab 27: Building Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482 Lab 28: Advanced Prompting and Validation . . . . . . . . . . . . . . . . . . . 482 Week 15: Final Project Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Lab 29: Agentic RAG System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Lab 30: Final Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Basic Setup (Weeks 1-7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Generative AI (Weeks 8-15) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Physical Computing (Weeks 12-15) . . . . . . . . . . . . . . . . . . . . . . . . . 484 Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Computer Vision and DL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Generative AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Physical Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Assessment Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 Tips for Success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 References 487 To learn more: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Online Courses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Projects Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 TinyML4D 488 About the author 489 10
Page 12
Preface In the rapidly evolving landscape of technology, the convergence of artificial intelligence and edge computing stands as one of the most exciting frontiers. This intersection promises to rev- olutionize how we interact with the world around us, bringing intelligence and decision-making capabilities directly to the devices we use every day. At the heart of this revolution lies the Raspberry Pi, a powerful yet accessible single-board computer (SBC) that has democratized computing and now stands poised to do the same for edge AI. This book, which serves as the official textbook for IESTI05 Edge AI Engineering at the Federal University of Itajubá (UNIFEI) in Brazil, represents both a passion for technology and a belief in its power to solve real-world problems. While developed to support UNIFEI’s engineering curriculum, the content is designed to be valuable for all learners, whether in academic settings or pursuing independent study. “Edge AI Engineering: Hands-on with the Raspberry Pi” is not just about theory or abstract concepts. It’s about getting your hands dirty, writing code, training models, and seeing your creations come to life. Each chapter blends foundational knowledge with practical application, focusing on what’s possible with the Raspberry Pi platform. From the compact Raspberry Pi Zero to the more powerful Pi 5, we explore how these incred- ible devices can become the brains of intelligent systems—recognizing images, understanding speech, detecting objects, and even running small language models. Each project serves as a stepping stone, building your skills and confidence as you progress. Beyond the technical skills, this book aims to instill something more valuable – a sense of curiosity and possibility. The field of edge AI is still in its infancy, with new applications and techniques emerging daily. By mastering the fundamentals presented here, you’ll be well- equipped to explore these frontiers, perhaps even pushing the boundaries of what’s possible on edge devices. Whether you’re a student seeking to understand AI’s practical applications, a professional expanding your skill set, or an enthusiast eager to add intelligence to your projects, we hope this book serves as both a guide and an inspiration. As you embark on this journey, remember that every expert was once a beginner. The learning path is filled with challenges and moments of joy and discovery. Embrace both, and let your creativity guide you. 11
Page 13
Thank you for joining us on this exciting adventure into edge machine learning. Let’s begin exploring what’s possible when we bring AI to the edge, one Raspberry Pi at a time. Happy coding, and may your models always converge! Prof. Marcelo Rovai Federal University of Itajubá, Brazil April, 2025 12
Page 14
Acknowledgments I extend my deepest gratitude to the entire TinyML4D Academic Network, comprised of distinguished professors, researchers, and professionals. Notable contributions from Marco Zennaro, Ermanno Petrosemoli, Brian Plancher, José Alberto Ferreira, Jesus Lopez, Diego Mendez, Shawn Hymel, Dan Situnayake, Pete Warden, and Laurence Moroney have been instrumental in advancing our understanding of Embedded Machine Learning (TinyML) and Edge AI. Special commendation is reserved for Professor Vijay Janapa Reddi of Harvard University. His steadfast belief in the transformative potential of open-source communities, coupled with his invaluable guidance and teachings, has served as a beacon and a cornerstone for our efforts from the beginning. Acknowledging these individuals, we pay tribute to the collective wisdom and dedication that have enriched this field and our work. Google ImageFX and OpenAI’s DALL-E generated illustrations of some of the images on the book and chapter covers. Claude Sonnet helped with code and text reviews. 13
Page 15
Introduction Edge AI Engineering In today’s rapidly evolving technological landscape, the convergence of artificial intelligence and edge computing represents one of the most promising frontiers of innovation. Edge AI— the practice of running AI algorithms locally on hardware devices rather than in the cloud— transforms how we interact with technology daily, enabling more responsive, private, and efficient intelligent systems. This book, “Edge AI Engineering: Hands-on with the Raspberry Pi,” is your practical guide to this exciting field. We’ll explore fixed-function AI (reactive systems that process specific inputs) and generative AI (proactive systems that create new content) through hands-on projects using the versatile and accessible Raspberry Pi platform. Why Edge AI Matters Traditional AI deployment often relies on cloud infrastructure, requiring constant connectivity and introducing latency. Edge AI addresses these limitations by bringing intelligence directly to where data is generated and actions occur. This approach offers several compelling advan- tages: • Reduced latency: Process data locally for near-instantaneous responses • Enhanced privacy: Keep sensitive information on your device rather than sending it to remote servers • Network independence: Maintain functionality even without internet connectivity • Lower bandwidth usage: Process data locally, sending only relevant results when needed • Energy efficiency: Optimize processing for resource-constrained environments The Raspberry Pi Advantage The Raspberry Pi, with its combination of affordability, processing capability, and extensive GPIO options, provides an ideal platform for exploring Edge AI concepts. From the compact Raspberry Pi Zero 2W to the more powerful Pi 5, these devices offer: 14
Page 16
• Sufficient computational power for running optimized AI models • A complete Linux-based operating system for straightforward development • Extensive connectivity options for integrating with sensors and actuators • A vibrant community and ecosystem of libraries and tools • An accessible entry point for students, hobbyists, and professionals alike What You’ll Learn This book takes a progressive approach to Edge AI engineering, starting with foundational concepts and building toward more advanced applications: 1. Essential setup and configuration: Prepare your Raspberry Pi for Edge AI develop- ment 2. Computer vision applications: Implement image classification and object detection systems 3. Small Language Models (SLMs): Run and optimize language models directly on your Raspberry Pi 4. Vision-Language Models: Explore multimodal AI with Florence-2 5. Physical computing integration: Connect AI systems with sensors and actuators 6. Advanced optimization techniques: Enhance model performance through methods like RAG, agents, and function calling Each chapter includes detailed explanations, step-by-step instructions, and practical projects demonstrating real-world applications of Edge AI concepts. Who This Book Is For Whether you’re a student exploring AI for the first time, an educator developing a curriculum, a maker building innovative projects, or a professional seeking to expand your skills, this book provides the knowledge and hands-on experience needed to implement Edge AI solutions on the Raspberry Pi platform successfully. Join us on this journey to the edge of AI innovation, where we’ll bridge theory and prac- tice through engaging, accessible projects that demonstrate the transformative potential of intelligent edge computing. 15
Page 17
About this Book Several chapters in this book are also part of the open book Machine Learning Systems, which we invite you to read. “Edge AI Engineering: Hands-on with the Raspberry Pi” is designed as a practical, project- based learning resource that bridges theoretical AI concepts with tangible implementations. This book is part of the open-source Machine Learning Systems initiative, democratizing access to AI education and applications. Key Features 1. Progressive Learning Path: The book structure follows a natural progression from basic to advanced concepts, beginning with foundational computer vision applications and advancing to generative AI techniques. 16
Page 18
2. Model-Specific Optimizations: Each chapter provides targeted guidance for different Raspberry Pi models, helping you maximize performance whether using a Pi Zero 2W or Pi 5. 3. Open-Source Foundation: We emphasize accessible tools and frameworks, including Edge Impulse Studio, TensorFlow Lite, PyTorch, Transformers, and Ollama, ensuring you can continue your learning journey with widely available resources. 4. Practical Problem-Solving: Rather than abstract exercises, each project addresses real-world challenges that demonstrate the practical value of Edge AI. 5. Resource Optimization Techniques: Learn essential strategies for deploying AI on resource-constrained devices, balancing performance needs with hardware limitations. 6. Cross-Domain Applications: Explore implementations spanning computer vision, natural language processing, and physical computing, showcasing the versatility of Edge AI. Structure and Organization The book is organized into two main sections: 1. Fixed Function AI (Computer Vision): Chapters covering image classification, ob- ject detection, and specialized applications like object counting. 2. Generative AI (Language and Vision Models): Chapters exploring Small Lan- guage Models, Vision-Language Models, physical computing integration, and advanced optimization techniques. Each chapter follows a consistent format that includes: • Conceptual background and theory • Step-by-step implementation guides • Practical projects with complete code • Performance optimization strategies • Ideas for further exploration Prerequisites While designed to be accessible, readers will benefit from: • Basic Python programming knowledge • Familiarity with Linux command-line basics • Elementary understanding of machine learning concepts • Previous experience with Raspberry Pi (helpful but not required) 17
Page 19
By completing this book, you’ll possess the skills to design, implement, and optimize Edge AI applications across a wide range of use cases, leveraging the unique capabilities of the Raspberry Pi platform to bring intelligence to the edge. 18
Page 20
Classification of AI Applications As we embark on our journey through Edge AI Engineering with the Raspberry Pi, it’s essential to understand the fundamental classification of AI applications that form the structure of this book. Our exploration is divided into two parts, each representing a different paradigm in artificial intelligence implementation. Fixed Function AI vs. Generative AI AI applications can be broadly categorized into two approaches that represent different capa- bilities, interaction models, and implementation strategies: Fixed Function AI (Reactive) Fixed Function AI, or Reactive AI, operates by analyzing specific inputs according to prede- termined patterns and rules and then producing consistent outputs for given scenarios. These systems: • Respond to specific triggers: They activate only when presented with particular inputs. • Follow defined patterns: Their behavior is predictable and consistent. • Excel at structured tasks: They perform exceptionally well at classification, detection, and pattern recognition • Operate within boundaries: Their capabilities are limited to their specific program- ming. In the first part of this book (Chapters 2-4), we explore fixed-function AI through computer vision applications: • Image classification for identifying objects in images • Object detection for locating and labeling multiple objects • Specialized detection applications like counting objects These applications demonstrate how edge devices can deliver reliable, efficient AI in constrained environments, focusing on specific, well-defined tasks. 19
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List