Skip to content

Commit bf72574

Browse files
wuyefeilinnepeplwu
authored andcommitted
导出流程适应不同的aug_method (#118)
* update solver.py and model_builder.py
1 parent ed41782 commit bf72574

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

pdseg/models/model_builder.py

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,56 @@ def sigmoid_to_softmax(logit):
124124
logit = fluid.layers.transpose(logit, [0, 3, 1, 2])
125125
return logit
126126

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+
127177

128178
def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
129179
if not ModelPhase.is_valid_phase(phase):
@@ -149,18 +199,8 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
149199
shape=[-1, -1, -1, cfg.DATASET.DATA_DIM],
150200
dtype='float32',
151201
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+
164204
else:
165205
image = fluid.layers.data(
166206
name='image', shape=image_shape, dtype='float32')
@@ -198,7 +238,6 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
198238
raise Exception(
199239
"softmax loss can not combine with dice loss or bce loss"
200240
)
201-
202241
logits = model_func(image, class_num)
203242

204243
# 根据选择的loss函数计算相应的损失函数
@@ -252,13 +291,17 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
252291
logit = sigmoid_to_softmax(logit)
253292
else:
254293
logit = softmax(logit)
294+
295+
# 获取有效部分
296+
logit = fluid.layers.slice(
297+
logit, axes=[2, 3], starts=[0, 0], ends=valid_shape)
298+
255299
logit = fluid.layers.resize_bilinear(
256300
logit,
257301
out_shape=origin_shape,
258302
align_corners=False,
259303
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)
262305
return origin_image, logit
263306

264307
if class_num == 1:

pdseg/utils/collect.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def check_and_infer(self):
122122
len(self.MODEL.MULTI_LOSS_WEIGHT) != 3:
123123
self.MODEL.MULTI_LOSS_WEIGHT = [1.0, 0.4, 0.16]
124124

125+
if self.AUG.AUG_METHOD not in ['unpadding', 'stepscaling', 'rangescaling']:
126+
raise ValueError(
127+
'AUG.AUG_METHOD config error, only support `unpadding`, `unpadding` and `rangescaling`'
128+
)
129+
130+
125131
def update_from_list(self, config_list):
126132
if len(config_list) % 2 != 0:
127133
raise ValueError(

0 commit comments

Comments
 (0)