0

My problem is that I am trying to train a convolution neural network with Keras in google colab that is able to distinguish between dogs and cats, but at the time of passing to the training phase my model takes a long time to train and I wanted to know how I can use the GPU in the right way in order to make the training time take less time.


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
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf

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('/content/drive/MyDrive/Colab Notebooks/files/dataset_CNN/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/Colab Notebooks/files/dataset_CNN/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
with tf.device('/device:GPU:0'):#I tried to put this part that I found by researching on the internet 
  
  classifier = Sequential()
  classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
  classifier.add(MaxPooling2D(pool_size = (2, 2)))
  classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
  classifier.add(MaxPooling2D(pool_size = (2, 2)))
  classifier.add(Flatten())
  classifier.add(Dense(units = 128, activation = 'relu'))
  classifier.add(Dense(units = 1, activation = 'sigmoid'))
  classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
  classifier.fit(training_set,
                         steps_per_epoch = 8000,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 2000)

Previously, I did not put this part of the code "with tf.device('/device:GPU:0')", I saw this part of the code in an example on the internet, but it is still slow when training. I already checked the Available GPUs using:


device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')


  • 1
    Have you configured Colab's runtime to use a GPU? by default, colab is launching a CPU instance. Go to runtime > change runtime type > Hardware accelerator and switch to GPU. – kyriakosSt Jan 12 '23 at 14:50
  • 1
    Also, maybe it is better to use `tf.test.is_gpu_available()` instead of device name since in some system you may not get "GPU:0", but some other device – kyriakosSt Jan 12 '23 at 14:52
  • Does this answer your question? [Can I run Keras model on gpu?](https://stackoverflow.com/questions/45662253/can-i-run-keras-model-on-gpu) – Daraan Jan 12 '23 at 14:53
  • I switched from CPU to GPU usage on Google colab, and yes I entered that forum [link](https://stackoverflow.com/questions/45662253/can-i-run-keras-model-on-gpu) and had tried, but anyway it is slow in the training phase. – Cesar Vigil Jan 12 '23 at 15:36
  • What does "slow" mean, GPUs are not a magic bullet that will make your code faster. – Dr. Snoopy Jan 12 '23 at 16:42

1 Answers1

0

To Select GPU in Google Colab -
Select Edit - Notebook Setting - Hardware accelerator - GPU - Save

ImageDataGenerator is not recommended for new code. Instead you can use these augmentation features directly through layers in model training as below:

classifier = tf.keras.Sequential([
#data augmention layers
  layers.Resizing(IMG_SIZE, IMG_SIZE),
  layers.Rescaling(1./255),
  layers.RandomFlip("horizontal"),
  layers.RandomZoom(0.1),
#model building layers
  layers.Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'),
  layers.MaxPooling2D(pool_size = (2, 2)),
  layers.Conv2D(32, (3, 3), activation = 'relu'),
  layers.MaxPooling2D(pool_size = (2, 2)),
  layers.Flatten(),
  layers.Dense(units = 128, activation = 'relu'),
  layers.Dense(units = 1, activation = 'sigmoid')
])

Also, you should use image_dataset_from_directory to import the images dataset which generates a tf.data.Dataset from image files in a directory. Please refer to this gist of replicated code.

Note: fit() automatically calculates Steps_per_epoch from the training set as per batch_size.
Steps_per_epoch = len(training_dataset)//batch_size