【发布时间】: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() 有什么问题,但我不太确定。每次都打开一个新会话吗?TensorFlow 不应该像我在“with tf.Session()”中使用的那样自动关闭会话阻止?)
【问题讨论】:
标签: python tensorflow optimization