训练VGG模型时报TypeError(动态图)
Created by: ENGWayne
python版本:python3.7 框架版本:PaddlePaddle 1.7.1 环境:AI studio GPU版本 定义好VGG模型后,运行如下代码:
if __name__ == '__main__':
# 创建模型
with fluid.dygraph.guard():
model = VGG("VGG", num_classes=3)
train(model, augment='imgaugLib') # 使用imgaug库进行数据増广的训练
报错:
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-9-ccf1dc315009> in <module>
95 # train(model) # 不使用数据増广的训练
96 with fluid.dygraph.guard():
---> 97 model = VGG("VGG", num_classes=3)
98
99 train(model, augment='imgaugLib') # 使用imgaug库进行数据増广的训练
<ipython-input-8-3792535017df> in __init__(self, name_scope, conv_arch, num_classes)
29 for (num_convs, num_channels) in conv_arch:
30 block = self.add_sublayer('block_' + str(iter_id),
---> 31 vgg_block(self.full_name(), num_convs, num_channels))
32 self.vgg_blocks.append(block)
33 iter_id += 1
<ipython-input-8-3792535017df> in __init__(self, name_scope, num_convs, num_channels)
11 for i in range(num_convs):
12 conv_layer = self.add_sublayer('conv_' + str(i), Conv2D(self.full_name(),
---> 13 num_filters=num_channels, filter_size=3, padding=1, act='relu'))
14 self.conv_list.append(conv_layer)
15 self.pool = Pool2D(self.full_name(), pool_stride=2, pool_size = 2, pool_type='max')
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py in __init__(self, num_channels, num_filters, filter_size, stride, padding, dilation, groups, param_attr, bias_attr, use_cudnn, act, dtype)
209 shape=filter_shape,
210 dtype=self._dtype,
--> 211 default_initializer=_get_default_param_initializer())
212
213 self.bias = self.create_parameter(
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py in _get_default_param_initializer()
202 filter_elem_num = filter_size[0] * filter_size[
203 1] * self._num_channels
--> 204 std = (2.0 / filter_elem_num)**0.5
205 return Normal(0.0, std, 0)
206
TypeError: unsupported operand type(s) for /: 'float' and 'str'
我的VGG网络定义如下:
class vgg_block(fluid.dygraph.Layer):
def __init__(self, name_scope, num_convs, num_channels):
"""
num_convs, 卷积层的数目
num_channels, 卷积层的输出通道数,在同一个Incepition块内,卷积层输出通道数是一样的
"""
super(vgg_block, self).__init__(name_scope)
self.conv_list = []
for i in range(num_convs):
conv_layer = self.add_sublayer('conv_' + str(i), Conv2D(self.full_name(),
num_filters=num_channels, filter_size=3, padding=1, act='relu'))
self.conv_list.append(conv_layer)
self.pool = Pool2D(self.full_name(), pool_stride=2, pool_size = 2, pool_type='max')
def forward(self, x):
for item in self.conv_list:
x = item(x)
return self.pool(x)
class VGG(fluid.dygraph.Layer):
def __init__(self, name_scope, conv_arch=((2, 64),
(2, 128), (3, 256), (3, 512), (3, 512)), num_classes=1):
super(VGG, self).__init__(name_scope)
self.vgg_blocks=[]
iter_id = 0
# 添加vgg_block
# 这里一共5个vgg_block,每个block里面的卷积层数目和输出通道数由conv_arch指定
for (num_convs, num_channels) in conv_arch:
block = self.add_sublayer('block_' + str(iter_id),
vgg_block(self.full_name(), num_convs, num_channels))
self.vgg_blocks.append(block)
iter_id += 1
self.fc1 = Linear(self.full_name(),
size=4096,
act='relu')
self.drop1_ratio = 0.5
self.fc2= Linear(self.full_name(),
size=4096,
act='relu')
self.drop2_ratio = 0.5
self.fc3 = Linear(self.full_name(),
size=num_classes,
)
def forward(self, x):
for item in self.vgg_blocks:
x = item(x)
x = fluid.layers.dropout(self.fc1(x), self.drop1_ratio)
x = fluid.layers.dropout(self.fc2(x), self.drop2_ratio)
x = self.fc3(x)
return x
请教是哪里的问题?