How to run TensorFlow GPU version on Google Colab with Python 2.7?

246 Views Asked by At

I have a code written using the TensorFlow version 1.15 for an image to image translation task and I want to run it on Google Colab environment which currently has Python 3.10.12 installed by default.

Due to my source code's dependencies, I have to use TensorFlow version 1.15 and that's why I have used other dependencies that match with this version of TensorFlow.

First, I install Python version 2.7 on Google Colab using the command below:

!apt-get install python2

After that, I check the installed Python's version:

!python2 --version

And I get: Python 2.7.18

Next, I connect to Google Drive and refer to the path where my source is located (main.py, util.py, model.py and ops.py):

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive/TFCode/

Then to install the necessary packages, first I install pip using the commands below:

!curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
!python2 get-pip.py

And then:

!python2 -m pip install six
!python2 -m pip install scipy
!python2 -m pip install imageio==2.4.1
!apt-get install build-essential
!python2 -m pip install grpcio==1.26.0
!python2 -m pip install tensorflow-gpu==1.15
!python2 -m pip install opencv-python==3.4.8.29
!python2 -m pip install scikit-image==0.14.5

Finally, I run my source using !python2 main.py and despite the fact that the code runs successfully, it takes a long time to finish the task becuase my code is not being run on GPU and I know this because of the following code block inside my main.py file:

if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

Inside my results, I see the "Please install GPU version of TF" string output which means the GPU cannot be detected but when I run this code like below directly in Colab:

import tensorflow as tf
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

I get this: Default GPU Device: /device:GPU:0 which means it finds the GPU but as this code is being run directly in Colab environment, I know that it uses Python 3.10.12 and TensorFlow version 2.

How can I run my own source code with GPU?

Using version 2.7 of Python is not a must but version 1.15 of TensorFlow must be used and other dependencies must match with this version of TensorFlow.

2

There are 2 best solutions below

4
VonC On

Note: there is a TensorFlow GPU 1.15 is compatible with Python 3.9-3.11. TensorFlow 1.15 (Jan. 2020) is no longer listed.

Using version 2.7 of Python is not a must

Since TensorFlow 1.15 is compatible with Python 3.7, you try and create a virtual environment with Python 3.7 instead, avoiding any compatibility issue with Python 2:

!sudo apt-get install python3.7
!python3.7 -m venv tf15env

Activate the virtual environment:

!source /content/tf15env/bin/activate

And install TensorFlow 1.15 and other dependencies within the virtual environment:

!pip install tensorflow-gpu==1.15
!pip install six scipy imageio==2.4.1 grpcio==1.26.0 opencv-python==3.4.8.29 scikit-image==0.14.5

Run this script to make sure TensorFlow is using the GPU:

import tensorflow as tf
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

After setting up the environment, you can run your code (main.py, util.py, model.py, ops.py) in this virtual environment.


See also "Changing the Python version on Google Colab" from Siladittya Manna for an alternative way to change the Python version on Google Colab.

1
anit3res On

I followed the guide "Changing the Python version on Google Colab" from Siladittya Manna stated by VonC and everything seems to work. I used Colab with an T4 GPU instance.

!apt-get update -y
!apt-get install python3.7 python3.7-distutils
!update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!update-alternatives --config python3
!apt-get install python3-pip
!python3 -m pip install --upgrade pip --user
!python3 --version # Python 3.7.17

With the install instruction already mentioned by VonC:

!pip install tensorflow-gpu==1.15
!pip install six scipy imageio==2.4.1 grpcio==1.26.0 opencv-python==3.4.8.29 scikit-image==0.14.5

Including your version dependency for protobuf:

!pip install protobuf==3.20.0

Running

import tensorflow as tf
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

I get Default GPU Device: /device:GPU:0.