How To: Super Fast Tensorflow 2 Setup with GPU Support in VS Code


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.

Hi,

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.

The Environment

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:

  1. Visual Studio Code
  2. Jupyter extension for VS Code

Pre-requisites

From the previous article – the pre-requisites are:

  1. Graphics Card: NVIDIA GeForce RTX 2070 8GB GDRR6 (any RTX or Quadro GPU is fine)
  2. 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.
  3. 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.

  1. Clone the git repo (if you haven’t cloned the repo ever)
  2. Move into the directory of the code example
  3. 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.

TitleDescription
1. Docker remote kernel for jupyter notebooksVisual 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 featuresVisual 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.

Conclusion

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.

Other posts

  • Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Discover how to perform object extraction using image segmentation with Detectron2 and Mask2Former in our step-by-step tutorial. Learn to set up the environment, configure the model, and visualize segmentation results, extracting objects from images with ease. Boost your computer vision skills and optimize your image processing projects with this comprehensive guide.

  • Building Your First Neural Network with TensorFlow – Deep Learning 2

    Building Your First Neural Network with TensorFlow – Deep Learning 2

    Neural networks are a fundamental concept in deep learning and are used for a wide range of applications such as image and speech recognition, natural language processing, and much more. In this article, we will walk you through the process of building your first neural network using TensorFlow, a popular open-source machine learning library. We'll…

  • Introduction to Deep Learning with TensorFlow – Deep Learning 1

    Introduction to Deep Learning with TensorFlow – Deep Learning 1

    In this article, we provide an introduction to deep learning with TensorFlow. We cover what deep learning is, what it can do, why TensorFlow is a great choice for deep learning, and an overview of TensorFlow itself. We also explore the different types of neural networks used in deep learning, and demonstrate how to build…

  • How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    Introduction Hello tech enthusiasts! Pradeep here, your trusted source for all things related to machine learning, deep learning, and Python. As you know, I’ve previously covered setting up TensorFlow on Windows. Today, I’m excited to bring you a detailed guide on setting up another popular deep learning framework, PyTorch, with GPU support on Windows 11.…

  • Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    In this article, we demonstrate how to solve a logistics optimization problem using the Pulp library in Python. By defining the variables, objective function, and constraints, and using the solve method to find the optimal solution, we are able to minimize the total cost of transportation while satisfying the constraints. This article concludes the multi-part…

  • Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    In this article, we used the Pulp library in Python to solve a linear programming problem to find the optimal investment portfolio. We defined variables, added constraints, defined objectives, and solved the problem to find the optimal solution that balances the trade-off between maximizing returns and minimizing risk. The code was concise and easy to…

  • Linear Programming in Python using Pulp – Part 2

    Linear Programming in Python using Pulp – Part 2

    In this article, we delve deeper into linear programming and explore how to solve a multi-objective optimization problem using the Pulp library in Python. We present a problem in which a nutritionist must find the optimal meal plan for a patient suffering from anemia, balancing the intake of Vitamin B12 and fat. We demonstrate how…

  • Linear Programming in Python using PuLP – Part 1

    Linear Programming in Python using PuLP – Part 1

    Linear programming is an optimization technique used to find the best outcomes for a given problem. This technique relies on a set of constructs which are all expressed using a system of linear equations. It is important to understand that you should be able to express your objective as a linear equation dependent on an…

  • How To: Setup Tensorflow With GPU Support using Docker

    How To: Setup Tensorflow With GPU Support using Docker

    Previously I published a guide for setting up tensorflow in an anconda environment with GPU support. A lot of people liked it and I have been working with this environment myself for more than a year now. I am happy with the results however the process is a bit involved and requires quite a bit…

  • How To: Setup Tensorflow With GPU Support in Windows 11

    How To: Setup Tensorflow With GPU Support in Windows 11

    It's been just 2 days since Windows 11 came out and I am already setting up my system for the ultimate machine learning environment. Today we are going to setup a new anaconda environment with tensorflow 2.5 with GPU support using NVIDIA CUDA 11.4 and CUDNN 8.2.4 along with Python 3.8. This is going to…

  • Tools of The Trade – II

    Tools of The Trade – II

    In continuation of my previous post today I will talk about the website tanooja.com. I did this project on request of my wife because she wanted to pursue blogging and didn't want to go through the ordeal needed to write, publish and manage SEO using most of the prominent blogging platforms like WordPress, Joomla, Drupal…

  • Tools of The Trade – I

    Tools of The Trade – I

    In this post I will share a few tools and technologies that I am using to run a couple of blazing fast websites using latest modern tools and technologies. The caveat here is that I don't pay any infrastructure/hosting costs for any of these websites and they can scale infinitely in terms of supported users…

  • Building Lizzie – IV

    Building Lizzie – IV

    Another post about Lizzie. I started off with a Raspberry Pi 3 to build a personal assistant for my car and I have come a long way both in terms of the concept and the functionality. Most importantly I have formalized the application flow and also extended the scope from one device to almost all…

  • OBD-II with Raspberry Pi3

    OBD-II with Raspberry Pi3

    I am writing this article in response to a question posted on my YouTube channel. Here I would be talking about communicating to an OBD-II device (ELM327 chip with Bluetooth) hooked into your car’s OBD-II port. The OS I am using is Windows 10 IoT core. This information is important because it makes a difference…

  • Building Lizzie – III

    Building Lizzie – III

    As mentioned in previous article today I would be talking about OBD-II integration in Lizzie using a Bluetooth serial communication with an ELM327 adapter that fits on a OBD-II port in your car. OBD stands for On Board Diagnostics which is connected to the ECU (Engine Control Unit) and provides a ton of information (both…

  • Building Lizzie – II

    Building Lizzie – II

    In the previous post I described my experiments around building an intelligent artificial personal assistant – Lizzie. The pseudo intelligent agents available today around us (Siri, Cortana or Google Next) are all great feats of engineering given the fact that they reside on small devices like mobile phones and are able to do powerful things…

  • Building Lizzie – I

    Building Lizzie – I

    Recently I have been busy building a personal assistant that I would be fitting in my car. Currently I am in experimentation mode and I am experimenting with speech capabilities. I would start with a description of my journey so far. First let me show off a little bit with these videos that I created…

  • How To: Super Fast Tensorflow 2 Setup with GPU Support in VS Code

    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…

%d bloggers like this: