model_builder.py 13.6 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# coding: utf8
# copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import struct

import paddle.fluid as fluid
import numpy as np
from paddle.fluid.proto.framework_pb2 import VarType

import solver
from utils.config import cfg
from loss import multi_softmax_with_loss
W
wuyefeilin 已提交
25 26
from loss import multi_dice_loss
from loss import multi_bce_loss
27 28
from lovasz_losses import lovasz_hinge
from lovasz_losses import lovasz_softmax
L
LielinJiang 已提交
29
from models.modeling import deeplab, unet, icnet, pspnet, hrnet, fast_scnn
W
wuzewu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73


class ModelPhase(object):
    """
    Standard name for model phase in PaddleSeg

    The following standard keys are defined:
    * `TRAIN`: training mode.
    * `EVAL`: testing/evaluation mode.
    * `PREDICT`: prediction/inference mode.
    * `VISUAL` : visualization mode
    """

    TRAIN = 'train'
    EVAL = 'eval'
    PREDICT = 'predict'
    VISUAL = 'visual'

    @staticmethod
    def is_train(phase):
        return phase == ModelPhase.TRAIN

    @staticmethod
    def is_predict(phase):
        return phase == ModelPhase.PREDICT

    @staticmethod
    def is_eval(phase):
        return phase == ModelPhase.EVAL

    @staticmethod
    def is_visual(phase):
        return phase == ModelPhase.VISUAL

    @staticmethod
    def is_valid_phase(phase):
        """ Check valid phase """
        if ModelPhase.is_train(phase) or ModelPhase.is_predict(phase) \
                or ModelPhase.is_eval(phase) or ModelPhase.is_visual(phase):
            return True

        return False


W
wuyefeilin 已提交
74 75 76 77 78 79 80 81 82 83 84 85
def seg_model(image, class_num):
    model_name = cfg.MODEL.MODEL_NAME
    if model_name == 'unet':
        logits = unet.unet(image, class_num)
    elif model_name == 'deeplabv3p':
        logits = deeplab.deeplabv3p(image, class_num)
    elif model_name == 'icnet':
        logits = icnet.icnet(image, class_num)
    elif model_name == 'pspnet':
        logits = pspnet.pspnet(image, class_num)
    elif model_name == 'hrnet':
        logits = hrnet.hrnet(image, class_num)
L
LielinJiang 已提交
86 87
    elif model_name == 'fast_scnn':
        logits = fast_scnn.fast_scnn(image, class_num)
W
wuzewu 已提交
88 89
    else:
        raise Exception(
L
LielinJiang 已提交
90
            "unknow model name, only support unet, deeplabv3p, icnet, pspnet, hrnet, fast_scnn"
W
wuyefeilin 已提交
91 92
        )
    return logits
W
wuzewu 已提交
93 94 95 96 97 98 99 100


def softmax(logit):
    logit = fluid.layers.transpose(logit, [0, 2, 3, 1])
    logit = fluid.layers.softmax(logit)
    logit = fluid.layers.transpose(logit, [0, 3, 1, 2])
    return logit

W
wuyefeilin 已提交
101

W
wuyefeilin 已提交
102 103 104 105 106 107 108 109 110 111 112
def sigmoid_to_softmax(logit):
    """
    one channel to two channel
    """
    logit = fluid.layers.transpose(logit, [0, 2, 3, 1])
    logit = fluid.layers.sigmoid(logit)
    logit_back = 1 - logit
    logit = fluid.layers.concat([logit_back, logit], axis=-1)
    logit = fluid.layers.transpose(logit, [0, 3, 1, 2])
    return logit

W
wuzewu 已提交
113

114 115 116 117 118 119 120 121 122 123 124
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(
W
wuyefeilin 已提交
125
            image, out_shape=[h_fix, w_fix], align_corners=False, align_mode=0)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    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')
W
wuyefeilin 已提交
147
    image = fluid.layers.pad2d(image, paddings=paddings, pad_value=127.5)
148 149 150 151 152 153 154 155 156 157 158 159

    # 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

W
wuzewu 已提交
160 161 162 163 164 165 166 167 168 169 170

def build_model(main_prog, start_prog, phase=ModelPhase.TRAIN):
    if not ModelPhase.is_valid_phase(phase):
        raise ValueError("ModelPhase {} is not valid!".format(phase))
    if ModelPhase.is_train(phase):
        width = cfg.TRAIN_CROP_SIZE[0]
        height = cfg.TRAIN_CROP_SIZE[1]
    else:
        width = cfg.EVAL_CROP_SIZE[0]
        height = cfg.EVAL_CROP_SIZE[1]

171 172
    image_shape = [-1, cfg.DATASET.DATA_DIM, height, width]
    grt_shape = [-1, 1, height, width]
W
wuzewu 已提交
173 174 175 176
    class_num = cfg.DATASET.NUM_CLASSES

    with fluid.program_guard(main_prog, start_prog):
        with fluid.unique_name.guard():
177 178 179
            # 在导出模型的时候,增加图像标准化预处理,减小预测部署时图像的处理流程
            # 预测部署时只须对输入图像增加batch_size维度即可
            if ModelPhase.is_predict(phase):
180 181 182 183 184 185 186 187 188 189
                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)
190

191
            else:
192
                image = fluid.data(
193
                    name='image', shape=image_shape, dtype='float32')
194 195
            label = fluid.data(name='label', shape=grt_shape, dtype='int32')
            mask = fluid.data(name='mask', shape=grt_shape, dtype='int32')
W
wuzewu 已提交
196

197
            # use DataLoader when doing traning and evaluation
W
wuzewu 已提交
198
            if ModelPhase.is_train(phase) or ModelPhase.is_eval(phase):
199
                data_loader = fluid.io.DataLoader.from_generator(
W
wuzewu 已提交
200 201 202 203
                    feed_list=[image, label, mask],
                    capacity=cfg.DATALOADER.BUF_SIZE,
                    iterable=False,
                    use_double_buffer=True)
204

W
wuyefeilin 已提交
205
            loss_type = cfg.SOLVER.LOSS
206 207
            if not isinstance(loss_type, list):
                loss_type = list(loss_type)
208

209 210 211
            # lovasz_hinge_loss或dice_loss或bce_loss只适用两类分割中
            if class_num > 2 and (("lovasz_hinge_loss" in loss_type) or
                                  ("dice_loss" in loss_type) or
W
wuyefeilin 已提交
212 213
                                  ("bce_loss" in loss_type)):
                raise Exception(
214
                    "lovasz hinge loss, dice loss and bce loss are only applicable to binary classfication."
W
wuyefeilin 已提交
215 216
                )

217 218 219
            # 在两类分割情况下,当loss函数选择lovasz_hinge_loss或dice_loss或bce_loss的时候,最后logit输出通道数设置为1
            if ("dice_loss" in loss_type) or ("bce_loss" in loss_type) or (
                    "lovasz_hinge_loss" in loss_type):
W
wuyefeilin 已提交
220
                class_num = 1
221 222
                if ("softmax_loss" in loss_type) or (
                        "lovasz_softmax_loss" in loss_type):
W
wuyefeilin 已提交
223
                    raise Exception(
224
                        "softmax loss or lovasz softmax loss can not combine with bce loss or dice loss or lovasz hinge loss."
W
wuyefeilin 已提交
225
                    )
W
wuyefeilin 已提交
226
            logits = seg_model(image, class_num)
W
wuzewu 已提交
227

228
            # 根据选择的loss函数计算相应的损失函数
W
wuzewu 已提交
229
            if ModelPhase.is_train(phase) or ModelPhase.is_eval(phase):
W
wuyefeilin 已提交
230 231
                loss_valid = False
                avg_loss_list = []
232
                valid_loss = []
L
LielinJiang 已提交
233 234
                if "softmax_loss" in loss_type:
                    weight = cfg.SOLVER.CROSS_ENTROPY_WEIGHT
W
wuyefeilin 已提交
235
                    avg_loss_list.append(
236 237
                        multi_softmax_with_loss(logits, label, mask, class_num,
                                                weight))
W
wuyefeilin 已提交
238
                    loss_valid = True
239
                    valid_loss.append("softmax_loss")
W
wuyefeilin 已提交
240 241 242
                if "dice_loss" in loss_type:
                    avg_loss_list.append(multi_dice_loss(logits, label, mask))
                    loss_valid = True
243
                    valid_loss.append("dice_loss")
W
wuyefeilin 已提交
244 245 246
                if "bce_loss" in loss_type:
                    avg_loss_list.append(multi_bce_loss(logits, label, mask))
                    loss_valid = True
247
                    valid_loss.append("bce_loss")
248 249 250 251 252 253 254 255 256 257 258
                if "lovasz_hinge_loss" in loss_type:
                    avg_loss_list.append(
                        lovasz_hinge(logits, label, ignore=mask))
                    loss_valid = True
                    valid_loss.append("lovasz_hinge_loss")
                if "lovasz_softmax_loss" in loss_type:
                    probas = fluid.layers.softmax(logits, axis=1)
                    avg_loss_list.append(
                        lovasz_softmax(probas, label, ignore=mask))
                    loss_valid = True
                    valid_loss.append("lovasz_softmax_loss")
W
wuyefeilin 已提交
259
                if not loss_valid:
W
wuyefeilin 已提交
260 261
                    raise Exception(
                        "SOLVER.LOSS: {} is set wrong. it should "
262 263
                        "include one of (softmax_loss, bce_loss, dice_loss, lovasz_hinge_loss, lovasz_softmax_loss) at least"
                        " example: ['softmax_loss'], ['dice_loss'], ['bce_loss', 'dice_loss'], ['lovasz_hinge_loss','bce_loss'], ['lovasz_softmax_loss','softmax_loss']"
W
wuyefeilin 已提交
264 265
                        .format(cfg.SOLVER.LOSS))

266 267
                invalid_loss = [x for x in loss_type if x not in valid_loss]
                if len(invalid_loss) > 0:
W
wuyefeilin 已提交
268 269 270
                    print(
                        "Warning: the loss {} you set is invalid. it will not be included in loss computed."
                        .format(invalid_loss))
271

W
wuyefeilin 已提交
272 273
                avg_loss = 0
                for i in range(0, len(avg_loss_list)):
274 275 276
                    loss_name = valid_loss[i].upper()
                    loss_weight = eval('cfg.SOLVER.LOSS_WEIGHT.' + loss_name)
                    avg_loss += loss_weight * avg_loss_list[i]
W
wuzewu 已提交
277 278 279 280 281 282 283 284 285 286 287 288

            #get pred result in original size
            if isinstance(logits, tuple):
                logit = logits[0]
            else:
                logit = logits

            if logit.shape[2:] != label.shape[2:]:
                logit = fluid.layers.resize_bilinear(logit, label.shape[2:])

            # return image input and logit output for inference graph prune
            if ModelPhase.is_predict(phase):
289
                # 两类分割中,使用lovasz_hinge_loss或dice_loss或bce_loss返回的logit为单通道,进行到两通道的变换
W
wuyefeilin 已提交
290 291 292 293
                if class_num == 1:
                    logit = sigmoid_to_softmax(logit)
                else:
                    logit = softmax(logit)
294 295

                # 获取有效部分
296 297 298 299 300 301 302 303 304 305 306 307 308
                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)
309
                return origin_image, logit
310

W
wuyefeilin 已提交
311 312 313 314 315
            if class_num == 1:
                out = sigmoid_to_softmax(logit)
                out = fluid.layers.transpose(out, [0, 2, 3, 1])
            else:
                out = fluid.layers.transpose(logit, [0, 2, 3, 1])
316

W
wuzewu 已提交
317 318 319
            pred = fluid.layers.argmax(out, axis=3)
            pred = fluid.layers.unsqueeze(pred, axes=[3])
            if ModelPhase.is_visual(phase):
W
wuyefeilin 已提交
320 321 322 323
                if class_num == 1:
                    logit = sigmoid_to_softmax(logit)
                else:
                    logit = softmax(logit)
W
wuzewu 已提交
324 325 326
                return pred, logit

            if ModelPhase.is_eval(phase):
327
                return data_loader, avg_loss, pred, label, mask
W
wuzewu 已提交
328 329 330 331

            if ModelPhase.is_train(phase):
                optimizer = solver.Solver(main_prog, start_prog)
                decayed_lr = optimizer.optimise(avg_loss)
332
                return data_loader, avg_loss, decayed_lr, pred, label, mask
W
wuzewu 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350


def to_int(string, dest="I"):
    return struct.unpack(dest, string)[0]


def parse_shape_from_file(filename):
    with open(filename, "rb") as file:
        version = file.read(4)
        lod_level = to_int(file.read(8), dest="Q")
        for i in range(lod_level):
            _size = to_int(file.read(8), dest="Q")
            _ = file.read(_size)
        version = file.read(4)
        tensor_desc_size = to_int(file.read(4))
        tensor_desc = VarType.TensorDesc()
        tensor_desc.ParseFromString(file.read(tensor_desc_size))
    return tuple(tensor_desc.dims)