Machine Learning Foundations: Ep #3 - Convolutions and pooling

Google for Developers · Beginner ·📐 ML Fundamentals ·6y ago

Key Takeaways

The video covers the basics of convolutions and pooling in machine learning, specifically in the context of computer vision using TensorFlow.

Full Transcript

Hi, and welcome back to machine learning foundations for Google developers. I'm Laurence Moroney and I'll be your host and guide. In the last episode, you took your first steps into computer vision and you built a neural network that could recognize items of clothing. You then did an exercise with a similarly sized data set, but with different content, and that was handwriting recognition. So, before we get on to the new stuff, let's take a look at the answer to the exercise that you did. So, let's take a look at the answer to exercise two. The code is here. One of the things I've done is to implement a callback. I've done that by creating a class called my callback. And in this class, I defined on epoch end, and this takes self, epoch, and logs. From the logs, I can get the accuracy. And if the accuracy, for example, is greater than 0.99, I can say print we've reached 99% accuracy, so the training will be canceled. So, now for MNIST, I can just say MNIST is tf.keras.datasets.mnist. I can load and normalize my data as before. I can create an instance of my callback that I'm just calling callbacks. And then when I'm training, I can add this extra parameter called callbacks and say callbacks equals this instance of my callback. So, if I were to run this, I'll get this warning, but let's see it running. And here you see after five epochs, the accuracy was 0.9917, which is greater than 99%, so it canceled training after only five of the 10 epochs, and it was able to hit that 99% accuracy. So, that's the answer to exercise two. Hope you were able to get it. Okay, now that you've looked at both fashion and handwriting examples for computer vision, you've probably noticed a big limitation for computer vision with these types of example. And that was that you could only have one item in the picture, and that it had to be centered, and it had to be well-defined. So, shoes had to face left, for example. A neural network type called a convolutional neural network can help here. It'll take us a little time to build up to it, and in this video, we'll talk all about what convolutions are, and how they can be used in combination with something called pooling to help a computer understand the contents of an image. It's going to take a little while to put it all together, but let's start with just understanding what a convolution is before you can start using one. The idea behind a convolution is very similar to image processing with filters. If you've used something like Photoshop before, it has filters to do things like sharpening an image or adding motion blur. So, while the effect is complex, the process behind it is quite simple. So, let's take a look at it in action. On the left here, I have an image of a boot from the Fashion-MNIST data set. On the right, I have a representation of nine of the pixels in the image. I'm going to call the center one my current pixel, and all of the others are its neighbors. Here, I define a filter, which is a set of values in the same shape as my pixel and its neighbors. So, for example, if I had one neighbor in each direction for a 3x3 grid of pixels, I'd have a 3x3 grid in my filter, too. Each value in the filter can be called a weight. So, to calculate a new value for my pixel, all I have to do is multiply each neighbor by their weight, my current pixel by its weight, and then add them all up. And I'm going to do this process for every pixel in the image. The result will be a transformed image. So, for example, if you look at the picture on the left here, if I apply the filter in the middle to it, I'll get the picture on the right. This filter has led to a huge emphasis on vertical lines. They really pop now. And similarly, this filter leads to an emphasis on horizontal lines. So, you might be wondering at this point, what does this have to do with computer vision? Ultimately, the goal of trying to understand what an item is isn't just matching the raw pixels to labels like we did with the fashion and handwriting examples. But what if we could extract features from the image instead? And then when an image had this set of features, it was this class. Or if it had that set of features, it was that class. And that's the heart of what convolutional neural networks do. They process the images down into raw features, and then they find sets of features that will match the label. They do this using filters. And just like neurons learned weights and biases to add up to what we needed, convolutions will learn the appropriate filters through an initial randomization, and then using the loss function and optimizer to tweak them for better results. So, for example, take a look at these images and you can see how they've been transformed by filters. And these filters learned how to isolate and abstract features within them. You can see, for example, that something like our horizontal line detector was able to detect a commonality in these images. They're all shoes, and that line is the sole of the shoe. But if you consider these images with the same filter, they had different results. So, if a lot of pixels are lighting up with the horizontal line filter, we could kind of sort of describe that as a sole detector. Now, you might hear that phrase a lot, detector. But what it's referring to is ultimately a filter that can extract a feature that can be used to determine a class. I first heard it in the cats versus dogs classifier, which we'll look at in a later video, when somebody visualized a floppy ear detector, which determined something was a dog and not a cat. One other thing that you might have noticed in the previous slides was also that the resolution of the images was decreasing. This is achieved through something called a pooling. The idea here is pretty simple. If there's a way that we can extract the feature while removing extraneous information, we can actually learn much faster. Now, let's take a look at how this works. The concept is actually quite simple. Imagine this is a chunk of our pixels. To keep it simple, I'm just going to have them as monochrome. We then look at them in blocks of 2 by 2 like this, and then we'll only keep the biggest value, which in this case is 192. We then repeat the process for the next 2 by 2 and keep the biggest, which is 144. And the next, which yields 255. And the next, which yields 168. We then put these four results together to yield a new 2 by 2 block, and this contains what had been the largest values in the 2 by 2 constituents of the previous 4 by 4 block. We've now reduced the size by 75% and maybe, just maybe, we've kept the important information. So, let's see what this actually looks like. And here is the image that we had filtered earlier to detect vertical lines. On the left is what it looks like before pooling, and on the right is what it looks like after pooling in the way that we just demonstrated. Notice how the information hasn't just been maintained, you could argue that it's also been emphasized. Pooling is an important way also of reducing the amount of information your model has to process. If you think about it, say you want to learn 100 filters, that means you would have to keep track of 101 versions of your image, the original plus the results of what it would look like after the filters have been applied. If you have thousands of images, you'll soon start to eat memory. And that's just one layer. What if you had another layer beneath this that also learned 100 filters? That would mean each of your first 100 will also have 100 products from it, giving you 10,000 images for every image you need to train. Any technique that can reduce size while keeping this information is obviously very valuable. Okay, so now that we've seen what a convolution is and how it works as well as how it can go hand-in-hand with pooling, let's take some time to see how to code for them. Here's the URL of a Colab containing codes that can filter an image as well as do pooling. You can play with the filter parameters or try different effects or just use the ones that I've provided. Pause the video now and give it a try. So, let's take a look at some convolutions in action and here's the lab that you'll be doing yourself in a few moments. You might remember some of the images from Fashion MNIST, but I want to talk about for example if you were going to classify a shoe instead of the one that you had in Fashion MNIST. One of the ways of doing this is with convolutions and in this lab you'll just take a quick experiment and just see how the filters of convolutions work. So, we'll start by importing some of the required Python libraries. And one of the things that's built into these libraries is this image called ascent. So, we can use the pyplot library to draw it so we can see what it looks like. And it's a person walking up some stairs. I'm now going to just copy that into a numpy array so that I can manipulate it. And here I've defined a number of different filters. Remember the 3 by 3 filters that we were showing? I'm just implementing them as three arrays of three items. So, for example, we use this one filter here. And one little thing that I've done is added a weight to this so that one of the things is that all the digits should add up to one, but if they do add up to something more than that, then you can multiply them out by a factor to normalize them. So, for example, if they added up to 10, you could set the weight to 0.1 so that final result would be normalized back to one. Now, here's just simply a loop going over the image and multiplying out the relevant pixel and its neighbors by the relevant item in the filter. Once it's done that, we can plot it to see the result. And here you can see the filter that I'm using is really emphasizing the image's vertical lines. For example, I could go and change to a different filter like this one. Run this to set the filter and then run the code again. And now plot the image again. We'll see the one that really emphasizes the horizontal lines. So, you can consider following filter values and look at what their impact on the image is or you can experiment with your own. As for pooling, we saw in the lesson that pooling is taking groups of pixels and just picking the biggest value. So, if we were to do a 2 by 2 pool, we'll take 2 by 2 blocks, pick the biggest value. So, a 4 by 4 block like this one would become a 2 by 2 block. We'd lose 3/4 of the information, but hopefully maintain what's in the image. So, for example, here I've implemented a simple pooling. So, if I'm to run this on the image that we've just seen, it will get pulled to this. If you look at the axis, the axis is now it's 256 by 256. And if we look at our original image, it was 512 by 512. This is the original image after the filter. This is the pulled image after the filter. We haven't really lost any information. So again, have a play with it and try it for yourself. Great. So, you've now had a look at how filters can convolve images, providing the basis of convolutional layers, as well as how pooling can reduce the image size while maintaining the features of an image. There's no assignment this time. Yay! So, in the next video, you'll start implementing convolutions and pooling in code and you'll see how these can improve the Fashion MNIST classifier. After that, you'll take on some more real-world and more challenging images and you can learn how convolutions can help you to classify them. So, don't forget to hit that subscribe button for more great content. Thank you. Mhm.

Original Description

Machine Learning Foundations is a free training course where you’ll learn the fundamentals of building machine learned models using TensorFlow. In Episode 3 we’ll expand on our computer vision example from last time and introduce the concept of convolutions--what they are, and how they can be used in combination with pooling to help a computer understand the contents of an image. Exercise 2 answer → https://goo.gle/3fsgPiY Colab: What are convolutions? → https://goo.gle/2L89MhI TensorFlow is Google’s end-to-end open source machine learning platform. For more videos about TensorFlow, subscribe to the TF YouTube channel → https://goo.gle/TensorFlow Machine Learning Foundations playlist → https://goo.gle/ml-foundations Subscribe to Google Developers → https://goo.gle/developers
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Google for Developers · Google for Developers · 47 of 60

1 Developer Journey - Sunnyvale DSC Summit ‘19
Developer Journey - Sunnyvale DSC Summit ‘19
Google for Developers
2 How Google is working with students - Sunnyvale DSC Summit ‘19
How Google is working with students - Sunnyvale DSC Summit ‘19
Google for Developers
3 Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Google for Developers
4 The Solution Challenge  - Sunnyvale DSC Summit ‘19
The Solution Challenge - Sunnyvale DSC Summit ‘19
Google for Developers
5 Firebase - Sunnyvale DSC Summit ‘19
Firebase - Sunnyvale DSC Summit ‘19
Google for Developers
6 Cloud Hero - Sunnyvale DSC Summit ‘19
Cloud Hero - Sunnyvale DSC Summit ‘19
Google for Developers
7 Panel discussion  - Sunnyvale DSC Summit ‘19
Panel discussion - Sunnyvale DSC Summit ‘19
Google for Developers
8 The art of negotiation - Sunnyvale DSC Summit ‘19
The art of negotiation - Sunnyvale DSC Summit ‘19
Google for Developers
9 Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Google for Developers
10 Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Google for Developers
11 [DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
[DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
Google for Developers
12 Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Google for Developers
13 Why Podcast.app chose a .app domain name
Why Podcast.app chose a .app domain name
Google for Developers
14 Machine Learning Bootcamp Jakarta 2019
Machine Learning Bootcamp Jakarta 2019
Google for Developers
15 Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Google for Developers
16 [DEPRECATING]  Importance of community (Assistant on Air)
[DEPRECATING] Importance of community (Assistant on Air)
Google for Developers
17 Why the Flutter team switched from .io to a .dev domain name
Why the Flutter team switched from .io to a .dev domain name
Google for Developers
18 3 website-building tips from .dev creators
3 website-building tips from .dev creators
Google for Developers
19 Why NimbleDroid chose a .app domain name
Why NimbleDroid chose a .app domain name
Google for Developers
20 Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Google for Developers
21 Google for Games Developer Summit: A free, digital experience for game developers
Google for Games Developer Summit: A free, digital experience for game developers
Google for Developers
22 Inspecting Home Graph (Assistant for Developers Pro Tips)
Inspecting Home Graph (Assistant for Developers Pro Tips)
Google for Developers
23 Google for Games Developer Summit Keynote
Google for Games Developer Summit Keynote
Google for Developers
24 Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Google for Developers
25 Empowering game developers with Stadia R&D (Google Games Dev Summit)
Empowering game developers with Stadia R&D (Google Games Dev Summit)
Google for Developers
26 Supercharging discoverability with Stadia (Google Games Dev Summit)
Supercharging discoverability with Stadia (Google Games Dev Summit)
Google for Developers
27 Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Google for Developers
28 Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Google for Developers
29 Live Captioning in Google Slides
Live Captioning in Google Slides
Google for Developers
30 [DEPRECATING]  User engagement for the Google Assistant
[DEPRECATING] User engagement for the Google Assistant
Google for Developers
31 TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
Google for Developers
32 Top 5 from the TensorFlow Dev Summit 2020
Top 5 from the TensorFlow Dev Summit 2020
Google for Developers
33 Developer Student Clubs 2019 Turkey Leads Summit
Developer Student Clubs 2019 Turkey Leads Summit
Google for Developers
34 Building simpler payment experiences | Google Pay Plugin for Magento 2
Building simpler payment experiences | Google Pay Plugin for Magento 2
Google for Developers
35 Become A Developer Student Club Lead
Become A Developer Student Club Lead
Google for Developers
36 Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Google for Developers
37 Test suite for Smart Home (Assistant for Developers Pro Tips)
Test suite for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
38 Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google for Developers
39 How to use error logs (Assistant for Developers Pro Tips)
How to use error logs (Assistant for Developers Pro Tips)
Google for Developers
40 Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Google for Developers
41 WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
Google for Developers
42 Offline handling for Smart Home (Assistant for Developers Pro Tips)
Offline handling for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
43 Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Google for Developers
44 Machine Learning Foundations: Ep #1 - What is ML?
Machine Learning Foundations: Ep #1 - What is ML?
Google for Developers
45 Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Google for Developers
46 Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Google for Developers
Machine Learning Foundations: Ep #3 - Convolutions and pooling
Machine Learning Foundations: Ep #3 - Convolutions and pooling
Google for Developers
48 Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Google for Developers
49 Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Google for Developers
50 Google Developers ML Summit
Google Developers ML Summit
Google for Developers
51 Real-world image classification using convolutional neural networks | Machine Learning Foundations
Real-world image classification using convolutional neural networks | Machine Learning Foundations
Google for Developers
52 Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Google for Developers
53 Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Google for Developers
54 Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Google for Developers
55 Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Google for Developers
56 Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Google for Developers
57 Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Google for Developers
58 Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Google for Developers
59 Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Google for Developers
60 AR Realism with the ARCore Depth API
AR Realism with the ARCore Depth API
Google for Developers

This video teaches the basics of convolutions and pooling in machine learning, with a focus on computer vision applications using TensorFlow. Viewers will learn how to apply these concepts to build and train models. The video is part of a larger course on machine learning foundations.

Key Takeaways
  1. Import necessary libraries and load data
  2. Define convolutional and pooling layers
  3. Configure model architecture
  4. Compile and train the model
  5. Evaluate model performance
💡 Convolutions and pooling are essential components of convolutional neural networks, allowing models to extract features from images and other spatial data.

Related Reads

Up next
Machine Learning with Rust and Candle: Part 3
Stephen Blum
Watch →