The website provides a comprehensive guide on setting up Tensorflow with GPU support on Windows 10/11 using WSL2, Ubuntu, and Docker Desktop, complemented by a YouTube tutorial.
Abstract
The article outlines a step-by-step process for configuring a Windows system to run Tensorflow with GPU acceleration. It begins by instructing readers to ensure they have the latest NVIDIA drivers installed, which is crucial for GPU compatibility. The guide then walks through installing Ubuntu via WSL2 on Windows, setting up Docker Desktop with WSL2 integration, and running Tensorflow within a Docker container. It also demonstrates how to access Jupyter Notebooks from the container and ensure work persistence using docker-compose. The article emphasizes the importance of using the provided GitHub repository for sample code and encourages readers to support the author by following them on Medium and using their affiliate link for learning resources.
Opinions
The author suggests that most machines used for machine learning also serve as gaming machines, implying a preference for the game ready driver over the studio driver.
The author recommends using the official NVIDIA drivers download page for installing the necessary drivers, indicating trust in NVIDIA's official resources.
The use of WSL2 is advocated for its ability to provide a Linux environment natively on Windows, which is beneficial for running Linux-based software like Docker.
Docker Desktop is recommended for its ease of installation and integration with WSL2, highlighting the "Use WSL 2 instead of Hyper-V" option during setup.
The author provides a personal GitHub repository link for readers to access sample code and Docker files, suggesting a hands-on approach to learning and implementation.
The article promotes the use of Jupyter Lab over traditional Jupyter Notebooks, implying that Jupyter Lab offers a more robust development environment.
By providing an affiliate link to a book by François Chollet, the creator of Keras, the author endorses this resource as a valuable tool for continued learning in deep learning with Python.
The author expresses enthusiasm and encouragement for readers to follow them on Medium and to cheer the article if they find it helpful, indicating a desire for community engagement and support.
Tensorflow with GPU on Windows with WSL and Docker
In this short article we’ll be setting up a desktop environment to run Tensorflow on Windows 10/11 using WSL2 Ubuntu and Docker Desktop for Windows. This article also has a companion video over on YouTube:
If you have trouble completing the steps in the article, please watch the video for a full demonstration of how to complete the steps.
This step is probably already complete on your system. If you are not sure, head over to the NVIDIA driver download page and it will walk you through the steps necessary to install the latest drivers. We probably want the game ready driver over the studio driver because most ML machines double as excellent gaming machines.
Before installing WSL2, you need to make sure that you are running Windows 11 or Windows 10 version 2004 and higher (Build 19041 and higher).
You can check your version of Windows 10 by right-clicking the start menu and selecting “System” to display information about your system. Find the Windows specifications as shown below and ensure that you are running build 19041 or higher.
Windows OS Build Version displayed in System menu
Now that we’ve confirmed that we are running the required version of Windows, let’s install WSL2. Open Windows PowerShell in administrator mode by searching for “PowerShell” in the start menu. Right click the App and select “Run as administrator.”
Opening PowerShell as administrator
Once PowerShell opens, run the command:
wsl --install
Follow the on screen instructions. At this point a system reboot will be required. After rebooting, you will create your username and password.
Once the Ubuntu terminal opens, you’ll want to update the system’s software by running the following two commands:
sudo apt update -y
sudo apt upgrade -y
Now that Ubuntu is up to date, let’s go back to Windows to install Docker.
To install Docker Desktop, navigate to the site linked above and download Docker Desktop for Windows. It installs similar to any Windows program. Just be sure to look for the “Use WSL 2 instead of Hyper-V (recommended)” option during installation. You want to ensure that this checkbox is selected.
Run Tensorflow in a Docker Container
To run Tensorflow in a Docker Container, we’ll run the following command from the WSL terminal:
docker run -it --rm -p8888:8888--gpusall tensorflow/tensorflow:latest-gpu-jupyter
The first time running the command, it will download the Tensorflow container from dockerhub. Once in downloads and runs, click the link to open the Jupyter Notebook server:
From here, we can create a new notebook and run the following Python code to confirm that we can see the GPU from the notebook running in the Tensorflow Docker Container.
import tensorflow as tf
tf.config.list_physical_devices()
Jupyter Notebook Python Code to List GPU(s)
This is a great start, but if we stop and restart the container, all of our work is gone. Let’s fix that.
Improve the Environment with docker-compose
Now that we have confirmed that the Docker container running Tensorflow has access to the GPU, let’s improve the development environment by creating an easy way to restart our environment and save our work. For this, we’ll need to create a few files. The three files named “requirements.txt”, “Dockerfile” and “docker-compose.yaml” have contents as shown below:
requirements.txt
jupyterlab
pandas
matplotlib
Dockerfile:
FROM tensorflow/tensorflow:latest-gpu
WORKDIR /tf-knugs # This specifies the directory to workRUN pip install --upgrade pipRUN pip install --upgrade -r requirements.txtEXPOSE8888ENTRYPOINT ["jupyter", "lab","--ip=0.0.0.0","--allow-root","--no-browser"]
Note that the directory “tf-knugs” is used for mapping a volume from the WSL system to the container. These names can be changed to any valid directories. The key is that the WSL directory is on the left side of the “:” and the container directory is on the right side. The directory on the right side should match the “WORKDIR” specified in the Dockerfile.
Once you have these three files saved in the same directory, we can run the new and improved environment by running the following commands to build the container then compose the environment:
docker build .
docker-compose up
Once these steps complete, we’ll see a familiar link to Jupyter Notebooks, but now it opens the newer Jupyter Lab environment that we installed from the “requirements.txt” file.
To stop the environment, go to the WSL terminal used to run “docker-compose up” and press ctrl-C. Alternatively, open a new terminal in the same directory and run:
docker-compose down
Best Learning Resource for Tensorflow and Keras
Now that your environment is set up, keep your progress moving by learning directly from the creator of Keras, François Chollet. Plus, you can help support KNuggies with this affiliate link 🤑: