From 78a73818cba44724db685f3796f0a304a1873d32 Mon Sep 17 00:00:00 2001 From: xzl Date: Fri, 20 Oct 2017 15:39:34 +0800 Subject: [PATCH 1/2] unify the model config --- .../flowers102/mobilenet/mobilenet.py | 56 ++++---- .../flowers102/mobilenet/train.py | 27 +--- .../mobilenet_pruning/mobilenet_pruning.py | 58 ++++---- .../flowers102/mobilenet_pruning/train.py | 34 +---- .../flowers102/resnet18/resnet.py | 17 ++- .../flowers102/resnet18/train.py | 26 +--- .../resnet18_pruning/resnet_pruning.py | 15 ++- .../flowers102/resnet18_pruning/train.py | 27 +--- models/README.md | 11 ++ models/mobilenet.py | 125 ++++++++++++++++++ models/resnet.py | 64 +++++++++ .../demo/mobilenet_with_bn.py | 47 ++++--- .../demo/mobilenet_without_bn.py | 42 +++--- 13 files changed, 344 insertions(+), 205 deletions(-) create mode 100644 models/README.md create mode 100644 models/mobilenet.py create mode 100644 models/resnet.py diff --git a/model_compression/flowers102/mobilenet/mobilenet.py b/model_compression/flowers102/mobilenet/mobilenet.py index ce3de55..d84bffa 100644 --- a/model_compression/flowers102/mobilenet/mobilenet.py +++ b/model_compression/flowers102/mobilenet/mobilenet.py @@ -1,10 +1,10 @@ # edit-mode: -*- python -*- import paddle.v2 as paddle - def conv_bn_layer(input, filter_size, num_filters, stride, padding, channels=None, num_groups=1, - active_type=paddle.activation.Relu()): + active_type=paddle.activation.Relu(), + layer_type=None): """ A wrapper for conv layer with batch normalization layers. Note: @@ -18,43 +18,42 @@ def conv_bn_layer(input, filter_size, num_filters, stride=stride, padding=padding, groups=num_groups, - #act=active_type, act=paddle.activation.Linear(), - bias_attr=False) - #act=active_type, - #bias_attr=True) + bias_attr=False, + layer_type=layer_type) return paddle.layer.batch_norm( input=tmp, act=active_type) - #use_global_stats=False, -def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride): + +def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride, scale): """ """ tmp = conv_bn_layer( input=input, filter_size=3, - num_filters=num_filters1, + num_filters=int(num_filters1*scale), stride=stride, padding=1, - num_groups=num_groups) + num_groups=int(num_groups*scale), layer_type='exconv') tmp = conv_bn_layer( input=tmp, filter_size=1, - num_filters=num_filters2, + num_filters=int(num_filters2*scale), stride=1, padding=0) return tmp -def mobile_net(img): - # For ImageNet - # image_data_layers(224, 1000, True) +def mobile_net(img_size, class_num, scale = 1.0): + + img = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(img_size)) # conv1: 112x112 tmp = conv_bn_layer(img, filter_size=3, channels=3, - num_filters=32, + num_filters=int(32*scale), stride=2, padding=1) @@ -63,57 +62,64 @@ def mobile_net(img): num_filters1=32, num_filters2=64, num_groups=32, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=64, num_filters2=128, num_groups=64, - stride=2) + stride=2, scale = scale) # 28x28 tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=128, num_groups=128, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=256, num_groups=128, - stride=2) + stride=2, scale = scale) # 14x14 tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=256, num_groups=256, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=512, num_groups=256, - stride=2) + stride=2, scale = scale) # 14x14 for i in range(5): tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=512, num_groups=512, - stride=1) + stride=1, scale = scale) # 7x7 tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=1024, num_groups=512, - stride=2) + stride=2, scale = scale) tmp = depthwise_separable(tmp, num_filters1=1024, num_filters2=1024, num_groups=1024, - stride=1) + stride=1, scale = scale) tmp = paddle.layer.img_pool( input=tmp, pool_size=7, stride=1, pool_type=paddle.pooling.Avg()) + out = paddle.layer.fc( + input=tmp, size=class_num, act=paddle.activation.Softmax()) - return tmp + return out + +if __name__ == '__main__': + img_size = 3 * 224 * 224 + data_dim = 102 + out = mobile_net(img_size, data_dim, 1.0) diff --git a/model_compression/flowers102/mobilenet/train.py b/model_compression/flowers102/mobilenet/train.py index b2c9341..fc59e2c 100644 --- a/model_compression/flowers102/mobilenet/train.py +++ b/model_compression/flowers102/mobilenet/train.py @@ -14,11 +14,9 @@ import sys import gzip -from paddle.trainer_config_helpers import * +from paddle.trainer_config_helpers import * import paddle.v2 as paddle - -#from mobilenet_new import mobile_net from mobilenet import mobile_net @@ -26,7 +24,6 @@ def main(): datadim = 3 * 224 * 224 classdim = 102 - # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=1, gpu_id = 1) @@ -35,28 +32,9 @@ def main(): momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH), learning_rate=0.005/ BATCH, - #learning_rate_decay_a=0.1, - #learning_rate_decay_b=50000 * 50, learning_rate_schedule='constant') - image = paddle.layer.data( - name="image", type=paddle.data_type.dense_vector(datadim)) - - net = mobile_net(image) - # option 2. vgg - #net = vgg_bn_drop(image) - - - out = paddle.layer.fc( - input=net, size=classdim, act=paddle.activation.Softmax()) - ''' - out = paddle.layer.img_conv( - input=net, - filter_size=1, - num_filters=classdim, - stride=1, - act=paddle.activation.Linear()) - ''' + out = mobile_net(datadim, classdim, 1.0) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) @@ -104,6 +82,5 @@ def event_handler(event): feeding={'image': 0, 'label': 1}) - if __name__ == '__main__': main() diff --git a/model_compression/flowers102/mobilenet_pruning/mobilenet_pruning.py b/model_compression/flowers102/mobilenet_pruning/mobilenet_pruning.py index 45f769c..31968e6 100644 --- a/model_compression/flowers102/mobilenet_pruning/mobilenet_pruning.py +++ b/model_compression/flowers102/mobilenet_pruning/mobilenet_pruning.py @@ -6,7 +6,8 @@ def conv_bn_layer(input, filter_size, num_filters, stride, padding, channels=None, num_groups=1, - active_type=paddle.activation.Relu(), param_attr=None): + active_type=paddle.activation.Relu(), param_attr=None, + layer_type=None): """ A wrapper for conv layer with batch normalization layers. Note: @@ -22,43 +23,45 @@ def conv_bn_layer(input, filter_size, num_filters, groups=num_groups, param_attr=param_attr, act=paddle.activation.Linear(), - bias_attr=False) - #act=active_type, - #bias_attr=True) + bias_attr=False, + layer_type=layer_type) + return paddle.layer.batch_norm( input=tmp, act=active_type) - #use_global_stats=False, -def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride): + +def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride, scale): """ """ tmp = conv_bn_layer( input=input, filter_size=3, - num_filters=num_filters1, + num_filters=int(num_filters1*scale), stride=stride, padding=1, - num_groups=num_groups) + num_groups=int(num_groups*scale), + layer_type='exconv') pa0 = ParamAttr(update_hooks = Hook('dynamic_pruning', sparsity_upper_bound=0.75)) tmp = conv_bn_layer( input=tmp, filter_size=1, - num_filters=num_filters2, + num_filters=int(num_filters2*scale), stride=1, padding=0, param_attr = pa0) return tmp -def mobile_net(img): - # For ImageNet - # image_data_layers(224, 1000, True) +def mobile_net(img_size, class_num, scale = 1.0): + + img = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(img_size)) # conv1: 112x112 tmp = conv_bn_layer(img, filter_size=3, channels=3, - num_filters=32, + num_filters=int(32*scale), stride=2, padding=1) @@ -67,52 +70,52 @@ def mobile_net(img): num_filters1=32, num_filters2=64, num_groups=32, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=64, num_filters2=128, num_groups=64, - stride=2) + stride=2, scale = scale) # 28x28 tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=128, num_groups=128, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=256, num_groups=128, - stride=2) + stride=2, scale = scale) # 14x14 tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=256, num_groups=256, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=512, num_groups=256, - stride=2) + stride=2, scale = scale) # 14x14 for i in range(5): tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=512, num_groups=512, - stride=1) + stride=1, scale = scale) # 7x7 tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=1024, num_groups=512, - stride=2) + stride=2, scale = scale) tmp = depthwise_separable(tmp, num_filters1=1024, num_filters2=1024, num_groups=1024, - stride=1) + stride=1, scale = scale) tmp = paddle.layer.img_pool( input=tmp, @@ -120,4 +123,13 @@ def mobile_net(img): stride=1, pool_type=paddle.pooling.Avg()) - return tmp + out = paddle.layer.fc( + input=tmp, size=class_num, act=paddle.activation.Softmax(), + param_attr = ParamAttr(update_hooks=Hook('dynamic_pruning', sparsity_upper_bound=0.8))) + + return out + +if __name__ == '__main__': + img_size = 3 * 224 * 224 + data_dim = 102 + out = mobile_net(img_size, data_dim, 1.0) diff --git a/model_compression/flowers102/mobilenet_pruning/train.py b/model_compression/flowers102/mobilenet_pruning/train.py index ac3496d..5a22d88 100644 --- a/model_compression/flowers102/mobilenet_pruning/train.py +++ b/model_compression/flowers102/mobilenet_pruning/train.py @@ -14,52 +14,26 @@ import sys import gzip -from paddle.trainer_config_helpers import * +from paddle.trainer_config_helpers import * import paddle.v2 as paddle - -#from mobilenet_new import mobile_net from mobilenet_pruning import mobile_net -from paddle.v2.attr import Hook -from paddle.v2.attr import ParamAttr - BATCH = 40 def main(): datadim = 3 * 224 * 224 classdim = 102 - # PaddlePaddle init - paddle.init(use_gpu=True, trainer_count=1, gpu_id = 2) + paddle.init(use_gpu=True, trainer_count=1, gpu_id = 0) momentum_optimizer = paddle.optimizer.Momentum( momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH), learning_rate=0.001/ BATCH, - #learning_rate_decay_a=0.1, - #learning_rate_decay_b=50000 * 50, learning_rate_schedule='constant') - image = paddle.layer.data( - name="image", type=paddle.data_type.dense_vector(datadim)) - - net = mobile_net(image) - # option 2. vgg - #net = vgg_bn_drop(image) - - - out = paddle.layer.fc( - input=net, size=classdim, act=paddle.activation.Softmax(), - param_attr = ParamAttr(update_hooks=Hook('dynamic_pruning', sparsity_upper_bound=0.8))) - ''' - out = paddle.layer.img_conv( - input=net, - filter_size=1, - num_filters=classdim, - stride=1, - act=paddle.activation.Linear()) - ''' + out = mobile_net(datadim, classdim, 1.0) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) @@ -67,7 +41,6 @@ def main(): # Create parameters parameters = paddle.parameters.create(cost) - #with gzip.open('Paddle_mobilenet.tar.gz', 'r') as f: with gzip.open('mobilenet_flowers102.tar.gz', 'r') as f: fparameters = paddle.parameters.Parameters.from_tar(f) for param_name in fparameters.names(): @@ -108,6 +81,5 @@ def event_handler(event): feeding={'image': 0, 'label': 1}) - if __name__ == '__main__': main() diff --git a/model_compression/flowers102/resnet18/resnet.py b/model_compression/flowers102/resnet18/resnet.py index d64b2d0..d316dcc 100644 --- a/model_compression/flowers102/resnet18/resnet.py +++ b/model_compression/flowers102/resnet18/resnet.py @@ -1,7 +1,5 @@ import paddle.v2 as paddle -__all__ = ['resnet_imagenet', 'resnet_cifar10'] - def conv_bn_layer(input, ch_out, filter_size, @@ -40,7 +38,9 @@ def layer_warp(block_func, input, ch_in, ch_out, count, stride): conv = block_func(conv, ch_out, ch_out, 1) return conv -def resnet18(input, class_dim, depth=18): +def resnet18(data_dim, class_dim, depth=18): + input = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(data_dim)) cfg = { 18: ([2, 2, 2, 2], basicblock), } @@ -54,6 +54,11 @@ def resnet18(input, class_dim, depth=18): res4 = layer_warp(block_func, res3, 256, 512, stages[3], 2) pool2 = paddle.layer.img_pool( input=res4, pool_size=7, stride=1, pool_type=paddle.pooling.Avg()) - #out = paddle.layer.fc( - # input=pool2, size=class_dim, act=paddle.activation.Softmax()) - return pool2 + out = paddle.layer.fc(name='resnetfc', + input=pool2, size=class_dim, act=paddle.activation.Softmax()) + return out + +if __name__ == '__main__': + data_dim = 3 * 224 * 224 + class_dim = 102 + resnet18(data_dim, class_dim) diff --git a/model_compression/flowers102/resnet18/train.py b/model_compression/flowers102/resnet18/train.py index d19393d..a747b09 100644 --- a/model_compression/flowers102/resnet18/train.py +++ b/model_compression/flowers102/resnet18/train.py @@ -14,18 +14,15 @@ import sys import gzip -from paddle.trainer_config_helpers import * +from paddle.trainer_config_helpers import * import paddle.v2 as paddle - from resnet import resnet18 - BATCH = 40 def main(): datadim = 3 * 224 * 224 classdim = 102 - # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=1, gpu_id = 3) @@ -34,29 +31,12 @@ def main(): momentum=0.9, regularization=paddle.optimizer.L2Regularization(rate=0.0005 * BATCH), learning_rate=0.005/ BATCH, - #learning_rate_decay_a=0.1, - #learning_rate_decay_b=50000 * 50, learning_rate_schedule='constant') - image = paddle.layer.data( - name="image", type=paddle.data_type.dense_vector(datadim)) - - #net = mobile_net(image) - # option 2. vgg - #net = vgg_bn_drop(image) - net = resnet18(image, 102) - + net = resnet18(datadim, classdim) out = paddle.layer.fc(name='resnetfc', input=net, size=classdim, act=paddle.activation.Softmax()) - ''' - out = paddle.layer.img_conv( - input=net, - filter_size=1, - num_filters=classdim, - stride=1, - act=paddle.activation.Linear()) - ''' lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) @@ -65,7 +45,6 @@ def main(): # Create parameters parameters = paddle.parameters.create(cost) with gzip.open('imagenet_pretrained_resnet18.tar.gz', 'r') as f: - #with gzip.open('resnet18_params_pass_32.tar.gz', 'r') as f: fparameters = paddle.parameters.Parameters.from_tar(f) for param_name in fparameters.names(): if param_name in parameters.names(): @@ -105,6 +84,5 @@ def event_handler(event): feeding={'image': 0, 'label': 1}) - if __name__ == '__main__': main() diff --git a/model_compression/flowers102/resnet18_pruning/resnet_pruning.py b/model_compression/flowers102/resnet18_pruning/resnet_pruning.py index 6d57d53..2135764 100644 --- a/model_compression/flowers102/resnet18_pruning/resnet_pruning.py +++ b/model_compression/flowers102/resnet18_pruning/resnet_pruning.py @@ -1,7 +1,5 @@ import paddle.v2 as paddle -__all__ = ['resnet_imagenet', 'resnet_cifar10'] - from paddle.v2.attr import Hook from paddle.v2.attr import ParamAttr @@ -45,7 +43,9 @@ def layer_warp(block_func, input, ch_in, ch_out, count, stride): conv = block_func(conv, ch_out, ch_out, 1) return conv -def resnet18(input, class_dim, depth=18): +def resnet18(data_dim, class_dim, depth=18): + input = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(data_dim)) cfg = { 18: ([2, 2, 2, 2], basicblock), } @@ -59,6 +59,9 @@ def resnet18(input, class_dim, depth=18): res4 = layer_warp(block_func, res3, 256, 512, stages[3], 2) pool2 = paddle.layer.img_pool( input=res4, pool_size=7, stride=1, pool_type=paddle.pooling.Avg()) - #out = paddle.layer.fc( - # input=pool2, size=class_dim, act=paddle.activation.Softmax()) - return pool2 + + out = paddle.layer.fc(name='resnetfc', + input=pool2, size=class_dim, act=paddle.activation.Softmax(), + param_attr = ParamAttr(update_hooks=Hook('dynamic_pruning', + sparsity_upper_bound=0.9))) + return out diff --git a/model_compression/flowers102/resnet18_pruning/train.py b/model_compression/flowers102/resnet18_pruning/train.py index 0fcbff9..855c964 100644 --- a/model_compression/flowers102/resnet18_pruning/train.py +++ b/model_compression/flowers102/resnet18_pruning/train.py @@ -14,13 +14,10 @@ import sys import gzip -from paddle.trainer_config_helpers import * +from paddle.trainer_config_helpers import * import paddle.v2 as paddle - from resnet_pruning import resnet18 -from paddle.v2.attr import Hook -from paddle.v2.attr import ParamAttr BATCH = 40 @@ -40,27 +37,7 @@ def main(): #learning_rate_decay_b=50000 * 50, learning_rate_schedule='constant') - image = paddle.layer.data( - name="image", type=paddle.data_type.dense_vector(datadim)) - - #net = mobile_net(image) - # option 2. vgg - #net = vgg_bn_drop(image) - net = resnet18(image, 102) - - - out = paddle.layer.fc(name='resnetfc', - input=net, size=classdim, act=paddle.activation.Softmax(), - param_attr = ParamAttr(update_hooks=Hook('dynamic_pruning', - sparsity_upper_bound=0.9))) - ''' - out = paddle.layer.img_conv( - input=net, - filter_size=1, - num_filters=classdim, - stride=1, - act=paddle.activation.Linear()) - ''' + out = resnet18(datadim, classdim) lbl = paddle.layer.data( name="label", type=paddle.data_type.integer_value(classdim)) diff --git a/models/README.md b/models/README.md new file mode 100644 index 0000000..2ff5091 --- /dev/null +++ b/models/README.md @@ -0,0 +1,11 @@ +## Models + +Here are the Paddle config file of the commonly used models. At same time, the link to download the pre-trained imagenet models are also provided. + + +### Image Classification + +| Name | Type | Dataset | Download | +|------|------|---------|-------------| +| [Mobilenet]() | image classification | ImageNet |[Download](https://pan.baidu.com/s/1slK3wlB)| +| [Resnet18]() | image classification | ImageNet |[Download](https://pan.baidu.com/s/1gf7rd0v)| diff --git a/models/mobilenet.py b/models/mobilenet.py new file mode 100644 index 0000000..681aae2 --- /dev/null +++ b/models/mobilenet.py @@ -0,0 +1,125 @@ +# edit-mode: -*- python -*- +import paddle.v2 as paddle + +def conv_bn_layer(input, filter_size, num_filters, + stride, padding, channels=None, num_groups=1, + active_type=paddle.activation.Relu(), + layer_type=None): + """ + A wrapper for conv layer with batch normalization layers. + Note: + conv layer has no activation. + """ + tmp = paddle.layer.img_conv( + input=input, + filter_size=filter_size, + num_channels=channels, + num_filters=num_filters, + stride=stride, + padding=padding, + groups=num_groups, + act=paddle.activation.Linear(), + bias_attr=False, + layer_type=layer_type) + return paddle.layer.batch_norm( + input=tmp, + act=active_type) + +def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride, scale): + """ + """ + tmp = conv_bn_layer( + input=input, + filter_size=3, + num_filters=int(num_filters1*scale), + stride=stride, + padding=1, + num_groups=int(num_groups*scale), layer_type='exconv') + + tmp = conv_bn_layer( + input=tmp, + filter_size=1, + num_filters=int(num_filters2*scale), + stride=1, + padding=0) + return tmp + +def mobile_net(img_size, class_num, scale = 1.0): + + img = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(img_size)) + + # conv1: 112x112 + tmp = conv_bn_layer(img, + filter_size=3, + channels=3, + num_filters=int(32*scale), + stride=2, + padding=1) + + # 56x56 + tmp = depthwise_separable(tmp, + num_filters1=32, + num_filters2=64, + num_groups=32, + stride=1, scale = scale) + tmp = depthwise_separable(tmp, + num_filters1=64, + num_filters2=128, + num_groups=64, + stride=2, scale = scale) + # 28x28 + tmp = depthwise_separable(tmp, + num_filters1=128, + num_filters2=128, + num_groups=128, + stride=1, scale = scale) + tmp = depthwise_separable(tmp, + num_filters1=128, + num_filters2=256, + num_groups=128, + stride=2, scale = scale) + # 14x14 + tmp = depthwise_separable(tmp, + num_filters1=256, + num_filters2=256, + num_groups=256, + stride=1, scale = scale) + tmp = depthwise_separable(tmp, + num_filters1=256, + num_filters2=512, + num_groups=256, + stride=2, scale = scale) + # 14x14 + for i in range(5): + tmp = depthwise_separable(tmp, + num_filters1=512, + num_filters2=512, + num_groups=512, + stride=1, scale = scale) + # 7x7 + tmp = depthwise_separable(tmp, + num_filters1=512, + num_filters2=1024, + num_groups=512, + stride=2, scale = scale) + tmp = depthwise_separable(tmp, + num_filters1=1024, + num_filters2=1024, + num_groups=1024, + stride=1, scale = scale) + + tmp = paddle.layer.img_pool( + input=tmp, + pool_size=7, + stride=1, + pool_type=paddle.pooling.Avg()) + out = paddle.layer.fc( + input=tmp, size=class_num, act=paddle.activation.Softmax()) + + return out + +if __name__ == '__main__': + img_size = 3 * 224 * 224 + data_dim = 1000 + out = mobile_net(img_size, data_dim, 1.0) diff --git a/models/resnet.py b/models/resnet.py new file mode 100644 index 0000000..c0b7d32 --- /dev/null +++ b/models/resnet.py @@ -0,0 +1,64 @@ +import paddle.v2 as paddle + +def conv_bn_layer(input, + ch_out, + filter_size, + stride, + padding, + active_type=paddle.activation.Relu(), + ch_in=None): + + tmp = paddle.layer.img_conv( + input=input, + filter_size=filter_size, + num_channels=ch_in, + num_filters=ch_out, + stride=stride, + padding=padding, + act=paddle.activation.Linear(), + bias_attr=False) + return paddle.layer.batch_norm(input=tmp, act=active_type) + +def shortcut(input, ch_in, ch_out, stride): + if ch_in != ch_out: + return conv_bn_layer(input, ch_out, 1, stride, 0, paddle.activation.Linear()) + else: + return input + +def basicblock(input, ch_in, ch_out, stride): + short = shortcut(input, ch_in, ch_out, stride) + conv1 = conv_bn_layer(input, ch_out, 3, stride, 1) + conv2 = conv_bn_layer(conv1, ch_out, 3, 1, 1, paddle.activation.Linear()) + return paddle.layer.addto( + input=[short, conv2], act=paddle.activation.Relu()) + +def layer_warp(block_func, input, ch_in, ch_out, count, stride): + conv = block_func(input, ch_in, ch_out, stride) + for i in range(1, count): + conv = block_func(conv, ch_out, ch_out, 1) + return conv + +def resnet18(data_dim, class_dim, depth=18): + input = paddle.layer.data( + name="image", type=paddle.data_type.dense_vector(data_dim)) + cfg = { + 18: ([2, 2, 2, 2], basicblock), + } + stages, block_func = cfg[depth] + conv1 = conv_bn_layer( + 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) + res1 = layer_warp(block_func, pool1, 64, 64, stages[0], 1) + res2 = layer_warp(block_func, res1, 64, 128, stages[1], 2) + res3 = layer_warp(block_func, res2, 128, 256, stages[2], 2) + res4 = layer_warp(block_func, res3, 256, 512, stages[3], 2) + pool2 = paddle.layer.img_pool( + input=res4, pool_size=7, stride=1, pool_type=paddle.pooling.Avg()) + out = paddle.layer.fc( + input=pool2, size=class_dim, act=paddle.activation.Softmax()) + return out + +if __name__ == '__main__': + data_dim = 3 * 224 * 224 + class_dim = 1000 + resnet18(data_dim, class_dim) diff --git a/tool/merge_batch_normalization/demo/mobilenet_with_bn.py b/tool/merge_batch_normalization/demo/mobilenet_with_bn.py index 63d8ebb..d84bffa 100644 --- a/tool/merge_batch_normalization/demo/mobilenet_with_bn.py +++ b/tool/merge_batch_normalization/demo/mobilenet_with_bn.py @@ -1,10 +1,10 @@ # edit-mode: -*- python -*- import paddle.v2 as paddle - def conv_bn_layer(input, filter_size, num_filters, stride, padding, channels=None, num_groups=1, - active_type=paddle.activation.Relu()): + active_type=paddle.activation.Relu(), + layer_type=None): """ A wrapper for conv layer with batch normalization layers. Note: @@ -18,35 +18,33 @@ def conv_bn_layer(input, filter_size, num_filters, stride=stride, padding=padding, groups=num_groups, - #act=active_type, act=paddle.activation.Linear(), - bias_attr=False) - #act=active_type, - #bias_attr=True) + bias_attr=False, + layer_type=layer_type) return paddle.layer.batch_norm( input=tmp, act=active_type) - #use_global_stats=False, -def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride): + +def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride, scale): """ """ tmp = conv_bn_layer( input=input, filter_size=3, - num_filters=num_filters1, + num_filters=int(num_filters1*scale), stride=stride, padding=1, - num_groups=num_groups) + num_groups=int(num_groups*scale), layer_type='exconv') tmp = conv_bn_layer( input=tmp, filter_size=1, - num_filters=num_filters2, + num_filters=int(num_filters2*scale), stride=1, padding=0) return tmp -def mobile_net(img_size, class_num): +def mobile_net(img_size, class_num, scale = 1.0): img = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(img_size)) @@ -55,7 +53,7 @@ def mobile_net(img_size, class_num): tmp = conv_bn_layer(img, filter_size=3, channels=3, - num_filters=32, + num_filters=int(32*scale), stride=2, padding=1) @@ -64,52 +62,52 @@ def mobile_net(img_size, class_num): num_filters1=32, num_filters2=64, num_groups=32, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=64, num_filters2=128, num_groups=64, - stride=2) + stride=2, scale = scale) # 28x28 tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=128, num_groups=128, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=256, num_groups=128, - stride=2) + stride=2, scale = scale) # 14x14 tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=256, num_groups=256, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=512, num_groups=256, - stride=2) + stride=2, scale = scale) # 14x14 for i in range(5): tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=512, num_groups=512, - stride=1) + stride=1, scale = scale) # 7x7 tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=1024, num_groups=512, - stride=2) + stride=2, scale = scale) tmp = depthwise_separable(tmp, num_filters1=1024, num_filters2=1024, num_groups=1024, - stride=1) + stride=1, scale = scale) tmp = paddle.layer.img_pool( input=tmp, @@ -120,3 +118,8 @@ def mobile_net(img_size, class_num): input=tmp, size=class_num, act=paddle.activation.Softmax()) return out + +if __name__ == '__main__': + img_size = 3 * 224 * 224 + data_dim = 102 + out = mobile_net(img_size, data_dim, 1.0) diff --git a/tool/merge_batch_normalization/demo/mobilenet_without_bn.py b/tool/merge_batch_normalization/demo/mobilenet_without_bn.py index d45372c..dcf887c 100644 --- a/tool/merge_batch_normalization/demo/mobilenet_without_bn.py +++ b/tool/merge_batch_normalization/demo/mobilenet_without_bn.py @@ -1,10 +1,10 @@ # edit-mode: -*- python -*- import paddle.v2 as paddle - def conv_bn_layer(input, filter_size, num_filters, stride, padding, channels=None, num_groups=1, - active_type=paddle.activation.Relu()): + active_type=paddle.activation.Relu(), + layer_type=None): """ A wrapper for conv layer with batch normalization layers. Note: @@ -22,31 +22,32 @@ def conv_bn_layer(input, filter_size, num_filters, # is paddle.activation.Linear() act=active_type, # !!! the bias_attr in origin network is False - bias_attr=True) + bias_attr=True, + layer_type=layer_type) # !!! we have deleted the batch_norm layer here. return tmp -def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride): +def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride, scale): """ """ tmp = conv_bn_layer( input=input, filter_size=3, - num_filters=num_filters1, + num_filters=int(num_filters1*scale), stride=stride, padding=1, - num_groups=num_groups) + num_groups=int(num_groups*scale), layer_type='exconv') tmp = conv_bn_layer( input=tmp, filter_size=1, - num_filters=num_filters2, + num_filters=int(num_filters2*scale), stride=1, padding=0) return tmp -def mobile_net(img_size, class_num): +def mobile_net(img_size, class_num, scale = 1.0): img = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(img_size)) @@ -55,7 +56,7 @@ def mobile_net(img_size, class_num): tmp = conv_bn_layer(img, filter_size=3, channels=3, - num_filters=32, + num_filters=int(32*scale), stride=2, padding=1) @@ -64,52 +65,52 @@ def mobile_net(img_size, class_num): num_filters1=32, num_filters2=64, num_groups=32, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=64, num_filters2=128, num_groups=64, - stride=2) + stride=2, scale = scale) # 28x28 tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=128, num_groups=128, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=128, num_filters2=256, num_groups=128, - stride=2) + stride=2, scale = scale) # 14x14 tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=256, num_groups=256, - stride=1) + stride=1, scale = scale) tmp = depthwise_separable(tmp, num_filters1=256, num_filters2=512, num_groups=256, - stride=2) + stride=2, scale = scale) # 14x14 for i in range(5): tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=512, num_groups=512, - stride=1) + stride=1, scale = scale) # 7x7 tmp = depthwise_separable(tmp, num_filters1=512, num_filters2=1024, num_groups=512, - stride=2) + stride=2, scale = scale) tmp = depthwise_separable(tmp, num_filters1=1024, num_filters2=1024, num_groups=1024, - stride=1) + stride=1, scale = scale) tmp = paddle.layer.img_pool( input=tmp, @@ -120,3 +121,8 @@ def mobile_net(img_size, class_num): input=tmp, size=class_num, act=paddle.activation.Softmax()) return out + +if __name__ == '__main__': + img_size = 3 * 224 * 224 + data_dim = 102 + out = mobile_net(img_size, data_dim, 1.0) From 91c61aec091f0d1fad03a6d8c183a4e1a4b50d8d Mon Sep 17 00:00:00 2001 From: xzl Date: Mon, 23 Oct 2017 15:09:05 +0800 Subject: [PATCH 2/2] modify readme in ./models --- models/README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/models/README.md b/models/README.md index 2ff5091..dad97e5 100644 --- a/models/README.md +++ b/models/README.md @@ -1,11 +1,7 @@ ## Models -Here are the Paddle config file of the commonly used models. At same time, the link to download the pre-trained imagenet models are also provided. - +Here are the Paddle config file of the commonly used models. ### Image Classification - -| Name | Type | Dataset | Download | -|------|------|---------|-------------| -| [Mobilenet]() | image classification | ImageNet |[Download](https://pan.baidu.com/s/1slK3wlB)| -| [Resnet18]() | image classification | ImageNet |[Download](https://pan.baidu.com/s/1gf7rd0v)| +- MobileNet +- Resnet18