【发布时间】: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