Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure tensorflow is using the GPU

I installed CUDA v9.2 and corresponding cuDNN manually to install tensorflow gpu But I realized that tensorflow 1.8.0 requires CUDA 9.0 so I ran

pip install tensorflow-gpu

from the anaconda prompt (base environment) where it automatically installed CUDA 9.0 and corresponding cuDNN. I started Spyder from the same command prompt. So here is my code in Python 3.6 where I'm using keras and tensorflow to train using 8000 odd images -

# Convolutional Neural Networks
# Part 1 - Building the CNN
# Not important

# Part 2- Fitting the CNN to the images - 
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
        'dataset/training_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'dataset/test_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')
with tf.device("/gpu:0"):   # Notice THIS
    classifier.fit_generator(
            training_set,
            steps_per_epoch=8000,
            epochs=25,
            validation_data=test_set,
            validation_steps=2000)

Notice that right before fitting the dataset at the end, I put it inside

with tf.device("/gpu:0"):

I think this should ensure that it uses the GPU for training? I'm not sure because changing " gpu:0 " to " cpu:0 " gives the exact same time (18-20 minutes per epoch) for training. How do I ensure that tensorflow in Spyder uses my GPU ?

I have a NVIDIA GTX 970 so its CUDA compatible. Also I'm using python 3.6 , is that a problem ? Should I create a seperate Python 3.5 environment and install tensorflow-gpu in that similarly and try ?

like image 259
Mihir Deshpande Avatar asked Jun 30 '18 12:06

Mihir Deshpande


People also ask

How ensure GPU is being used by TensorFlow?

tf. keras models if GPU available will by default run on a single GPU. If you want to use multiple GPUs you can use a distribution strategy. Once you get this output now go to the terminal and type “nvidia-smi“.

Will TensorFlow automatically use GPU?

TensorFlow GPU OperationsBy default, if a GPU is available, TensorFlow will use it for all operations. You can control which GPU TensorFlow will use for a given operation, or instruct TensorFlow to use a CPU, even if a GPU is available.

How can I tell if keras is using my GPU?

Checking Your GPU Availability With Keras The easiest way to check if you have access to GPUs is to call tf. config. experimental. list_physical_devices('GPU').


1 Answers

Creates a graph.

 with tf.device('/device:GPU:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
    # Creates a session with log_device_placement set to True.
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    # Runs the op.
    r = sess.run(c)
    print(r)
    import numpy as np
    assert np.all(r == np.array([[22., 28.], [49., 64.]]))

or go tensorflow website (https://www.tensorflow.org/programmers_guide/using_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")

or this :

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
like image 171
dimension Avatar answered Oct 12 '22 08:10

dimension