5
5
import paddle .v2 .fluid .nets as nets
6
6
import paddle .v2 .fluid .optimizer as optimizer
7
7
from paddle .v2 .fluid .executor import Executor
8
- from paddle .v2 .fluid .framework import g_startup_program , g_main_program
8
+ import paddle .v2 .fluid .framework as framework
9
9
from paddle .v2 .fluid .initializer import XavierInitializer
10
10
11
11
12
- def resnet_cifar10 (input , depth = 32 , main_program = None , startup_program = None ):
12
+ def resnet_cifar10 (input , depth = 32 ):
13
13
def conv_bn_layer (input ,
14
14
ch_out ,
15
15
filter_size ,
16
16
stride ,
17
17
padding ,
18
- act = 'relu' ,
19
- main_program = None ,
20
- startup_program = None ):
18
+ act = 'relu' ):
21
19
tmp = layers .conv2d (
22
20
input = input ,
23
21
filter_size = filter_size ,
24
22
num_filters = ch_out ,
25
23
stride = stride ,
26
24
padding = padding ,
27
25
act = None ,
28
- bias_attr = False ,
29
- main_program = main_program ,
30
- startup_program = startup_program )
26
+ bias_attr = False )
31
27
return layers .batch_norm (
32
28
input = tmp ,
33
- act = act ,
34
- main_program = main_program ,
35
- startup_program = startup_program )
29
+ act = act )
36
30
37
31
def shortcut (input , ch_in , ch_out , stride , program , init_program ):
38
32
if ch_in != ch_out :
@@ -44,40 +38,30 @@ def shortcut(input, ch_in, ch_out, stride, program, init_program):
44
38
def basicblock (input ,
45
39
ch_in ,
46
40
ch_out ,
47
- stride ,
48
- main_program = main_program ,
49
- startup_program = startup_program ):
41
+ stride ):
50
42
tmp = conv_bn_layer (
51
43
input ,
52
44
ch_out ,
53
45
3 ,
54
46
stride ,
55
- 1 ,
56
- main_program = main_program ,
57
- startup_program = startup_program )
47
+ 1 )
58
48
tmp = conv_bn_layer (
59
49
tmp ,
60
50
ch_out ,
61
51
3 ,
62
52
1 ,
63
53
1 ,
64
- act = None ,
65
- main_program = main_program ,
66
- startup_program = startup_program )
67
- short = shortcut (input , ch_in , ch_out , stride , main_program ,
68
- startup_program )
54
+ act = None )
55
+ short = shortcut (input , ch_in , ch_out , stride )
69
56
return layers .elementwise_add (
70
57
x = tmp ,
71
58
y = short ,
72
- act = 'relu' ,
73
- main_program = main_program ,
74
- startup_program = startup_program )
59
+ act = 'relu' )
75
60
76
- def layer_warp (block_func , input , ch_in , ch_out , count , stride , program ,
77
- startup_program ):
78
- tmp = block_func (input , ch_in , ch_out , stride , program , startup_program )
61
+ def layer_warp (block_func , input , ch_in , ch_out , count , stride ):
62
+ tmp = block_func (input , ch_in , ch_out , stride )
79
63
for i in range (1 , count ):
80
- tmp = block_func (tmp , ch_out , ch_out , 1 , program , startup_program )
64
+ tmp = block_func (tmp , ch_out , ch_out , 1 )
81
65
return tmp
82
66
83
67
assert (depth - 2 ) % 6 == 0
@@ -87,53 +71,41 @@ def layer_warp(block_func, input, ch_in, ch_out, count, stride, program,
87
71
ch_out = 16 ,
88
72
filter_size = 3 ,
89
73
stride = 1 ,
90
- padding = 1 ,
91
- main_program = main_program ,
92
- startup_program = startup_program )
74
+ padding = 1 )
93
75
res1 = layer_warp (
94
76
basicblock ,
95
77
conv1 ,
96
78
16 ,
97
79
16 ,
98
80
n ,
99
- 1 ,
100
- main_program = main_program ,
101
- startup_program = startup_program )
81
+ 1 )
102
82
res2 = layer_warp (
103
83
basicblock ,
104
84
res1 ,
105
85
16 ,
106
86
32 ,
107
87
n ,
108
- 2 ,
109
- main_program = main_program ,
110
- startup_program = startup_program )
88
+ 2 )
111
89
res3 = layer_warp (
112
90
basicblock ,
113
91
res2 ,
114
92
32 ,
115
93
64 ,
116
94
n ,
117
- 2 ,
118
- main_program = main_program ,
119
- startup_program = startup_program )
95
+ 2 )
120
96
pool = layers .pool2d (
121
97
input = res3 ,
122
98
pool_size = 8 ,
123
99
pool_type = 'avg' ,
124
- pool_stride = 1 ,
125
- main_program = main_program ,
126
- startup_program = startup_program )
100
+ pool_stride = 1 )
127
101
return pool
128
102
129
103
130
- def vgg16_bn_drop (input , main_program = None , startup_program = None ):
104
+ def vgg16_bn_drop (input ):
131
105
def conv_block (input ,
132
106
num_filter ,
133
107
groups ,
134
- dropouts ,
135
- main_program = None ,
136
- startup_program = None ):
108
+ dropouts ):
137
109
return nets .img_conv_group (
138
110
input = input ,
139
111
pool_size = 2 ,
@@ -143,51 +115,34 @@ def conv_block(input,
143
115
conv_act = 'relu' ,
144
116
conv_with_batchnorm = True ,
145
117
conv_batchnorm_drop_rate = dropouts ,
146
- pool_type = 'max' ,
147
- main_program = main_program ,
148
- startup_program = startup_program )
118
+ pool_type = 'max' )
149
119
150
- conv1 = conv_block (input , 64 , 2 , [0.3 , 0 ], main_program , startup_program )
151
- conv2 = conv_block (conv1 , 128 , 2 , [0.4 , 0 ], main_program , startup_program )
152
- conv3 = conv_block (conv2 , 256 , 3 , [0.4 , 0.4 , 0 ], main_program ,
153
- startup_program )
154
- conv4 = conv_block (conv3 , 512 , 3 , [0.4 , 0.4 , 0 ], main_program ,
155
- startup_program )
156
- conv5 = conv_block (conv4 , 512 , 3 , [0.4 , 0.4 , 0 ], main_program ,
157
- startup_program )
120
+ conv1 = conv_block (input , 64 , 2 , [0.3 , 0 ])
121
+ conv2 = conv_block (conv1 , 128 , 2 , [0.4 , 0 ])
122
+ conv3 = conv_block (conv2 , 256 , 3 , [0.4 , 0.4 , 0 ])
123
+ conv4 = conv_block (conv3 , 512 , 3 , [0.4 , 0.4 , 0 ])
124
+ conv5 = conv_block (conv4 , 512 , 3 , [0.4 , 0.4 , 0 ])
158
125
159
126
drop = layers .dropout (
160
127
x = conv5 ,
161
- dropout_prob = 0.5 ,
162
- main_program = main_program ,
163
- startup_program = startup_program )
128
+ dropout_prob = 0.5 )
164
129
fc1 = layers .fc (input = drop ,
165
130
size = 512 ,
166
131
act = None ,
167
- param_attr = {"initializer" : XavierInitializer ()},
168
- main_program = main_program ,
169
- startup_program = startup_program )
132
+ param_attr = {"initializer" : XavierInitializer ()})
170
133
reshape1 = layers .reshape (
171
134
x = fc1 ,
172
- shape = list (fc1 .shape + (1 , 1 )),
173
- main_program = main_program ,
174
- startup_program = startup_program )
135
+ shape = list (fc1 .shape + (1 , 1 )))
175
136
bn = layers .batch_norm (
176
137
input = reshape1 ,
177
- act = 'relu' ,
178
- main_program = main_program ,
179
- startup_program = startup_program )
138
+ act = 'relu' )
180
139
drop2 = layers .dropout (
181
140
x = bn ,
182
- dropout_prob = 0.5 ,
183
- main_program = main_program ,
184
- startup_program = startup_program )
141
+ dropout_prob = 0.5 )
185
142
fc2 = layers .fc (input = drop2 ,
186
143
size = 512 ,
187
144
act = None ,
188
- param_attr = {"initializer" : XavierInitializer ()},
189
- main_program = main_program ,
190
- startup_program = startup_program )
145
+ param_attr = {"initializer" : XavierInitializer ()})
191
146
return fc2
192
147
193
148
@@ -225,7 +180,7 @@ def conv_block(input,
225
180
place = core .CPUPlace ()
226
181
exe = Executor (place )
227
182
228
- exe .run (g_startup_program )
183
+ exe .run (framework . default_startup_program () )
229
184
230
185
for pass_id in range (PASS_NUM ):
231
186
batch_id = 0
@@ -243,7 +198,7 @@ def conv_block(input,
243
198
tensor_img .set (img_data , place )
244
199
tensor_y .set (y_data , place )
245
200
246
- outs = exe .run (g_main_program ,
201
+ outs = exe .run (framework . default_main_program () ,
247
202
feed = {"pixel" : tensor_img ,
248
203
"label" : tensor_y },
249
204
fetch_list = [avg_cost , accuracy ])
0 commit comments