提交 14967bb0 编写于 作者: C Cao Ying 提交者: GitHub

Merge pull request #312 from guoshengCS/fix-resnet

Fix ResNet in image classification.
...@@ -22,24 +22,24 @@ def conv_bn_layer(input, ...@@ -22,24 +22,24 @@ def conv_bn_layer(input,
return paddle.layer.batch_norm(input=tmp, act=active_type) return paddle.layer.batch_norm(input=tmp, act=active_type)
def shortcut(input, ch_in, ch_out, stride): def shortcut(input, ch_out, stride):
if ch_in != ch_out: if input.num_filters != ch_out:
return conv_bn_layer(input, ch_out, 1, stride, 0, return conv_bn_layer(input, ch_out, 1, stride, 0,
paddle.activation.Linear()) paddle.activation.Linear())
else: else:
return input return input
def basicblock(input, ch_in, ch_out, stride): def basicblock(input, ch_out, stride):
short = shortcut(input, ch_in, ch_out, stride) short = shortcut(input, ch_out, stride)
conv1 = conv_bn_layer(input, ch_out, 3, stride, 1) conv1 = conv_bn_layer(input, ch_out, 3, stride, 1)
conv2 = conv_bn_layer(conv1, ch_out, 3, 1, 1, paddle.activation.Linear()) conv2 = conv_bn_layer(conv1, ch_out, 3, 1, 1, paddle.activation.Linear())
return paddle.layer.addto( return paddle.layer.addto(
input=[short, conv2], act=paddle.activation.Relu()) input=[short, conv2], act=paddle.activation.Relu())
def bottleneck(input, ch_in, ch_out, stride): def bottleneck(input, ch_out, stride):
short = shortcut(input, ch_in, ch_out * 4, stride) short = shortcut(input, ch_out * 4, stride)
conv1 = conv_bn_layer(input, ch_out, 1, stride, 0) conv1 = conv_bn_layer(input, ch_out, 1, stride, 0)
conv2 = conv_bn_layer(conv1, ch_out, 3, 1, 1) conv2 = conv_bn_layer(conv1, ch_out, 3, 1, 1)
conv3 = conv_bn_layer(conv2, ch_out * 4, 1, 1, 0, conv3 = conv_bn_layer(conv2, ch_out * 4, 1, 1, 0,
...@@ -48,10 +48,10 @@ def bottleneck(input, ch_in, ch_out, stride): ...@@ -48,10 +48,10 @@ def bottleneck(input, ch_in, ch_out, stride):
input=[short, conv3], act=paddle.activation.Relu()) input=[short, conv3], act=paddle.activation.Relu())
def layer_warp(block_func, input, ch_in, ch_out, count, stride): def layer_warp(block_func, input, ch_out, count, stride):
conv = block_func(input, ch_in, ch_out, stride) conv = block_func(input, ch_out, stride)
for i in range(1, count): for i in range(1, count):
conv = block_func(conv, ch_out, ch_out, 1) conv = block_func(conv, ch_out, 1)
return conv return conv
...@@ -67,10 +67,10 @@ def resnet_imagenet(input, class_dim, depth=50): ...@@ -67,10 +67,10 @@ def resnet_imagenet(input, class_dim, depth=50):
conv1 = conv_bn_layer( conv1 = conv_bn_layer(
input, ch_in=3, ch_out=64, filter_size=7, stride=2, padding=3) input, ch_in=3, ch_out=64, filter_size=7, stride=2, padding=3)
pool1 = paddle.layer.img_pool(input=conv1, pool_size=3, stride=2) pool1 = paddle.layer.img_pool(input=conv1, pool_size=3, stride=2)
res1 = layer_warp(block_func, pool1, 64, 64, stages[0], 1) res1 = layer_warp(block_func, pool1, 64, stages[0], 1)
res2 = layer_warp(block_func, res1, 64, 128, stages[1], 2) res2 = layer_warp(block_func, res1, 128, stages[1], 2)
res3 = layer_warp(block_func, res2, 128, 256, stages[2], 2) res3 = layer_warp(block_func, res2, 256, stages[2], 2)
res4 = layer_warp(block_func, res3, 256, 512, stages[3], 2) res4 = layer_warp(block_func, res3, 512, stages[3], 2)
pool2 = paddle.layer.img_pool( pool2 = paddle.layer.img_pool(
input=res4, pool_size=7, stride=1, pool_type=paddle.pooling.Avg()) input=res4, pool_size=7, stride=1, pool_type=paddle.pooling.Avg())
out = paddle.layer.fc( out = paddle.layer.fc(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册