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.
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.
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:
Activate the virtual environment:
And install TensorFlow 1.15 and other dependencies within the virtual environment:
Run this script to make sure TensorFlow is using the GPU:
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.