alexnet.py 2.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
D
dangqingqing 已提交
14 15 16

from paddle.trainer_config_helpers import *

17 18
height = 227
width = 227
D
dangqingqing 已提交
19
num_class = 1000
20
batch_size = get_config_arg('batch_size', int, 128)
T
tensor-tang 已提交
21
gp = get_config_arg('layer_num', int, 1)
T
tensor-tang 已提交
22 23
is_infer = get_config_arg("is_infer", bool, False)
num_samples = get_config_arg('num_samples', int, 2560)
D
dangqingqing 已提交
24

T
tensor-tang 已提交
25 26 27 28 29 30 31 32
args = {
    'height': height,
    'width': width,
    'color': True,
    'num_class': num_class,
    'is_infer': is_infer,
    'num_samples': num_samples
}
33
define_py_data_sources2(
34 35 36 37 38
    "train.list" if not is_infer else None,
    "test.list" if is_infer else None,
    module="provider",
    obj="process",
    args=args)
D
dangqingqing 已提交
39 40

settings(
41 42 43 44
    batch_size=batch_size,
    learning_rate=0.01 / batch_size,
    learning_method=MomentumOptimizer(0.9),
    regularization=L2Regularization(0.0005 * batch_size))
D
dangqingqing 已提交
45 46 47

# conv1
net = data_layer('data', size=height * width * 3)
48 49 50 51 52 53 54
net = img_conv_layer(
    input=net,
    filter_size=11,
    num_channels=3,
    num_filters=96,
    stride=4,
    padding=1)
D
dangqingqing 已提交
55
net = img_cmrnorm_layer(input=net, size=5, scale=0.0001, power=0.75)
56
net = img_pool_layer(input=net, pool_size=3, stride=2)
D
dangqingqing 已提交
57 58

# conv2
59
net = img_conv_layer(
T
tensor-tang 已提交
60
    input=net, filter_size=5, num_filters=256, stride=1, padding=2, groups=gp)
D
dangqingqing 已提交
61 62 63 64
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
65 66
net = img_conv_layer(
    input=net, filter_size=3, num_filters=384, stride=1, padding=1)
D
dangqingqing 已提交
67
# conv4
68
net = img_conv_layer(
T
tensor-tang 已提交
69
    input=net, filter_size=3, num_filters=384, stride=1, padding=1, groups=gp)
D
dangqingqing 已提交
70 71

# conv5
72
net = img_conv_layer(
T
tensor-tang 已提交
73
    input=net, filter_size=3, num_filters=256, stride=1, padding=1, groups=gp)
D
dangqingqing 已提交
74 75
net = img_pool_layer(input=net, pool_size=3, stride=2)

76 77 78 79 80 81 82 83 84 85
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 已提交
86 87
net = fc_layer(input=net, size=1000, act=SoftmaxActivation())

T
tensor-tang 已提交
88 89 90 91 92 93
if is_infer:
    outputs(net)
else:
    lab = data_layer('label', num_class)
    loss = cross_entropy(input=net, label=lab)
    outputs(loss)