未验证 提交 1b36f1ab 编写于 作者: W wuyefeilin 提交者: GitHub

Remove preprocessing and postprocessing of model exporting (#260)

* update *_batch_size_like ops

* remove the preprocess and postprocess of model exporting
上级 d7148519
...@@ -111,53 +111,6 @@ def sigmoid_to_softmax(logit): ...@@ -111,53 +111,6 @@ def sigmoid_to_softmax(logit):
return logit return logit
def export_preprocess(image):
"""导出模型的预处理流程"""
image = fluid.layers.transpose(image, [0, 3, 1, 2])
origin_shape = fluid.layers.shape(image)[-2:]
# 不同AUG_METHOD方法的resize
if cfg.AUG.AUG_METHOD == 'unpadding':
h_fix = cfg.AUG.FIX_RESIZE_SIZE[1]
w_fix = cfg.AUG.FIX_RESIZE_SIZE[0]
image = fluid.layers.resize_bilinear(
image, out_shape=[h_fix, w_fix], align_corners=False, align_mode=0)
elif cfg.AUG.AUG_METHOD == 'rangescaling':
size = cfg.AUG.INF_RESIZE_VALUE
value = fluid.layers.reduce_max(origin_shape)
scale = float(size) / value.astype('float32')
image = fluid.layers.resize_bilinear(
image, scale=scale, align_corners=False, align_mode=0)
# 存储resize后图像shape
valid_shape = fluid.layers.shape(image)[-2:]
# padding到eval_crop_size大小
width = cfg.EVAL_CROP_SIZE[0]
height = cfg.EVAL_CROP_SIZE[1]
pad_target = fluid.layers.assign(
np.array([height, width]).astype('float32'))
up = fluid.layers.assign(np.array([0]).astype('float32'))
down = pad_target[0] - valid_shape[0]
left = up
right = pad_target[1] - valid_shape[1]
paddings = fluid.layers.concat([up, down, left, right])
paddings = fluid.layers.cast(paddings, 'int32')
image = fluid.layers.pad2d(image, paddings=paddings, pad_value=127.5)
# normalize
mean = np.array(cfg.MEAN).reshape(1, len(cfg.MEAN), 1, 1)
mean = fluid.layers.assign(mean.astype('float32'))
std = np.array(cfg.STD).reshape(1, len(cfg.STD), 1, 1)
std = fluid.layers.assign(std.astype('float32'))
image = (image / 255 - mean) / std
# 使后面的网络能通过类似image.shape获取特征图的shape
image = fluid.layers.reshape(
image, shape=[-1, cfg.DATASET.DATA_DIM, height, width])
return image, valid_shape, origin_shape
def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN): def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
if not ModelPhase.is_valid_phase(phase): if not ModelPhase.is_valid_phase(phase):
raise ValueError("ModelPhase {} is not valid!".format(phase)) raise ValueError("ModelPhase {} is not valid!".format(phase))
...@@ -176,21 +129,7 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN): ...@@ -176,21 +129,7 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
# 在导出模型的时候,增加图像标准化预处理,减小预测部署时图像的处理流程 # 在导出模型的时候,增加图像标准化预处理,减小预测部署时图像的处理流程
# 预测部署时只须对输入图像增加batch_size维度即可 # 预测部署时只须对输入图像增加batch_size维度即可
if ModelPhase.is_predict(phase): image = fluid.data(name='image', shape=image_shape, dtype='float32')
if cfg.SLIM.PREPROCESS:
image = fluid.data(
name='image', shape=image_shape, dtype='float32')
else:
origin_image = fluid.data(
name='image',
shape=[-1, -1, -1, cfg.DATASET.DATA_DIM],
dtype='float32')
image, valid_shape, origin_shape = export_preprocess(
origin_image)
else:
image = fluid.data(
name='image', shape=image_shape, dtype='float32')
label = fluid.data(name='label', shape=grt_shape, dtype='int32') label = fluid.data(name='label', shape=grt_shape, dtype='int32')
mask = fluid.data(name='mask', shape=grt_shape, dtype='int32') mask = fluid.data(name='mask', shape=grt_shape, dtype='int32')
...@@ -293,21 +232,7 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN): ...@@ -293,21 +232,7 @@ def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
else: else:
logit = softmax(logit) logit = softmax(logit)
# 获取有效部分 return image, logit
if cfg.SLIM.PREPROCESS:
return image, logit
else:
logit = fluid.layers.slice(
logit, axes=[2, 3], starts=[0, 0], ends=valid_shape)
logit = fluid.layers.resize_bilinear(
logit,
out_shape=origin_shape,
align_corners=False,
align_mode=0)
logit = fluid.layers.argmax(logit, axis=1)
return origin_image, logit
if class_num == 1: if class_num == 1:
out = sigmoid_to_softmax(logit) out = sigmoid_to_softmax(logit)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册