使用 conv2d 方法 在动态图中出现错误
Created by: sirfuwh
- 版本、环境信息: 1)PaddlePaddle版本:1.6.1 2)CPU:Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz 3)GPU:未使用 4)系统环境:windows 7 64Bit,Python 3.7.4
- 模型信息 主要使用 fluid.layers.conv2d 方法
- 复现信息:使用paddle的 conv2d算法建立一个简单的BN操作程序(程序No.1),在连续使用 conv2d时,动态图模式出现错误。 为了查找问题,我建立一个静态运行模式程序(程序 No.2) ,一模一样的算法,却正确通过。
- 问题描述:问题我都注释在代码文档里面了 , 主要是两个: 1.同样的算法,为何静态没问题,动态却出错,是我的理解错误导致代码不当,还是 Bug? 2.报错的信息中脚本位置错误,是否是Bug? 谢谢!
下面贴的代码显示有点问题,请看这个issue的附件
No.1 Program 出错的脚本
import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr from paddle.fluid.dygraph.nn import Conv2D, Pool2D, FC, BatchNorm from paddle.fluid.regularizer import L2Decay import numpy as np
class BN(fluid.dygraph.Layer): def init(self, name_scope, num_filters, filter_size, stride, padding): super(BN, self).init(name_scope)
self._conv2d = Conv2D(
self.full_name(),
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=padding,
param_attr=ParamAttr(
initializer=fluid.initializer.Normal(0, 0.02)),
bias_attr=False,
act=None)
self._bn = BatchNorm(
self.full_name(),
num_channels = num_filters,
act=None,
param_attr=ParamAttr(
initializer=fluid.initializer.Normal(0., 0.02),
regularizer=L2Decay(0.)),
bias_attr=ParamAttr(
initializer=fluid.initializer.Constant(0.0),
regularizer=L2Decay(0.)))
def forward(self, inputs):
# print("forward from dbl_block,inputs:", inputs)
x = self._conv2d(inputs)
x = self._bn(x)
return x
place = fluid.CPUPlace() with fluid.dygraph.guard(place):
x = fluid.layers.ones(shape=[1, 1024, 9, 9], dtype='float32')
conv1 = BN("conv1", 256, 1, 1, 0)
conv2 = BN("conv2", 512, 3, 1, 1)
print("Step 0,x shape is:", x.shape) # Step 0,x shape is: [1, 1024, 9, 9]
x1 = conv1(x)
print("Step 1,x shape is:", x1.shape) #Step 1,x shape is: [1, 256, 9, 9]
x2 = conv2(x1)
print("Step 2,x shape is:", x2.shape) # Step 2,x shape is: [1, 512, 9, 9]
x3 = conv1(x2)
# PaddleCheckError: Expected input_channels == filter_dims[1] * groups,
# but received input_channels:512 != filter_dims[1] * groups:1024.
# ShapeError: The number of input channels should be equal to
# filter channels * groups. But received: the input channels is [512],
# the shapeof input is [1, 512, 9,9], the filter channel is [1024],
# the shape of filter is [256, 1024, 1, 1],the groups is [1] at
# [D:\1.6.1\paddle\paddle\fluid\operators\conv_op.cc:90]
# Interrupted here , and the scripts path is wrong ,
# My scripts path is something like below:
# "C:\Users\Administrator\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\layers\nn.py
# And instead of it ,I have no "D:\1.6.1\..." dir.
print("Step 3,x shape is:", x3.shape)
x4 = conv2(x3)
print("Step 4,x shape is:", x4.shape)
No.2 Program 未报错的脚本
import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr from paddle.fluid.initializer import Constant from paddle.fluid.regularizer import L2Decay import numpy as np
def BN( input, ch_out, filter_size, stride, padding, name=None):
conv1 = fluid.layers.conv2d(
input=input,
num_filters=ch_out,
filter_size=filter_size,
stride=stride,
padding=padding,
param_attr=ParamAttr(
initializer=fluid.initializer.Normal(0., 0.02)),
bias_attr=False,
act=None)
out = fluid.layers.batch_norm(
input=conv1,
act=None,
param_attr=ParamAttr(
initializer=fluid.initializer.Normal(0., 0.02),
regularizer=L2Decay(0.)),
bias_attr=ParamAttr(
initializer=fluid.initializer.Constant(0.0),
regularizer=L2Decay(0.)))
return out
place = fluid.CPUPlace() exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
x = fluid.layers.ones(shape=[1, 1024, 9, 9], dtype='float32')
x1 = BN(x, 256, 1, 1, 0) x2 = BN(x1, 512,3, 1, 1) x3 = BN(x2, 256, 1, 1, 0) x4 = BN(x3, 512,3, 1, 1)
print("Step 0,x shape is:", x.shape) #Step 0,x shape is: (1, 1024, 9, 9) print("Step 1,x shape is:", x1.shape) #Step 1,x shape is: (1, 256, 9, 9) print("Step 2,x shape is:", x2.shape) #Step 2,x shape is: (1, 512, 9, 9) print("Step 3,x shape is:", x3.shape) #Step 3,x shape is: (1, 256, 9, 9) print("Step 4,x shape is:", x4.shape) #Step 4,x shape is: (1, 512, 9, 9)
All pass and the result is ok
TheTwoPythonProgram.zip 上面的代码复制显示有点问题,请看附件吧 ,谢谢