 Hi everybody, welcome to lesson two. Thanks for coming back. A slight change of environment here. We had a bit of an administrative issue at our university. Somebody booked our room. I'm doing this from the study at home. Sorry about the lack of decorations behind me. I'm actually really, really pumped about this lesson. It feels like going back to what things were like in the very early days because we're doing some really new, really cool stuff, which is stuff that hasn't really been in courses like this before. So I'm super, super excited. So thanks a lot for coming back after lesson one and I hope it's worth you coming back. I think you're going to love it. I am really excited about this. Now remember that, you know, the course goes with the book. So be sure that you're following, I mean, not following along in the book because we're covering similar things in different directions, but like read the book as well. And remember the book is entirely available for free as well. You can go to the Fast.ai, Fast.book repo to see the notebooks or through course.fast.ai. You can read it there, for example, through Colab. And also remember that the book, I mean, the book's got a lot of stuff that we didn't cover in the course, like, you know, stuff I find pretty interesting about the history of neural networks, some of which has some really interesting personal stories, actually, as you'll read here. And at the end of each chapter there is a quiz. And remember it's not a bad idea before you watch the video to read the quiz. So if you want to read the chapter two quiz, you know, and then come back, that's not a bad idea. And then make sure that you can do the quiz after you've watched the video and you've read chapter two of the book. Something I didn't mention last week is there's also a very cool thing that Radek, who I mentioned last week, has written called aiquizzes.com, which is a site specifically for quizzes about the book. And it actually uses repetitive space learning techniques to make sure that you never forget. So do check out aiquizzes.com. It's all brand new questions. They're different to the ones in the book. And they're really nicely curated and put together. So check out aiquizzes.com as well. Remember as well as course.fast.ai, there's also forums.fast.ai. So course.fast.ai is where you want to go to get, you know, links to all the notebooks and Kaggle stuff and all that stuff. You'll also find on forums.fast.ai every lesson has an official topic. You know, with all the information you'll need. Generally, there'll be a bit more info on the forums. We try to keep the course lean and mean and the forums are a bit more detailed. So if you find, in this case, you'd want to look at the lesson two official topic, but here's the lesson one official topic so far. So from the lesson one official topic already after just a few days since I recorded it, we haven't even launched the course so it's just the people doing it live. There's already a lot of replies and that can get pretty overwhelming. So be aware that there's a button at the bottom of my post that says summarize this topic. And if you hit that, then you'll just see the most upvoted replies. And that's a really good way to just make sure that you hit on the main stuff. So there's the button. And here's what it looks like after you hit it. You'll just get the upvoted stuff from fast.ai legends like Sanyam and Tanishk. So hopefully you'll find that a useful way to use the forum. So one of the cool things about this week is I, as promised, put up the show us what you've made post. And already a lot of people have posted. I took this screenshot a few days ago. It's way above 39 replies already if I remember correctly. I had a lot of trouble deciding which ones to share because they're all so good. So I've actually decided to kind of, you know, went the easy route and I just picked the first. So I'm just going to show you the first ones that were posted because they're also good. So the first, the very, very first one to be posted is a damaged car classifier. So that worked out pretty well. It looks like. And I really liked what Matt, the creator said about this is that, you know, wow, it's been uncomfortable to run this code. I don't really understand yet, but I'm just doing it. And so I'm like, yeah, good on you, Matt, for just, for just doing it. That's the way to get started. It's all going to make sense. Don't worry. Very nice to see that the next one posted was actually a blog post in fast pages. Very nice to see. So just describing some stuff, some experiments that they ran over the week and what did they find? Next one was the amazing beard detector, which if I understand correctly was mainly because it's very easy to get from bird to beard by just changing one letter to two. And this is doing a very good job of finding gentlemen with beards. So very nice. And then this one is another level again. It's a whole in production web app to classify food, which is kind of like extra credit. Apparently we're up to 80 replies now in that thread. Thank you, Sanyam. Very cool. So, you know, obviously, so this was actually created by Suvas who's been doing the courses for a few years now, I believe. And so, you know, one day you too might be able to create your very own web app and put it in production. And when I say one day, more specifically today, I'm actually going to show you how to do this right now. So it's actually quite lucky coincidence that Suvesh put this up there because it's exactly the topic that we're going to pick today. So how do we go about putting a model in production? Step one is, well, you've kind of done step one, right? Step one is step one, two, three, four. It's figure out what problem you want to solve, figure out how to find the data for it, gather some data. And so forth. So what's the kind of first step after you've got your data? The next step is data cleaning. And if you go to chapter two of the book, which I'm going to go ahead and open up now. So here is the book. So you can open it in co-lab directly from the course or if you've plowed it to your computer or whatever you can do it there. So remember course.fast.ai will run you through exactly how to run these notebooks. And so you can see chapter two is all about putting stuff in production. And so here is chapter two. All right. And so remember we hit shift, enter to run cells. Okay. To execute them. And so we're going to go to the part of the book where we start cleaning the data. So I click on navigate. And we'll go down here, gathering data. There we are. So we could do a quick bit of revision first. Now, by the way, I will mention a lot of people ask me what are the little tricks I use for getting around Jupyter Notebook so quickly and easily. One of the really nice ones as you'll see is this navigate menu, which actually doesn't appear by default. So if you install something called Jupyter Notebook Extensions, Jupyter Notebook Extensions. And so you just, you just pip install them, follow the instructions. And then restart Jupyter. Obviously this, Colab already has a table of contents by the way. So this is just if you're using something local, for example. Then you'll see here that this NB extension single up here. And if you click on table of contents to that gets you this handy navigation bar. The other thing I really like is this one here called collapsible headings. And that's the one which gives me these nice little things here to close and open up. And actually that's not even the best part. The best part for me is if I hit right arrow, it goes to the end of a section. And if I hit left arrow, it goes to the start of a section. So it's like if I want to move around sections, I just press up left, down right, down right. Very handy. And if you hit left again when you hear it'll close it up, hit right again here, open it up. So that's collapsible headings. Anyway, couple of really handy things. And we'll be talking a lot more about getting your notebook set up today in a moment. Okay, so one thing you'll notice is in the book we use the Bing API for searching images. I've just kind of had and replaced Bing with DDG because the Bing API requires getting an SDK key, which honestly it's like the hardest thing in deep learning is figuring out the Bing Azure website and getting that sorted out. DDG doesn't. So it's basically exactly the same. And I'll share this notebook as well on the course website and the forum. But all I've basically done is I've replaced Bing with DDG and got rid of the key. So then just like we did last week, we can search for things. And so in the book, we did a bear detector because at the time I wrote it, my then toddler was very interested in me helping identify teddy bears. And I certainly didn't want her accidentally cuddling a grizzly bear. So we show here how we can search for grizzly bears just like last week. Something that loops through grizzly bears, black bears and teddy bears just like last week. Get rid of the ones that failed just like last week. And one thing a few people have asked on the forum is how do I find out more information about basically any Python or fast AI or PyTorch thing. There's a few tips here in the book. One is that if you put a double question mark next to any function name, you'll actually get the whole source code for it. And by the same token, if you put a single question mark, you'll get a brief, you know, a little bit of information. If you've got NB Dev installed, I think it's NB Dev you need, then you can type doc and that'll give you, perhaps most importantly, a link straight to the documentation where you can find out more information. And generally, there'll be examples as well. And also a link here to the source code if you want to run. Let's do that with a control. Okay, a link to the source code and that way you can jump around. Notice that in GitHub in the source code, you can click on things and jump to their definition. So it's kind of a nice way of skipping around to understand exactly what's going on. Okay, so lots of great ways of getting help. But what I promised you is that we're going to now clean the data. So I'm going to tell you something that you might find really surprising. Before you clean the data, you train a model. Now I know that's going to sound really backwards to what you've probably heard a thousand times, which is that first you build, you train your data and then you train your model. But I'm going to show you something really amazing. First, we're going to train a model and you'll see why in a moment. So to train a model, just like before, we use a data block to grab our data loaders. There's lots of information here in the book about what's going on here. There we go. And so then we can call show batch to see them as per usual. There's a little sidebar here in the book, I'll quickly mention which is about the different ways we can resize. I think we briefly mentioned it last week. We can squish. Last week I used a string. You can use a string or this kind of enum like thing that we have. You can see with a squish, you can end up with some very thin bears, right? So this is the real size that's the shape of the bear. Here it's become thin. But you can see now we've got all of its cubs. Are they called cubs? Yeah, bear cubs. So it squished it to make sure we can see the whole picture. Same one here. This one was out of the picture. We squished it. This guy now looks weirdly thin that we can see the whole thing. So that's squishing. Or else this one here is cropping. It's cropped out just the center of the image so we get a better aspect ratio but we lose some stuff. This is so we can get square images. And the other approach is we can use pad. And so you can pad with various different things. If you pad with zeros, which is black, you can see here now we've got the whole image and the correct aspect ratio. So that's another way we can do it. And different situations result in different quality models. You can try them all. It doesn't normally make too big a difference so I wouldn't worry about it too much. I'll tell you one though that is very interesting is random resized crop. So instead of saying resize, we can say random resized crop. And if we do that, you'll see we get a different bit of an image every time. So during the week this week somebody asked on the forum, I'm trying to... This is a really interesting idea which it turned out worked slightly because they wanted to recognize pictures of French and German texts. So obviously this is not the normal way you would do that but just for a bit of an experiment. And I love experiments. So they had very big scans of documents and they wanted to figure out whether it was French or German just by looking at images. And they said the pictures were too big. What should I do? I said use random resized crop and that way you would grab different bits of the image. And this is very nice because you could run lots and lots of epochs and get slightly different pictures each time. So this is a very good technique. And this idea of getting different pictures each time from the same image is called data augmentation. And again I'm not going to go into too much detail about data augmentation because it's in the book. But I'll just quickly point out here that if you use this thing called aug transforms, so augmentation transforms. And here I have multiplied them by two. So I've made them super big so you can see them more easily. You can see that these teddies are getting turned and squished and warped and recolored and saturated. All this stuff to make every picture different. And generally speaking if you're training for more than about 5 or 10 epochs which you'll probably want to do most of the time unless you've got a super easy problem to solve you'll probably want to use random resized crop and these aug transforms. Don't put the multicles too. Just leave that empty. I'm just putting it there so you can see them more clearly. So I've got an interesting question here from Alex in our audience which is, is this copying the image multiple times doing something like this or something like this? And the answer is no, we're not copying the image. What happens is that image, so each epoch every image gets read. And what happens here is though is kind of in memory in RAM the image is being warped. It's being recropping it and recoloring it and so forth. So it's a real time process that's happening during model training. So there's no copies being stored on your computer but effectively it's almost like there's infinitely slightly different copies because that's what the model ends up seeing. So I hope that makes sense. Alex and everybody else. That's a great question. Okay, so we've got, we're going to use random resize crop, we're going to use augmentation transforms so that we can get a data loaders from that and then we can go ahead and train our model. It takes about a minute. In this case we only did four epochs of fine tuning. We'll talk about why there's five here later in the course but four main epochs of fine tuning. So we probably didn't really need random resize crop and org transforms because there's so few epochs but if you want to run more epochs this is a good approach. Under 3% error, that's good. Okay, so remember I said we're going to train a model before we clean. Okay, so let's go ahead and train it. So while that's training that's running on my laptop which only has a four gigabyte GPU it's pretty basic but it's enough to get started. While that's training we'll take a look at the next one. So the first thing we're going to look at is the confusion matrix. The confusion matrix is something that it only is meaningful for when your labels are categories. And what it says is what category errors are you making? And so this is showing that the model that we've got at this point there was two times when there was actually a grizzly bear and it thought it was a black bear and there was two times when there was actually a black bear and it thought it was a grizzly bear and there was two times that it got Teddy's wrong. Which makes sense, right? Teddy's do look quite different to both. In a lot of situations when you look at this it'll kind of give you a real sense of like okay, well what are the hard ones? So for example if you use the Pets data set that we quite often play with in the book and the course this classification matrix for different breeds of pet really shows you which ones are difficult to identify red Wikipedia pages and pet breeding reports about how to identify these particular types because they're so difficult and even experts find it difficult. And one of the things I've learnt from doing the course actually is black bears and grizzly bears are much harder to pick apart than I had realized so I'm not even going to try. But I'll show you the really interesting thing we can do with this model is that now we've created this classification interpretation object which we use for confusion matrix we can say plot top losses we can say plot top losses and this is very interesting. What it does is it tells us the places where the loss is the highest. Now if you remember from the last lesson the loss is that measurement of how good our model is that we take after each time we run through an item of data. A loss will be bad if we predict wrongly and we're very confident about that prediction. So here's an example where we predicted prediction actual loss probability where we predicted grizzly and it was actually a black and we were 96% sure our model was that it's a grizzly. Now I don't know enough about bears to know whether the model made a mistake or whether this actually is a picture of a grizzly bear but so an expert would obviously go back and check those out. Now you'll notice a couple here it's got grizzly grizzly teddy teddy they're actually correct. So why is this loss bad when it's correct and the reason is because it wasn't very confident it was only 66% confident. So here's a teddy it's only 72% confident. So you can have a bad loss either by being wrong and confident or being right and unconfident. Now the reason that's really helpful is that now we can use something called the fastai image classifier cleaner to clean up the ones that are wrongly labeled in our data set. So when we use the image classifier cleaner it actually runs our models that's why we pass it and learn. And I mentioned that I don't know much about black bears and grizzly bears but I do know a lot about teddy bears so I'll pick teddy bears and if I click teddy bears it's now showing me all the things in the training set you can pick training or valid that were marked as teddy bears and here's what's really important they're ordered by loss so they're ordered by confidence so I can scroll through just the first few and check they're correct. And oh here's a mistake so when I find one that was wrongly gathered I can either put it if it's in the wrong category I can choose the correct category or if it shouldn't be there at all I click delete. So here I'll go ahead and click delete. So you can see some reasons that some of these are hard like for example here's two teddies which is just I guess confusing because it doesn't see that often. This one here is a bit weird looking just like a wombat. This is an awful lot of teddies. This one maybe is just a bit hard to see from the background but these other ways they look fine. Fine so we just look through the first few and if you don't see any problem or problems in the first few you're probably fine. So that's cleaned up our training set that's cleaned up our validation set as well. So here's that one it had trouble with I don't know why it had trouble with that one but so be it and we'll have a quick scroll through. Okay I'm not really sure that's a bear. So I'm just going to go ahead and delete it. It's a teddy something but you know it's a problem. Okay that's not a teddy either. So you see the idea right? So after we've done that what that does is the cleaner has now stored a list of the ones that we changed and the list of the ones we deleted. So we can now go ahead and run this cell and so that's going to go through a list of all of the indexes that we said to delete and it will delete those files and it'll go through all the ones we said to change and it will move them to the new folder. There we go done. So this is like not just something for image models it's just it's actually a really powerful technique that almost nobody knows about and uses which is before you start data cleaning always build a model to find out what things are difficult to recognize in your data and to find the things that the model can help you find data problems and then as you see them you'll kind of say okay I see the kinds of problems we're having and you might find better ways to gather the next data set or you might find ways to kind of automate some of the cleaning and so forth. Okay so that is data cleaning and since I only have a 4 gigabyte GPU it's very important for me to close and halt because that will free up the memory so it's important to know on your computer your normal RAM doesn't really get filled up because if you use up too much RAM what will happen is that instead your computer will start it's got swapping which is basically to save that RAM onto the hard disk to use it later. GPUs can't swap. GPUs when they run out of RAM that's it, you're done. So you need to make sure that you close any notebooks that are using the GPU that you're not using and really only use one thing at a time on the GPU otherwise you're almost certainly run out of memory. So we've got the first few reds starting to appear so remember to ask and in terms of the yellows it's important to know as you watch the video, I'm not asking you to run all this code the idea is to kind of watch it and then go back and pause as you go along or you can just stop try, stop, try. The approach I really like and a lot of students really like for watching these videos is to actually watch the entire thing without touching the keyboard to get a sense of what the video is about and then go back to the start and watch it again and follow along that way at every point you know what it is you're doing, you know what's going to happen next that can actually save you some time it's a bit of an unusual way because obviously like real life lectures you can't do that you can't rewind the professor and get them to say it again but it's a good way to do it here. So now that we've cleaned our data how are we going to put it into production or in the book we use something called Voila and it's pretty good but there's actually something that I think most of you are probably going to find a lot more useful nowadays which is something called Hugging Face Spaces and there's a couple of things you can use with that we're going to look at something called Gradio today and there isn't a chapter about this in the book but that doesn't matter because Tanishk Abraham who's actually one of the TAs in the course has written a fantastic blog post about really everything we're going to cover today so there's a link to that from the forum and from the course page so this is like the equivalent of the chapter of the book if you like and I would be remiss if I didn't stop for a moment and call out Tanishk in a big way for two reasons the first is he is one of the most helpful people in the fast AI community he's been around quite a few years incredibly tenacious thoughtful and patient and also because I have this fantastic picture of him a few years ago with Conan when he was a famous child prodigy so now you know what happens to famous child prodigies when they grow up they became even more famous fast AI community members and deep learning experts so you should definitely check out this video of him telling jokes to Conan I think he's still only 18 actually so this is probably not that many years ago so thank you very much Tanishk for all your help in the community and sorry for embarrassing you with that picture of you as a nine year old I'm not really haha now the thing is for doing radio and hugging face spaces well it's easy enough to start okay we start over here on the hugging face spaces page which we've linked to from the forum and the course and we're going to put a model in production where we're going to take the model we trained and we are going to basically copy it to this hugging face spaces server and write a user interface for it so start let's go create new space okay so you can just go ahead and say alright so obviously you sign up the whole thing's free and basically everything I'm showing you in this entire course you can do for free that's the good news okay so give it a name just create something minimal I always use the Apache license because it means other people can use your work really easily but you don't have to worry too much about patents as I say there's a few different products you can use with it we're going to use also free if you make it public then you can share it which is always a good idea when you're a student particularly to really be building up that portfolio okay so we're done we've created a space now what do we do next well spaces works forget now most software developers will be very familiar with get some data scientists might not be and so gets a very very useful tool I'm not going to talk about it in detail but let's kind of quickly learn about how to use it right now get you can use it through something called github desktop which is actually pretty great and even people who use get through the console should probably be considering using github desktop as well because some things just much faster and easier in it in fact I was talking to my friend Hamel today and I was like oh help I've accidentally committed this two things by mistake what's the easiest way to revert it and he used to work at github and I thought he was going to have some fancy console command and he was like oh you should use github desktop and you can just click on it oh that's a great idea so that's useful but most of the time we do use get from the console from the terminal if you're a linux user or a mac user you've already got a terminal very straightforward no worries if you're a windows user I've got good news nowadays windows has a terrific terminal it's called windows terminal you get it from the Microsoft store so in fact every time you see me using a terminal I'm actually using that windows terminal works very well God knows why I'd want it to have these ridiculous colors but there you go now what do you want to be running inside your terminal obviously if you're in linux or mac you've already got a shell set up in windows you almost certainly want to use ubuntu so windows believe it or not can actually run a full linux environment and to do it is typing a single line which is this so if you go to just google for wsl install run power shell as administrator post that command wait about 5 minutes reboot you're done you now have a complete linux environment now one of the reasons I'm mentioning this is I'm going to show you how to do stuff on your own machine now and so this is like going to a bit of extra level of geekery which some data scientists may be less familiar with so don't be worried about the terminal I think you're going to find it really helpful and much less scary than you expect and I particularly say like for me I choose to use windows and that's because I get all the nice windows gooey apps and I can draw on my screen and do presentations and I have a complete linux environment as well and that linux environment uses my gpu and everything so for me my first choice is to use windows my second choice not very much really like it would be to use linux mac is a little bit harder but it's still usable so some things are a little bit trickier on mac but you should be fine so whatever you've got at this point you've now got a terminal available and so in your terminal one of the really nice things about using a terminal is you don't have to follow lots of instructions about click here click here click here you just copy and paste things so I'm just going to copy this and you go over to your terminal and you paste it in and you run it and after you do that you'll find that you've now got a directory and so that new directory initially is empty and they tell you okay go ahead and create a file with this in it okay so how do you create a file with that in it when we're in here in our linux environment on windows or in the terminal on mac or whatever well all you do in windows if you just type explorer.exe. it'll open up explorer here or better still on either mac or linux or windows so yeah so regardless of what type of computer on you can just type code. and it will pop up official studio code and open up your folder and so then you can just go ahead and if you haven't used VS Code before it's really well worth taking a few minutes to read the some tutorials it's a really great IDE and so you can go ahead and create an app.py file like they tell you to app.py file containing what they told you to put in it here it is here alright nearly there so you can now go ahead and save that and then you need to commit it to a Gradio to Hugging Face Spaces so one really easy way is just in visual studio itself you can just click here and that'll give you a place where you type a message and you hit tick and it'll send it off to Hugging Face Spaces for you so once you've done that you can then go to back to the exact same website you're on before Hugging Space Spaces JPHOO Minimal and what you'll find now is that it'll take about a minute to build your website and it's the website it's building is going to have a Gradio interface with a text input a text output and it's going to run a function called greet on the input and my function called greet will return hello name so that's what it's going to do no there it goes let's try it it'll say hello to Tanishk I'm not always very good at remembering how to spell this name I think it's like that and there you go so you can see it's put the output for our input so not a very exciting app but we now have to be fair an app running in production right now I told you we'd have a deep learning model running in production so now we have to take the next step which is to turn this into a deep learning model alright so first we're going to need a deep learning model and there's a few different ways we can get ourselves a deep learning model but basically we're going to have to train one so I've got a couple of examples I've got a caggle example and a colab example maybe I'll quickly show you both that they're going to do the same thing and I'm just going to create a dog or a cat classifier okay so here's our caggle model I'll click on edit so you can actually see what it looks like in edit view now caggle already has FastAI installed but I always put this first just to make sure we've got the latest version and obviously import stuff so we're going to grab the pets data set a function to check whether it's a cat that's our labeling function for our image data loaders remember this is just another way of doing data blocks it's like a little shorthand and we create our learner and we fine tune it so that's all stuff we've seen before so in caggle every notebook has an edit view you just saw and a read of you and so you can share your notebook if you want to and then anybody can read the read of you as you see and you can see it shows you what happened when I ran it and so I trained it it took so the GPUs on caggle are a bit slower than most modern GPUs but they're still fast enough I mean it takes 5 minutes and there's one bit at the end here which you haven't seen before which is I go learn.export and I give it a name now that's going to create a file containing our trained model and that's the only thing creating this file is the only thing you need a GPU for so you do that on caggle or on colab so here's exactly the same thing on colab you can see pip install is cat entire data, image data loaders so I've got to show batch here as well just for fun create my learner and then export so while we wait I might go ahead and just run that one nice thing about caggle is once you've run it and saved it you can then go to the data tab and here is basically anything you've saved it's going to appear here and here it is model.pickle so now I can go ahead and download that and that will then be downloaded to my downloads folder and then I need to copy it into the same directory that my Huckingface Spaces apps in now my Huckingface Spaces app is currently open in my terminal on Mac you can type open. or in Windows you can type explorer.exe. and that'll bring up your finder or explorer in that directory and so then you can just paste that thing you downloaded into this directory something by the way in Windows I do which I find really helpful is I actually grab my home directory in Linux and I pin it to my quick access and that way I can always jump in Windows straight to my Linux files not really something you have to worry about on Mac because it's all kind of integrated but on Windows they're like kinda like two separate machines so I created a space called testing and I downloaded my model.pickle and I pasted it into testing so now we need to know how do we do predictions on a saved model so we've got a notebook for that ok so we've got a notebook for that and so I'm going to take you through how we use a model that we've trained to make predictions there's a few funny things with hash pipe which I'll explain in a moment just ignore those for now so we import fast.ai as usual we import radio as we did before and we copy in the exact same is cat definition we had before that's important any external functions that you used in your labelling need to be included here as well because that learner refers to those functions that learner has saved everything about your model but it doesn't have the source code to the function so you need to keep those with you so let's try running this so for example I just grabbed as you might have seen in my explorer I just popped a dog picture there and so we can create a python image library image from that dog turn it into a slightly smaller one so it doesn't overwhelm our whole screen and there is a picture of a dog so how do we make predictions of whether that's a dog or a cat well it's very simple all we do is instead of training a learner we use load learner we pass in the file name that we saved and that returns a learner this learner is exactly the same as the learner you get when you finish training so here we are here's colab we've just been training a learner so at the end of that there's a learner that's been trained and so we kind of froze it in time something called a pickle file which is a python concept a frozen object we saved it to disk we transferred it to our computer and we've now alerted it and we've now un-thought it here's our unpickled learner and we can now do whatever we like with that so one of the things that the one of the methods that a learner has is a dot predict method so if I run it you can see even on my laptop it's basically instant in fact we can see how long it took if you in Jupyter things that start with percent are called magics there's special Jupyter things so for example there's a thing to see how long something takes there you go okay so it took 54 milliseconds to figure out that this is not a cat so it's returning two things is it a cat as a string is it a cat as a zero or one and then the probability that it's a dog and the probability that it's a cat so the probability of zero false and one true but is it a cat so definitely a dog so we now want to create a Gradio interface which basically has this information so Gradio requires us to give it a function that it's going to call so here's our function so we're going to call predict and that returns as we said three things the prediction is a string the index of that and the probabilities of whether it's a dog or a cat and what Gradio wants is it wants to get back a dictionary containing each of the possible categories which in this case is dog or cat and the probability of each one so if you haven't done much Python before a dict of a zip may be something you haven't seen very handy little idiom well worth checking out ditto if you haven't seen map before anyway here it is one slightly annoying thing about Gradio at the moment is that it doesn't handle PyTorch tensors you can see here PyTorch is not returning normal numbers it's returning tensors it's not even returning NumPy arrays in fact Gradio can't handle NumPy either so we have to change everything just to a normal float so that's all that this is doing is changing each one to a float so for example if I now call classify image with our doggy image we get back a dictionary of a dog yes definitely cat definitely not so now we've got all that we can go ahead and create a Gradio interface so Gradio interface is something where we say well what function do you call to get the output what is the input in this case we say oh the input is an image and so to check out the Gradio docs it can be all kinds of things like a webcam picture or a text or you know all kinds of things give it a shape that it's going to put it into the outputs just going to be a label so we're going to create very very simple interface and we can also provide some examples and so there's a dog, a cat and a donno which I'll do about in a moment which you'll see here there's a dog and a cat and a donno so once I launch it it says okay that's now running on this URL so if I open that up you can see now we have just like Suvash we have our own not yet in production but running on our own box classifier so let's check dog so you can click and upload one or just choose the examples yeah yeah so it's running on my own laptop basically instant and I really have to tell you this story about this guy here this is the donno submit wait why is it saying 100? normally this says like 50-50 that's a bummer this model's got messed up my whole story so last time I trained this model and I ran it on the donno it said like it's almost exactly 50-50 and the way we found this picture is I showed my 6 year old daughter she's like what are you doing dad? like I'm coding what are you coding? oh you know dog cat classifier she checks it out and the first question is can I take your keyboard for a moment and she goes to Google and she's like what is a dog mixed with a cat called? like there's no such thing as a dog mixed with a cat anyway she goes to the images tab and finds this picture and she's like look there's a dog mixed with a cat she said run it on that dad run it on that and I ran it and it was like 50-50 it had no idea if it was a dog or a cat now this model I just retrained today and I was sure it's a cat so there you go I think I used a slightly different training schedule or something or I gave it an extra epoch anyway that's a dog cat but apparently it's a cat I guess it is a cat it's probably right shouldn't have trained it for as long okay so there's our interface now that's actually running so you actually have to click the stop button to stop it running so otherwise you won't be able to do anything else in your notebook okay so now we have to turn that into a python script so one way to turn it into a python script would be to copy and paste into a python script all the things that you need would be to copy and paste into a python script all the parts of this that you need so for example we wouldn't need this it's just to check something out we wouldn't need this it was just experimenting this was just experimenting we'd need this so what I did is I went through and I wrote hash pipe export at the top of each cell that contains information that I'm going to need in my final script and then so there are the steps right and then at the very bottom here I've imported something called notebook to script from nbdev and if I run that and pass in the name of this notebook that creates a file for me called app.py containing that script so this is a nice easy way to like when you're working with stuff that's expecting a script and not a notebook like hugging facespaces does it's fine to just copy and paste into a text file if you like but I really like this way of doing it because that way I can do all of my experimentation in a notebook and when I'm done I just have a cell at the bottom I just run and export it how does that know to call it app.py that's because there's a special thing at the top default export which says what Python file name to create so that's just a little trick that I use so now we've got an app.py we need to upload this to Gradio how do we do that you just push it to get so you can either do it with digital studio code or you can type get commit and then get push and once you've done that if we change minimal to testing I think this hopefully might still be running my previous model because I didn't push it and that way we can see our crazy dog cat alright so here it is you can see it running in production so now this is something that anybody if you set it to public anybody can go here and check out your model and so they can upload it and so here's my doggy yep definitely a dog cat yeah I think I might have trained this for an epoch or two less so it's less confident yeah definitely a cat dog cat hey dog cat hmm still thinks it's definitely a cat oh well so be it so that is okay so that is an example of getting a simple model in production there's a couple of questions from the forum from the community okay so one person's asking what's the difference between a PyTorch model and a fast AI learner okay that's fine we'll get to that shortly don't know if it'll be this lesson it might be this lesson or the next lesson and then somebody else asked basically is asking how many epochs do we train for so as you train a model your error rate as you can see it improves and so the question is should I run more should I increase the number of epochs this is doing three epochs right here's my three epochs plus one to get started look it's up to you right I mean for this is here saying there's a 1% error I'm okay with a 1% error you know if you want it to be better then you could use more data augmentation and you could train it for longer if you train for long enough as we'll learn about soon maybe in the next lesson if you train for long enough your error rate actually starts getting worse and you'll see we'll learn about why so basically yeah you can train until it's good enough or until you've run out of patience or time or run out of compute or until the error rate starts getting worse okay oh and then in colab how do you grab your model all you need to do in colab is after you've exported it is if you go into their file browser you'll actually see it here right and you can click download it's a bit weird it doesn't like pop up a box saying where do you want to download it to but instead this kind of progress circle thing pops up and so depending on how big it is and so forth it can take a few minutes and once that circle fills up then the browser thing will finally pop up and say okay you can save it okay so that's how you actually grab your model so as you can see the step where you actually need a GPU you can use these totally free resources colab, kaggle there are other ones we'll talk about in future lessons and then you can do everything else on your own computer including the predictions are fast right so you really don't need to use a GPU for that unless you're doing thousands of them okay here we go so now it's asking me to save it okay so now one big issue is we needed to run it on our computer we needed python and jupyta notebooks running on our computer so how do you do that because this is where often people get in all kinds of travel I'm trying to figure out how to get this all working so the good news is we've actually got something that makes it very very straightforward it's called fast setup there's really only just one part of it you need so let me show you it's actually a git repository on git hub git hub is the place where most git repositories live so if you go to git hub fast.io fast setup you'll see it and so what you can do is you can now grab this whole repository just by clicking here on code and if you've got git hub desktop installed click on open with git hub desktop and as you'll see it brings this up saying okay I'm ready to save this for you so I click clone so it's making a copy of it there we go so basically once you've cloned it you'll then find there's a file in there called setupconder.sh which you know the details don't really matter it's pretty short but that's the thing that's going to install python for you so at that point you can just run .slash setupconder and it'll run this installer now if you've got linux or mac you've already got python on your machine don't use that python and the reason is because that python is called the system python it's used by your computer to do computer-y stuff right it's actually it's actually needed you don't want to be messing with it I promise you like it always leads to disaster always you want your own development version of python it's also going to make sure you've got the latest version and all the libraries you want and by the by far the best one for you is almost certainly going to be these conder based python distributions so if you run setupconder you'll get the one that we recommend and the one we recommend at the moment is something called MambaForge so basically once you run it you'll find that you've now and you close your terminal and reopen it you'll find you've now got one extra command which is called Mamba and Mamba lets you install stuff so once you've run it you'll be able to go to Mamba, install Fast.ai and that's going to actually we should probably I should mention this is actually more bit more detail about how to install it correctly if we go to docs.fast.ai installing yeah okay we actually want to do conder install minus C fast-chain let's just copy and paste actually and then the other thing I'll say instead of using conder replace conder with Mamba because nowadays it's much faster so Mamba install minus C fast-chain fast.ai now this is going to install everything you need it's going to install PyTorch it's going to install NumPy it's going to install Fast.ai and so forth right and it's obviously I've already got it and then the other thing you'll want to do is install nbdev so you can do exactly the same thing for nbdev you don't have to right it's just but that'll install Jupyter for you amongst other things and so at that point you can now you can now use Jupyter and so the way Jupyter works is you can see it over here this is by I'll go ahead and close it so we can start again so basically to use Jupyter you just type Jupyter notebook okay and when you run it it'll say okay we're now running a server for you and so if you click on that hyperlink it'll pop up this okay which is exactly what you see me use all the time okay so you know that hopefully is enough to kind of get you started with python and with Jupyter notebook the other way people tend to install software is using something called pip instead of mamba pretty much anything you can do with mamba you can also do with pip but if you've got a GPU pip isn't going to install things generally so that it works on your GPU you have to install lots of other stuff which is annoying so that's why I kind of tell people to use mamba but you can use pip otherwise still a little bit of red please let us know how we can help you going okay so let's see how we going with our steps I forgot I had these steps here to remind myself we created a space tick we created a basic interface tick okay we got get set up we got condor set up or mamba so mamba mamba and condor are the same thing mamba is just a much faster version and we'll keep some notes on the course website because at the moment they're actually working on including the speedups from mamba into condor so at some point maybe it'll be fine to use condor again at the moment condor is way too slow so don't use it okay we've done dogs versus cats no problem yeah so we could also look at pet breeds well yeah we'll re-review if you look at that okay we've used xported learner no problem used mb dev no problem oh okay try the api yeah alright this is interesting so I think we can all agree hopefully that this is pretty cool that we can provide to anybody who wants to use it for free a real working model and you know with Gradio there's actually a really small amount of flexibility around like how you can make your website look using these various different widgets it's not amazingly flexible but it's flexible enough to kind of it really just for prototyping right so Gradio has lots of widgets and things that you can use the other main platform at the moment that Hugging First Basis supports is called streamlet streamlet is more flexible I would say than Gradio not quite as easy to get started with but it's kind of that nice in between I guess so also a very good thing again mainly for kind of building prototypes but at some point you're going to want to build more than a prototype you want to build an app and one of the things I really like about Gradio in Hugging First Basis is there's a button down here View the API so we can actually create any app we want and the key point is that the thing that does the actual model predictions for us is going to be handled by Hugging First Basis Gradio and then we can write a JavaScript application that then talks to that now there's going to be two reactions here anybody who's done some front-end engineering is going to be like oh great I can now literally create anything in the world because I just write any code and I can do it and they'll be excited and a lot of data scientists might be going uh oh I have no idea how to use JavaScript it's not in my you know so this is again where I'm going to say look don't be too afraid of JavaScript I mean obviously one option here is just to kind of say hey I've got a model throw it over to your mate who does know JavaScript so please create a JavaScript interface for me but let me just give you a sense of like how really not hard this actually is so there's a endpoint there's now a URL that's running with our model on it and if you pass it some data an image some image data to this to this URL it's going to return back the dictionary it's going to do exactly the same thing that this UI does but as an as an API as a function we can call and so it's got like examples here of how to call it so for example I can actually let me show you the API as an example using that minimal interface we had because it's just going to be a bit simpler so if I click curl and I copy that copy that and paste so you can see there oh that's not a great example passing in hello world if I pass in Tanishk again let's see how I'm going with his name Tanishk and returns back hello Tanishk so this is how these APIs work right so we can use JavaScript to call the API and we've got some examples so I've created a website and here is my website tiny pets and on this website as you can see it's not the most amazingly beautiful thing but it's a website it's a start right and up here I've got some examples single file click choose file click and in this example I'm actually doing full pet classification so I actually trained a model to classify breed which we'll talk about more next week rather than just dog vs cat so let's pick a particular breed and we run it oh and there it is now not very amazing right but the fact is that this is now a JavaScript app means we have no restrictions about what we can do and let's take a look at that HTML that's it it easily fits in a screen right and the basic steps are not crazy right it's basically we create an import for our photo we add an event listener that says when you change the photo call the read function the read function says create a file reader read the file and when you finish loading call loaded and then loaded says fetch that now that path there is that path there right looks like we're doing the full pets one so this is basically just copied and pasted from their from their sample and then grab the JSON and then grab from the data the first thing the confidences the label and then set the HTML so as you can see it's like okay if you haven't used JavaScript before these are all new things right but they're not it's not harder than Python right it's just another language to learn and so from here you can start to build up right so for example we've created a multi-file version so with the multi-file version let me show multi-file choose so we can now click a few so we've got a new fee, a rag doll, a basset hound and some kind of cat I'm not much of a cat person so we chose four files and bang! they've all been classified apparently it's a Bengal I wouldn't know so there's the multi-file version and if you look at the code it's not much more right it's now just getting all the files and mapping them to read and now appending each one so not much more code at all and as you might have seen on our site here there's a few more examples which is some of the community during the week has created their own versions so this one here is I think this is from one of the Gradio guys they called it get to know your pet so if I choose a pet I really like this because it actually combines two models so first of all it says oh it's a basset hound and then it lets me type in and ask things about it so I can say oh what kind of tail does it have search and so that's now going to call an NLP model which asks about it's a curved sabertail there we go what maintenance does it need so again like here you can kind of see how a basset hound's ears must be cleaned and inserted out frequently so this is like combining models so you can see this is something that you couldn't do with just a kind of a ready to go interface and so the next thing I wanted to point out is how did we create the website I showed you how to create an HTML file but like how do you create those and how do you make a website out of them well watch this let's here's a here's the source code to our most basic version so I could just save this there we go so we could open that with visual studio code and what we could actually do is I could double click on it and here it is it's a working app so you can see I don't need any software installed on my computer to use a javascript app it's a single file I just run it in a browser a browser is our complete execution environment it's got a debugger it's got the whole thing so here you can see it's just calling out to this external all sitting here on my computer so once I've got my HTML file that's working fine on my computer in VS Code how do I then put it on the web so that other people can use it again the whole thing's free there's a really cool thing called github pages which basically will host your website for you and because it's just javascript it'll all work just fine the easiest way to create a github pages site in my opinion is to use something called fastpages which is a fastai thing and basically all you do is you follow the setup process so first let's just go through it it says generate a copy by clicking on this link so I click the link alright okay give it a name I try to make everything public I always think it's good practice you don't have to create repo generating and then there's basically two more steps it takes about five minutes, we don't have five minutes so I'll show you the one that I've already built which is fastai slash tinypets and so once it's done you'll basically end up with this empty site which again you just go code open with github desktop or open with visual studio whatever so open with github desktop or you can copy and paste this to your terminal and so any one of those is going to get you this whole thing on your computer you can save your html files there push it back up to github and what you'll find is fastpages will show you the link to the website that is created for you now the website that's created for you you can make it look however you want using something called a theme so you'll see it's created a file called config.yaml where you can pick a theme so in this case I picked a theme called alembic for no particular reason so github pages uses something called jekyll and so any jekyll theme will basically work so I picked out this theme and so as a result when I now save things into this repo they will automatically appear in this website and the files automatically appear up here in this list so if you look at my index that's the home page the entire file is just this the only slightly weird thing is at the top of every github pages file you have to have three dashes title and layout and three dashes it's called front meta and so once you do that and save it it will appear in your website so something else I did then it's like okay well that's all very well that fastai has created this website but I don't really like what it looks like I would have created a different version no worries you can go to fastai tinypets and click fork and when you click fork it's going to create your own copy so I did that under my personal account which is jph00 and look now I've got my own version of it and now I can make changes here so I made a few changes one change I made was I went to config.yaml and I changed the theme to pages themes hacker so once you fork one thing you have to do which normally fastpages does for you so you do have to go to settings and click pages and actually enable github pages so you basically have to by default it's turned off so here you'll just have to turn it on so use the master branch root save and then it will say no worries it's ready to be published and so I changed the config.yaml file to point at a different theme and so if you look at now the jph's tinypets it's different okay so it's got the same info but it's much more hackerish jph00 is a serious hacker as you can tell from his website so anyway look it's a very brief taste of this kind of world of javascript and websites and so forth but I wanted to give you a sense of like you know you don't need any money you don't need any ideas you know you don't really need much code to get started with writing your own web apps and thanks to Hugging Face Spaces you know they'll even host your model for you and all you need to do is just have the magic string for the thing to call okay so signing out hacker Jeremy Howard thanks very much for watching and in the next lesson we're going to be digging into some natural language processing we're going to be doing some of the same stuff but we're going to be doing it with language rather than pictures and we're going to be diving under the hood to see how these models actually work we're going to learn about things like stochastic gradient descent and we might even be having to brush off a little bit of calculus I hope I haven't put you off by saying the C word we'll see you next time thanks all, bye