“952f8c139d0c46092cb33a697a275488f83e538f”上不存在“doc/design/multi_language_interface/00.why_plain_c.md”
test_image_classification_layer.py 3.3 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import unittest

3
import paddle.v2.fluid as fluid
Q
Qiao Longfei 已提交
4 5
import paddle.v2.fluid.nets as nets
from paddle.v2.fluid.framework import Program
Q
Qiao Longfei 已提交
6 7 8 9 10 11


def conv_block(input,
               num_filter,
               groups,
               dropouts,
12 13
               main_program=None,
               startup_program=None):
Q
Qiao Longfei 已提交
14 15 16 17 18 19 20 21 22 23
    return nets.img_conv_group(
        input=input,
        pool_size=2,
        pool_stride=2,
        conv_num_filter=[num_filter] * groups,
        conv_filter_size=3,
        conv_act='relu',
        conv_with_batchnorm=True,
        conv_batchnorm_drop_rate=dropouts,
        pool_type='max',
24 25
        main_program=main_program,
        startup_program=startup_program)
Q
Qiao Longfei 已提交
26 27 28 29


class TestLayer(unittest.TestCase):
    def test_batch_norm_layer(self):
30 31
        main_program = Program()
        startup_program = Program()
32
        images = fluid.layers.data(
Q
Qiao Longfei 已提交
33 34
            name='pixel',
            shape=[3, 48, 48],
F
fengjiayi 已提交
35
            dtype='float32',
36
            main_program=main_program)
37
        hidden1 = fluid.layers.batch_norm(
38 39 40
            input=images,
            main_program=main_program,
            startup_program=startup_program)
41 42 43 44 45 46 47 48
        hidden2 = fluid.layers.fc(input=hidden1,
                                  size=128,
                                  act='relu',
                                  main_program=main_program)
        hidden3 = fluid.layers.batch_norm(
            input=hidden2,
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
49

50
        print str(main_program)
Q
Qiao Longfei 已提交
51 52

    def test_dropout_layer(self):
53 54
        main_program = Program()
        startup_program = Program()
55
        images = fluid.layers.data(
Q
Qiao Longfei 已提交
56 57
            name='pixel',
            shape=[3, 48, 48],
F
fengjiayi 已提交
58
            dtype='float32',
59
            main_program=main_program)
60
        fluid.layers.dropout(
Q
Qiao Longfei 已提交
61 62
            x=images,
            dropout_prob=0.5,
63 64
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
65

66
        # print str(main_program)
Q
Qiao Longfei 已提交
67 68

    def test_img_conv_group(self):
69 70
        main_program = Program()
        startup_program = Program()
Q
Qiao Longfei 已提交
71

72
        images = fluid.layers.data(
Q
Qiao Longfei 已提交
73 74
            name='pixel',
            shape=[3, 48, 48],
F
fengjiayi 已提交
75
            dtype='float32',
76 77 78 79 80 81
            main_program=main_program,
            startup_program=startup_program)
        conv1 = conv_block(images, 64, 2, [0.3, 0], main_program,
                           startup_program)
        conv2 = conv_block(conv1, 256, 3, [0.4, 0.4, 0], main_program,
                           startup_program)
Q
Qiao Longfei 已提交
82

83
        # print str(main_program)
Q
Qiao Longfei 已提交
84

Q
Qiao Longfei 已提交
85
    def test_elementwise_add_with_act(self):
86 87
        main_program = Program()
        startup_program = Program()
88
        image1 = fluid.layers.data(
Q
Qiao Longfei 已提交
89 90
            name='pixel1',
            shape=[3, 48, 48],
F
fengjiayi 已提交
91
            dtype='float32',
92 93
            main_program=main_program,
            startup_program=startup_program)
94
        image2 = fluid.layers.data(
Q
Qiao Longfei 已提交
95 96
            name='pixel2',
            shape=[3, 48, 48],
F
fengjiayi 已提交
97
            dtype='float32',
98 99
            main_program=main_program,
            startup_program=startup_program)
100
        out = fluid.layers.elementwise_add(
Q
Qiao Longfei 已提交
101 102 103
            x=image1,
            y=image2,
            act='relu',
104 105 106
            main_program=main_program,
            startup_program=startup_program)
        # print(main_program)
Q
Qiao Longfei 已提交
107

Q
Qiao Longfei 已提交
108 109 110

if __name__ == '__main__':
    unittest.main()