save_load.py 4.6 KB
Newer Older
W
WuHaobo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

W
WuHaobo 已提交
19
import errno
W
WuHaobo 已提交
20 21
import os
import shutil
W
WuHaobo 已提交
22
import tempfile
W
WuHaobo 已提交
23 24 25 26 27 28 29 30 31 32 33

import paddle
import paddle.fluid as fluid

from ppcls.utils import logger

__all__ = ['init_model', 'save_model']


def _mkdir_if_not_exist(path):
    """
W
WuHaobo 已提交
34
    mkdir if not exists, ignore the exception when multiprocess mkdir together
W
WuHaobo 已提交
35
    """
W
WuHaobo 已提交
36 37 38 39 40 41 42 43 44
    if not os.path.exists(path):
        try:
            os.makedirs(path)
        except OSError as e:
            if e.errno == errno.EEXIST and os.path.isdir(path):
                logger.warning(
                    'be happy if some process has already created {}'.format(
                        path))
            else:
W
WuHaobo 已提交
45
                raise OSError('Failed to mkdir {}'.format(path))
W
WuHaobo 已提交
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 74 75


def _load_state(path):
    if os.path.exists(path + '.pdopt'):
        # XXX another hack to ignore the optimizer state
        tmp = tempfile.mkdtemp()
        dst = os.path.join(tmp, os.path.basename(os.path.normpath(path)))
        shutil.copy(path + '.pdparams', dst + '.pdparams')
        state = fluid.io.load_program_state(dst)
        shutil.rmtree(tmp)
    else:
        state = fluid.io.load_program_state(path)
    return state


def load_params(exe, prog, path, ignore_params=[]):
    """
    Load model from the given path.
    Args:
        exe (fluid.Executor): The fluid.Executor object.
        prog (fluid.Program): load weight to which Program object.
        path (string): URL string or loca model path.
        ignore_params (list): ignore variable to load when finetuning.
            It can be specified by finetune_exclude_pretrained_params
            and the usage can refer to docs/advanced_tutorials/TRANSFER_LEARNING.md
    """
    if not (os.path.isdir(path) or os.path.exists(path + '.pdparams')):
        raise ValueError("Model pretrain path {} does not "
                         "exists.".format(path))

S
shippingwang 已提交
76
    logger.info(logger.coloring('Loading parameters from {}...'.format(path), 'HEADER'))
W
WuHaobo 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    ignore_set = set()
    state = _load_state(path)

    # ignore the parameter which mismatch the shape
    # between the model and pretrain weight.
    all_var_shape = {}
    for block in prog.blocks:
        for param in block.all_parameters():
            all_var_shape[param.name] = param.shape
    ignore_set.update([
        name for name, shape in all_var_shape.items()
        if name in state and shape != state[name].shape
    ])

    if ignore_params:
        all_var_names = [var.name for var in prog.list_vars()]
        ignore_list = filter(
            lambda var: any([re.match(name, var) for name in ignore_params]),
            all_var_names)
        ignore_set.update(list(ignore_list))

    if len(ignore_set) > 0:
        for k in ignore_set:
            if k in state:
S
shippingwang 已提交
102
                logger.warning('variable {} is already excluded automatically'.format(k))
W
WuHaobo 已提交
103 104 105 106
                del state[k]
    fluid.io.set_program_state(prog, state)


littletomatodonkey's avatar
littletomatodonkey 已提交
107
def init_model(config, program, exe):
W
WuHaobo 已提交
108
    """
W
WuHaobo 已提交
109
    load model from checkpoint or pretrained_model
W
WuHaobo 已提交
110 111
    """
    checkpoints = config.get('checkpoints')
W
WuHaobo 已提交
112
    if checkpoints:
littletomatodonkey's avatar
littletomatodonkey 已提交
113
        fluid.load(program, checkpoints, exe)
S
shippingwang 已提交
114
        logger.info(logger.coloring("Finish initing model from {}".format(checkpoints),"HEADER"))
W
WuHaobo 已提交
115 116 117
        return

    pretrained_model = config.get('pretrained_model')
W
WuHaobo 已提交
118
    if pretrained_model:
littletomatodonkey's avatar
littletomatodonkey 已提交
119 120 121 122
        if not isinstance(pretrained_model, list):
            pretrained_model = [pretrained_model]
        for pretrain in pretrained_model:
            load_params(exe, program, pretrain)
S
shippingwang 已提交
123
        logger.info(logger.coloring("Finish initing model from {}".format(pretrained_model),"HEADER"))
W
WuHaobo 已提交
124 125 126 127


def save_model(program, model_path, epoch_id, prefix='ppcls'):
    """
W
WuHaobo 已提交
128
    save model to the target path
W
WuHaobo 已提交
129 130 131 132 133
    """
    model_path = os.path.join(model_path, str(epoch_id))
    _mkdir_if_not_exist(model_path)
    model_prefix = os.path.join(model_path, prefix)
    fluid.save(program, model_prefix)
S
shippingwang 已提交
134
    logger.info(logger.coloring("Already save model in {}".format(model_path),"HEADER"))