'function' object has no attribute 'dtype'
Created by: wangzhankun
建立issue时,为快速解决问题,请您根据使用情况给出如下信息:
- 标题:简洁、精准描述您的问题,例如“ssd 模型前置lstm报错 ”
- 版本、环境信息: 1)PaddlePaddle版本:1.6.3和1.7.2 2)CPU:i7-7500U 3)GPU:940MX 4)系统环境:windows10, python3.7.6
- 模型信息 1)模型名称 unet
错误描述
在本地自定义的unet网络出现'function' object has no attribute 'dtype'
报错。
网络模型:
# 定义U-net网络中的上采样步骤
def up_conv(x, output_channel, factor):
w_attr = fluid.param_attr.ParamAttr(
learning_rate=0.,
regularizer=fluid.regularizer.L2Decay(0.),
initializer=fluid.initializer.Bilinear())
conv_up = fluid.layers.conv2d_transpose(
input=x,
num_filters=output_channel,
output_size=None,
filter_size=2 * factor - factor % 2,
padding=int(math.ceil((factor - 1) / 2.)),
stride=factor,
groups=output_channel,
param_attr=w_attr,
bias_attr=False)
return conv_up
# 定义U-net网络
def unet(input1):
conv1 = fluid.layers.conv2d(
input1, num_filters=64, filter_size=3, act="relu", padding=1)
conv1 = fluid.layers.conv2d(
conv1, num_filters=64, filter_size=3, act="relu", padding=1)
pool1 = fluid.layers.pool2d(
conv1, pool_size=2, pool_type="max", pool_stride=2)
conv2 = fluid.layers.conv2d(
pool1, num_filters=128, filter_size=3, act="relu", padding=1)
conv2 = fluid.layers.conv2d(
conv2, num_filters=128, filter_size=3, act="relu", padding=1)
pool2 = fluid.layers.pool2d(
conv2, pool_size=2, pool_type="max", pool_stride=2)
conv3 = fluid.layers.conv2d(
pool2, num_filters=256, filter_size=3, act="relu", padding=1)
conv3 = fluid.layers.conv2d(
conv3, num_filters=256, filter_size=3, act="relu", padding=1)
pool3 = fluid.layers.pool2d(
conv3, pool_size=2, pool_type="max", pool_stride=2)
conv4 = fluid.layers.conv2d(
pool3, num_filters=512, filter_size=3, act="relu", padding=1)
conv4 = fluid.layers.conv2d(
conv4, num_filters=512, filter_size=3, act="relu", padding=1)
drop4 = fluid.layers.dropout(conv4, dropout_prob=0.5)
pool4 = fluid.layers.pool2d(
drop4, pool_size=2, pool_type="max", pool_stride=2)
conv5 = fluid.layers.conv2d(
pool4, num_filters=1024, filter_size=3, act="relu", padding=1)
conv5 = fluid.layers.conv2d(
conv5, num_filters=1024, filter_size=3, act="relu", padding=1)
drop5 = fluid.layers.dropout(conv5, dropout_prob=0.5)
up1 = up_conv(drop5, 512, 2)
merg1 = fluid.layers.concat(input=[conv4, up1], axis=1)
conv6 = fluid.layers.conv2d(
merg1, num_filters=512, filter_size=3, act="relu", padding=1)
conv6 = fluid.layers.conv2d(
conv6, num_filters=512, filter_size=3, act="relu", padding=1)
up2 = up_conv(conv6, 256, 2)
merg2 = fluid.layers.concat(input=[conv3, up2], axis=1)
conv7 = fluid.layers.conv2d(
merg2, num_filters=256, filter_size=3, act="leaky_relu", padding=1)
conv7 = fluid.layers.conv2d(
conv7, num_filters=256, filter_size=3, act="relu", padding=1)
up3 = up_conv(conv7, 128, 2)
merg3 = fluid.layers.concat(input=[conv2, up3], axis=1)
conv8 = fluid.layers.conv2d(
merg3, num_filters=128, filter_size=3, act="relu", padding=1)
conv8 = fluid.layers.conv2d(
conv8, num_filters=128, filter_size=3, act="relu", padding=1)
up4 = up_conv(conv8, 64, 2)
merg4 = fluid.layers.concat(input=[conv1, up4], axis=1)
conv9 = fluid.layers.conv2d(
merg4, num_filters=64, filter_size=3, act="relu", padding=1)
conv9 = fluid.layers.conv2d(
conv9, num_filters=64, filter_size=3, act="relu", padding=1)
conv10 = fluid.layers.conv2d(
conv9, num_filters=3, filter_size=3, act="relu", padding=1)
output = fluid.layers.conv2d(
conv10, num_filters=1, filter_size=1, act="relu")
# 用的灰度图像,所以这里最后是输出1通道的,要是用RGB图片,就把num_filters改成3
return output
报错信息:
# In[8]:...
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
g:\test\main.py in
693 resnet128_001_30_1_ = CNN(
----> 694 BATCH_SIZE=128, learning_rate=0.001, EPOCH_NUM=3, use_cuda=False, network=4)
g:\test\main.py in __init__(self, BATCH_SIZE, learning_rate, EPOCH_NUM, use_cuda, network, optimizer)
411 self.cnn_ = 0 # 无意义,用于声明
412 self.__optimizer_type_ = optimizer
---> 413 self.__config()
414
415 self.train_iters_ = []
g:\test\main.py in __config(self)
569
570 # 神经网络
--> 571 self.cnn_ = self.__network(images)
572
573 # cost
g:\test\main.py in __network(self, img)
522
523 if self.__network_ == 4:
--> 524 output = unet(img)
525
526
g:\test\main.py in unet(input1)
351 conv5, num_filters=1024, filter_size=3, act="relu", padding=1)
352 drop5 = fluid.layers.dropout(conv5, dropout_prob=0.5)
---> 353 up1 = up_conv(drop5, 512, 2)
354 merg1 = fluid.layers.concat(input=[conv4, up1], axis=1)
355
g:\test\main.py in up_conv(x, output_channel, factor)
316 groups=output_channel,
317 param_attr=w_attr,
---> 318 bias_attr=False)
319 return conv_up
320 # 定义U-net网络
D:\Anaconda3\envs\pp\lib\site-packages\paddle\fluid\layers\nn.py in conv2d_transpose(input, num_filters, output_size, filter_size, padding, stride, dilation, groups, param_attr, bias_attr, use_cudnn, act, name, data_format)
5176
5177 img_filter = helper.create_parameter(
-> 5178 dtype=input.dtype, shape=filter_shape, attr=helper.param_attr)
5179
5180 pre_bias = helper.create_variable_for_type_inference(dtype=input.dtype)
D:\Anaconda3\envs\pp\lib\site-packages\paddle\fluid\layer_helper_base.py in create_parameter(self, attr, shape, dtype, is_bias, default_initializer, stop_gradient)
328 dtype=dtype,
329 shape=shape,
--> 330 **attr._to_kwargs(with_initializer=True))
331 return self.main_program.global_block().create_parameter(
332 dtype=dtype, shape=shape, **attr._to_kwargs())
D:\Anaconda3\envs\pp\lib\site-packages\paddle\fluid\framework.py in create_parameter(self, *args, **kwargs)
2437 pass
2438 else:
-> 2439 initializer(param, self)
2440 param.stop_gradient = False
2441 return param
D:\Anaconda3\envs\pp\lib\site-packages\paddle\fluid\initializer.py in __call__(self, var, block)
837 values = [float(v) for v in weight.flat]
838 else:
--> 839 raise ValueError("Unsupported dtype %s", input.dtype)
840 if np.prod(shape) > 1024 * 1024:
841 raise ValueError("The size of input is too big. ")
AttributeError: 'function' object has no attribute 'dtype'