test_image_classification_layer.py 2.9 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import unittest

Q
Qiao Longfei 已提交
3 4 5
import paddle.v2.fluid.layers as layers
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()
Q
Qiao Longfei 已提交
32 33 34 35
        images = layers.data(
            name='pixel',
            shape=[3, 48, 48],
            data_type='float32',
36
            main_program=main_program)
Q
Qiao Longfei 已提交
37
        layers.batch_norm(
38 39 40
            input=images,
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
41

42
        # print str(main_program)
Q
Qiao Longfei 已提交
43 44

    def test_dropout_layer(self):
45 46
        main_program = Program()
        startup_program = Program()
Q
Qiao Longfei 已提交
47 48 49 50
        images = layers.data(
            name='pixel',
            shape=[3, 48, 48],
            data_type='float32',
51
            main_program=main_program)
Q
Qiao Longfei 已提交
52 53 54
        layers.dropout(
            x=images,
            dropout_prob=0.5,
55 56
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
57

58
        # print str(main_program)
Q
Qiao Longfei 已提交
59 60

    def test_img_conv_group(self):
61 62
        main_program = Program()
        startup_program = Program()
Q
Qiao Longfei 已提交
63 64 65 66 67

        images = layers.data(
            name='pixel',
            shape=[3, 48, 48],
            data_type='float32',
68 69 70 71 72 73
            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 已提交
74

75
        # print str(main_program)
Q
Qiao Longfei 已提交
76

Q
Qiao Longfei 已提交
77
    def test_elementwise_add_with_act(self):
78 79
        main_program = Program()
        startup_program = Program()
Q
Qiao Longfei 已提交
80 81 82 83
        image1 = layers.data(
            name='pixel1',
            shape=[3, 48, 48],
            data_type='float32',
84 85
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
86 87 88 89
        image2 = layers.data(
            name='pixel2',
            shape=[3, 48, 48],
            data_type='float32',
90 91
            main_program=main_program,
            startup_program=startup_program)
Q
Qiao Longfei 已提交
92 93 94 95
        out = layers.elementwise_add(
            x=image1,
            y=image2,
            act='relu',
96 97 98
            main_program=main_program,
            startup_program=startup_program)
        # print(main_program)
Q
Qiao Longfei 已提交
99

Q
Qiao Longfei 已提交
100 101 102

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