0

I installed python 3.6 along with cuda 10.0 and cudnn using:

tensorflow - pip install tensorflow-gpu

but it only takes 2-3% of my gpu (as shown in task manager).

I am getting 5 mins per epoch in my 2060Super; when I run it on the terminal it selects the gpu but the time is taken per epoch is too much and, also, it is not utilizing the whole gpu.

#Building CNN
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
#initialize the CNN
classifier = Sequential()

#step 1 - Convolution
classifier.add(Conv2D(32,3,3,input_shape = (64,64,3), activation='relu'))
#step 2 Pooling
classifier.add(MaxPooling2D(pool_size=(2,2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

#step3 Flattening
classifier.add(Flatten())

#Full Connection
classifier.add(Dense(units = 128,activation='relu'))
classifier.add(Dense(units = 1,activation='sigmoid'))

#compile CNN
classifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

#Fitting CNN to 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')

classifier.fit_generator(training_set,
                         steps_per_epoch = 8000,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 2000)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • please format you text and, read you question a second time – Zig Razor Dec 21 '19 at 14:54
  • @zigrazor I made an *attempt* at correct formatting - maybe you'd like to check it over, and possibly improve/correct. – Adrian Mole Dec 21 '19 at 19:51
  • This appears to work as expected - see [here](https://stackoverflow.com/questions/58676082/how-to-make-tensorflow-use-100-of-gpu/58676370#58676370); TL;DR your model's too small, and "only 3%" is good otherwise GPU would crash for larger models. As for speed, unless you've seen others do faster for same model and GPU, 5 mins for 8k steps doesn't seem unreasonable - regardless [this](https://stackoverflow.com/questions/58441514/why-is-tensorflow-2-much-slower-than-tensorflow-1#answer-58653632) may help – OverLordGoldDragon Dec 22 '19 at 15:45
  • Actually as deep learning beginner I see the instructor of udemy of deep learning course just have 70s per epoch although he use mac.So i think that my TF is getting some issue.@OverLordGoldDragon – Indranil Dasgupta Dec 22 '19 at 18:45

1 Answers1

0

To configure TensorFlow GPU you can do the following (tensorflow r1.14 and less)

import os
import tensorflow as tf


def configure_tf_gpu():
    # Reduce logging output.
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    tf.logging.set_verbosity(tf.logging.INFO)
    config = tf.ConfigProto()
    # dynamically grow the memory used on the GPU
    config.gpu_options.allow_growth = True
    # Allowing TF to automatically choose an existing and
    # supported device to run the operations in case the specified one doesn't exist
    config.allow_soft_placement = True
    # to log device placement (on which device the operation ran)
    config.log_device_placement = False
    config.gpu_options.per_process_gpu_memory_fraction = 0.99
    # (nothing gets printed in Jupyter, only if you run it standalone)
    sess = tf.Session(config=config)
    # set this TensorFlow session as the default session for Keras
    # set_session(sess)
ElSheikh
  • 321
  • 6
  • 28
  • @EI Sheikh thanks for the answer. But it shows **AttributeError: module 'tensorflow' has no attribute 'logging'** when I paste the function with my code. – Indranil Dasgupta Dec 22 '19 at 05:50
  • @IndranilDasgupta if you are using `tensorflow v2.0` this is expected – ElSheikh Dec 22 '19 at 15:34
  • Yes I am using Tensorflow v2.0.0 @EISheikh. What to do with this code? can you please explain – Indranil Dasgupta Dec 22 '19 at 18:35
  • I made sure to comment it well, please point out for me what you really don't understand – ElSheikh Dec 23 '19 at 15:13
  • @EI Sheikh thanks for reply . Actually I am not understand what to do with these block of code. Are you saying to paste in my cnn classifier while I run the code or something else. – Indranil Dasgupta Dec 23 '19 at 19:13
  • No I am saying that this function defines your tensorflow configuration on your GPU, you should call it before creating/building your model. And then create, compile and train your model – ElSheikh Dec 24 '19 at 02:33
  • If I call the function before building the model It says attribute error as I mentioned in my 1st comment @EI Sheikh – Indranil Dasgupta Dec 24 '19 at 10:25
  • Yes of course it wont, because as I mentioned and edited the answer this is not going to work with tf v2.0 so you have to work around tf.logging part – ElSheikh Dec 25 '19 at 16:36