InvalidArgumentError(回溯,请参见上面的内容):张量必须为4-D,最后为暗淡1、3或4,而不是[5,100,100,120]

时间:2019-01-14 22:21:01

标签: python-3.x tensorflow tensorboard

tf.summary.image中,max_outputs = 3。我想可视化max_output为100的卷积层的输出。在这种情况下,tf.summary.image无法正常工作。请提出如何可视化所有要素地图(= 100)?

代码段

conv2

with tf.variable_scope('conv2') as scope:
    kernel2 = _variable_with_weight_decay('weights', shape=[5, 5, 100, 120], stddev=1e-4, wd=0.0)
    conv2 = tf.nn.conv2d(norm1, kernel2, [1, 1, 1, 1], padding='SAME')
    biases2 = _variable_on_cpu('biases', [120], tf.constant_initializer(0.1))
    bias2 = tf.nn.bias_add(conv2, biases2)
    conv2 = tf.nn.relu(bias2, name=scope.name)
    print(tf.abs(conv2))
    _activation_summary(conv2)
    tf.summary.image('conv2', conv2, max_outputs=3)

跟踪

tf.summary.image('conv2', conv2, max_outputs=3)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/summary/summary.py", line 146, in image
    tag=tag, tensor=tensor, max_images=max_outputs, name=scope)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/ops/gen_logging_ops.py", line 388, in image_summary
    bad_color=bad_color, name=name)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Tensor must be 4-D with last dim 1, 3, or 4, not [5,100,100,120]
     [[Node: tower_3/conv2/conv2_1 = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](tower_3/conv2/conv2_1/tag, tower_3/conv2/conv2-0-1-TransposeNCHWToNHWC-LayoutOptimizer/_1787)]]
     [[Node: tower_1/gradients/tower_1/deconv8/deconv8/BiasAdd_grad/BiasAddGrad/_2204 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device_incarnation=1, tensor_name="edge_2343_tower_1/gradients/tower_1/deconv8/deconv8/BiasAdd_grad/BiasAddGrad", _device="/job:localhost/replica:0/task:0/device:GPU:1"](tower_1/gradients/tower_1/deconv8/deconv8/BiasAdd_grad/BiasAddGrad)]]

任何类型的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

下面是上述问题的解决方案。答案来自Reference

此处,渠道= 120; img_size = 100

with tf.variable_scope('conv2'):
    V = tf.slice(conv2, (0, 0, 0, 0), (1, -1, -1, -1), name='slice_first_input')
    V = tf.reshape(V, (100, 100, 120))

  # Reorder so the channels are in the first dimension, x and y follow.
    V = tf.transpose(V, (2, 0, 1))
  # Bring into shape expected by image_summary
    V = tf.reshape(V, (-1, 100, 100, 1))

    tf.summary.image("first_conv", V)