Tensorflow with GPU on Windows WSL using Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to nuggies in this video we're going to set up a Windows machine to run tensorflow without installing the Cuda drivers from Nvidia we're going to do it through Docker and WSL and it's going to work great that's the theory so it's already set up on my desktop it works great so I'm over on a separate machine that I can test and Destroy anytime I want to so let's run through what's currently happening I downloaded and installed the latest GeForce driver this is just a normal gaming driver this is a like a Alienware gaming laptop the One requirement you do need to have is an Nvidia GPU that has scooter cores so yeah all you have to do is go to Nvidia and download the driver that you need and it'll kind of walk you through the steps I'm not actually going to cover that here you probably already have the driver installed if you have a machine with a Nvidia GPU the next thing we need is we need the tensorflow image we're going to use the image that's provided by tensorflow because it works great in order to run this in a container we're going to install Docker on Windows but before we do that we want to get wsl2 installed because Docker runs much better on Windows if you're using WSL too what we're going to do is jump over to the instructions for installing WSL and for all of the newer machines you need to make sure that you get wsl2 and they tell you that you have to have a certain version of Windows so let's go ahead and check for that to check for the proper version we're going to check the system menu so you just go to the start menu we'll just right click on the start menu and then up here system you can see down here we're running Windows 10 Pro the pro might be needed for virtualization and then right here we have 19044 and what we need is 19 041 so we're good our current version is higher than this so we can continue so we can install WSL so we just need to go to Powershell and it says to run it as administrator so if you just go to the start menu and type in Powershell Windows Powershell then you right click it run as administrator then WSL install or WSL dash dash install and then you just let this run okay Ubuntu has been installed and it will set up the first time a change will not be effect until the system is rebooted okay so we're gonna have to reboot and we'll be back one other thing to note is about the system you want to make sure one you have a Nvidia graphics card if you just go to your task manager with control alt delete you can display all this information so you do need an Nvidia graphics card if you're trying to do GPU accelerated machine learning training and then the other thing that we need to do for Docker and WSL to work well together we need to have virtualization enabled so if on your CPU it doesn't say virtualization enabled you need to look up your CPU model number and make sure that it supports virtualization because it might not and then if you have a CPU that supports virtualization but it does not say enabled here you can enable it in your bio settings so we need to reboot and then we'll be able to continue with this and it looks like when I log back in it's automatically installing the rest of Ubuntu so we'll catch that in the middle of the process here and we're back all they want is a username so I'm just going to use first name here and then a password usually it's just password and now you can see that we have our Linux environment set up so now we're on Ubuntu and of course the first thing that you typically want to do before installing anything is just do a sudo app install or app update and just say yes to all so that'll take a minute to update and then it's going to tell us a bunch of packages are ready for upgrade and we're going to do that and then we'll push forward so now we can upgrade okay we made it so now let's go back over to Docker and we're going to install it for Windows so Docker desktop for Windows so this is the important step we're looking for which is use wsl2 instead of hyper-v and that's recommended so we definitely want to do that and this laptop the fans are screaming it's actually due to be uh sold on eBay but it's such a hassle to sell Electronics on eBay so if any of my viewers want me to run a giveaway for this laptop at uh 10K subs or hit that subscribe button throw a comment down there I'll do it if I hit 10K Subs I will give away this laptop because selling it on eBay is a nightmare here's the Glorious specs one more time it's got the nice CPU got a GPU it's like a four-year-old gaming laptop sick I gotta go fans are screaming it's barely even working hard you can't imagine what doing real like ml training on a laptop would do to the poor thing I guess it'd be like mining crypto on your laptop which is a bad idea oh no we have to log out to complete installation much better than restarting so we'll be broadcasting again in a moment hello Docker pop-up uh whatever they're saying I'm just gonna accept you should probably read this but if you're um docker's not free for commercial use anymore so if you have more than I think 10 million revenue is what I caught there uh you have to actually pay for Docker and now Docker is starting and it's nice that this new version defaults to WSL some of the older tutorials out there and older blogs and websites that you see will probably tell you to go enable it manually but uh that's no longer the case and now we want to actually run this tensorflow image and go from there so let's go over to vs code boom okay so now we're running the latest version with gpus with Jupiter and it should automatically launch the Jupiter notebook and now we just go here to the Local Host control click this and Jupiter notebooks opens up so I actually like Jupiter lab much better so the way that I do it is I run an installation for Jupiter lab and then go from there but this will actually work for now so we have our Jupiter notebooks running and we can just create a new notebook here and we can go in import tensorflow and then we're going to list logical devices and you can see that we have a CPU and a GPU so we we're in a notebook that is being served from this container this Local Host is running on a container we specified this port in the command let's actually go back to that command and talk about it a little bit and then we're going to go and do this a better way but this is working and you can actually start working right now but anything you do in here will be lost because the command that we used to start this will not save your work so let's get out of here I like Jupiter lab better than notebooks so I don't run this jupyter image that they have you actually need to stop it okay clear all right so let's go over this command so all we're doing is we're running this Docker image this RM right here says remove the container whenever we stop running it this port mapping here meant that Port 888 on the local machine is a mapped to Port 888 inside the container then we gave it this container access to our gpus from our local machine now we ran the tensorflow image the latest version with GPU support and Jupiter notebooks already installed but I like to use jupyter lab and I also want to do I want to do this in a few different ways this is good to get you started to make sure that you can run notebooks but this is not a development environment that you want to use yet so what I'm going to do is I'm going to go over here to our extensions and we're going to go to WSL we're going to install the remote wso and then we can just open we can just open a new window with WSL but normally that's not going to be available the way to do it is to go down here to your bottom left these little arrows and then you can open your WSL from there and at this point we can close the old version and now we're running within WSL if we go to a terminal we'll see that we're actually in Linux because we actually want to use Docker compose and a Docker file to build ourselves a better workflow so what we're going to do is we're going to code let's uh let's make ourselves a a folder here project so we need to code a Docker file and we're going to open that and from here we want to grab the image that we wanted without the regular Jupiter notebooks installed because I like to use Jupiter lab so we're going to go from tensorflow tensorflow and then we're going to grab the latest GPU so this is our base image and the way these Docker files work is you're just building up layers of things that you want and we're going to specify a working directory I'm going to call it TF nugs because that's what I used in my repo and we're going to copy a file from our local machine into our Docker file so that way we can actually install the additional packages that we want let's go ahead and save this for now and I'm going to jump over to that file so let's code requirements Dot txt and now there's just a few things that we need here the number one thing I want here is Jupiter lab additionally I'll be using pandas like I know I'll be using pandas and numpy is included in somewhere in these packages as part of pandas but I do need matplotlib so we can save this now this requirements file is what we're going to use to install the python libraries that we want when we're building our Docker container so now we want to run a command from within our Docker container and we're gonna do our regular pip install requirements.txt and this is going to install all of those requirements inside of the docker environment or inside of Docker container environment and just like before we want to expose a port from of the container and then we need an entry point which is the command that will run Upon Our container starting it's asking if we want the in the extensions for Docker and I definitely am going to want whatever vs code kind of has to offer they have a nice environment for managing your Docker stuff I'm going to close it because we're not using it right now but that is where it is so here we Define our entry point and then it's going to be Jupiter lab [Music] and each piece of the command is separated by Common space if you just build a list to build up your command and then we need to set our IP equal to zero because we're launching uh Jupiter lab from within a container we're going to allow root and we're going to say no browser because our container Linux image does not have a browser that should be good the next thing that we want is our Docker compose so let's get our yaml and this is the file that will be used by Docker compose to build and run our environment so version is what we choose I'm just going to put 1.0 and then we have to Define our services and I call it Jupiter lab because that's what I wanted to run that has nothing to do with the actual command that's being run though this is your name that you choose and we want to build the path that's just going to be the current directory and then we're going to map our ports here and I chose 888 again this is the part where now we want to be able to save and access files which we never could have done with that previous container one because we were removing the container as soon as we were done and then even if you didn't remove the container right away the layer where your editing files does not stay between instances of running that container so this volume will actually map it to a local folder that way you can access your files afterwards so we're going to use the TF nugs directory and then again it's just going to be tfnugs inside of our container this is where it's really helpful to be inside of WSL where I mentioned that doing this in Windows is a pain that's because of this directory service you can't use the standard Linux directory system inside of Windows and you you end up having to put a full you can't use a local reference here you have to put the full path and it has to be in Windows format and that really just kind of messes things up and that's why I do all of this from wso that's the number one reason at least there are there are other problems that you run into in Windows but right now that that's the main reason and then the final thing we need to do is share the GPU let's give it a shot so now we can do docker compose up and the first thing it's going to do is go build this this container which I've already done so what this is going to give us is now we have Jupiter lab running instead of Jupiter notebooks which I prefer just because I'm trying to use the new stuff and as always first thing you do settings theme Jupiter lab dark it's glorious and what we should have is the directory TF nugs let's just create our new notebook yeah so we are inside of tfnug so if you hover over this you can see that we're inside TF nugs now when we make a notebook we should be able to access it outside of this container whenever we're all done so let's again let's ensure we have our GPU probably get some warnings here it's not a big deal and I'm just going to go back up and run that again so we don't have to see the warnings and as you can see we do have our CPU and our GPU from here what we want to do is just save this notebook and now all of these changes have been saved so we can we can close out of this and we'll come down here Ctrl C it's going to stop the container and shut down Jupiter lab and if we go over here to our directory and open the TF nugs folder actually go back let's just open the project folder now we have WSL inside of a directory just so we can see all of our files that we were working with and now if we look in this TF nugs you can see we still have this notebook and we have checkpoints even though our container is no longer running so if we go over to the docker app and we can install python here this is just a new system here but if we go over here and look at our containers it's exited and we can actually go back to WSL and we can start that container again and it will actually be able to pick up where we left off this directory can be synced with a repo and then inside you actually sync your Docker file and your Docker compose yaml to the GitHub repo and then the environment is tied to your project so no matter what computer you're working on you actually have a Docker environment that is your workspace for your project and it makes moving from machine to machine or reformatting your machine and starting clean it makes that much easier let's just do one last check here we'll go take a look and see that we have our notebook here and everything is running we should get the warnings again and that is it for this one hopefully this was helpful to get you up and running on windows with WSL and Docker being able to use tensorflow and access your system's gpus this works for multiple gpus as well you just have to change your Docker compose file a little bit but that's it for this one hopefully this helped thanks for watching if you found this helpful be sure to like And subscribe for more videos
Info
Channel: KNuggies
Views: 25,241
Rating: undefined out of 5
Keywords:
Id: YozfiLI1ogY
Channel Id: undefined
Length: 17min 0sec (1020 seconds)
Published: Mon Jul 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.