I am publishing this article in response to a comment I received on How To: Setup Tensorflow With GPU Support using Docker article. Docker is all good and nice but all of us agree that we use IDEs (integrated dev environment – think VSCode) on our machines. So let’s say you want to get the best of both worlds:
- You want the isolation and ease of setup of using Docker to start your machine learning environment with GPU support and all libraries available.
- You also want the comfort of being able to us VSCode to write your code and get nice auto-completion and other goodies like support for different kinds of files (CSV etc.)
How do you do it? This the question that I will answering in this article, but before that little bit background. This question from one of the readers led me to write the current article.
I have enjoyed your tutorials. I was able to follow the Docker and got it running on my Windows 11 PC. I wanted to ask you a question. Instead of using Jupyter Notebooks, how can I use the Docker TensorFlow Image as a template to develop and run models in Visual Studio Code?https://thegeeksdiary.com/2023/01/29/how-to-setup-tensorflow-with-gpu-support-using-docker/comment-page-1/#comment-216
In the past I have published a couple of guides of setting up Tensorflow with GPU support:
- Setting up Tensorflow with GPU support using Miniconda on Windows 11 along with Jupyter notebooks: This article was published as soon as Windows 11 was available as a preview build and is quite popular and gets quite a number of visits everyday from people trying to get it working on their machines. This guide uses Miniconda to create a virtual environment and I keep it up-to-date as Windows 11 evolves.
- Setting Tensorflow with GPU support using Docker on Windows 11 along Jupyter Notebooks: This is a newer article that I published because of 2 reasons – constants updates to windows 11 (drivers & other stuff) often end up breaking the tensorflow so I was looking for a way to isolate my environment from everything that happens on my Windows 11 operating system. Docker is the ultimate virtualization environment which allows us to create a snapshot of an application/s along with all it’s dependencies, with improved support from windows to share hardware with containers it becomes the best option as I can recreate the containers as many times as I like. Please checkout the article to see more details.
As the title explains we are going to use VSCode and will continue from the steps explained in my previous article. Before continuining I would highly recommend you to follow that guide first as we would be using some of things that were setup in that tutorial/guide.
Here are the set of tools that we are going to use:
From the previous article – the pre-requisites are:
- Graphics Card: NVIDIA GeForce RTX 2070 8GB GDRR6 (any RTX or Quadro GPU is fine)
- GPU driver for your Nvidia Graphics Card, you can follow the instructions listed in Step 1: NVIDIA Graphics Driver Installation section in How To: Setup Tensorflow With GPU Support in Windows 11 article that I published earlier.
- Docker desktop: To install docker desktop on your OS – please follow the guide available at Get Docker documentation.
Install Visual Studio Code & Jupyter Extension
To install the visual studio code download the installer from the official website.
Once downloaded run the setup program and it will install the Visual Studio Code, once installed – launch the visual studio code and you should see an interface like shown below – the idea is to install the Jupyter extension (some people call it plugin) – basically Visual Studio Code is bring what you need – at it’s core it’s just a fancy text editor with extensibility that allows others to build extensions. The below image shows the steps to install the Jupyter plugin
The steps are very simple to follow and once installed this allows you to work with Jupyter notebooks directly inside VS Code. VS Code support syntax highlighting and autocompletion and you can use the full developer experience inside a jupyter notebook when using VS Code.
The Link: Connecting VS Code to the Docker
Okay now you might be asking but how do I build machine learning models that leverage tensorflow and GPU support – I haven’t installed Python on my machine?
This is where the docker images come on handy that I have published to the Docker hub. You can checkout the example code for this article at machine-learning-guides github repo maintained by me. You can start by cloning the repo by running the following commands in a terminal/command prompt:
git clone https://github.com/the-geeks-diary/machine-learning-guides.git #If you haven't cloned the repo cd ./machine-learning-guides/articles/vscode-with-docker-gpu-images docker-compose up
The above code does the following.
- Clone the git repo (if you haven’t cloned the repo ever)
- Move into the directory of the code example
- Start the docker container.
Once the docker container is up and running we are ready to connect to the python (and all the machine libraries installed inside the container – including tensorflow) through jupyter server running in the docker container. The jupyter server is available at http://localhost:8888.
Connect to Jupyter From VS Code to access Tensorflow & Python
Step 1: Open the sample notebook provided in the code example as shown below.
Step 2: Click on the Select Kernel button as shown below
Step 3: Select the 3rd option “Existing Jupyter Server” as shown below.
Step 4: Type the address (http://localhost:8888) in the address, press enter and select “Yes” on the following prompt at lower right corner of the screen dialog as shown below.
Step 5: Type password ‘test‘ (without the single quotes) when prompted for password as show below.
Step 6: Select the default kernel “Python 3 (ipykernel)” when prompted to select the kernel on the server as shown below.
Step 7: Run the first cell in the notebook (you might be prompted to give a name to the newly connected server as last step and you can call it whatever you want). You should see output to what is shown below.
Going Under The Hood
If you are the curious one – here is a little bit of detail around the setup and stuff. Here is a breakdown and summary of what happens.
|1. Docker remote kernel for jupyter notebooks||Visual Studio Code Jupyter Notebooks plugin/extension allows us to connect to jupyter kernels which is:|
1. Exposed as a conda/venv environments from our local python installation.
2. Exposed over a remote HTTP endpoint
|2. VS Code language features||Visual studio code allows us connect to a language server that helps us with the features like code completion, debugging etc. So you need to ensure that the python plugin is installed for python.|
I have been personally using this in my workflow a lot and found this to be super helpful. This setup is really convenient and allows me to quickly setup working environment quickly and throw them away when not needed. No need to have a local python environment on this host machine.