@@ -124,6 +124,56 @@ def sigmoid_to_softmax(logit):
124
124
logit = fluid .layers .transpose (logit , [0 , 3 , 1 , 2 ])
125
125
return logit
126
126
127
+ def export_preprocess (image ):
128
+ """导出模型的预处理流程"""
129
+
130
+ image = fluid .layers .transpose (image , [0 , 3 , 1 , 2 ])
131
+ origin_shape = fluid .layers .shape (image )[- 2 :]
132
+
133
+ # 不同AUG_METHOD方法的resize
134
+ if cfg .AUG .AUG_METHOD == 'unpadding' :
135
+ h_fix = cfg .AUG .FIX_RESIZE_SIZE [1 ]
136
+ w_fix = cfg .AUG .FIX_RESIZE_SIZE [0 ]
137
+ image = fluid .layers .resize_bilinear (
138
+ image ,
139
+ out_shape = [h_fix , w_fix ],
140
+ align_corners = False ,
141
+ align_mode = 0 )
142
+ elif cfg .AUG .AUG_METHOD == 'rangescaling' :
143
+ size = cfg .AUG .INF_RESIZE_VALUE
144
+ value = fluid .layers .reduce_max (origin_shape )
145
+ scale = float (size ) / value .astype ('float32' )
146
+ image = fluid .layers .resize_bilinear (
147
+ image , scale = scale , align_corners = False , align_mode = 0 )
148
+
149
+ # 存储resize后图像shape
150
+ valid_shape = fluid .layers .shape (image )[- 2 :]
151
+
152
+ # padding到eval_crop_size大小
153
+ width = cfg .EVAL_CROP_SIZE [0 ]
154
+ height = cfg .EVAL_CROP_SIZE [1 ]
155
+ pad_target = fluid .layers .assign (
156
+ np .array ([height , width ]).astype ('float32' ))
157
+ up = fluid .layers .assign (np .array ([0 ]).astype ('float32' ))
158
+ down = pad_target [0 ] - valid_shape [0 ]
159
+ left = up
160
+ right = pad_target [1 ] - valid_shape [1 ]
161
+ paddings = fluid .layers .concat ([up , down , left , right ])
162
+ paddings = fluid .layers .cast (paddings , 'int32' )
163
+ image = fluid .layers .pad2d (
164
+ image , paddings = paddings , pad_value = 127.5 )
165
+
166
+ # normalize
167
+ mean = np .array (cfg .MEAN ).reshape (1 , len (cfg .MEAN ), 1 , 1 )
168
+ mean = fluid .layers .assign (mean .astype ('float32' ))
169
+ std = np .array (cfg .STD ).reshape (1 , len (cfg .STD ), 1 , 1 )
170
+ std = fluid .layers .assign (std .astype ('float32' ))
171
+ image = (image / 255 - mean ) / std
172
+ # 使后面的网络能通过类似image.shape获取特征图的shape
173
+ image = fluid .layers .reshape (
174
+ image , shape = [- 1 , cfg .DATASET .DATA_DIM , height , width ])
175
+ return image , valid_shape , origin_shape
176
+
127
177
128
178
def build_model (main_prog , start_prog , phase = ModelPhase .TRAIN ):
129
179
if not ModelPhase .is_valid_phase (phase ):
@@ -149,18 +199,8 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
149
199
shape = [- 1 , - 1 , - 1 , cfg .DATASET .DATA_DIM ],
150
200
dtype = 'float32' ,
151
201
append_batch_size = False )
152
- image = fluid .layers .transpose (origin_image , [0 , 3 , 1 , 2 ])
153
- origin_shape = fluid .layers .shape (image )[- 2 :]
154
- mean = np .array (cfg .MEAN ).reshape (1 , len (cfg .MEAN ), 1 , 1 )
155
- mean = fluid .layers .assign (mean .astype ('float32' ))
156
- std = np .array (cfg .STD ).reshape (1 , len (cfg .STD ), 1 , 1 )
157
- std = fluid .layers .assign (std .astype ('float32' ))
158
- image = fluid .layers .resize_bilinear (
159
- image ,
160
- out_shape = [height , width ],
161
- align_corners = False ,
162
- align_mode = 0 )
163
- image = (image / 255 - mean ) / std
202
+ image , valid_shape , origin_shape = export_preprocess (origin_image )
203
+
164
204
else :
165
205
image = fluid .layers .data (
166
206
name = 'image' , shape = image_shape , dtype = 'float32' )
@@ -198,7 +238,6 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
198
238
raise Exception (
199
239
"softmax loss can not combine with dice loss or bce loss"
200
240
)
201
-
202
241
logits = model_func (image , class_num )
203
242
204
243
# 根据选择的loss函数计算相应的损失函数
@@ -252,13 +291,17 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
252
291
logit = sigmoid_to_softmax (logit )
253
292
else :
254
293
logit = softmax (logit )
294
+
295
+ # 获取有效部分
296
+ logit = fluid .layers .slice (
297
+ logit , axes = [2 , 3 ], starts = [0 , 0 ], ends = valid_shape )
298
+
255
299
logit = fluid .layers .resize_bilinear (
256
300
logit ,
257
301
out_shape = origin_shape ,
258
302
align_corners = False ,
259
303
align_mode = 0 )
260
- logit = fluid .layers .transpose (logit , [0 , 2 , 3 , 1 ])
261
- logit = fluid .layers .argmax (logit , axis = 3 )
304
+ logit = fluid .layers .argmax (logit , axis = 1 )
262
305
return origin_image , logit
263
306
264
307
if class_num == 1 :
0 commit comments