【问题标题】:Image distortion returns error: The tensor returned for Reshape_5:0 was not valid图像失真返回错误:为 Reshape_5:0 返回的张量无效
【发布时间】:2017-01-08 16:23:19
【问题描述】:

我正在尝试将图像失真添加到我的 ConvNet 模型中,但遇到了一个非常奇怪的错误。我的数据采用TFRecords 格式,我正在使用CFIAR10 代码中的color_distorter() 函数。下面是我拼凑起来的一些虚拟代码,以查看一切都在按照我的预期进行。当我在图像失真后查看图像时,没有问题。在展平张量或运行后,问题似乎出现了。由于某种原因,它会执行一次,但第二次会抛出错误。以下是我的代码及其返回的错误。我目前的怀疑是它可能是tf.map_fn(),但我不知道。

def color_distorer(image, thread_id=0, scope=None):
    with tf.op_scope([image], scope, 'distort_color'):
        color_ordering = thread_id % 2

       if color_ordering == 0:
           image = tf.image.random_brightness(image, max_delta=32. / 255.)
           image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
           image = tf.image.random_hue(image, max_delta=0.2)
           image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
    elif color_ordering == 1:
           image = tf.image.random_brightness(image, max_delta=32. / 255.)
           image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
           image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
           image = tf.image.random_hue(image, max_delta=0.2)

           # The random_* ops do not necessarily clamp.
        image = tf.clip_by_value(image, 0.0, 1.0)
        return image

X_test_batch, y_test_batch = inputs(FLAGS.train_dir,
                                FLAGS.test_file,
                                FLAGS.batch_size,
                                FLAGS.n_epochs,
                                FLAGS.n_classes,
                                one_hot_labels=True,
                                imshape=160*160*3)

with tf.Session() as sess:

    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    # X = sess.run([X_test_batch])
    for i in range(5): #epochs
        image = tf.reshape(X_test_batch, [-1, 160, 160, 3])
        result = tf.map_fn(lambda img: color_distorer(img), image)
        X, y = sess.run([result, y_test_batch]) #to see what the distortion did
        for i in range(50): #all the images look distorted..
            if i%25 ==0:
                plt.title(y[i])
                plt.imshow(X[i])
                plt.show()
        print('******************') #This runs once... then breaks. Why?
        result = tf.reshape(result, [-1, 76800])
        dX, dy = sess.run([result, y_test_batch])
        print(dX)

错误:

******************
[[ 1.  1.  1. ...,  1.  1.  1.]
[ 1.  1.  1. ...,  1.  1.  1.]
[ 1.  1.  1. ...,  1.  1.  1.]
..., 
[ 1.  1.  1. ...,  1.  1.  1.]
[ 1.  1.  1. ...,  1.  1.  1.]
[ 1.  1.  1. ...,  1.  1.  1.]]
WARNING:tensorflow:tf.op_scope(values, name, default_name) is deprecated, use tf.name_scope(name, default_name, values)
Traceback (most recent call last):
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1021, in _do_call
return fn(*args)
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1003, in _run_fn
status, run_metadata)
File "/home/mcamp/anaconda3/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: The tensor returned for map_1/TensorArrayPack_1/TensorArrayGatherV2:0 was not valid.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/media/mcamp/Local SSHD/Python Projects/Garage Door   Project/FreshStart/ReadTFREcords.py", line 80, in <module>
  dX, dy = sess.run([result, y_test_batch])
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 766, in run
run_metadata_ptr)
 File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 964, in _run
feed_dict_string, options, run_metadata)
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1014, in _do_run
target_list, options, run_metadata)
File "/home/mcamp/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1034, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: The tensor returned for map_1/TensorArrayPack_1/TensorArrayGatherV2:0 was not valid.

【问题讨论】:

  • 这听起来像是TensorFlow中的一个bug,你能举个自包含的例子吗?
  • 我顿悟了,我想我可能明白发生了什么,但仍在努力解决它..并将很快发布代码..
  • 我现在已经让它运行了,但是每隔几次我运行它就会再次出现错误......所以也许这也是 Tensorflow 中的一个错误......也许图表被缓存在内存中或其他东西..

标签: python-3.x tensorflow


【解决方案1】:

好吧,我相信正在发生的事情是我将 X_test_batch 的大小重新调整为图像的形状。然后我在它上面运行了我的失真并称之为result。这就是我跑和查看的。查看后,我将模型的大小重新调整为平面图像。在此期间,我更改了一些东西的名称,首先是 image,然后是 result。如果代码从上到下只是红色,我认为它会很好,但 Tensorflow 有它工作的图表,所以当它到达第二次迭代时,它期望结果是一个 3d 张量,但它真的是一个平面试图通过tf.map_fn 的张量。 tf.map_fn 是驻留在图表上的东西,直到 sess.run() 才会执行,所以这将是我看到的错误的原因。我希望这是有道理的,并且可以帮助其他人。

with tf.name_scope('TestingData'):
    X_test_batch, y_test_batch = inputs(FLAGS.train_dir,
                                       FLAGS.test_file,
                                       FLAGS.batch_size,
                                       FLAGS.n_epochs,
                                       FLAGS.n_classes,
                                       one_hot_labels=True,
                                       imshape=160*160*3)

image = tf.reshape(X_test_batch, [-1, 160, 160, 3])

image = tf.map_fn(lambda img: color_distorer(img), image)
with tf.Session() as sess:

    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
     sess.run(init_op)
     coord = tf.train.Coordinator()
     threads = tf.train.start_queue_runners(coord=coord)
     # X = sess.run([X_test_batch])
     for i in range(5):
         # dX, dy = sess.run([result, y_test_batch])
         print('******************')
         image = tf.reshape(image, [-1, 76800])
         dX, dy = sess.run([image, y_test_batch])
         distorted = np.reshape(dX, (-1, 160, 160, 3))
         print(dX.shape)
         print(distorted.shape)
         for i in range(50):
             if i%25 ==0:
                  print(distorted[i], dy[i])
                  plt.title(dy[i])
                  plt.imshow(distorted[i])
                  plt.show()
                  print(dX.shape)

【讨论】:

  • tf.reset_default_graph(); sess = tf.Session() 是你的朋友 :)
  • 看来,如果我在失真点停止训练并尝试再次启动模型,它会抛出错误。我目前找到了一个解决方法,我只需注释掉 tf.map_fn 行运行模型,直到它保存一个检查点,然后使用 tf.map_fn 再次运行它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多