【问题标题】:Processing time gets longer and longer after each iteration (TensorFlow)每次迭代后处理时间越来越长(TensorFlow)
【发布时间】:2016-07-13 20:04:19
【问题描述】:

我正在使用 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

该代码适用于对我的图像进行修改。

问题是:

在我的训练循环的每次迭代(前馈 + 反向传播)之后,将相同的函数应用到我的下一个训练批次稳定地需要比上次多 5 秒。

处理大约需要 1 秒,经过 10 次以上的迭代后达到一分钟以上的处理时间。

是什么导致了这种放缓? 如何预防?

(我怀疑distorted_image.eval() 有什么问题,但我不太确定。每次都打开一个新会话吗?Te​​nsorFlow 不应该像我在“with tf.Session()”中使用的那样自动关闭会话阻止?)

【问题讨论】:

标签: python tensorflow optimization


【解决方案1】:

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

【讨论】:

  • 我将图像转换回 ndarray 以便能够使用 matplotlib.pyplot 将它们可视化。也许我对 TensorFlow 太陌生了,但我觉得 TensorBoard 并不是选择性地可视化我的数据集的最方便的方法。
  • @Julep 如果需要,即使没有“破坏张量链”,您仍然可以通过在训练循环中请求图像张量的结果来获取图像(您可能有类似 sess .run([train_op, loss]) - 如果你也要求输入批次,你会得到一个 ndarray 并可以在那时将其可视化。
猜你喜欢
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多