我正在尝试通过while_loop在拉普拉斯金字塔中对RGB图像进行升采样。 但是我一直收到以下错误。
Caused by op u'lambda_1/while/Slice_1', defined at:
File "/home/ziyi/PycharmProjects/ForegroundMotion/opration/high_layer.py", line 138, in <module>
out = layer(output_bt)
File "/home/ziyi/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 554, in __call__
output = self.call(inputs, **kwargs)
File "/home/ziyi/.local/lib/python2.7/site-packages/keras/layers/core.py", line 659, in call
return self.function(inputs, **arguments)
File "/home/ziyi/PycharmProjects/ForegroundMotion/opration/high_layer.py", line 31, in expand_image
n.get_shape()])
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3232, in while_loop
return_same_structure)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2952, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2887, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "/home/ziyi/PycharmProjects/ForegroundMotion/utils/utils_lap_pyramid.py", line 129, in body
slice = tf.slice(output_bot,[0,0,i], [-1,-1,1])
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 577, in slice
return gen_array_ops._slice(input_, begin, size, name=name)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 7177, in _slice
"Slice", input=input, begin=begin, size=size, name=name)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
return func(*args, **kwargs)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
op_def=op_def)
File "/home/ziyi/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1717, in __init__
self._traceback = tf_stack.extract_stack()
InvalidArgumentError (see above for traceback): Expected size[2] in [0, 0], but got 1
[[Node: lambda_1/while/Slice_1 = Slice[Index=DT_INT32, T=DT_FLOAT, _class=["loc:@gradients/lambda_1/while/Slice_1_grad/Pad"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](lambda_1/while/ExpandDims_4, lambda_1/while/Slice_1/begin, lambda_1/while/Slice/size)]]
[[Node: mul_1/_131 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_13482_mul_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Process finished with exit code 1
这似乎是在tf.slice中发生的错误。我已经在线搜索了,大部分是尺寸原因,但是我无法弄清楚我的问题。
这是循环代码:
# Expand bottom images (has the same size as high layer images)
def expand_image(output_bt):
bot_p_expand = 0
for i in range(config.data.batch_size):
bot_p = tf.squeeze(tf.slice(output_bt, [i, 0, 0, 0], [1, -1, -1, -1]))
index = tf.constant(0)
n = tf.constant(config.model.level)
bot_p, index, n = tf.while_loop(cond, body, [bot_p, index, n],
shape_invariants=[tf.TensorShape([None, None, None]),
index.get_shape(),
n.get_shape()])
bot_p = tf.expand_dims(bot_p, axis=0)
if i == 0:
bot_p_expand = bot_p
else:
bot_p_expand = tf.concat([bot_p_expand, bot_p], axis=0)
bot_p_expand = tf.reshape(bot_p_expand,[-1,256,256,3])
return bot_p_expand
这是循环的主体: 因为这是RGB图像,所以我将每个通道切成薄片并分别进行扩展,然后将它们串联起来。
def body(output_bot, i, n):
zeros = 0
paddings = tf.constant([[0, 0], [2, 2], [2, 2], [0, 0]])
for i in range(3):
slice = tf.slice(output_bot, [0, 0, i], [-1, -1, 1])
squeeze = tf.squeeze(slice)
output_bot = dilatezeros(squeeze)
output_bot = tf.pad(output_bot, paddings, "REFLECT")
output_bot = applygaussian(output_bot)
output_bot = tf.expand_dims(output_bot, axis=2)
if i == 0:
zeros = output_bot
else:
zeros = tf.concat([zeros, output_bot], axis=2)
return zeros, tf.add(i, 1), n
def applygaussian(imgs):
gauss_f = call2dtensorgaussfilter() #here is gauss filter
gauss_f = tf.expand_dims(gauss_f, axis=2)
gauss_f = tf.expand_dims(gauss_f, axis=3)
result = tf.nn.conv2d(imgs, gauss_f * 4, strides=[1, 1, 1, 1], padding="VALID")
result = tf.squeeze(result, axis=0)
result = tf.squeeze(result, axis=2)
return result
def dilatezeros(imgs):
zeros = tf.zeros_like(imgs)
column_zeros = tf.reshape(tf.stack([imgs, zeros], 2), [-1, tf.shape(imgs)[1] + tf.shape(zeros)[1]])[:,:-1]
row_zeros = tf.transpose(column_zeros)
zeros = tf.zeros_like(row_zeros)
dilated = tf.reshape(tf.stack([row_zeros, zeros], 2), [-1, tf.shape(row_zeros)[1] + tf.shape(zeros)[1]])[:,:-1]
dilated = tf.transpose(dilated)
paddings = tf.constant([[0, 1], [0, 1]])
dilated = tf.pad(dilated, paddings, "REFLECT")
dilated = tf.expand_dims(dilated, axis=0)
dilated = tf.expand_dims(dilated, axis=3)
return dilated