每次迭代后,处理时间越来越长(TensorFlow)

Jul*_*lep 10 python optimization tensorflow

我正在使用TensorFlow培训CNN用于医学图像应用.

由于我没有大量数据,因此我试图在训练循环期间对我的训练批次应用随机修改,以人为地增加我的训练数据集.我在不同的脚本中创建了以下函数,并在我的训练批处理中调用它:

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
Run Code Online (Sandbox Code Playgroud)

该代码适用于对我的图像应用修改.

问题是 :

在我的训练循环(feedfoward + backpropagation)的每次迭代之后,将相同的功能应用于我的下一个训练批次比上一次稳定地花费5秒.

处理过程大约需要1秒钟,经过10多次迭代后,处理时间超过一分钟.

是什么导致这种放缓?我该怎样预防呢?

(我怀疑有些东西,distorted_image.eval()但我不太确定.我每次都打开一个新的会话?TensorFlow不应该自动关闭会话,因为我在"with tf.Session()"块中使用?)

eta*_*ion 9

您可以在每次迭代中调用该代码,因此每次迭代都会将这些操作添加到图形中.你不想那样做.您希望在开始时和训练循环中构建图形,只执行它.另外,为什么你需要在之后再次转换为ndimage,而不是将内容放入TF图中并且一直使用张量?