light_mnist.py 2.7 KB
Newer Older
Z
zhuoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

Z
zhuoyuan 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
from paddle.trainer_config_helpers import *

is_predict = get_config_arg("is_predict", bool, False)

####################Data Configuration ##################

if not is_predict:
    data_dir = './data/'
    define_py_data_sources2(
        train_list=data_dir + 'train.list',
        test_list=data_dir + 'test.list',
        module='mnist_provider',
        obj='process')

######################Algorithm Configuration #############
Z
zhuoyuan 已提交
30
settings(batch_size=50, learning_rate=0.001, learning_method=AdamOptimizer())
Z
zhuoyuan 已提交
31 32 33 34 35 36 37

#######################Network Configuration #############

data_size = 1 * 28 * 28
label_size = 10
img = data_layer(name='pixel', size=data_size)

Z
zhuoyuan 已提交
38

Z
zhuoyuan 已提交
39
# light cnn
Z
zhuoyuan 已提交
40 41 42
# A shallower cnn model: [CNN, BN, ReLU, Max-Pooling] x4 + FC x1
# Easier to train for mnist dataset and quite efficient
# Final performance is close to deeper ones on tasks such as digital and character classification 
Z
zhuoyuan 已提交
43
def light_cnn(input_image, num_channels, num_classes):
Z
zhuoyuan 已提交
44 45 46 47 48 49
    def __light__(ipt,
                  num_filter=128,
                  times=1,
                  conv_filter_size=3,
                  dropouts=0,
                  num_channels_=None):
Z
zhuoyuan 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        return img_conv_group(
            input=ipt,
            num_channels=num_channels_,
            pool_size=2,
            pool_stride=2,
            conv_padding=0,
            conv_num_filter=[num_filter] * times,
            conv_filter_size=conv_filter_size,
            conv_act=ReluActivation(),
            conv_with_batchnorm=True,
            conv_batchnorm_drop_rate=dropouts,
            pool_type=MaxPooling())

    tmp = __light__(input_image, num_filter=128, num_channels_=num_channels)
    tmp = __light__(tmp, num_filter=128)
    tmp = __light__(tmp, num_filter=128)
    tmp = __light__(tmp, num_filter=128, conv_filter_size=1)

Z
zhuoyuan 已提交
68
    tmp = fc_layer(input=tmp, size=num_classes, act=SoftmaxActivation())
Z
zhuoyuan 已提交
69 70
    return tmp

Z
zhuoyuan 已提交
71

Z
zhuoyuan 已提交
72 73 74 75 76 77 78 79
predict = light_cnn(input_image=img, num_channels=1, num_classes=label_size)

if not is_predict:
    lbl = data_layer(name="label", size=label_size)
    inputs(img, lbl)
    outputs(classification_cost(input=predict, label=lbl))
else:
    outputs(predict)