Practical Deep Learning for Coders: Lesson 1
Disclaimer: The transcript on this page is for the YouTube video titled "Practical Deep Learning for Coders: Lesson 1" from "Jeremy Howard". All rights to the original content belong to their respective owners. This transcript is provided for educational, research, and informational purposes only. This website is not affiliated with or endorsed by the original content creators or platforms.
Watch the original video here: https://www.youtube.com/watch?v=8SF_h3xF3cE&list=PLfYUBJiXbdtSvpQjSnJJ_PmDQB_VyT5iU&index=1
Welcome to Practical Deep Learning for Coders, Lesson 1. This is version five of this course, and it's the first new one we've done in two years. So, we've got a lot of cool things to cover! It's amazing how much has changed.
Here is an xkcd from the end of 2015. Who here has seen xkcd comics before? Pretty much everybody, not surprising. So the basic joke here is... I'll let you read it, and then I'll come back to it.
So, it can be hard to tell what's easy and what's nearly impossible. In 2015, or at the end of 2015, the idea of checking whether something is a photo of a bird was considered nearly impossible—so impossible, it was the basic idea of a joke because everybody knows that that's nearly impossible. We're now going to build exactly that system for free in about two minutes!
So, let's build an “is it a bird” system. We're going to use Python, and I'm going to run through this really quickly. You're not expected to run through it with me because we're going to come back to it. But let's go ahead and run that cell.
Okay, so what we're doing is we're searching DuckDuckGo for images of bird photos, and we're just going to grab one. Here is the URL of the bird that we grabbed. We'll download it.
Okay, so there it is. We've grabbed a bird, so we've now got something that can download pictures of birds. Now we're going to need to build a system that can recognize things that are birds versus things that aren't birds from photos.
Now, of course, computers need numbers to work with, but luckily images are made of numbers. I actually found this really nice website called Pixby where I can grab a bird, and if I wiggle over it—let's pick its beak—you'll see here that that part of the beak was 251 brightness of red, 48 of green, and 21 of blue. So that's RGB.
As I wave around, those numbers are changing. So this picture—the thing that we recognize as a picture—is actually $256 \times 171 \times 3$ numbers between 0 and 255, representing the amount of red, green, and blue on each pixel. So that's going to be an input to our program that's going to try and figure out whether this is a picture of a bird or not.
Okay, so let me go ahead and run this cell, which is going to go through... I needed bird and non-bird, but you can't really search Google Images or DuckDuckGo images for "not a bird"; it just doesn't work that way. So I just decided to use forest. I thought, okay, pictures of forest versus pictures of birds sounds like a good starting point.
So I go through each of forest and bird. I search for forest photo and bird photo, download images, and then resize them to be no bigger than 400 pixels on a side—just because we don't need particularly big ones and it takes a surprisingly large amount of time just for a computer to open an image. Okay, so we've now got 200 of each.
I find when I download images, I often get a few broken ones, and if you try and train a model with broken images, it will not work. So here's something which just verifies each image and unlinks—deletes—the ones that don't work.
Okay, so now we can create what's called a data block. I'll go through the details of this later, but a data block gives fast.ai (the library) all the information it needs to create a computer vision model. In this case, we're basically telling it to get all the image files that we just downloaded. And then we say show me a few, up to six. Let me see... yeah, so we've got some birds, forest, bird, bird, forest.
One of the nice things about doing computer vision models is it's really easy to check your data because you can just look at it, which is not the case for a lot of kinds of models.
Okay, so we've now downloaded 200 pictures of birds and 200 pictures of forests, so we'll now press run. This model is actually running on my laptop, so this is not using a vast data center; it's running on my presentation laptop. And it's doing it at the same time as my laptop is streaming video, which is possibly a bad idea.
What it's going to do is run through every photo out of those 400. For the ones that are forest, it's going to learn a bit more about what forest looks like, and for the ones that are bird, it'll learn a bit more about what bird looks like.
Overall, it took under 30 seconds. Believe it or not, that's enough to finish doing the thing which was in that xkcd comic. Let's check by passing in that bird that we downloaded at the start. "This is a bird. Probability it's a bird: 1.0000" (rounded to the nearest four decimal places).
So something pretty extraordinary has happened since late 2015, which is literally something that has gone from so impossible it's a joke to so easy that I can run it on my laptop computer in about two minutes. Hopefully, that gives you a sense that creating really interesting, real working programs with deep learning doesn't take a lot of code, didn't take any math, and didn't take more than my laptop computer. It's pretty accessible, in fact. That's really what we're going to be learning about over the next seven weeks.
Where have we got to now with deep learning? Well, it moves so fast, but even in the last few weeks, we've taken it up another notch as a community. You might have seen that something called DALL-E 2 has been released, which uses deep learning to generate new pictures.
I thought this was an amazing thing that this guy Nick did, where he took his friends' Twitter bios and typed them into the DALL-E 2 input, and it generated these pictures. This guy typed in "commitment, sympathetic, psychedelic, philosophical," and it generated these pictures. I'll just show you a few of these. I'll let you read them...
I love that. That one's pretty amazing, I reckon! I love this: "Happy Sisyphus" has actually got a happy rock to move around. When I look at these, I still get pretty blown away that this is a computer algorithm using nothing but this text input to generate these arbitrary pictures—in this case, fairly complex and creative things.
The guy who made those points out that he spends about two minutes or so creating each of these. He tries a few different prompts and a few different pictures. Here's an example of about ten different things he gets back when he puts in "expressive painting of a man shining rays of justice and transparency on a blue bird Twitter logo."
It's not just DALL-E 2, to be clear. There are a lot of different systems doing something like this now. There's something called Midjourney, which this Twitter account posted: "female scientist with a laptop writing code, in a symbolic, meaningful, and vibrant style." This one here is "an HD photo of a rare psychedelic pink elephant." And this one is "a blind bat with big sunglasses holding a walking stick in his hand."
When actual artists use this—for example, this guy said he knows nothing about art and has no artistic talent, it's just something he threw together. This other guy is an artist who actually writes his own software based on deep learning and spends months building stuff, and as you can see, you can really take it to the next level.
It's been really great to see how a lot of fast.ai alumni with backgrounds as artists have gone on to bring deep learning and art together. It's a very exciting direction.
And it's not just images, to be clear. Another interesting thing that's popped up in the last couple of weeks is Google's PaLM language model, which can take any arbitrary English text or question and create an answer which not only answers the question but also explains its thinking (whatever it means for a language model to be thinking).
One of the ones I found pretty amazing was that it can explain a joke. I'll let you read this...
So, this is actually a joke that probably needs explanations for anybody who's not familiar with TPUs. This model just took the text as input and created this text as output. Again, deep learning models are doing things which I think very few, if any of us, would have believed would be possible for computers to do even in our lifetime.
This means that there are a lot of practical and ethical considerations. We will touch on them during this course, but we can't possibly hope to do them justice. So I would certainly encourage you to check out ethics.fast.ai to see our whole data ethics course taught by my co-founder, Dr. Rachel Thomas, which goes into these issues in a lot more detail.
All right, so as well as being an AI researcher at the University of Queensland and fast.ai, I am also a homeschooling primary school teacher, and for that reason, I study education a lot. One of the people who I love in education is a guy named Dylan Wiliam. He has this great approach in his classrooms of figuring out how his students are getting along, which is to put a colored cup on their desk: green to mean that they're doing fine, yellow cup to mean "I'm not quite sure," and a red cup to mean "I have no idea what's going on."
Now, since most of you are watching this remotely, I can't look at your cups, and I don't think anybody brought colored cups with them today. So instead, we have an online version of this. What I want you to do is go to cups.fast.ai/fast. Don't do this if you're a fast.ai expert who's done the course five times, because if you're following along, that doesn't really mean much. This is really for people who are not already fast.ai experts. Click one of these colored buttons, and what I will do is go to the teacher version and see what buttons you're pressing.
All right! So, so far people are feeling we're not going too fast on the whole. We've got one brief red. Okay! Hey Nick, if you can keep that URL open as well and let me know if it suddenly gets covered in red.
If you are somebody who's red, I'm not going to come to you now because there's not enough of you to stop the class. So it's up to you to ask on the forum or on the YouTube live chat, and there are a lot of folks luckily who will be able to help you, I hope.
All right! I wanted to do a big shout-out to Radek. Radek created cups.fast.ai for me. I said to him last week I need a way of seeing colored cups on the internet, and he wrote it in one evening. I also wanted to shout out that Radek just announced today that he got a job at Nvidia AI! Fast.ai alumni around the world very frequently—like every day or two—email me to say that they've got their dream job.
If you're looking for inspiration on how to get into the field, nothing would be better than checking out Radek's work. He's actually written a book about his journey, which has a lot of tips in particular about how to take advantage of fast.ai and make the most of these lessons. Check that out as well. If you're here live, he's one of our TAs as well, so you can say hello to him afterwards. He looks exactly like this picture here.
I mentioned I spent a lot of time studying education, both for my homeschooling duties and also for my courses. You'll see that there's something very different about this course, which is that we started by training a model. We didn't start by doing an in-depth review of linear algebra and calculus.
That's because two of my favorite writers and researchers on education, Paul Lockhart and David Perkins (and many others), talk about how much better people learn when they learn with a context in place. The way we learn math at school—where we do counting, then adding, then fractions, then decimals, and 15 years later we start doing the really interesting stuff at grad school—is not the way most people learn effectively.
The way most people learn effectively is from the way we teach sports, for example, where we show you a whole game. We show you how much fun it is. You go and start playing simple versions of it; you're not very good, and then you gradually put more and more pieces together. So that's how we do deep learning.
You will go into as much depth as the most sophisticated, technically detailed classes you'll find—later! But first, you'll learn to be very good at actually building and deploying models, and you will learn why and how things work as you need to get to the next level.
Those of you that have spent a lot of time in technical education (like if you've done a PhD or something) will find this deeply uncomfortable because you'll be wanting to understand why everything works from the start. Just do your best to go along with it. Those of you who haven't will find this very natural.
And this is Dylan Wiliam, who I mentioned before—the guy who came up with the really cool cups thing. There'll be a lot of tricks that have come out of the educational research literature scattered through this course. On the whole, I won't call them out, but maybe from time to time we'll talk about them.
All right! Before we start talking about how we actually built that model and how it works, I guess I should convince you that I'm worth listening to. I'll try to do that reasonably quickly, because I don't like tooting my own horn, but I know it's important.
The first thing to mention about me is that my friend Sylvain and I wrote this extremely popular book, *Deep Learning for Coders*, and that book is what this course is heavily based on. We're not going to be using any material from the book directly, and you might be surprised by that, but the reason actually is that the educational research literature shows that people learn things best when they hear the same thing in multiple different ways. So I want you to read the book, and you'll also see the same information presented in a different way in these videos. One of the bits of homework after each lesson will be to read a chapter of the book.
A lot of people like the book. Peter Norvig, Director of Research at Google, loves the book: "one of the best sources for a programmer to become proficient in deep learning." Eric Topol loves the book. Hal Varian, Emeritus Professor at Berkeley and Chief Economist at Google, likes the book. Jerome Pesenti, who is the head of AI at Facebook, likes the book. So hopefully you'll find that you like this material as well.
I've spent about 30 years of my life working in and around machine learning, including building a number of companies that relied on it. I became the highest-ranked competitor in the world on Kaggle in machine learning competitions. My company Enlitic, which I founded, was the first company to specialize in deep learning for medicine, and MIT voted it one of the 50 smartest companies in 2016, just above Facebook and SpaceX.
I started fast.ai with Rachel Thomas quite a few years ago now, but it's had a big impact on the world already. Work we've done with our students has been globally recognized, such as our win in the DAWNBench competition, which showed how we could train big neural networks faster than anybody in the world, and cheaper than anybody in the world.
That was a really big step in 2018. Google started using our special approaches in their models, and Nvidia started optimizing their stuff using our approaches.
I'm the inventor of the ULMFiT algorithm, which, according to the Transformers book, was one of the two key foundations behind the modern NLP revolution. An interesting point about that is it was actually invented for a fast.ai course. The first time it appeared was not actually in a journal; it was in lesson four of the 2016 course.
Most importantly, I've been teaching this course since Version One. A lot of people have been watching the course and it's been widely used. Alumni have gone from this to doing really amazing things. Andrej Karpathy told me that at Tesla, pretty much everybody who joins Tesla in AI is meant to do this course. At OpenAI, they told me that all the residents joining there first do this course. So this course is really widely used in industry and research.
All right, so let me get back to what's happened here. Why are we able to create a bird recognizer in a minute or two, and why couldn't we do it before?
I'm going to go back to 2012. In 2012, this was how image recognition was done. This is the Computational Pathologist—a project done at Stanford, a very famous project that looked at the five-year survival of breast cancer patients by looking at their histopathology image slides.
This is what I would call a classic machine learning approach. I spoke to the senior author of this, Daphne Koller, and asked her why they didn't use deep learning. She said, "Well, it just wasn't really on the radar at that point." So this was a pre-deep-learning approach.
The way they did this was they got a big team of mathematicians, computer scientists, pathologists, and so forth to build these ideas for features, like relationships between epithelial nuclear neighbors. They created thousands of features, and each one required a lot of expertise from a cross-disciplinary group of experts at Stanford. This project took years, a lot of people, a lot of code, and a lot of math. Then once they had all these features, they fed them into a machine learning model—in this case, logistic regression—to predict survival. It was very successful, but it's not something that I could create for you in a minute at the start of a course.
The difference with neural networks is that neural networks don't require us to build these features. They build them for us! What actually happened was in 2015, Matt Zeiler and Rob Fergus took a trained neural network and looked inside it to see what it had learned. We don't give it features; we ask it to learn features.
When Zeiler and Fergus looked inside a neural network, they looked at the actual weights in the model and drew a picture of them. Here are nine of the sets of weights they found. This set of weights finds diagonal edges; this set finds yellow to blue gradients; and this set finds red to green gradients, and so forth. Down here are examples of bits of photos which closely matched that feature detector.
Deep learning is "deep" because we can take these features and combine them to create more advanced features. These are some layer two features: a feature that finds corners, a feature that finds curves, and a feature that finds circles. Here are some examples of bits of pictures that the circle finder found.
Remember, with a neural net, we don't have to hand-code any of these or come up with any of these ideas. You start with a random neural network, feed it examples, and have it learn to recognize things, and it turns out these are the things that it creates for itself.
When you combine layer two features, it creates a feature detector that finds repeating geometric shapes, frilly little things (which looks like the edges of flowers), or words. The deeper you get, the more sophisticated the features it can find are. Trying to code these things by hand would be insanely difficult, and you wouldn't even know what to encode by hand! What we're going to learn is how neural networks do this automatically. This is the key difference in why we can now do things that previously we didn't even conceive of as possible: we don't have to hand-code the features we look for; they can all be learned.
It's important to recognize that image-based algorithms are not just for images. In fact, this is going to be a general theme: we're going to show you foundational techniques, but with creativity, these foundational techniques can be used very widely.
For example, an image recognizer can also be used to classify sounds. This was an example from one of our students who posted on the forum and said for their project they would try classifying sounds. They took sounds, created pictures from their waveforms, and then used an image recognizer on that—and they got a state-of-the-art result, by the way!
Another student on the forum said they did something very similar to take time series data and turn them into pictures, and then use image classifiers. Another student created pictures from mouse movements from users of a computer system: the clicks became dots, the movements became lines, and the speed of the movement became colors, and then they used that to create an image classifier. So with some creativity, there are a lot of things you can do with images.
There's something else I wanted to point out: as you saw when we trained a real working bird recognizer image model, we didn't need lots of math (there wasn't any), we didn't need lots of data (we had 200 pictures), and we didn't need lots of expensive computers (we just used my laptop). This is generally the case for the vast majority of deep learning that you'll need in real life.
There will be some math that pops up during this course, but we will teach it to you as needed or refer you to external resources, but it'll just be the little bits that you actually need. The myth that deep learning needs lots of data is mainly passed along by big companies that want to sell you computers to store lots of data and process it. We find that most real-world projects don't need extraordinary amounts of data at all. As you'll see, there are a lot of fantastic places where you can do state-of-the-art work for free nowadays.
One of the key reasons for this is because of something called transfer learning, which we'll be learning about a lot during this course, and it's something where very few people are aware of the payoff.
In this course, we'll be using PyTorch. For those of you who are not particularly close to the deep learning world, you might have heard of TensorFlow and not PyTorch. You might be surprised to hear that TensorFlow has been dying in popularity in recent years, and PyTorch is growing rapidly. In research repositories amongst the top papers, TensorFlow is a tiny minority now compared to PyTorch.
There's also great research that's come out from Ryan O'Connor. He discovered that the majority of researchers who were doing TensorFlow in 2018 have now shifted to PyTorch. What people use in research is a very strong leading indicator of what's going to happen in industry, because this is where all the new algorithms and papers are going to come out. It's going to be increasingly difficult to use TensorFlow. We've been using PyTorch since before its initial release because we knew from technical fundamentals that it was far better.
I will say, however, that PyTorch requires a lot of hairy code for relatively simple things. This gray box is the code required to implement a particular optimizer called AdamW in plain PyTorch. I actually copied this code from the PyTorch repository, so as you can see, there's a lot of it.
This smaller bit here is the code required to do the same thing with fast.ai. fast.ai is a library we built on top of PyTorch. This huge difference is not because PyTorch is bad; it's because PyTorch is designed to be a strong foundation to build things on top of, like fast.ai.
When you use the fast.ai library, you get access to all the power of PyTorch as well, but you shouldn't be writing all this code if you only need to write this much code. The problem with writing lots of code is that there are lots of things to make mistakes with, lots of things to not have best practices in, and lots of things to maintain. In general, particularly with deep learning, less code is better.
The code you don't write in fast.ai is code where we've basically found best practices for you. When you use the code that we've provided, you'll generally find you get better results. fast.ai has been a really popular library, widely used in industry, academia, and teaching. As we go through this course, we'll be seeing more and more pure PyTorch as we get deeper underneath to see exactly how things work.
The fast.ai library just won the 2020 best paper award in *Information*, so again, it's a very well-regarded library.
You may have noticed something interesting, which is that I'm actually running code in these slides! That's because these slides are not in PowerPoint; these slides are in a Jupyter notebook. Jupyter Notebook is the environment in which you will be doing most of your computing. It's a web-based application which is extremely popular and widely used in industry, academia, and teaching, and it is a very powerful way to experiment, explore, and build.
Nowadays, most students run Jupyter notebooks not on their own computers (particularly for data science), but on a cloud server. If you go to course.fast.ai, you can see how to use various different cloud servers.
One example I'm going to show is Kaggle. Kaggle doesn't just have competitions; it also has a cloud notebook server, and I've got quite a few examples there. Let me give you a quick example of how we use Jupyter notebooks to build stuff, experiment, and explore.
On Kaggle, if you start with somebody else's notebook—say, "Jupyter Notebook 101"—if it's your own notebook, you'll see a button called "Edit". If it's somebody else's, that button will say "Copy and Edit". If you use somebody's notebook that you like, make sure you click the upvote button to encourage them and to help other people find it before you go ahead and copy and edit.
Once we're in edit mode, we can now use this notebook. To use it, we can type in any arbitrary expression in Python and click run. The very first time we do that, it says "Session is starting"—it's basically launching a virtual computer for us to run our code. This is all free.
In a sense, it's like the world's most powerful calculator, where you have all the capabilities of the world's most popular programming language directly at your disposal. Python does know how to do `1 + 1`, so as you can see, it spits out the answer. I hate clicking; I always use keyboard shortcuts, so instead of clicking this little arrow, you just press `Shift+Enter` to do the same thing.
As you can see, there's not just calculations here; there's also prose. Jupyter notebooks are great for explaining to your future self in six months what on earth you were doing, or to your co-workers, the open-source community, or people you're blogging for. When you create a new cell, you can create a code cell (which lets you type calculations) or a markdown cell (which lets you create prose).
The prose uses formatting in a little mini-language called Markdown. There are so many tutorials around I won't explain it to you, but it lets you do things like links and formatting. I'll let you follow through the tutorial in your own time because it explains what to do.
One thing to point out is that sometimes you'll see me use cells with an exclamation mark at the start (`!`). That's not Python; that's a bash shell command. You can also put images into notebooks—the image I popped in here shows Jupyter winning the 2017 ACM Software System Award, which is pretty much the biggest award there is for this kind of software. That's the basic idea of how we use notebooks.
So let's have a look at how we do our bird or non-bird model. One thing I always like to do when using cloud platforms like Colab or Kaggle that I'm not controlling is make sure that I'm using the most recent version of any software. So my first cell here is `!pip install -U -q fastai`. That makes sure that we have the latest version of fast.ai, so you never have those awkward forum threads where you say "Why isn't this working?" and somebody says "Oh, you're using an old version!"
You'll see here this notebook is the exact thing that I was showing you at the start of this lesson. If you haven't done much Python, you might be surprised about how little code there is here. Python is a concise, but not too concise, language. There's less boilerplate than some other languages, and I'm also taking advantage of a lot of libraries. fast.ai provides a lot of convenient things for you.
To use an external library, we use `import` to import a symbol from a library. fast.ai provides many libraries, generally starting with `fast`—for example, to make it easy to download a URL, `fastdownload` has `download_url()`. To make it easy to create a thumbnail, we have `Image.to_thumb()`.
As I'm building a model, I always like to view my data at every step. That's why I first grab one bird photo and one forest photo, and look at them to make sure they look reasonable. Once I see that they look okay, then I go ahead and download.
fast.ai has `download_images()`, where you just provide a list of URLs, and it does that in parallel surprisingly quickly. Another fast.ai function I'm using here is `resize_images()`. For computer vision algorithms, you don't need particularly big images, so I'm resizing these to a maximum side length of 400 pixels. GPUs are so quick that for big images, most of the time is taken up just opening the file. The neural net itself often takes less time, so that's another good reason to make them smaller.
The main thing I wanted to tell you about was this `DataBlock` command. The `DataBlock` is the key thing that you're going to want to get familiar with as deep learning practitioners at the start of your journey, because the main thing you'll be trying to figure out is: "How do I get my data into my model?"
That might surprise you. You might think we should be spending all our time talking about neural network architectures, matrix multiplication, and gradients. The truth is, very little of that comes up in practice. The reason is that at this point, the deep learning community has found a reasonably small number of types of model that work for nearly all the main applications you'll need, and fast.ai will create the right type of model for you the vast majority of the time.
All that stuff about tweaking neural network architectures—we'll get to it eventually in this course, but it almost never comes up in practice. It's kind of like if you did a computer science course where they spent all this time on compilers and operating systems, and then in the real world you never use it again. This course is called Practical Deep Learning, so we're going to focus on the stuff that is practically important.
Okay, so our images have finished downloading, and two of them were broken, so we just deleted them. Another thing you'll note if you're a keen software engineer is that I tend to use a lot of functional style in my programs. A functional style works very well for this kind of work, which is why you'll see me using things like `map` quite a lot.
A `DataBlock` is the key thing you need to know about to use different kinds of datasets. When we designed the `DataBlock`, we looked across hundreds of projects and asked: "What are all the things that change from project to project to get data into the right shape?" We realized we could split it down into five key components:
1. **What kind of input do we have?** There are blocks in fast.ai for different inputs; here we specify `ImageBlock`. 2. **What kind of output/label do we have?** Here it's `CategoryBlock`, meaning one of a set of possibilities. 3. **How do we get the items?** In this case, `get_image_files`, which returns a list of all image files in a path. 4. **How do we create a validation set?** It is critical to set aside data for testing accuracy. fast.ai won't let you train without one, so here we say `RandomSplitter(valid_pct=0.2)` to randomly set aside 20%. 5. **How do we get the label for each item?** Here `parent_label` returns the parent folder name (`forest` or `bird`). 6. **Item transforms:** Most computer vision architectures need inputs to be the same size. `Resize(192, Method='squish')` resizes each image to $192 \times 192$ pixels by squishing it.
From the data block, we create `DataLoaders`. `DataLoaders` are what PyTorch iterates through to grab a batch of your data at a time. The GPU processes thousands of items simultaneously, so a data loader feeds the training algorithm with a "batch" or "mini-batch" of images at once.
When we call `dls.show_batch()`, it shows an example batch passing into the model, displaying both the input image and its label.
When building your own models, you'll want to know what splitters, labeling functions, and transforms exist. docs.fast.ai is where you go to get that information. A great place to start is the tutorials, such as the DataBlock tutorial, or the underlying API documentation.
At the end of all this, we've got an object called `dls` (DataLoaders), which contains iterators that PyTorch can run through to grab batches of randomly split training images and validation images.
Now we need a model. The critical concept here in fast.ai is called a `Learner`. A `Learner` combines a model (the neural network function we'll be training) and the data we use to train it with. That's why you pass in two things: the data (`dls`) and the model framework.
If you pass in a symbol like `resnet18`, fast.ai integrates a wonderful library by Ross Wightman called `timm` (PyTorch Image Models), which is the largest collection of computer vision models in the world. fast.ai is the first framework to integrate this seamlessly. The `resnet` family of models will be fine for nearly all the vision tasks you want to do, but it's fun to try different architectures out.
When I ran this code on Kaggle, the first thing it printed was `Downloading resnet18.pth`. Why? Because somebody else has already trained this model to recognize over 1 million images across 1,000 types of objects in the ImageNet dataset, and made those pre-trained weights available on the internet. By default, fast.ai downloads those weights so you don't start with a random network that knows nothing.
fast.ai features a unique `fine_tune()` method. It takes those pre-trained weights and adjusts them in a carefully controlled way to teach the model the differences between your specific dataset and what it was originally trained for. At the end of a few seconds of fine-tuning, the error rate drops to zero—it's 100% accurate!
Now we have a trained learner that can recognize bird pictures from forest pictures. You can call `learn.predict(img)` to make predictions on new images. This returns the label as a string, its integer index, and the category probabilities. In production, your application just needs that single line of code: `learn.predict()`.
What about other kinds of models beyond basic image classification? Even within computer vision, there is segmentation.
Segmentation is where we take photos—such as road scenes—and color in every pixel according to what object it belongs to (e.g., cars, fences, buildings). On the left are ground-truth labeled images, and on the right are our model's predictions. It gets the vast majority of pixels correct after just 20 seconds of training on a tiny dataset.
Notice how little code is required. For common tasks, fast.ai provides high-level data loader classes like `SegmentationDataLoaders.from_label_func()`. You pass in image files and a label function, create a `unet_learner()`, and call `fine_tune()`. That's all it takes to build a segmentation model!
Stepping away from computer vision, perhaps the most widely used models in industry are for tabular analysis—predicting columns in spreadsheets or database tables.
Tabular analysis looks very similar in fast.ai. We download a dataset using `untar_data()`, which decompresses common benchmark datasets. We create `TabularDataLoaders.from_df()`, specifying which columns are categorical (taking discrete values) and which are continuous (real numbers). `show_batch()` displays a clean view of the tabular data.
To build a model, we instantiate a `tabular_learner()`. Notice that instead of `fine_tune()`, we call `fit_one_cycle()`. That's because for tabular data, there generally aren't pre-trained models available since every table structure is unique, unlike images which share common visual fundamentals.
Another major application area is collaborative filtering, which is the basis of most recommendation systems (like Spotify or Apple Music). It predicts which products a user might like based on patterns from similar users.
We create `CollabDataLoaders.from_csv()`. The data typically consists of user IDs, item IDs (like movie titles), and ratings. We create a `collab_learner()`, specify the expected range of ratings (e.g., 0.5 to 5.5), and train it. Calling `learn.show_results()` displays side-by-side comparisons of actual versus predicted ratings.
A lot of people on the forum ask how I'm turning this Jupyter notebook into a slide presentation. I'm using a free notebook extension called RISE. It adds metadata controls to each cell so you can designate slides and fragments, rendering interactive presentations directly from notebooks.
Jupyter notebooks are extremely versatile: our entire *Deep Learning for Coders* book was written in notebooks in the `fastai/fastbook` repository. The core `fastai` library source code, documentation, blog posts, and test suites are all authored and maintained entirely inside Jupyter notebooks using `nbdev`.
What can deep learning do today? We are still just scratching the surface. When we started in 2014, deep learning was inaccessible and pre-trained models barely existed. Today, almost every time someone applies deep learning to a new domain, they set new state-of-the-art results.
Deep learning is state-of-the-art across NLP, computer vision, medicine, biology, recommendation systems, gaming, and robotics. A good rule of thumb: if a task is something a human expert can perform in a few seconds (like identifying an object on a game board), deep learning will likely perform very well. If it requires extended multi-step logical reasoning with minimal data (like predicting election results), deep learning may not be suitable.
Neural networks date back to Frank Rosenblatt's Perceptron in 1957. While the underlying math hasn't changed drastically, modern GPUs, fast SSDs, and massive datasets have unlocked their true capability after decades of foundational research.
Let's step back and look at how machine learning differs from traditional programming. In traditional programming, you provide inputs and human-written code (loops, conditionals) to produce results.
In machine learning, the hardcoded program is replaced by a flexible model with parameters (weights). A neural network is a mathematical function that multiplies inputs by weights, sums them up, applies a non-linear activation (like setting negative values to zero), and repeats this across layers.
Initially, weights are assigned randomly. To make the model useful, we pass inputs through the model, obtain results, and evaluate them using a loss function (a metric indicating how far off the predictions are).
The key mechanism in deep learning is updating the weights using optimization (gradient descent) to slightly improve the loss. Because multi-layer neural networks are universal function approximators, repeating this updating process iteratively allows the model to learn any computable function given sufficient data and time.
Once training is complete, the loss function is discarded, and the weights are frozen into the trained model. The trained model acts just like a standard function that takes inputs and produces outputs, allowing you to integrate `learn.predict()` anywhere inside traditional software applications.
To wrap up lesson one: if you're already familiar with Python and Jupyter, start experimenting right away by running and modifying the Kaggle notebooks. If you're new to Python, check out the recommended learning resources in our forums.
Your main assignment before lesson two: 1. Run and modify the bird-or-forest classifier notebook. Try creating your own classifier with 3 or 4 categories of your choice. 2. Read Chapter 1 of the *Deep Learning for Coders* book. 3. Share your initial projects and results in the "Share your work here" thread on the fast.ai forums. 4. Try answering the review questions at the end of Chapter 1.
Thanks so much for coming, everybody! See you next time. Bye!