Sloth or Pastry? Using PyTorch and Deep Learning for Image Classification

Mohammed Khan builds a binary image classifier using PyTorch and ResNet-18 to tell sloths apart from peniche-chocolat pastries. The model reaches near-perfect accuracy in 25 epochs with standard preprocessing, fine-tuning, and a learning-rate scheduler. The notebook follows a reusable workflow for any binary or multi-class classification task.
Chapters
Foreign.
Thank you for joining today's Live training. My name is Rhys and I'm going to be your moderator for today. We're going to kick off today's session in about a minute or so. We're just waiting so everyone has a chance to filter in, but in the meantime we'd love to hear from you. So, yeah, let us know where you're joining from in the comments and yeah, let me know what you'd like to learn in today's session. Just to note, we are using datacamp Workspace for today's session, so if you don't have an account already, now is the perfect time to go and register for one. I'll also be posting a link in the chat to the specific workspace that we'll be using. You'll be able to code along with us and yeah, make a copy so that you can join in as well. I'll be sharing that shortly after the session starts, but yeah, until then, just make sure you've got a datacamp workspace account.
Foreign.
Thank you for joining today's Live training. My name is Rhys and I'm going to be your moderator for today's session. We're going to kick off the session in about 30 seconds or so. We're just waiting so everyone has a chance to join. But in the meantime we'd love to hear from you. So let us know where you're joining from in the chat and yeah, let me know what you'd like to learn from today's session.
Session.
Just to note as well, we are going to be using datacamp Workspace during this session, so if you don't already have an account, please, yeah, sign up for a datacam workspace account and you'll be able to code along with us in the session. I'll be sharing a link to how, how you can duplicate the workspace and yeah, code along with us shortly after the session starts. But yeah, until then, if you don't have a datacamp workspace account, make sure you sign up for.
Brilliant.
Seeing lots of people joining from India, Tunisia, Sri Lanka, us, Mexico. Lovely to see such a wide variety of people joining. Brilliant. I think that's about us. I will now hand you over to today's host of the session, Richie. Richie, please take it away.
Hi there, Data Scamps and Data Champs, this is Richie. So a lot of the time in data science, we concentrate on working with numbers. However, images are absolutely everywhere and so it's really helpful if we can analyze those images in the same way that we analyze numbers. And so today we're exploring image classification, which means taking an image and trying to assign the content to that image to one of several categories. And this is important for so many things. So from a simple thing where you've been clicking on the QR code here, you need to, your photo application needs to understand there's a QR code in the image. And then it's interesting for social media tagging your friends in photos, it's useful for self driving cars to understand where the road is. All sorts of use cases for this. And today's instructor is Mohammed Khan, a senior content developer here at Data Camp. So Ham is one of our regular live training instructors and the creator of four data cap courses including Introduction to Deep Learning with Pytorch. So I'm going to leave you in Maham's capable hands now.
Thank you. Thanks Richie. And welcome everyone to this session on deep learning for image classification in Pytorch. So the purpose of today's session is to perform something called a binary classification task for those of you familiar with traditional machine learning or logistic regression. Classically we were taking some data and we're trying to classify it into one of two categories, in this case a sloth or a pastry. The reason is, the reason for this is that while human beings are very good traditionally at detecting whether something is an animal or a pastry, machines historically have not been. However, with recent advances in deep learning, we're able to do astonishing things with image classification tasks. So the dataset we'll be using is a dataset we scoured from the Internet just for you. So just from Google, Googled images of slots, Googled images of pastries, and we're going to be using them in a data set to perform this classification task. We're going to be taking an existing deep learning model to do this. If you have never seen a deep learning model in your life, don't know a thing about Pytorch, have a little bit of sense of what a classification task is, you're probably good to go. We'll be taking you from zero to deep learning in this session. The secondary goal of the session is to learn about fine tuning an existing model to make it even better at your task. Um, existing models are already so good that actually you probably don't need to fine tune for this particular task, but I'll show you how to do it anyway in case you have a classification task where your images are niche and the model hasn't seen them before. Um, the reason that we're going to be taking an existing model is that these days, data science seldom write models from scratch. Um, so, so it's very important to learn how to load a model and use it with your own custom data set. So with that, I'm going to switch to a short primer on what neural networks are for those of you who are unfamiliar, and then we're going to move into the coding part of this session. So this is the 5 minute neural network primer. This is the. Don't, don't, don't use this in a university lecture. But it's really the fundamentals for understanding what's going on in your network. This is our task. We want a model to be able to tell us which of these is a sloth or a pastry. In the past, machines weren't very good at this. Very recently though, they have become, and there are now models you can use that have been trained on billions of images that can do this, sometimes even arguably better than human scan. So how would we use the model to do this? Very simply, a neural network takes an input. If we have this one, images are input. We need a way to code it into numbers. Fortunately, images are very easy to code into numbers because images are made of pixels. Each pixel has a value that we can store as an input value for our network. In this particular diagram, we have three inputs, so three numbers, three features for this. In reality, it'll have as many inputs as the number of pixels. This image certainly doesn't have three pixels. It probably has thousands and thousands of pixels in there. But just for simplification, say that this was an image of three pixels. Each pixel has a value, a number, decimal point number. All right, so we take that input and we do some matrix computations on it. We're not going to be going into any of the computations that are happening in this session. But just for a simple schematic, what's happening is we're taking those inputs and passing them through a matrix transformation. And what we're getting is an output. So one of two values, either it's a pastry or it's a sloth. What? Those two values are probabilities. So if the probability is above 0.5, we'll say pastry. If it's below 0.5, we'll say sloth. So these two outputs here in orange will add up to one. So one of them will be the probability of pastry, the other will be probability of sloth. That's it. That's what a neural network is in the most basic form. All right, last, last thing before we move into coding. In reality, neural networks aren't as simple as the one we saw. What we actually have are many computations happening in between that input and output we saw. So the input could have as many numbers as the number of pixels in our image. For example, the output will be 2, 2 numbers. The probability of sloth, probability, probability of pastry. And in between, we'll have a number of intermediate outputs. So not all of them will be of the shape 2, which is the one we have in the end. But those intermediate outputs will help us get closer and closer to the value we want at the end. So that's it. And what we're going to be training in this model is what the strength of each of the connections between these network, between these neurons in the network is. So, so what's the value in. In between each of these connections? So with that, I'm going to move to the coding part of the session. Hopefully everybody has the workspace set up. If not urge you to do so now. All right, so here we are with our workspace notebook. What we're going to be doing is using the files in the workspace provided to perform our classification tasks. If you navigate over to this file section up here, we've preloaded some data called slots versus penichocolat. The particular pastry here that looks ridiculously like a sloth is called a penichocolat. It's a French pastry. And we have these two folders in here called train and Val. In the training folder, we have again both of our categories, peniche, palat and slots. And then in the VAL section, we, we have the same two categories. So if you're familiar with machine learning, we will use the train folder to train the model and then we're going to use the VAL folder to test how well our model is doing. We really want to make sure none of the images in the VAL folder are in the train folder so that when you're running predictions on the model, it's seeing images that it's never seen before. If your validation contains images that the training set has already, the model has already seen when it was training, that's cheating. And your model probably won't do well when it sees new data. All right, last thing we are going to be using adapting code from pytorch.org tutorials. Pytorch.org has a huge host of tutorials available in Pytorch. We're going to be taking a lot of the models that are available on Pytorch tutorials, but we're going to be applying them specifically to our data set. Okay, so let's get started. Like all great Python projects, we're going to start with some package imports. There's quite a lot of package imports here. This is one of the things with Pytorch that you will find and with deep learning in general. Actually there's quite a lot of moving parts because the data sets are so large and often the compute power required is so large. With traditional machine learning, you can get away with Numpy Pandas and Scikit Learn, but. But now we're working with a bigger set of packages, so we're going to be using NumPy. I'm going to start typing as well, so encourage you to code along with me in the same folder. We also have another notebook called Notebook Solution appimb. So if you click on this one, it has all the answers in case you get lost. I encourage you not to look at the answers unless you are lost so that you can find. Follow along. But just in case, the solution is also provided for you. Also for those who are joining late. All right, so let's start importing some packages. So we're going to start importing NumPy as NP. This is for manipulating numerical arrays. So like I said, what we're doing with images is turning them into numbers. And fundamentally numpy array is, is, is the, is a core Python library that helps us do that. So numpy numpy is foundational for a lot of deep learning that we do. We're going to load matplotlib common library for matplotlib Common library for plotting. Right. We're going to be importing time, which is a library for time related functions. We'll be importing os. This is for interacting with our operating system in case you want to change some files, remove delete files, files, etc. A lot of deep learning is about manipulating files. So like this file structure I showed you that we have here, you're going to be. Sometimes you have a nice drag and drop interface and you can just do it there. But oftentimes you want to manipulate the file structure programmatically rather than through a drag and drop interface. And so it's important to use that OS package. We're going to import something called copy for copying objects. Then Torch. Torch is the core Pytorch library. It's called Torch. It's set up in your workspace already. If you use it locally, you'll have to install it. Torch is the core PyTorch library. Then we have something called Torch NN which we're going to import as NN. This is the basic building blocks for neural networks. The NN stands for neural networks. A lot of the neurons that you saw in that diagram, those particular pieces are built from Torch nn. Then we have TORCH optimization. This is an optimizer. This will help us train our neural network to perform this image classification task. Then we're going to import something called LR Scheduler from Torch Optim as well. This is for scheduling something called a learning rate. That's how fast or slow your model is learning. And then TORCH back ends. This is a way for Pytorch to talk to your gpu. If you have a gpu, it's a sleight of hand to help us optimize our code to run faster if possible. If not, the code we'll be using won't do anything, but it's just nice in case you do have a GPU that's available to you. Then last two packages we'll import our torchvision. This is for image processing, specifically from Torchvision as well, we'll be importing data sets which contain some built in data sets, models which contain some of the models we'll be using, and then transforms which we'll be using to transform our images when we do image processing. Hopefully I have no typos and we can run this. Okay, I do have a typo here. So in the last line I had from Torchvision, import datasets, models, transforms. Right. Shortcut if you're in on a Mac is to hit Command enter to run your cell. I think shift enter works as well if you're on a Windows and a Mac. Right. So for just for that, what I mentioned about faster runtime, this line of code gives you faster runtime. So boot benchmark equals true. You can read more about it here. I've linked a link some literature on what it's doing. We won't get into it now. All right, so now we're going to get into reading and transforming the data. So like I said, we have this folder called Data which has files in a particular file structure that are structured in that way so that Pytorch can read them. In particular so that our data loader can read them. Right. I'm just going to write a couple of lines of code. Sometimes what when you do upload your data in Python, in Jupyter, Notebook settings or in workspace settings, this.ipynb file.ipnb checkpoints shows up in annoying Places, and we don't like that. So what we're going to do is just banish those files, just delete them so that in case they have shown up in your folders, we just delete them. This is just very quick. It's not. It's not so much deep learning as file management, but useful to do nonetheless. All right, so what I'm doing is this is. This is bash code. So remove. So what the RM stands for remove. So we're removing the file called ipynbcheckpoints in data slots versus Parachocola train, which is that folder there. And then we're going to do the same for the folder called val. So just change that part of the. That part of the path. And then if I run this, it says, cannot remove no such file or directory. Cannot remove no such file or directory. That is what we want to hear. It means the file doesn't exist, so there was nothing to remove. If it did have something to remove, it would have removed it and it would have given you that message. That's just something we want to do before we move on to make sure directories are clean and they're only images and there are no other weird files. All right, now let's go into loading and transforming our data using the specific transforms we'd like to use from Torchvision. Now there, I've listed a bunch of transforms here. The reason we do this is because when you're loading images, so I'm going to actually open an image and show it to you. Here's an image. When we load this, it could be in any shape or any size, Right? I'm going to open another one from the slots category. Right? This one is tiny, this one is tiny. This one is very large. So this one's not opening. So what we want to do is standardize our images in some way so that when our model sees them, they're presented in some kind of consistent format. The other thing is we want to transform them in ways that help our models learn. Right? So for both those reasons, we perform these transforms. So we're going to create a dictionary containing all the transforms we're going to perform. And we're going to perform these transforms separately on the training set and separately on the validation set. The reason for this is we want to transform the images in a different way when we're training and in a different way when we are validating. So we're going to take our images and we're going to create a dictionary and we're going to create an item in our dictionary for training first. So just going to create our first item called train. That's the key. And the values are going to be something called Transforms Compose. So we're composing the transforms, we're going to perform and then we're going to create a list inside Transforms Compose. Right. Transforms is something, if you remember we imported imported during our package imports. It's just one of those libraries we imported earlier. Transforms has a property called random resizescrap. This is a function that randomly crops your image within a square of 224 pixels. So because like I remember when I said that images can have different shapes and sizes, so we want them to be of a consistent shape so that they're fed into the model consistently. Otherwise the model will throw you an error. So we want all of our images to be cropped in a 224 sized square, 224 pixels square. And we want it to be random. We don't want it to be taking the top left square or the bottom left or the center, a square from the center. We want to take a random square every time. All right, then we're going to do something called random horizontal flip. So what this is going to do is some of the time flip the images horizontally like a mirror. Like a mirror image. Not all of the time. And the reason for this is that we're trying to introduce noise and variability on the data so that the model is really good at seeing new images and classifying them accurately. So some of the time it's not always the case that slots will be smiling or, I don't know, leaning towards the left as they are in our image or have their left hand up as they have in an image. Sometimes they have the right hand up. So we're going to introduce some random horizontal flipping in order to introduce this noise. Next we're going to introduce a, a transform called to tensor. This is because our images are going to be read in as numpy arrays and we want them to be in a format called tensor, which is the format that Pytorch uses. So Torch transforms to Tensor, then transforms normalize. Now what this is, is a good way to standardize the values and variability in our data. So normalizing, put very simply, is to take a number, which are the numbers that we're quoting our images as, subtract the mean and divide by the standard deviation. That's all that's happening in the normalized. So we're going to provide two values, a list of the means and a list of the standard deviations. The means that we're going to be using are the means listed up here. So I'm going to paste that list right here. And then the standard deviations are these standard deviations provided here. We're going to paste them in here. So for every value, we're going to be subtracting these means and dividing these standard deviations. The reason that we normalize with these particular means and standard deviations is because the model that we're using, called resnet, has been trained on images which have this mean and this standard deviation. If we were using a model train on a different data set, we would have to use specific means and standard deviations for those. But for this particular model, these are standard means and standard means and standard deviations that we'll be using. All right, Next thing we're going to do is to move into the train, the VAL part. I'm just going to make sure this runs. Yeah, okay. No typos there. The next is the VAL part. So that's similarly, how are we going to transform our validation set? So the images, which we're not going to use in training, but we are going to use on validation. So. So we're going to resize. So only resizing, not cropping in this particular instance, only changing the shape. Then we're going to do a center crop so that you just see the image from the center, you're not seeing a random part of the image. Then we're going to do a transforms two tensor, same as what we did with the training set. And then we're going to do transforms, normalize, transform, store, normalize, same values as before. I'm just going to copy those over from here. Right, we're going to run this. This is our data transform. All right. Next we're going to move quickly into performing this transformation on our data set. So we're going to provide Pytorch with a directory for where our data is stored. So that's just that folder that I showed you earlier. It's called slots versus in the data folder. Right. Then we're going to create another dictionary which creates image folders separately for our training and validation data. So. So this is. Another torch function called image folder. And it will take, sorry, Os, which is library, we imported, then something called OS path join. And we're going to join the data directory we just provided with X. This is a lambda function that uses the value X and some variables we're defining, such as the data directory. Then we're going to. Perform data Transforms. Right. The, the dictionary we just defined earlier on this, and then it's going to be 4x in train val. So we're going to create two separate directories for train and val where we're going to apply the data transforms that we define in that dictionary up there separately on the training images and on the validation images. I'm just going to run this also to make sure it's working. Okay. Seems to be working. All right. Then we're going to find the sizes of each data set. So. How many images are there in each data set? That's, that's all that's happening here. And then we're going to get the class names and I'm actually going to print them to show you if they were read in successfully. So what are the names of the classes in the image data sets the, that we just created up here? So print class names and then data loaders is something that we're going to use to read the data. So again, another dictionary. X Torch is a lambda function. And we're going to use Torch Utils data dataloader to take image data sets which we just defined up there. Again, X. Then we're going to define something called the batch size. This means that we're only taking four images at a time when we actually do our training. So the batch size is four. And we're going to set something called shuffle equals true. What shuffle equals true does is takes a different four images each time. Why are you doing that? Okay, Shuffle equals true. And then for X in train, val. So again, for each of X in train and val, we're going to take four images and we're going to shuffle them. So that's, that's something we're going to be using when we're, when we're performing training. Okay. I have a typo. Why do I have a typo? Data set sizes. A bracket missing here and an extra bracket here. Yeah. Okay, cool. So the two classes we got are panachocula and slots. And then we're going to. Use those two different classes in batches of four. Okay. This is just some code that tells you that you can use a GPU if available, otherwise not. So we're going to ignore that. I've already provided that code for you. Okay, Very quickly we're going to move a bit faster now because we want to get to the training section, which is the fun part. What we're going to do here is define a function that enables us to visualize all that hard work that we've Done. We've created those images, we loaded them, and now we want to be able to visualize them. We're going to create. I just filled out that line over there that said a function's name. What we're going to be doing is transposing the images into the correct shape. Then we're going to be multiplying our images with the standard deviation and adding the mean. The reason for this is because, if you remember, earlier, we performed a normalization to subtract the mean and multiply the standard deviation in order to visualize them. Now we're going to have to add the mean back in and multiply the standard deviation again. So that's all that's happening there. We're going to perform something called numpy clip. This is to get a clipped version of the image rather than plotting the whole image. And then we're going to do something for matplotlib, matplotlib called plt imshow to visualize the input. So what we're doing is just taking an input from our data loader and performing all these calculations that will help us visualize it. Okay. If you're. If you're lost, you have the solution code. I know this is a lot, but what we're trying to do is just take a bunch of code, transform our data and get some outputs. So if you are lost here, I'd recommend switching to the solution code running up to this point point and then just getting. Just trying to visualize the data that we've loaded so far. All right, so let's call that function we just created. We're going to get a batch of training data from our data loaders. That's what we defined earlier. We're going to. Make a grid from the batch. There's a function called make grid that helps us make a grid and then we're going to plot our images. Right. So if we. What did I do wrong? Torch vision. Oh, my gosh. How many typos do I have? Inputs is not defined. All right. Oh, it's telling me M show is not defined. I'm just going to make sure we did import it up there. So that's weird. I think I just need to run mshow. So that function that you just defined, called mshow, make sure you run it so that we can actually. Why is it doing that? Oh, yeah, I think that ran correctly. Okay, so here's what we have. We have our cropped, transformed, normalized images and unnormalized rather. And here they are. These. This is from our training data set. Now, if we copy that exact code and put it in our validation data set, we're going to have is the images from our validation data set. So that that's all that's happening in this M show. Okay, I'm going to take a take a few seconds for you guys to pause and process all of that. And then what we're going to be doing is moving into running the model and trying to get some outputs out of it. I'm going to pause coding for now, but just talk to you a bit. What we're going to be doing is stealing this big function from pytorch.org that's the function that trains our model. Then we're going to be stealing another function from pytorch.org that helps visualize our model. Not going to make you write any of that code, really. The hard part was getting to this point where you've loaded your data, transformed it into a shape that's acceptable. Then it's in a shape. Now that this model and visualizer is going to accept. That's where we are at. Going to learn some terminology. So a sample is a single image or single row of data. A batch is a group of images. So that's what we set earlier. Batch size equals four. That means when we run our training, we're going to be running our training with four images at a time. And then an epoch is the number of times the model or learning algorithm will encounter all all samples in the data set. So in one epoch, the complete data set will have had an opportunity to update the internal model parameters. So in one epoch, so the model is only seeing four images at a time. Say you have a thousand images, right? You're going to have to keep running it again and again and again until it eventually sees all 1000. That's one epoch. We're going to run it on 25 epochs. So quite a lot of shuffling and randomization happening to get to the point where we consider the model trained. These are all tunable parameters, by the way. You can set whatever batch size you want. Sometimes it makes models faster, sometimes it makes them slower. You can run whatever epoch size you want. Again, more epochs, more training time, but potentially more accuracy and so on. These are all tunable. As you become experts in deep learning with Pytorch, you will be comfortable. But we were starting with a num ebox of 25, right? I'm going to breeze past this code so we don't have to look at it. And then we're just going to run a model. The last bit of. Last bit of code we'll be writing for the specific model and then after this we'll move into fine tuning that model. Let's go into loading the model that we're going to be using. It's called resnet. It's been developed by the authors listed in this paper provided and it's been trained on billions of parameters. So it's a pre trained model. It's been trained on billions of images. And what that means is that we don't have to show the model billions of slots and pastries in order to be very good at this task. It's already seen them. All we're doing is tweaking some of the layers in the model and we're good to go. So we're going to load the model called models Resnet 18. We're going to define a variable called num features. Right. Which. Sorry, one second. Yeah. So we're going to run a model called. We're going to create a variable called num features equals modelft.fc.in features. This is quite a lot going on. I'm just going to explain what it is, but I just want to run the code first so that I can talk to you while it's running because it's going to take some time to run. So hopefully you all are with me. If not, move into the solution code that's also provided in the notebook and just make sure you've gotten to this point where the data is in the right shape and you're able to get the model running. Just going to walk through everything that's going on, but I just want to get it written up. All right, I think we are good to go. Oops. Resnet has no object parameters. What do I have missing? Try again. Okay. Also I think I had the momentum missing in there, so I added. So I have model FD parameters. LR equals 0.001 and momentum equals 0.9. Okay, so what's happening here is we're loading the model, we are finding the, from our last layer what the size of the input features is. So if you remember from that image I showed you earlier, right. There is this layer that you see here. This is the final layer. Right. And we want to know what the size of that is and we're going to replace whatever it is with something that we want for our particular task. Right. So what's happening in this line is we found out how many features the last layer had in resnet and then we overwrote that, so we replaced it. That's the final line is called FT fc. So we replace FT FC with a linear layer with the number of features. We want the number of features that it had comma, two, two. The reason that we use two is because we have. It's a binary classification task. So two images that we are two categories that we're classifying into. Then we move it to the device we defined earlier. This is not important, but it's just for the optimization of speed. And then we define a criterion to determine how well our model is doing. We define something called an optimizer, which will be used to train our model. And then we're going to use something called the learning rate, which determines how fast or slow our model is running. And the learning rate will decay every seven epochs. Okay, now we're just going to call our model and we're off to the races. So modelfd equals trainmodel. That's the function we just defined. And it's going to be model. We're going to pass it the model we defined. We're going to pass it the criterion we defined. We're going to pass it. Optimize the optimizer we defined. We're going to pass it the learning rate scheduler we defined, and we're going to pass it the number of epochs, which is a five. I forgot to run the train model. So make sure you run the cell above and then this one and then run this cell. Why? Okay, so it's loading. So now what's happening is we have 25 epochs. 0 of 24 means that the 25th one is 0. It's training the model with each epoch. Okay, so started with a training. Loss of loss is the thing that it uses to get better and better each time. Start with a loss of 0.834. And what we're trying to do is minimize loss. So, so loss is going down with each epoch, as we can see. And the train, we're only looking at train. Okay, don't look at validation yet. And the accuracy seems to be going up with each epoch. All right, so that's what we want. This is great. We want loss to be going down and we want accuracy to be going up. So accuracy is 0.6228. Now it's 0.88204 and so on. We're in the sixth epoch now, and we are at about the training. Accuracy is now hovering at about the 0.8563, 0.87 mark. Now moving into 0.9 and so on. So it looks like our model's gotten pretty good already and it's only been nine epochs. This is not because our training data was great and it's not because I particularly have some real knack for finding images that are the best suited for generalizability. It's because the model we're using has seen billions of images before and so only is really being tweaked at a very granular level right at the end to be able to perform this classification task. But as we can see with the validation loss, the validation loss is so small that the accuracy is 100%. At this point we're getting, we're classifying every slot and every peninsulcola accurately because of how good the model was to begin with. And all we did was tell the model what shape we want as the output. So we want the shape to be two features, either sloth or branch of lab. That's it. All right, we are at 18 out of 24 epochs. As soon as we've hit 24, what I'm going to do is visualize our model. So we're just going to visualize some results from the model. So just waiting for that to end. I'll run it in advance. And then while we're waiting for that to happen, I'm just going to paste some code in here that's in your solution code. I'm not going to write it out because it's. We're running short of time and I do want to get to the Q and A. So I'll just paste it from the solution notebook. What this is doing is just slightly fine tuning the model further. So if it had been the case that you hadn't found the accuracy you were looking for and you wanted to fine tune the model to your specific data set, this is the code you would run. In reality, right now we don't actually need this bit of code because our model seems to be doing great already with the parameters that it did have. So in this particular, in this particular code that I provided, if you had some niche images the model hadn't seen before and it was performing badly on, you'd use this last bit of code that I provided here, same thing. You would get the. I'll put the rest of the code in as well. You'd get the. Training code same as before, and you'll visualize payments before and you would get slightly better results. But we're going to ignore that. But for now we're going to focus on the results that we did get, which is pretty much 100% accuracy. So we got slots for this one, slots for this one, slots for this one. Peru Chocolat for this one, slots for this one, slots for this one. I'm going to visualize a different set of five. We got predicted pena. Chocolat. Predicted slots. Predicted. This one's pretty close, actually. That does really look like a box of pastries to me. Predicted slots. Predicted slots. Predicted. This one's kind of weird, isn't it? It's predicted slots, but it is. I actually don't know what that is. Predicted bench. APPLAUSE all right, I'm going to stop there. This is the fine tuning code in case your model was performing slightly badly. In this case, it pretty much looks like we have 100% accuracy in case it were performing badly on your particular image, you would run this and you would be fine tuning the model a little bit. I won't do that for now. What I'll touch on before I stop sharing screen is where to next? So we have a tutorial which is a really holistic overview called a Beginner's Guide to Object Detection. We have a few data camp courses. Some are coming soon, some are currently live. The live one is called Introduction to Deep Learning with Pytorch. And then we have a skill track called Image Processing with Python. This is not in Pytorch, but it does give you a really good handle on what images are, how you manipulate them and what are all of the ways of familiarizing yourself with images so that when you do get into Pytorch image manipulation, you're really familiar with them. So I'm going to stop sharing here and move back into Restream and excited to hear all of your questions.
All right, thank you, Maham. That was really cool stuff and it seemed like it performed really well, that model. All right, so for everyone in the audience, if you've got any questions for Maham, then please do ask in the chat. But before we get to audience questions, I've got one question for you, Maham. So it seemed like we had to get through quite a lot of different stages to get to that result. There's a lot of sort of data transformation, whatever before we get to modeling. So if you want to do something simple, get started yourself. How much of that code is stuff that is a standard workflow and how much of it is just specific to.
That data set for image classification and specifically for a binary image classification task? Really, you could use this notebook as Your standard classification, you could use this notebook as your reference notebook. And all you would need to do is change the images that you're loading and change the folder structure so that the labels are not slots and they're whatever binary classification labels you want. And it'll pretty much run, I think, pretty well on most image classification tasks. The code that I've used is largely what is used on the introduction, introductory pytorch.org tutorials. And this is, this is a lot of these transformations and imports are standard. And then if you're finding, I mean you'll find your model performs well most of the time. If you do find it's not performing well, maybe it's that you didn't you want to introduce some different special kind of transformations, maybe you want to crop them differently, maybe you only want black and white imagery. You can do all of that. But I think this is a great starting point. And then the other thing is sometimes you have multi class classification, right? So maybe you're, you're doing slots, pastries, slots, pem, chocolat and blueberry muffins, right? In that case you would just change that number where I said n classes equals two, right at the end you would change it to three. And then you could have three categories, but you would have to change your folder structure accordingly. But otherwise a lot of code that you can repurpose for a lot of different tasks.
Fantastic. That's because it's nicely reusable. And so how does image classification fit into more general object detection?
Yeah, so object detection is the next level up front from image classification. In an image, quite often you'll see, especially in things like self driving car applications, you'll see a scene and you want to be able to detect this is a tree, this is a road, this is a car, this is a person, don't hit the person, things like that. An object detection task is doing a few more things than an image classification is doing it. It's first taking those bounding boxes of all of the images you're interested in. And then once you have those bounding boxes, it's then passing them to an image classifier so that you can classify if what's in that particular box is a tree or a car. Right. So with the object detection, the first step is actually identifying all of those boxes in the scene. What are all the different objects we have? And then the next step would be what we've done here, which is the image classification. However, if you don't need to dissect a visual scene and you already just have images and you, you want to classify them, then you would start here.
Okay, super. Thank you. All right, so let's go to some audience questions now. And there's a question from Shuri saying, so we had an accuracy of more than 90% in this, and that's just overfitting. Would that defeat the purpose of the generalization of the code? So that's fighting talk.
I don't know.
Do you think that 90% accuracy does show overfish fitting in this case, or do you think that's a valid statement?
I think it's certainly a valid statement, especially in traditional machine learning. If you were seeing that kind of accuracy, the alarm bells are ringing, especially alarm bells would be ringing, especially if the, the validation accuracy is 100%. Right. But that, what, what happens to be the case here is that you just have a model that's trained on so much data that it doesn't have a problem classifying these two. The model is so large and the task is so simple for the model that it's just doing a really good job. You should certainly continue to test it on a holdout set as well and make sure it's not overfitting, and we really don't want that situation. But in this particular case, I'm not so worried about overfitting because the model, it's just seen so much data already.
All right, fantastic. That sounds like we're okay there then. So next question comes from Yukian, who has got a couple of technical, technical questions. So one is, when's the best low level for loss? I'm not quite sure what that means, but I guess how do you reduce loss in, in this case? And the second one's like, how do you determine the best epoch size?
Yeah, so for loss is very similar to what you have in traditional machine machine learning. If you were doing regression or anything like that, where you were trying to minimize mean squared error loss. Right, Minimize the mean squared error. You just want the smallest possible mean squared error that you could get for your data set that's corresponding to the best, you know, accuracy in this case. So if you're seeing with each epoch that your loss is dropping and it's dropped quite substantially in our case to like near 0.1 or less than 0.1, your model is getting better and better. If you're finding your losses not dropping, then it seems that your model is not learning anything or there's nothing to learn, etc. So there's no, like, one best low level, but the lower the Better when it comes to loss. And if your model is predicting perfectly, then loss is zero. How should we determine the best epoch size? I think 25 is standard from it's just enough times that your data set has your enough time that the model has been able to see the data set enough times. Does that make sense? So you're taking the data set and you're chopping it up into batches. Right. And if you have one epoch, that means it's seen all the data once, but you don't necessarily want it to see all the data once, you want it to see all the data once, then you want to shuffle it and crop it and normalize it and restructure it again and then pass it for another epoch. And then it's seen the whole data set twice in batches of four. And I mean, it's not. There's no exact science, it's a little bit dependent on your data set size. So if your data set is very large, like thousands and thousands of images, then an epoch of 25 is probably sufficient because it's going to see all of those thousands of images 25 times. However, if you have a smaller data set size, you maybe want to introduce some more randomness, shuffle it a bit and have some extra epochs so that it's seen as much randomness and variation as possible. Encourage you to look into the relationship between batch size, sample size and epochs so that you can learn a little bit more about how those interact.
Okay, so more data, less epochs, and the other way around, it sounds like.
Okay, that's cool. I think so. I could be wrong. So do double check that.
All right, next question comes from Fahad saying, how do you go about getting started? Do you have any good resources or help just for beginning with deep learning?
Yeah. So this is. This tutorial was very advanced. This is not an introductory tutorial. The goal was really to get people comfortable doing the image classification. It seems like this big mountain that you have to climb and you have to first learn what tensors are and then manipulate the tensors and blah, blah, blah. And it sounds like you have to learn a month of things before you can actually train a model. What the objective of this is is just do the thing and figure out the code later on. It's kind of learning in reverse. But at the same time, there's of course a lot more introductory material that is available at Data Camp. I would recommend getting started with a course called Introduction to Deep Learning in Pytorch. That's much more basic than this. In fact, it does not even have image classification in starts, just with Pytorch objects. What is a tensor? How does it relate to a numpy array? How do you manipulate tensors, how do you add them, how to manipulate them, et cetera. Then how do you create your most basic neural networks? This model that we loaded called Resnet, it has many, many layers and many, many parameters. Introduction to Deep Learning with Pytorch will, will get you started with a very small network, kind of like the one I showed you at the start. So like a network with four layers and maybe 36 parameters. Right. Something manageable so that your plugging things into it, getting outputs and getting really familiar with what's happening in between all of those layers and then you know what's going on inside the box before you go off and start, you know, using other people's models and tuning them for one particular work. So I'd get started there. And then we also have. Couple of other courses in the pipeline. So intermediate Deep Learning with Pytorch is coming up this quarter as well, Next quarter as well. And then we, we have a couple of other courses called Deep Learning with Text and Deep Learning with Images. There will be a data cam course specifically on Pytorch, Deep Learning with Images that will be based on the introductory and intermediate courses.
Okay, so if you're interested more on image classification, then you have to wait a little while that Deep Learning with Images courses to come out. Please be patient and yeah, watch this space. All right, so in the meantime, I think, yeah, the solution is please take Maham's course on Production to Fly torch and that'll help you get started. Okay. And with that word about. We're coming up to the hour, so unless we have any more last minute questions, the only thing I wanted to say was that we have some more sessions coming up shortly. So tomorrow we've got a session on DataCamp's MLOps curriculum. So if you're interested in learning about deploying models to production. So that's pretty much a standard skill if you're into machine learning these days. So please do come back to that. Tomorrow on Thursday we've got a session on empowering data Teams with continuous upskilling. So for those of you who are managers in the room, it's going to be very important to attend that. And then next Tuesday we've got a guide to running data hackathons with workspace. So lots more exciting stuff coming up. Please go to datacamp.comwebinars to register for those. I hope to see you in future sessions. All right. Thank you once again, Maham. Thank you to Reese for moderating. Thank you to everyone who asked a question. Thank you to everyone who showed up and watched today. Hope to see you in future sessions. Goodbye.
Thank you. Thanks, Richie.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







