alexnet.py 2.1 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4
#!/usr/bin/env python

from paddle.trainer_config_helpers import *

5 6
height = 227
width = 227
D
dangqingqing 已提交
7
num_class = 1000
8
batch_size = get_config_arg('batch_size', int, 128)
T
tensor-tang 已提交
9
use_mkldnn = get_config_arg('use_mkldnn', bool, False)
T
tensor-tang 已提交
10 11
is_infer = get_config_arg("is_infer", bool, False)
num_samples = get_config_arg('num_samples', int, 2560)
D
dangqingqing 已提交
12

T
tensor-tang 已提交
13 14 15 16 17 18 19 20
args = {
    'height': height,
    'width': width,
    'color': True,
    'num_class': num_class,
    'is_infer': is_infer,
    'num_samples': num_samples
}
21 22
define_py_data_sources2(
    "train.list", None, module="provider", obj="process", args=args)
D
dangqingqing 已提交
23 24

settings(
25 26 27 28
    batch_size=batch_size,
    learning_rate=0.01 / batch_size,
    learning_method=MomentumOptimizer(0.9),
    regularization=L2Regularization(0.0005 * batch_size))
D
dangqingqing 已提交
29 30 31

# conv1
net = data_layer('data', size=height * width * 3)
32 33 34 35 36 37 38
net = img_conv_layer(
    input=net,
    filter_size=11,
    num_channels=3,
    num_filters=96,
    stride=4,
    padding=1)
D
dangqingqing 已提交
39
net = img_cmrnorm_layer(input=net, size=5, scale=0.0001, power=0.75)
40
net = img_pool_layer(input=net, pool_size=3, stride=2)
D
dangqingqing 已提交
41 42

# conv2
43
net = img_conv_layer(
T
tensor-tang 已提交
44 45 46 47 48 49
    input=net,
    filter_size=5,
    num_filters=256,
    stride=1,
    padding=2,
    groups=2 if use_mkldnn else 1)
D
dangqingqing 已提交
50 51 52 53
net = img_cmrnorm_layer(input=net, size=5, scale=0.0001, power=0.75)
net = img_pool_layer(input=net, pool_size=3, stride=2)

# conv3
54 55
net = img_conv_layer(
    input=net, filter_size=3, num_filters=384, stride=1, padding=1)
D
dangqingqing 已提交
56
# conv4
57
net = img_conv_layer(
T
tensor-tang 已提交
58 59 60 61 62 63
    input=net,
    filter_size=3,
    num_filters=384,
    stride=1,
    padding=1,
    groups=2 if use_mkldnn else 1)
D
dangqingqing 已提交
64 65

# conv5
66
net = img_conv_layer(
T
tensor-tang 已提交
67 68 69 70 71 72
    input=net,
    filter_size=3,
    num_filters=256,
    stride=1,
    padding=1,
    groups=2 if use_mkldnn else 1)
D
dangqingqing 已提交
73 74
net = img_pool_layer(input=net, pool_size=3, stride=2)

75 76 77 78 79 80 81 82 83 84
net = fc_layer(
    input=net,
    size=4096,
    act=ReluActivation(),
    layer_attr=ExtraAttr(drop_rate=0.5))
net = fc_layer(
    input=net,
    size=4096,
    act=ReluActivation(),
    layer_attr=ExtraAttr(drop_rate=0.5))
D
dangqingqing 已提交
85 86 87
net = fc_layer(input=net, size=1000, act=SoftmaxActivation())

lab = data_layer('label', num_class)
88
loss = cross_entropy(input=net, label=lab)
D
dangqingqing 已提交
89
outputs(loss)