Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing time gets longer and longer after each iteration (TensorFlow)

I am training a CNN with TensorFlow for medical images application.

As I don't have a lot of data, I am trying to apply random modifications to my training batch during the training loop to artificially increase my training dataset. I made the following function in a different script and call it on my training batch:

def randomly_modify_training_batch(images_train_batch, batch_size):

    for i in range(batch_size):
        image = images_train_batch[i]
        image_tensor = tf.convert_to_tensor(image)

        distorted_image = tf.image.random_flip_left_right(image_tensor)
        distorted_image = tf.image.random_flip_up_down(distorted_image)
        distorted_image = tf.image.random_brightness(distorted_image, max_delta=60)
        distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)

        with tf.Session():
            images_train_batch[i] = distorted_image.eval()  # .eval() is used to reconvert the image from Tensor type to ndarray

return images_train_batch

The code works well for applying modifications to my images.

The problem is :

After each iteration of my training loop (feedfoward + backpropagation), applying this same function to my next training batch steadily takes 5 seconds longer than the last time.

It takes around 1 second to process and reaches over a minute of processing after a bit more than 10 iterations.

What causes this slowing? How can I prevent it?

(I suspect something with distorted_image.eval() but I'm not quite sure. Am opening a new session each time? TensorFlow isn't supposed to close automatically the session as I use in a "with tf.Session()" block?)

like image 741
Julep Avatar asked Mar 26 '16 01:03

Julep


1 Answers

You call that code in each iteration, so each iteration you add these operations to the graph. You don't want to do that. You want to build the graph at the start and in the training loop only execute it. Also, why do you need to convert to ndimage again afterwards, instead of putting things into your TF graph once and just use tensors all the way through?

like image 188
etarion Avatar answered Nov 19 '22 17:11

etarion