Deep Learning For Computer Vision With Python

3y ago
31 Views
11 Downloads
8.05 MB
50 Pages
Last View : 1m ago
Last Download : 1m ago
Upload by : Halle Mcleod
Transcription

Deep Learning for Computer Vision withPythonStarter BundleDr. Adrian Rosebrock1st Edition (1.1.0)

Copyright c 2017 Adrian Rosebrock, PyImageSearch.comP UBLISHED BY P Y I MAGE S EARCHPYIMAGESEARCH . COMThe contents of this book, unless otherwise indicated, are Copyright c 2017 Adrian Rosebrock,PyimageSearch.com. All rights reserved. Books like this are made possible by the time invested bythe authors. If you received this book and did not purchase it, please consider making future bookspossible by buying a copy at er-visionpython-book/ today.First printing, September 2017

To my father, Joe; my wife, Trisha;and the family beagles, Josie and Jemma.Without their constant love and support,this book would not be possible.

Contents1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.1I Studied Deep Learning the Wrong Way. . . This Is the Right Way151.2Who This Book Is For171.2.11.2.2Just Getting Started in Deep Learning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Already a Seasoned Deep Learning Practitioner? . . . . . . . . . . . . . . . . . . . . . . 171.3Book Organization1.3.11.3.21.3.31.3.4Volume #1: Starter Bundle . . . . .Volume #2: Practitioner Bundle .Volume #3: ImageNet Bundle . .Need to Upgrade Your Bundle?1.4Tools of the Trade: Python, Keras, and Mxnet1.4.11.4.2What About TensorFlow? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Do I Need to Know OpenCV? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191.5Developing Our Own Deep Learning Toolset191.6Summary202What Is Deep Learning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.1A Concise History of Neural Networks and Deep Learning222.2Hierarchical Feature Learning242.3How "Deep" Is Deep?272.4Summary303Image Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.1Pixels: The Building Blocks of Images3.1.1Forming an Image From Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3417.171818181831

3.2The Image Coordinate System343.2.13.2.2Images as NumPy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35RGB and BGR Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.3Scaling and Aspect Ratios363.4Summary384Image Classification Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394.1What Is Image Classification?4.1.14.1.24.1.3A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40The Semantic Gap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424.2Types of Learning4.2.14.2.24.2.3Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Unsupervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Semi-supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474.3The Deep Learning Classification Pipeline4.3.14.3.24.3.34.3.44.3.54.3.64.3.7A Shift in Mindset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Step #1: Gather Your Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Step #2: Split Your Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Step #3: Train Your Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Step #4: Evaluate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Feature-based Learning versus Deep Learning for Image ClassificationWhat Happens When my Predictions Are Incorrect? . . . . . . . . . . . . . . .4.4Summary5Datasets for Image Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535.1MNIST535.2Animals: Dogs, Cats, and Pandas545.3CIFAR-10555.4SMILES555.5Kaggle: Dogs vs. Cats565.6Flowers-17565.7CALTECH-101575.8Tiny ImageNet 52525.10.1 What Is ImageNet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585.10.2 ImageNet Large Scale Visual Recognition Challenge (ILSVRC) . . . . . . . . . . . . 585.11Kaggle: Facial Expression Recognition Challenge595.12Indoor CVPR605.13Stanford Cars605.14Summary60

6Configuring Your Development Environment . . . . . . . . . . . . . . . . . . . . 636.1Libraries and Packages6.1.16.1.26.1.36.1.4Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Keras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Mxnet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .OpenCV, scikit-image, scikit-learn, and more6.2Configuring Your Development Environment?646.3Preconfigured Virtual Machine656.4Cloud-based Instances656.5How to Structure Your Projects656.6Summary667Your First Image Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677.1Working with Image Datasets7.1.17.1.27.1.37.1.4Introducing the “Animals” Dataset . . .The Start to Our Deep Learning ToolkitA Basic Image Preprocessor . . . . . . . .Building an Image Loader . . . . . . . . . .7.2k-NN: A Simple Classifier7.2.17.2.27.2.37.2.47.2.5A Worked k-NN Examplek-NN Hyperparameters .Implementing k-NN . . . .k-NN Results . . . . . . . . . .Pros and Cons of k-NN . .7.3Summary8Parameterized Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818.1An Introduction to Linear Classification8.1.18.1.28.1.38.1.4Four Components of Parameterized Learning . . . . . . . . . . . . . . .Linear Classification: From Images to Labels . . . . . . . . . . . . . . . . .Advantages of Parameterized Learning and Linear ClassificationA Simple Linear Classifier With Python . . . . . . . . . . . . . . . . . . . . . .8.2The Role of Loss Functions8.2.18.2.28.2.3What Are Loss Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Multi-class SVM Loss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Cross-entropy Loss and Softmax Classifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918.3Summary9Optimization Methods and Regularization . . . . . . . . . . . . . . . . . . . . . . 959.1Gradient Descent9.1.19.1.29.1.39.1.49.1.59.1.6The Loss Landscape and Optimization Surface . .The “Gradient” in Gradient Descent . . . . . . . . . .Treat It Like a Convex Problem (Even if It’s Not) . .The Bias Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . .Pseudocode for Gradient Descent . . . . . . . . . . .Implementing Basic Gradient Descent in 8485889496.9697989899100

9.1.7Simple Gradient Descent Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049.2Stochastic Gradient Descent (SGD)9.2.19.2.29.2.3Mini-batch SGD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Implementing Mini-batch SGD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107SGD Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1109.3Extensions to SGD9.3.19.3.29.3.3Momentum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Nesterov’s Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Anecdotal Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1139.4Regularization9.4.19.4.29.4.39.4.4What Is Regularization and Why Do We Need It? . . . . . . . . . . . . .Updating Our Loss and Weight Update To Include RegularizationTypes of Regularization Techniques . . . . . . . . . . . . . . . . . . . . . . . .Regularization Applied to Image Classification . . . . . . . . . . . . . . .9.5Summary10Neural Network Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12110.1Neural Network Basics106111113.11311511611711912110.1.1 Introduction to Neural Networks . . . . . . . . . . . . .10.1.2 The Perceptron Algorithm . . . . . . . . . . . . . . . . . .10.1.3 Backpropagation and Multi-layer Networks . . . .10.1.4 Multi-layer Networks with Keras . . . . . . . . . . . . . .10.1.5 The Four Ingredients in a Neural Network Recipe10.1.6 Weight Initialization . . . . . . . . . . . . . . . . . . . . . . .10.1.7 Constant Initialization . . . . . . . . . . . . . . . . . . . . . .10.1.8 Uniform and Normal Distributions . . . . . . . . . . . . .10.1.9 LeCun Uniform and Normal . . . . . . . . . . . . . . . . .10.1.10 Glorot/Xavier Uniform and Normal . . . . . . . . . . .10.1.11 He et al./Kaiming/MSRA Uniform and Normal . . .10.1.12 Differences in Initialization Implementation . . . . 811Convolutional Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16911.1Understanding nvolutions versus Cross-correlation . . . . . . . .The “Big Matrix” and “Tiny Matrix" Analogy . . . .Kernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Hand Computation Example of ConvolutionImplementing Convolutions with Python . . . . . .The Role of Convolutions in Deep Learning . . . .11.2CNN Building yer Types . . . . . . . . .Convolutional Layers . .Activation Layers . . . . .Pooling Layers . . . . . . .Fully-connected LayersBatch Normalization . .Dropout . . . . . . . . . . . .170.170171171172173179179.181181186186188189190

11.3Common Architectures and Training Patterns19111.3.1 Layer Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19111.3.2 Rules of Thumb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19211.4Are CNNs Invariant to Translation, Rotation, and Scaling?19411.5Summary19512Training Your First CNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19712.1Keras Configurations and Converting Images to Arrays19712.1.1 Understanding the keras.json Configuration File . . . . . . . . . . . . . . . . . . . . . . . 19712.1.2 The Image to Array Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19812.2ShallowNet20012.2.1 Implementing ShallowNet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20012.2.2 ShallowNet on Animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20212.2.3 ShallowNet on CIFAR-10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20612.3Summary20913Saving and Loading Your Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21113.1Serializing a Model to Disk21113.2Loading a Pre-trained Model from Disk21413.3Summary21714LeNet: Recognizing Handwritten Digits . . . . . . . . . . . . . . . . . . . . . . . . 21914.1The LeNet Architecture21914.2Implementing LeNet22014.3LeNet on MNIST22214.4Summary22715MiniVGGNet: Going Deeper with CNNs . . . . . . . . . . . . . . . . . . . . . . . 22915.1The VGG Family of Networks22915.1.1 The (Mini) VGGNet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23015.2Implementing MiniVGGNet23015.3MiniVGGNet on CIFAR-1023415.3.1 With Batch Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23615.3.2 Without Batch Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23715.4Summary23816Learning Rate Schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24116.1Dropping Our Learning Rate24116.1.1 The Standard Decay Schedule in Keras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24216.1.2 Step-based Decay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24316.1.3 Implementing Custom Learning Rate Schedules in Keras . . . . . . . . . . . . . . . . 24416.2Summary249

17Spotting Underfitting and Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . 25117.1What Are Underfitting and Overfitting?25117.1.1 Effects of Learning Rates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25317.1.2 Pay Attention to Your Training Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25417.1.3 What if Validation Loss Is Lower than Training Loss? . . . . . . . . . . . . . . . . . . . . . 25417.2Monitoring the Training Process25517.2.1 Creating a Training Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25517.2.2 Babysitting Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25717.3Summary26018Checkpointing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26318.1Checkpointing Neural Network Model Improvements26318.2Checkpointing Best Neural Network Only26718.3Summary26919Visualizing Network Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27119.1The Importance of Architecture Visualization27119.1.1 Installing graphviz and pydot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27219.1.2 Visualizing Keras Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27219.2Summary27520Out-of-the-box CNNs for Classification . . . . . . . . . . . . . . . . . . . . . . . . 27720.1State-of-the-art CNNs in Keras20.1.120.1.220.1.320.1.420.1.5VGG16 and VGG19 .ResNet . . . . . . . . . . .Inception V3 . . . . . .Xception . . . . . . . . .Can We Go Smaller?20.2Classifying Images with Pre-trained ImageNet CNNs.277.27827928028028028120.2.1 Classification Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28420.3Summary28621Case Study: Breaking Captchas with a CNN . . . . . . . . . . . . . . . . . . 28721.1Breaking Captchas with a CNN21.1.121.1.221.1.321.1.421.1.521.1.621.1.7A Note on Responsible Disclosure . . . . . . . . . .The Captcha Breaker Directory Structure . . . .Automatically Downloading Example ImagesAnnotating and Creating Our Dataset . . . . . .Preprocessing the Digits . . . . . . . . . . . . . . . . . .Training the Captcha Breaker . . . . . . . . . . . . .Testing the Captcha Breaker . . . . . . . . . . . . . .21.2Summary22Case Study: Smile Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30722.1The SMILES Dataset288.288290291292297299303305307

22.2Training the Smile CNN30822.3Running the Smile CNN in Real-time31322.4Summary31623Your Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31923.1So, What’s Next?319

Companion WebsiteThank you for picking up a copy of Deep Learning for Computer Vision with Python! To accompanythis book I have created a companion website which includes: Up-to-date installation instructions on how to configure your development environment Instructions on how to use the pre-configured Ubuntu VirtualBox virtual machine andAmazon Machine Image (AMI) Supplementary material that I could not fit inside this bookAdditionally, you can use the “Issues” feature inside the companion website to report any bugs,typos, or problems you encounter when working through the book. I don’t expect many problems;however, this is a brand new book so myself and other readers would appreciate reporting anyissues you run into. From there, I can keep the book updated and bug free.To create your companion website account, just use this link:http://pyimg.co/fnkxkTake a second to create your account now so you’ll have access to the supplementary materialsas you work through the book.

1. Introduction“The secret of getting ahead is to get started.” – Mark TwainWelcome to Deep Learning for Computer Vision with Python. This book is your guide tomastering deep learning applied to practical, real-world computer vision problems utilizing thePython programming language and the Keras mxnet libraries. Inside this book, you’ll learn howto apply deep learning to take-on projects such as image classification, object detection, trainingnetworks on large-scale datasets, and much more.Deep Learning for Computer Vision with Python strives to be the perfect balance betweentheory taught in a classroom/textbook and the actual hands-on knowledge you’ll need to besuccessful in the real world.To accomplish this goal, you’ll learn in a practical, applied manner by training networks onyour own custom datasets and even competing in challenging state-of-the-art image classificationchallenges and competitions. By the time you finish this book, you’ll be well equipped to applydeep learning to your own projects. And with enough practice, I have no doubt that you’ll be ableto leverage your newly gained knowledge to find a job in the deep learning space, become a deeplearning for computer vision consultant/contractor, or even start your own computer vision-basedcompany that leverages deep learning.So grab your highlighter. Find a comfortable spot. And let me help you on your journey todeep learning mastery. Remember the most important step is the first one – to simply get started.1.1I Studied Deep Learning the Wrong Way. . . This Is the Right WayI want to start this book by sharing a personal story with you:Toward the end of my graduate school career (2013-2014), I started wrapping my head aroundthis whole "deep learning" thing due to a timing quirk. I was in a very unique

Deep Learning for Computer Vision with Python Starter Bundle Dr. Adrian Rosebrock 1st Edition (1.1.0)

Related Documents:

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

Deep Learning: Top 7 Ways to Get Started with MATLAB Deep Learning with MATLAB: Quick-Start Videos Start Deep Learning Faster Using Transfer Learning Transfer Learning Using AlexNet Introduction to Convolutional Neural Networks Create a Simple Deep Learning Network for Classification Deep Learning for Computer Vision with MATLAB

10 tips och tricks för att lyckas med ert sap-projekt 20 SAPSANYTT 2/2015 De flesta projektledare känner säkert till Cobb’s paradox. Martin Cobb verkade som CIO för sekretariatet för Treasury Board of Canada 1995 då han ställde frågan

service i Norge och Finland drivs inom ramen för ett enskilt företag (NRK. 1 och Yleisradio), fin ns det i Sverige tre: Ett för tv (Sveriges Television , SVT ), ett för radio (Sveriges Radio , SR ) och ett för utbildnings program (Sveriges Utbildningsradio, UR, vilket till följd av sin begränsade storlek inte återfinns bland de 25 största

Hotell För hotell anges de tre klasserna A/B, C och D. Det betyder att den "normala" standarden C är acceptabel men att motiven för en högre standard är starka. Ljudklass C motsvarar de tidigare normkraven för hotell, ljudklass A/B motsvarar kraven för moderna hotell med hög standard och ljudklass D kan användas vid

LÄS NOGGRANT FÖLJANDE VILLKOR FÖR APPLE DEVELOPER PROGRAM LICENCE . Apple Developer Program License Agreement Syfte Du vill använda Apple-mjukvara (enligt definitionen nedan) för att utveckla en eller flera Applikationer (enligt definitionen nedan) för Apple-märkta produkter. . Applikationer som utvecklas för iOS-produkter, Apple .

2.3 Deep Reinforcement Learning: Deep Q-Network 7 that the output computed is consistent with the training labels in the training set for a given image. [1] 2.3 Deep Reinforcement Learning: Deep Q-Network Deep Reinforcement Learning are implementations of Reinforcement Learning methods that use Deep Neural Networks to calculate the optimal policy.

51 Image Processing and Computer Vision with MATLAB Both deep learning and image processing are great tools for computer vision. Use the right combination of tools to get the job done. MATLAB makes it easy and efficient to do both image processing and deep learning together. MATLAB helps you integrate a computer vision algorithm into the rest of