未验证 提交 d7d35578 编写于 作者: S shaohua.zhang 提交者: GitHub

Update utility.py

上级 82a48a72
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. #you may not use this file except in compliance with the License.
# You may obtain a copy of the License at #You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software #Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, #distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and #See the License for the specific language governing permissions and
# limitations under the License. #limitations under the License.
import logging
import os
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
def initial_logger(): import errno
FORMAT = '%(asctime)s-%(levelname)s: %(message)s' import os
logging.basicConfig(level=logging.INFO, format=FORMAT) import shutil
logger = logging.getLogger(__name__) import tempfile
return logger
import paddle.fluid as fluid
import importlib from .utility import initial_logger
import re
logger = initial_logger()
def create_module(module_str):
tmpss = module_str.split(",")
assert len(tmpss) == 2, "Error formate\ def _mkdir_if_not_exist(path):
of the module path: {}".format(module_str) """
module_name, function_name = tmpss[0], tmpss[1] mkdir if not exists, ignore the exception when multiprocess mkdir together
somemodule = importlib.import_module(module_name, __package__) """
function = getattr(somemodule, function_name) if not os.path.exists(path):
return function try:
os.makedirs(path)
except OSError as e:
def get_check_global_params(mode): if e.errno == errno.EEXIST and os.path.isdir(path):
check_params = ['use_gpu', 'max_text_length', 'image_shape',\ logger.warning(
'image_shape', 'character_type', 'loss_type'] 'be happy if some process has already created {}'.format(
if mode == "train_eval": path))
check_params = check_params + [\ else:
'train_batch_size_per_card', 'test_batch_size_per_card'] raise OSError('Failed to mkdir {}'.format(path))
elif mode == "test":
check_params = check_params + ['test_batch_size_per_card']
return check_params def _load_state(path):
if os.path.exists(path + '.pdopt'):
# XXX another hack to ignore the optimizer state
def get_check_reader_params(mode): tmp = tempfile.mkdtemp()
check_params = [] dst = os.path.join(tmp, os.path.basename(os.path.normpath(path)))
if mode == "train_eval": shutil.copy(path + '.pdparams', dst + '.pdparams')
check_params = ['TrainReader', 'EvalReader'] state = fluid.io.load_program_state(dst)
elif mode == "test": shutil.rmtree(tmp)
check_params = ['TestReader'] else:
return check_params state = fluid.io.load_program_state(path)
return state
def get_image_file_list(img_file):
imgs_lists = [] def load_params(exe, prog, path, ignore_params=[]):
if img_file is None or not os.path.exists(img_file): """
raise Exception("not found any img file in {}".format(img_file)) Load model from the given path.
Args:
img_end = ['jpg', 'png', 'jpeg', 'JPEG', 'JPG', 'bmp'] exe (fluid.Executor): The fluid.Executor object.
if os.path.isfile(img_file) and img_file.split('.')[-1] in img_end: prog (fluid.Program): load weight to which Program object.
imgs_lists.append(img_file) path (string): URL string or loca model path.
elif os.path.isdir(img_file): ignore_params (list): ignore variable to load when finetuning.
for single_file in os.listdir(img_file): It can be specified by finetune_exclude_pretrained_params
if single_file.split('.')[-1] in img_end: and the usage can refer to docs/advanced_tutorials/TRANSFER_LEARNING.md
imgs_lists.append(os.path.join(img_file, single_file)) """
if len(imgs_lists) == 0: if not (os.path.isdir(path) or os.path.exists(path + '.pdparams')):
raise Exception("not found any img file in {}".format(img_file)) raise ValueError("Model pretrain path {} does not "
return imgs_lists "exists.".format(path))
logger.info('Loading parameters from {}...'.format(path))
from paddle import fluid
ignore_set = set()
state = _load_state(path)
def create_multi_devices_program(program, loss_var_name):
build_strategy = fluid.BuildStrategy() # ignore the parameter which mismatch the shape
build_strategy.memory_optimize = False # between the model and pretrain weight.
build_strategy.enable_inplace = True all_var_shape = {}
exec_strategy = fluid.ExecutionStrategy() for block in prog.blocks:
exec_strategy.num_iteration_per_drop_scope = 1 for param in block.all_parameters():
compile_program = fluid.CompiledProgram(program).with_data_parallel( all_var_shape[param.name] = param.shape
loss_name=loss_var_name, ignore_set.update([
build_strategy=build_strategy, name for name, shape in all_var_shape.items()
exec_strategy=exec_strategy) if name in state and shape != state[name].shape
return compile_program ])
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:
logger.warning('variable {} not used'.format(k))
del state[k]
fluid.io.set_program_state(prog, state)
def init_model(config, program, exe):
"""
load model from checkpoint or pretrained_model
"""
checkpoints = config['Global'].get('checkpoints')
if checkpoints:
path = checkpoints
fluid.load(program, path, exe)
logger.info("Finish initing model from {}".format(path))
pretrain_weights = config['Global'].get('pretrain_weights')
if pretrain_weights:
path = pretrain_weights
load_params(exe, program, path)
logger.info("Finish initing model from {}".format(path))
def save_model(program, model_path):
"""
save model to the target path
"""
fluid.save(program, model_path)
logger.info("Already save model in {}".format(model_path))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册