Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

unify the model config #14

Merged
merged 2 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions model_compression/flowers102/mobilenet/mobilenet.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)

Expand All @@ -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)
27 changes: 2 additions & 25 deletions model_compression/flowers102/mobilenet/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@

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


BATCH = 40
def main():
datadim = 3 * 224 * 224
classdim = 102


# PaddlePaddle init
paddle.init(use_gpu=True, trainer_count=1, gpu_id = 1)
Expand All @@ -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))
Expand Down Expand Up @@ -104,6 +82,5 @@ def event_handler(event):
feeding={'image': 0,
'label': 1})


if __name__ == '__main__':
main()
58 changes: 35 additions & 23 deletions model_compression/flowers102/mobilenet_pruning/mobilenet_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand All @@ -67,57 +70,66 @@ 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())

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)
Loading