Simple PyTorch working neural network. Beginner Friendly!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome clarity coders in today's video we're going to use PI tortes to create your first neural network and use that neural network to classify hand-drawn digits in the final part of this video I'm going to show you how you can process your own images to pass through this neural network as well best of all if you get stuck at any point feel free to jump into our programmers this court where we can help you walk through any problems you have let's jump right in [Music] so the first thing we're gonna do is install PI torch so I'm gonna go to this website you can just Google PI torch it is actually PI torch org now if you don't have Python installed already for this tutorial I'm gonna recommend that you go out and grab the distribution called anaconda anaconda is going to have Python for you and spider IDE that's the IDE that I'm gonna use in this lecture I have a video here on how to install it and how to open up Spyder as well once you have that you're ready to follow along so now when you're on this website you can just click get started and you can see here they're gonna show you how to start locally on your computer now your GPU is going to be way faster than your CPU to do machine learning calculations and that's because the GPU has a lot more cores where it can process a lot more small calculations at the same time now I don't know exactly what your system setup is or even if you have a GPU so we're gonna install it to run on your CPU right now and in a future video I'll show you how to move over to the GPU so right now we're gonna leave all these defaults your should've have your operating system the package if you're installing with anaconda like I am you're gonna leave this as Conda if you're not using anaconda you can switch to pip and install that way or your CUDA version down here we're gonna flip this to none so this is gonna run on the CPU only and you can see here it gives us this nice little command to install everything we need so I'm just gonna copy that and jump into Spyder IDE so I'm gonna paste that command in here now again you can you don't have to do this in anaconda you could use pip install and do it in in terminal like you normally would this is just if you want to follow along exactly with me so I'm gonna go ahead and run this command and we'll let that install you can see here that we finished installing our packages it says you may need to restart the kernel to use the updated packages so just to avoid any issue let's go ahead and do that you can go up to consoles restart kernel yes all right now we have a new kernel to work with what we're gonna create today is a very basic feed-forward neural network so how that works is we break up a set of inputs a set of what's called hidden nodes in the middle and then some output nodes now the data set we're going to be using is the M nest handwritten number data set we're using this just to set up and it's been done in a lot of different tutorials in a lot of different forms and we're gonna see how they use the data to come up with these answers so first things first we're gonna use this data set as they have laid out already ready for us to input into our network you can see here that I've done a crude drawing of breaking up an image into four pieces now we're gonna break it up into individual pixels so every pixel is going to have an input in this graph just to keep it simple I show our for our image being broken into four inputs in reality the images we're going to be using are 28 pixels so we're actually gonna have 784 inputs in our neural network 28 times 28 those are all gonna feed through to our hidden layers which are going to be set up with random weights and we may have more than one hidden layer in the middle then it's gonna feed through to our output node which is gonna have ten different outputs zero through nine and which ever has the highest value is going to be our guest so it's going to be a 0 through a 9 guessing what the number actually is and it's gonna go at the most certain guess now as we train this network with a training piece of our data set we're going to update the weights of these hidden layers in the middle and that's going to increase the accuracy of our network over time once we're done with that training set and we're done training our network then we can evaluate it so we're gonna stop trying to improve the network we're gonna pass through a test data set it's going to show us how accurate our neural network currently is now these test images were not used in the training set so this will give us an accurate picture of how it's going to do in the wild first thing we're gonna do is do some imports so I'm going to import torch and from torch vision I'm gonna import datasets now I'm going to pull in the data sets that we're going to use so as I talked about earlier this is a little image of the data set that we're going to use and we're just gonna set that in two variables right now so I'm gonna call one data set train and that's gonna be our training data set call it training and we're gonna pull in using data sets we're gonna pull in the EM NIST data set so this is gonna be the root the first argument is our route of where we want this directory to be I'm just gonna put it in our base directory so I'm just gonna do a blank string and before we get any farther let's make sure we're saving this in a folder so we're gonna put this entire project in a folder that's where our data sets gonna be and all that I'm gonna open this up I have just a temp python folder here that I'm gonna use so I'll stay there I'm gonna save this document inside of that folder I'll just call it PI torch example so we're in our root directory here I'm going to set train equal to true because this is going to be our training data set download equal to true so we're going to download it to our local computer here and we'll set transform equal to arms pose transforms dot too so all we're doing here is we're setting this training data set equal to true we're downloading it on to our local machine here and we're allowing them to transform them to get our data all ready to put in our network so we're letting them do the heavy lifting for us basically in this case and now I'm gonna copy this and I'll call this my testing data set I'll set the only difference I'm gonna set test equal to false grab the test values in this case and store them in our testing variable and I also have to import that transforms function as well so I'm going to import transforms up here and that'll take care of your errors I'm gonna update my font size here just so we can see a little bit better so how I'm gonna run code is just a little different in this series I'm going to run it kinda in chunks so instead of running the whole program with the run file up here I'm just gonna run individual pieces of the code so I'm gonna highlight all this I got to get my imports in there and I got to get my data set I'm gonna right-click and run this cell so when I run the code like this this means I'm not gonna have to redownload this data set every time if you want to do different imports or stuff like that you're going to have to right-click and import it again or if you make changes that sort of thing so it should go through and grab our data sets if this goes successfully and you have no errors in your variable Explorer if you flip over here you should have a testing and a training variable that RM nist objects' if all that goes okay then you should be ready to go to the next step so now that we have these two training and testing data set setup I'm gonna now get them ready to go into our network so we're gonna talk about a couple things here if we pass the exact same set of data through our network over and over again in the exact same order our network could learn tendencies for example if we have five ones in a row or ten ones in a row or something like that where it's just guessing ones at the beginning and then it starts guessing something else it learns because of the pattern of our data so when we're using this training set we're passing it through our network multiple times to train our network over time in things called epochs so an epoch is going to be how many times we want to pass the same training set through our neural network to train it hi torch is gonna give us some simple functions to allow us to mix that up so one of those is to shuffle our data so every epoch the data is in a different order and also it's gonna allow us to use batch sizes so how many items are we passing through the network at one time and this can affect the speed of your network how fast you can train it so I'm gonna call my first one just trainset and I'm gonna set that equal to I'm gonna do torch dot utils dot data dot data loader and then you can see here the functions that are the parameters that we have to pass in we need to pass in our data set this is our Train underscore set so I'm gonna pass in our training data from above I'm gonna pass in our batch size and I'm gonna set that just equal to one for right now we can adjust that down the road I'm gonna set shuffle equal to true and that's gonna allow our data to be shuffled each time and I'm gonna do basically the same thing so I'm gonna copy that and down below I'm going to have a test set and I'm gonna pass in the test data instead testing data you see from my variable above here and let's change actually let's set up our batch sizes to be ten we'll just see how that looks now it's time to actually set up our neural network so to do that we're going to use a class to define our neural network and we're gonna have to do one more import from up top so I'm gonna do import torch dot and n as n N and then I'm going to create a class from our neural network now if you don't fully understand classes in Python that's okay you can access this video above where I talk about class is a little more basically we're creating our own object to work with that has its own parameters and attributes and different things like that so I'm gonna create a class and we can name it whatever we want I'm gonna call it Network and then we're gonna pass in n n dot module this notation might be a little weird for you even if you're kind of familiar with classes what we're doing here is we're creating our own class called network and we're letting inherit from this parent class and in modules so we're gonna be able to inherit attributes and methods and that sort of thing inside of this class that we're setting up so the first thing we're gonna do is create an init method for when we create our neural network so we're gonna find that using this special in its intact so I'm gonna do underscore underscore make sure you're doing two underscores they're a knit underscore underscore again two underscores then we're gonna pass in self again reference my video if you're a little confused by that notation this is just gonna allow the object to know about its own attributes methods that sort of thing then we're gonna call the initialize method on our parent so this n n dot module so I'm gonna do super dot underscore underscore a knit underscore underscore and that's gonna call the initialization method from our parent that we passed in that we're inheriting from and now we're ready to create our own Network now if you remember me talking about our data set a little before it's going to be an image it's 28 by 28 and what pi torch is done for us is its flattened the pixels of that image into a single array or list if you will so basically if there's 28 pixels in a row and there's 28 rows we're doing 28 times 28 which is 784 total pixels now pi torch has also done some different things with this image that I'll talk about later like gray scaling and different things of that nature and normalizing the numbers so we're going to talk about that a little bit at the end when we start using our own images so now we're going to set up the individual layers of our network now our network is going to have an input layer first that I showed in this picture it's gonna have more than four inputs it's gonna have 784 actually so we're gonna define that attribute we're gonna say self dot and then we can name this whatever we want I'm gonna call it input layer and I'm gonna set that equal to n n dot linear 784 so that's how many input nodes and the next is how many output nodes this will have and remember this is going to our middle hidden layer we can really decide any amount of nodes want on the next level it's kind of up to us so let's just start by saying 64 and a lot of this neural networks is kind of learning over time and playing with different parameters so right now we're kind of getting set up and just seeing what happens we're gonna have 784 inputs going to a middle layer of 64 nodes and then we can create our next layer so the next layer is going to be it in one we'll call it and we'll set that equal to n n dot linear now the first number is going to be in and now if we're following the flow of our program here the input layer is going to pass it to the hidden 1 layer so we have to have 64 inputs because that's how many outputs we have right here now we could have any number for our output again but we're gonna have one more hidden layer so we'll go ahead and put 64 again we'll have self dot hidden two we'll set that equal to n n dot linear we have to receive 64 and this is going to pass to our output node so we'll say 64 again we'll do self dot output and we'll set that equal to n n dot linear will receive 64 we said we have 10 possible outputs so the output could be a 0 a 1 a 2 a 3 a 4 5 a 6 7 & 8 or a 9 so a total of 10 different outputs so you can see here from how we ordered this this is actually the flow of our network so input we're gonna put 784 different pixels we're gonna pass it to 64 in the hidden layer that's gonna pass it to 64 and hidden layer 2 that's gonna pass it to 64 nodes in our output layer and it's gonna give us a final guess of one of our 10 different numbers linear is the type of function our nodes are gonna use to determine their output we can get really deep in the mathematics of it but right now we're just gonna stick with using that to determine our output from the node so the next thing we have to do is define how data will flow through our network now our network is just a feed-forward neural network so we're just gonna define one function here and it's going to be forward well pass in self again and then data this is data is just a variable that I chose you can choose whatever you want and then we're gonna say how data is gonna flow through this network and we're gonna do one more import up here actually we're gonna import torch dot and end functional as capital F so this is going to allow us to call this parent as a function instead of the module that we have up here and now we're going to keep pushing this data through the network so the first thing we're going to do is set data equal to we're gonna call that f the functional one that we have above and we're gonna call rel you rel u stands for rectified linear unit and it's essentially going to figure out what nodes are firing and what nodes aren't firing in our data set as we pass data through it so now we're gonna pass our data through the first layer so we're gonna do self dot input layer we're gonna pass in our data so now once this rel u runs it's gonna reassign the values in our data variable and then we can call that again so here I'm gonna reassign this variable again and I'm gonna do the same thing as above I'm gonna do r lu again except now i'm gonna pass it through the second layer of our network so that's gonna be hidden one and i'm gonna pass in our data again and then you can guess i'm gonna do the same thing a couple more times i'm gonna do in two this time and then finally i'm gonna set our data equal to self dot output this is our final node data so essentially even though we have them in an order up here we didn't really pass any data through we were just setting up our network now down here we actually push data through our network and we went in order just like we did above we don't have to that's just how we did it and this should define our neural network we need to return the actual output from our network so we're gonna return f dot log soft max we're gonna use our data and we have to say dim equals one that's the dimension of our output data remember our output data is just a single set of choices zero through nine so that's the axis that we're working on similar lady to pandas and you'll notice we're using a soft max here basically what that's going to do is give us a probability of each choice that sums up to a hundred percent or one so essentially if we have two choices like say you're doing dog or a cat it might say the picture is 0.64 cat and point four for dog and then we would pick the cat because it has the higher percentage of being correct and we're gonna do that with our ten different number numeric choices so now if you remember we haven't ran any of this code yet into our program you can see our class isn't defined we haven't done these two imports so I'm gonna highlight this right click and run cell now I could run the entire program but then it's gonna read download our data and take a little while so I'm just gonna run these cells if you have problems you can go ahead and run it from the screen arrow up here and rerun the whole program and that should fix any issues and then I can come down in our program and we should be able to actually create our network now using that class so I'm gonna set my network equal to our class Network now remember in our nip function we don't pass anything into itself is given so we're not gonna pass any parameters inside of this and this should create our neural network now if I run just this cell you'll see if there's any errors and it looks like we're good to go so now we have to set some learning parameters so as we go through our network and our network learns from its own mistakes how much should it adjust the weights to try and make a better prediction now more is not always better and being teeny-tiny is not better either so what we want to do is kind of play around with a learning rate that can allow our network to learn as much as possible from our data set without over correcting and going back and forth like crazy so I'm going to call this learn rate and I'm going to set it equal to but I'm gonna go up and I'm gonna do another import at up top here I'm gonna import torch dot op Tim as op Tim now I can run this sell oh I left my learning rate blank down here comment that out for right now I'm gonna run this import run sell that was successful now we can go back down here and use that so let's set up our learning rate so I'm going to do op Tim dot Adam and now the first argument here is going to be our network's parameter so you're gonna use the variable that you have your network stored in so ours was stored in network and I'm gonna do dot parameters now remember we didn't set up this method this is an inherited method from the parent so we inherited this method from this NN module so it's gonna tell our parameters and then we just have to say what we want our learning rate to be so we can set LR equal to and you can pick any number here you can play around with this I'm going to pick a point zero one for our learning rate for right now again this is another parameter that you can play with inside of your network to try and come up with the best solution and finally we need to say how many epochs we want in our network so this is how many times we want to pass the training set through our network while we're training and let those network weights adjust again more is not always better we can over train our network so we're gonna pick a number here and I'm just gonna set it equal to five times and now we're actually ready to train our neural network so first we want to create a for loop that's the amount of times that we're going to train our neural network so I'm just gonna say for I in range eeep ox so this is just gonna execute however many times is in our epochs variable and now inside of this I want to iterate over our training data set so our train underscore said so I'm gonna say for data in our train set I'm just gonna print out our data now I'm gonna highlight this and I'm gonna run it and you can see this is gonna print out a lot of stuff especially because I ate it over at five times and you can see the printout is a little strange we have our actual data and then we have a tensor object along with that I'm gonna go ahead and stop this push that red stop button over there so let's actually break this data out into two pieces so instead of data I'm gonna have our image and output so I'm going to break up this data variable into two variables called image and output in here I'm gonna print out image then I'm going to print out output I'm only gonna go over this one time while I'm showing you this and then I'm gonna break out of this for loop as soon as I hit the first one I'm going to run this again and you can see this output here and it's a little confusing at first but I'm printing out too let me make it a little more let me make it a little simpler to see like print just a dash here and a dash I'm gonna run this sell one more time there that cleaned it up just a little bit so you can see here are different items so you'll notice after our first - we're printing out what we call quote-unquote image so what this image is is all of the parameters in our image in a flattened tensor object so this is the actual image data here and they all look like zero because it's all been normalized between zero and one so they're all some amount a decimal number which this output isn't very helpful there but they're at is actually data in here and you'll notice there are 10 well actually it hides some of them here but there are 10 images that it's passing through and that's because we set our batch size equal to 10 now in the second one that I named output the second variable that I named output you can see that there are 10 output results again this is because our batch size is 10 and that's the actual answer of what the image really is so the first image was a 1 the second image was a 1 the third image was a 5 and so on and so forth now if we run this again you can see that the image is changed now it's an image the first ones a 4 the second one is a 0 and why is that that's because our data is being shuffled so I can highlight this one more time by doing one more import our there might be more imports I'm gonna import matplotlib as PLT you should have this if you have anaconda you can do an install if you don't so I'm going to run this sell and now along with printing out the image variable remember this image is a list of ten different sets of image data so it's not just one image so I'm gonna do PLT dot I am show now I'm going to pass in my image variable but this is all a list of ten different images and I just want to show the first one so the first index is going to be 0 so if I run this again you'll see this is an invalid image shape so I'm gonna do dot view and this is going to allow me to convert the image and I'm gonna say I want it to be 28 by 28 try the skin run this cell and you can see you should have another little output down here you can see our first image looks like a three to me you can see that it's a three in the output node the answer so that was meant to be a three all right perfect so we've shown what we're looking at here so we're looking at the output the individual image is let's just comment these out for right now we don't want the break anymore either so now we have our actual image data and the actual output let's go ahead and do something with that let's train our network so each time we go through here we're gonna reset our networks gradient so we're going to reset any of the loss that we had before so we're going to do dot zero underscore grad so this is so each pass through our network is unique for each image is unique so now let's actually run our data through our network so I'm gonna say my result equals let's call our network and let's pass in our image remember image and output here so our image we actually want to change the view we're gonna say negative one and then whatever our total amount of pixels was so remember we're flattening this down so it's 28 by 28 we're gonna flatten it to 784 pixels this should give us our results now our loss is going to be how far off our guess our networks guess of the number was from the actual result so say for example our network predicted that the number one prediction for the number one it gave a prediction of 0.4 so the loss would be 0.6 and then we're going to use that to correct our network so we're gonna say F dot and ll underscore loss is equal to our result and now we're gonna pass in what the answers actually should have been so remember that was stored in our output variable so this is gonna give us our loss amount and now we're going to update the weights in our network by backward propagating our results so we're gonna say loss dot backward and then we're going to step through our learn race so we're gonna do learn rate dot step and that should be it then after each epoch we can print out our loss and now we're ready to run this now this is going to take a while I'm gonna do two epochs let's if you're on a CPU so hang tight highlight all this right click run cell and we'll wait for it to finish running now this took a few minutes on my computer you can see here that the loss the first epoch was 41 the loss the next epoch was 8 so it's definitely getting down there I'm gonna run this one more time this time I'm gonna do 4 epochs and then we'll move on awesome now that that's finished you can see our learning rate over here and this may raise an alarm for you you can see we start out at point zero three then we actually went up our network got worse then came back down a little and finally flowed to the lowest point it hit yet so this is kind of what the training looks like and you can see how if we overcorrect sometimes we actually get worse on a later epoch now hopefully we're working our way down towards the bottom to a lower loss percentage but again this is just our training data set so even if we got that number two point zero zero zero zero one that network might do worse against a test data set it's totally different data than a training network that had a point a loss of point zero three maybe we've over trained our network so whatever your number here is as long as you're getting lower to the point zero three somewhere in that range you should be good to go so now we should have our network trained and we can move on to testing our network to see how it performs against our test data I'm gonna go ahead and delete these print statements we don't need them anymore and I'm gonna do a couple things now we're gonna put our network in eval mode and that's gonna tell pi torch that hey we're not teaching our network anything else it's not gonna be learning we're just gonna evaluate how it's doing with our test data set so I'm gonna do Network dot eval and while we go through our test data I'm actually gonna add a width statement here I'm gonna say with torch dot no underscore grad now this is going to keep us from using any sort of back propagation but we're not going to do that anyways because we're not training our network anymore we're testing it so this is gonna speed up our computations with no real cost to our program because we're not doing any type of learning or back propagation so now I want to do something similar to what I did with our training data set now I'm not gonna use multiple epochs because I'm just gonna pass our test data through once because we're not training but I'm gonna use a for loop just like I did before to go over that data so I'm gonna say for data in test set this time so you'll notice here I'm going over a different data set this was the test data set that's the data that we reserved and didn't use to Train on so we have an accurate picture of how well our network has learned I'm gonna do the same thing I'm gonna break up that data variable into image and output I'm gonna set that equal to data so I'm unpacking this tuple into a image and an output just like we did with the training data set I'm gonna get our result just like we did before I'm gonna pass in I just copied this line from above I'm taking the image I'm flattening it I'm passing it into our network and I'm getting the result now remember this is going to be a batch of results so we want to keep track of how many we're getting correct but each result is gonna have multiple well 10 in this case batches 10 individual images coming back so we need to test all those to see if they were correct we can do correct here equal to zero and our total is equal to zero this is going to be our total so these are just two variables that I'm going to use to hold our the number of images we guess correct and the total overall images so after I get my result I'm going to have ten results back of sorts ten a list of ten so now we're going to write a for loop to go over that result and I'm gonna enumerate over that result so I can keep track of the index that I'm currently on and the value of the result itself so I'm gonna say four index comma and the next is going to be my value which is actually a tensor object in result so now once I'm inside this for loop I know I want to increase my total amount of images no matter what so I'm gonna say my total plus equals one so I'm gonna add one on to my total no matter what and then I need to see if my guess is correct or not so I'm gonna do a little if statement to see if my guess is correct so I'm gonna say if and I'm gonna do torch dot Arg max so it's gonna find the maximum value in my tensor object and that's going to be my guess so the tensor objects gonna have ten different values out of those ten values which is the most likely number according to our neural network so I'm going to pass in our tensor value that we set up above and I'm gonna say if that equals our image at this index so up here we have an enumerated result so that's going to tell us what our current index is in that result so it should be 0 1 2 3 4 5 6 7 8 or 9 because there's 10 batches so it's gonna ask which one we're on and then it's gonna say what is that actual value there and I don't want to look at the actual image data I actually want to look at the output I want to know what the actual image was so what I'm doing here again just one more time is I'm getting our best guess at what the actual number is and then I'm seeing if that equals what the actual answer is here I'm just looking and asking for the actual answer if those are equal then I can increase my correct by one so no matter what I'm increasing my total by one if they match I'm correct I'm increasing my correct number by one and then after this is all over I can print out my accuracy so my accuracy can just equal number correct divided by the total and then I can just print that out to the screen I'll just use an F string I can say accuracy space and then I can use curly brackets and use my variable that I created above to print that out to the screen that should work so I'm gonna highlight this and I've been using the wrong I've been rerunning the entire program even though I said I wasn't so what I want to do is I want to right click if you're in spider pop-ups for spider a little annoying and I'm gonna run selection or current line so this is only gonna run this little chunk of code now if you restart the kernel or something like that you're gonna have to rerun everything again but I have already trained the network I don't want to sit and watch that again so I'm just gonna run selection or current line and you can see here I forgot to put a numerate I said I was gonna enumerate over the result and I didn't so and remember enumerates just gonna give us back that index value so I'm going to actually enumerate over our result so that will give us an index value here along with the actual tensor value itself I'm gonna highlight that same chunk of code right click run selection or current line and now you can see it spits out that our accuracy was 94 percent now that looks pretty good and that's about what I would expect and there are a lot of things in this network that you could play around with again we picked 64 nodes 2 hidden layers that could all be changed we could have more hidden layers we could have less nodes in our hidden layer or more nodes our learning rate is at point zero one that could be point zero zero one it could be anything we wanted you could play around with that as well to try and get that accuracy higher again this accuracy is way more important with your test data set than it was with your training loss because that training loss we can over trained and perfect our network to guess our training set and that doesn't necessarily mean it's going to be good with totally new data here so again 94 is about what I'd expect as long as you're somewhere above 90 you probably did everything right if you have something like 100 percent accuracy or something you probably messed up somewhere and accidentally used your test data in your training and now your networks way too good for it's actual or way to way more accurate than it actually should be so this is it we pretty much trained our network with this training data set I wanted to talk about one more thing and that's the kind of start to show you how high torch got this data ready for us to go through our network I'll add some more comments on this before I put it on github so you guys can kind of understand but here's where we're going into testing our network there we're going to take a look at image processing so I want to take a look at exactly what pi torch is doing for us to get these images up and ready to go so we know we're working with 28 by 28 images so if we just pull up paint and we create a 28 by 28 image you can see here that we have kind of an issue right out of the gate like we're already processing processing this teeny-tiny image that isn't very accurate of what we would draw an image in so I'm gonna go ahead and triple that size and we'll do 84 by 84 a little better there and I'll try to draw an image here my own image I got a three so now I'm going to save that image into our directory wherever that was and I just called it first test so I got my own unique image that wasn't in my training set wasn't in my test set I'm gonna open back up my code if you go to file explorer you should see your image in here that first test image so that's gonna be at 3 and I'm gonna import 3 libraries that are gonna help us and I'll talk about what I'm using them for as we use those libraries the first is I'm gonna use to pull in an image and a third and the middle one is numpy which is going to allow me to do some calculations with my image so the first thing I'm going to do is just open my image so I'm gonna say image equals image dot open and now I put the image in the same directory as my Python script itself so I don't have to use a path or anything like that I can just do first test PNG and now let's see if we can open up our image and show it so I'm gonna do PLT dot M show and then our variable that we just created image and highlight these run my current selection and you can see that opens up my image now I'm gonna show an image from that was already processed with the library on the side here and you can see that they're very different so they've done a lot of image processing to get this ready to go through our program so we're gonna have to try and mimic that to make this work accurately if I put this through our network right now it definitely wouldn't work right well we might even be able to try it here I'm gonna get the result of our network by passing in this new image that we just set up there after I flattened it and I'm gonna see what the output is gonna run the selection you can see we actually can't use our image file we have to convert this to a numpy array so I'm just gonna convert it to an umpire array then PI torch has a from numpy line so I'm gonna use that so I'm gonna pass in our variable that we have above that's now numpy array I'm gonna pass that and create a tensor torch from that and I'll save it in a variable called image and then I can pass that into our function instead let's try and run this again says that once our image as a float so we're gonna go ahead and do that as well you already see how much hi torts did for us before this so we're just gonna convert it to a float type instead of byte highlight again run the current selection and you see our network thinks it's an 8 but again we still have a lot of stuff that isn't done with this not only visually that you can see but also some behind this behind the scenes things that you didn't notice so each of these pixels right now has a value between 1 and 255 now something that we do in almost every neural network is normalized our data to fall between a value of 0 and 1 you can dig way deeper into this but when it comes down to it it basically gets us a value that it's kind of an equal value with everything else in our neural network and a response to the 0 off 1 on kind of computing that we're used to so each of these pixels that we're passing in right now is a value between 0 and 255 we want to normalize that so it's always a value between 0 and 1 so what we're going to do is before we can create our tensor object here we're actually going to normalize this image so I'm going to take the image and I'm gonna divide everything in it by 255 so that's going to normalize all of our data between 0 and 1 now I want to do some other stuff to the visual side of the image as well so I did all this just by researching and kind of looking at the difference between what our image looks like right now and what the image looks like the hi torch is passing in so the first thing I'm gonna do is convert the image to grayscale so I'm gonna do image convert capital L this I'm looking up from the pill library here to see how to convert it to greyscale and then I also want to resize our image remember our image was 84 by 84 just so I could draw it and all the other images we've trained our network on are 28 by 28 and this method actually you pass in a tuple again I'm getting this just from the documentation so we should have converted our image now we're resizing it to 28 by 28 let's run this and see if our image looks any different now you can see we're way closer right this looks a lot more like the image that we're getting from our library but you can see the colors kind of inverted and I don't know exactly why PI torch did this I'm sure there's a reason but we're gonna follow that as well we're gonna invert the colors so we match what they were passing in so I'm gonna say image equals image actually I'm gonna run something out the library here so I'm gonna say PIL got image ops dot invert and now I'm gonna pass in the image that I want to invert now if I run this selection again you can see now we're very close to the look that the other image had that we passed in now let's run everything again and you can see it's still guessing 8 which is a pretty good guess it's getting it wrong and you'll see I did a bunch of tests here because this wasn't working quite right the only thing that I was doing wrong is we were converting the image after we need to resize the image first so that was the very first thing I did and that seemed to fix everything now if we go back and do our first test and run this you can see our network guesses correctly with a three I got a few other images in here that I was testing on some of them won't work others will depending on how your networks learned but this kind of shows you the process that pi torch went through to set up these images to get it ready to pass through our network they did all this work for us with the original data set just a peek on what we're gonna do moving forward to train on more real-world datasets and that's what we're gonna do on our future lessons hang tight with us here we are moving on and PI torch and trying to use some real datasets next to get a better picture of how we can train our network using a real world example until next time keep coding
Info
Channel: ClarityCoders
Views: 5,499
Rating: undefined out of 5
Keywords: Python, PyTorch, Python PyTorch, PyTorch Machine Learning
Id: Rtmi-H-mlKw
Channel Id: undefined
Length: 43min 24sec (2604 seconds)
Published: Wed May 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.