Reputation: 1036
I am training a Cat-Dog Classifier with the help transfer learning on a GPU using TF 2.0. I used Keras ImageDataGenerator for performing data augmentation. While training the model, I monitored the usage of GPU, Disk(HDD) and CPU, and noted the following:-
From these observations, I made the following inferences:-
However, there were a few things I could not wrap my head around:-
Here are a few code snippets:-
train_datagen = ImageDataGenerator(rotation_range = 30,
width_shift_range = 0.4,
height_shift_range = 0.4,
shear_range = 0.4,
zoom_range = 0.25,
horizontal_flip = True,
brightness_range = [0.5, 1.5],
preprocessing_function = preprocess_input)
valid_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)
train_generator = train_datagen.flow_from_dataframe(train_data,
directory = 'train/',
x_col = 'Photo',
y_col = 'Class',
target_size = (299,299),
class_mode = 'binary',
seed = 42,
batch_size = 8)
validation_generator = valid_datagen.flow_from_dataframe(valid_data,
directory = 'train/',
x_col = 'Photo',
y_col = 'Class',
target_size = (299,299),
class_mode = 'binary',
seed = 42,
batch_size = 8)
inception_resnet_v2 = InceptionResNetV2(include_top = False,
weights = 'imagenet',
input_shape = (299, 299, 3),
pooling = 'avg',
classes = 2)
inception_resnet_v2.trainable = False
out = Dense(1, activation = 'sigmoid')(inception_resnet_v2.output)
model = Model(inputs = inception_resnet_v2.inputs, outputs = out)
checkpoint = ModelCheckpoint('model.h5',
monitor = 'val_accuracy',
verbose = 0,
save_best_only = True,
save_weights_only = False,
mode = 'max',
period = 1)
optim = tf.keras.optimizers.Adam(lr = 0.0001)
model.compile(optimizer = optim, loss = 'binary_crossentropy', metrics = ['accuracy'])
hist = model.fit_generator(train_generator,
steps_per_epoch = len(train_generator),
epochs = 10,
callbacks = [checkpoint],
validation_data = validation_generator,
verbose = 1,
validation_steps = len(validation_generator),
validation_freq = 1)
I would be grateful if someone could answer my questions and also point out, if my inferences were correct or wrong.
Thanks.
Upvotes: 0
Views: 829
Reputation: 267
By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICES) visible to the process.
You can limit the same using,
import tensorflow as tf
from keras import backend as k
config = tf.ConfigProto() # TensorFlow wizardry
config.gpu_options.allow_growth = True # Don't pre-allocate memory; allocate as-needed
config.gpu_options.per_process_gpu_memory_fraction = 0.95 # Only allow a total fraction the GPU memory to be allocated
k.tensorflow_backend.set_session(tf.Session(config=config)) # Create a session with the above options specified.
Also you can check this for more information : https://www.tensorflow.org/guide/gpu
Upvotes: 1