31
31
os .environ ["CPU_NUM" ] = "1"
32
32
33
33
34
- def residual_block (img , label , num = 1 ):
35
- def conv_bn_layer (input ,
36
- ch_out ,
37
- filter_size ,
38
- stride ,
39
- padding ,
40
- act = 'relu' ,
41
- bias_attr = False ):
42
- tmp = fluid .layers .conv2d (
43
- input = input ,
44
- filter_size = filter_size ,
45
- num_filters = ch_out ,
46
- stride = stride ,
47
- padding = padding ,
48
- act = None ,
49
- bias_attr = bias_attr )
50
- return fluid .layers .batch_norm (input = tmp , act = act )
51
-
52
- hidden = img
53
- for _ in six .moves .xrange (num ):
54
- conv = conv_bn_layer (hidden , 20 , 3 , 1 , 1 , act = None , bias_attr = True )
55
- short = conv_bn_layer (hidden , 20 , 1 , 1 , 0 , act = None )
56
- hidden = fluid .layers .elementwise_add (x = conv , y = short , act = 'relu' )
57
- fc = fluid .layers .fc (input = hidden , size = 10 , act = 'softmax' )
58
- loss = fluid .layers .cross_entropy (input = fc , label = label )
59
- loss = fluid .layers .mean (loss )
60
- return loss
34
+ def conv_net (img , label ):
35
+ conv_pool_1 = fluid .nets .simple_img_conv_pool (
36
+ input = img ,
37
+ filter_size = 5 ,
38
+ num_filters = 20 ,
39
+ pool_size = 2 ,
40
+ pool_stride = 2 ,
41
+ pool_type = 'max' ,
42
+ act = "relu" )
43
+ conv_pool_1 = fluid .layers .batch_norm (conv_pool_1 )
44
+ conv_pool_2 = fluid .nets .simple_img_conv_pool (
45
+ input = conv_pool_1 ,
46
+ filter_size = 5 ,
47
+ num_filters = 50 ,
48
+ pool_size = 2 ,
49
+ pool_stride = 2 ,
50
+ pool_type = 'avg' ,
51
+ act = "relu" )
52
+ hidden = fluid .layers .fc (input = conv_pool_2 , size = 100 , act = 'relu' )
53
+ prediction = fluid .layers .fc (input = hidden , size = 10 , act = 'softmax' )
54
+ loss = fluid .layers .cross_entropy (input = prediction , label = label )
55
+ avg_loss = fluid .layers .mean (loss )
56
+ return avg_loss
61
57
62
58
63
59
class TestQuantizationScalePass (unittest .TestCase ):
@@ -76,7 +72,7 @@ def build_program(main, startup, is_test):
76
72
name = 'image' , shape = [1 , 28 , 28 ], dtype = 'float32' )
77
73
label = fluid .layers .data (
78
74
name = 'label' , shape = [1 ], dtype = 'int64' )
79
- loss = residual_block (img , label , 1 )
75
+ loss = conv_net (img , label )
80
76
if not is_test :
81
77
opt = fluid .optimizer .Adam (learning_rate = 0.0001 )
82
78
opt .minimize (loss )
0 commit comments