tensorflow越跑越慢_每次迭代后处理时间变得越来越长(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?)

解决方案

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值