Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

Switch输出结果dim存在问题

Open Gaffey opened this issue 7 years ago • 3 comments

  • 版本、环境信息:    1)PaddlePaddle版本:1.4.1    2)CPU:-    3)GPU:v100    4)系统环境:python2.7

  • 问题描述:请详细描述您的问题,同步贴出报错信息、日志/代码关键片段 相关代码

        cond1 = fluid.layers.data(name="cond1", shape=[1], dtype='float32')
        zero_var = fluid.layers.fill_constant(shape=[1], dtype='float32', value=0.0)
        with fluid.layers.control_flow.Switch() as switch:
            with switch.case(cond1 == zero_var):
                conv1 = fluid.layers.zeros(shape=[input.shape[0], input.shape[1], input.shape[2]//2, input.shape[3]//2], dtype=input.dtype)

            with switch.default():
                conv1 = self.conv_bn_layer(input=input, num_filters=filter1, filter_size=5, stride=2, act='relu')
        conv2 = self.conv_bn_layer(input=input, num_filters=filter1, filter_size=3, stride=2, act='relu')
        conv = fluid.layers.elementwise_add(conv2, conv1)

调试信息

(Pdb) p conv1
name: "batch_norm_0.tmp_3"
type {
  type: LOD_TENSOR
  lod_tensor {
    tensor {
      data_type: FP32
      dims: -1
      dims: 224
      dims: 56
      dims: 56
    }
    lod_level: 0
  }
}
persistable: false

(Pdb) p conv2
name: "batch_norm_1.tmp_3"
type {
  type: LOD_TENSOR
  lod_tensor {
    tensor {
      data_type: FP32
      dims: -1
      dims: 224
      dims: 56
      dims: 56
    }
    lod_level: 0
  }
}
persistable: false

(Pdb) p conv1.shape==conv2.shape
True
(Pdb) conv = fluid.layers.elementwise_add(conv1, conv2)
*** EnforceNotMet: Enforce failed. Expected x_dim.size() >= y_dim.size(), but received x_dim.size():1 < y_dim.size():4.
Rank of first input must >= rank of second input. at [/paddle/paddle/fluid/operators/elementwise/elementwise_op.h:56]

打印结果conv1和conv2的shape是相等的,为什么会报rank x小于rank y的错误呢? 如果顺序对调是正常的

(Pdb) p conv2.shape
(-1L, 224L, 56L, 56L)
(Pdb) p conv1.shape
(-1L, 224L, 56L, 56L)
(Pdb) conv = fluid.layers.elementwise_add(conv2, conv1)
(Pdb) p conv.shape
(-1L, 224L, 56L, 56L)

Gaffey avatar Jul 05 '19 03:07 Gaffey

我发现了问题,是conv1实际上的channel数应该是3L,但为什么打印出来是224L呢?

Gaffey avatar Jul 05 '19 03:07 Gaffey

补充错误 修改两个switch case的代码,改成一致的layer,后文输入其他layer的时候,输入纬度被识别为1

         with fluid.layers.control_flow.Switch() as switch:
             with switch.case(cond1 < zero_var):
                 conv = self.conv_bn_layer(input=input, num_filters=filter1, filter_size=3, stride=1, act='relu')
             with switch.default():
                 conv = self.conv_bn_layer(input=input, num_filters=filter1, filter_size=3, stride=1, act='relu')
          conv2 = self.conv_bn_layer(input=conv, ......)

错误信息

Traceback (most recent call last):
  File "train_classifier_automl_oneshot_multi_path.py", line 414, in <module>
    main()
  File "train_classifier_automl_oneshot_multi_path.py", line 292, in main
    train_pyreader, train_out, conv1, conv2, conv = build_program(train_program, startup_program, True)
  File "train_classifier_automl_oneshot_multi_path.py", line 179, in build_program
    loss, acc = model.net(image, label)
  File "/home/slurm/job/tmp/job-51684/train/models/sphere_resnet_automl_oneshot.py", line 133, in net
    for block in range(len(depth)):
  File "/home/slurm/job/tmp/job-51684/train/models/sphere_resnet_automl_oneshot.py", line 209, in basicneck_block
    input=input, num_filters=num_filters, filter_size=3, stride=stride, act='relu')
  File "/home/slurm/job/tmp/job-51684/train/models/sphere_resnet_automl_oneshot.py", line 191, in conv_bn_layer
    bias_attr=False)
  File "/home/slurm/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/layers/nn.py", line 2073, in conv2d
    'fuse_relu_before_depthwise_conv': False
  File "/home/slurm/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op
    return self.main_program.current_block().append_op(*args, **kwargs)
  File "/home/slurm/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1654, in append_op
    attrs=kwargs.get("attrs", None))
  File "/home/slurm/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1058, in __init__
    self.desc.infer_shape(self.block.desc)
paddle.fluid.core.EnforceNotMet: Conv intput should be 4-D or 5-D tensor, get 1 at [/paddle/paddle/fluid/operators/conv_op.cc:50]

Gaffey avatar Jul 05 '19 06:07 Gaffey

hi, 我查看了一下,当前的实现,我们的静态图并不支持这种语法: image swtich 中创建的Variable是在subblock中,这种写法是会有问题的。您可能得尝试一下使用其他方式来实现相应逻辑,或者尝试一下我们1.5版本开始的动态图功能

liupluswei avatar Jul 05 '19 12:07 liupluswei