trainer.py 18.8 KB
Newer Older
X
xixiaoyao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- coding: UTF-8 -*-
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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 paddlepalm.interface import reader as base_reader
from paddlepalm.interface import task_paradigm as base_paradigm
import os
import json
from paddle import fluid
X
xixiaoyao 已提交
21 22
import importlib
from paddlepalm.default_settings import *
X
xixiaoyao 已提交
23

X
xixiaoyao 已提交
24

X
xixiaoyao 已提交
25 26 27 28 29
def Trainer(object):

    def __init__(self, name, reader, task, mix_ratio=1.0, \
                 save_predict_model=True, save_path=None, save_steps=-1)\
                 reuse_with=None, silent=False):
X
xixiaoyao 已提交
30 31 32 33

        self._name = name
        self._verbose = not silent

X
xixiaoyao 已提交
34 35 36 37 38 39
        if save_predict_model:
            assert save_path is not None, "save_path is required when save_predict_model is set."
            assert save_steps == -1 or save_steps > 0, "save_steps should be -1 (only save the last step of this task) or larger than 0"
            assert pred_reader is not None and pred_task is not None, ""

        self._save_infermodel_path = os.path.join(self._config['save_path'], self._name, 'infer_model')
X
xixiaoyao 已提交
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 74 75 76 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        else:
            self._save_infermodel_path = infermodel_save_path

        self._save_infermodel_every_n_steps = save_infermodel_every_n_steps

        self._is_target = as_target
        self._first_target = False
        self._task_reuse_scope = name if task_layer_reuse is None else task_layer_reuse

        self._feeded_var_names = None
        self._target_vars = None

        # training process management
        self._mix_ratio = mix_ratio
        self._expected_train_steps = None
        self._expected_train_epochs = None
        self._steps_pur_epoch = None
        self._cur_train_epoch = 0
        self._cur_train_step = 0
        self._train_finish = False

        # 存放不同运行阶段(train,eval,pred)的数据集reader,key为phase,value为Reader实例
        self._reader = {'train': reader, 'eval': None, 'pred': pred_reader}
        self._input_layer = None
        self._inputname_to_varname = {}
        self._task_layer = {'train': tasklayer, 'eval': None, 'pred': pred_tasklayer}
        self._pred_input_name_list = []
        self._pred_input_varname_list = []
        self._pred_fetch_name_list = []
        self._pred_fetch_var_list = []

        self._exe = fluid.Executor(fluid.CPUPlace())

        self._save_protocol = {
            'input_names': 'self._pred_input_name_list',
            'input_varnames': 'self._pred_input_varname_list',
            'fetch_list': 'self._pred_fetch_name_list'}

        self._lock = False

    def _build_task_layer(self, net_inputs, phase, scope=""):
        output_vars = self._task_layer[phase].build(net_inputs, scope_name=scope)
        if phase == 'pred':
            if output_vars is not None:
                self._pred_fetch_name_list, self._pred_fetch_var_list = zip(*output_vars.items())
            else:
                self._pred_fetch_name_list = []
                self._pred_fetch_var_list = []
        return output_vars

    def _postprocess(self, rt_outputs, phase):
        return self._task_layer[phase].postprocess(rt_outputs)

    def _epoch_postprocess(self, epoch_inputs, phase):
        return self._task_layer[phase].epoch_postprocess(epoch_inputs)
    
    def save(self, suffix=''):
        dirpath = self._save_infermodel_path + suffix
        self._pred_input_varname_list = [str(i) for i in self._pred_input_varname_list]

        prog = fluid.default_main_program().clone()
        fluid.io.save_inference_model(dirpath, self._pred_input_varname_list, self._pred_fetch_var_list, self._exe, prog)

        conf = {}
        for k, strv in self._save_protocol.items(): 
            d = None
            v = locals()
            exec('d={}'.format(strv), globals(), v)
            conf[k] = v['d']
        with open(os.path.join(dirpath, '__conf__'), 'w') as writer:
            writer.write(json.dumps(conf, indent=1))
        print(self._name + ': inference model saved at ' + dirpath)

    def _load(self, infer_model_path=None):
        if infer_model_path is None:
            infer_model_path = self._save_infermodel_path
        for k,v in json.load(open(os.path.join(infer_model_path, '__conf__'))).items(): 
            strv = self._save_protocol[k]
            exec('{}=v'.format(strv))
        pred_prog, self._pred_input_varname_list, self._pred_fetch_var_list = \
            fluid.io.load_inference_model(infer_model_path, self._exe)
        print(self._name+': inference model loaded from ' + infer_model_path)
        return pred_prog

    @property
    def name(self):
        return self._name

    @property
    def _Reader(self):
        return self._Reader

    @property
    def _Paradigm(self):
        return self._Paradigm

    @property
    def _reader(self):
        return self._reader

    @property
    def _pred_input(self):
        return zip(*[self._pred_input_name_list, self._pred_input_varname_list])

    @_pred_input.setter
    def _pred_input(self, val):
        assert isinstance(val, dict)
        self._pred_input_name_list, self._pred_input_varname_list = \
            zip(*[[k, v.name] for k,v in val.items()])

    @property
    def _pred_fetch_list(self):
        return [self._pred_fetch_name_list, self._pred_fetch_var_list]

    @property
    def _task_layer(self):
        return self._task_layer

    @property
    def _is_first_target(self):
        return self._is_first_target

    @_is_first_target.setter
    def _is_first_target(self, value):
        self._is_first_target = bool(value)
        if self._is_first_target:
            assert self._is_target, "ERROR: only target task could be set as main task."
        if self._verbose and self._is_first_target:
            print("{}: set as main task".format(self._name))

    @property
    def _is_target(self):
        if self._is_target is not None:
            return self._is_target
        else:
            raise ValueError("{}: is_target is None".format(self._name))

    @_is_target.setter
    def _is_target(self, value):
        self._is_target = bool(value)
        if self._verbose:
            if self._is_target:
                print('{}: set as target task.'.format(self._name))
            else:
                print('{}: set as aux task.'.format(self._name))

    @property
    def mix_ratio(self):
        if self._mix_ratio is not None:
            return self._mix_ratio
        else:
            raise ValueError("{}: mix_ratio is None".format(self._name))

    @mix_ratio.setter
    def mix_ratio(self, value):
        self._mix_ratio = float(value)
        if self._verbose:
            print('{}: mix_ratio is set to {}'.format(self._name, self._mix_ratio))

    @property
    def save_infermodel_every_n_steps(self):
        return self._save_infermodel_every_n_steps

    @save_infermodel_every_n_steps.setter
    def save_infermodel_every_n_steps(self, val):
        self._save_infermodel_every_n_steps = val

    @property
    def expected_train_steps(self):
        return self._expected_train_steps

    @expected_train_steps.setter
    def _expected_train_steps(self, value):
        self._expected_train_steps = value
        self._expected_train_epochs = value / float(self._steps_pur_epoch)

    @property
    def expected_train_epochs(self):
        return self._expected_train_epochs

    @property
    def cur_train_epoch(self):
        return self._cur_train_epoch

    @cur_train_epoch.setter
    def _cur_train_epoch(self, value):
        self._cur_train_epoch = value

    @property
    def cur_train_step(self):
        return self._cur_train_step

    @cur_train_step.setter
    def _cur_train_step(self, value):
        self._cur_train_step = value
        if self._cur_train_step > self._steps_pur_epoch:
            self._cur_train_epoch += 1
            self._cur_train_step = 1
        if self._is_target and self._cur_train_step + self._cur_train_epoch * self._steps_pur_epoch >= self._expected_train_steps:
            self._train_finish = True

    @property
    def steps_pur_epoch(self):
        return self._steps_pur_epoch

    @steps_pur_epoch.setter
    def _steps_pur_epoch(self, value):
        self._steps_pur_epoch = value

    @property
    def train_finish(self):
        return self._train_finish

    def tasklayer_reuse_with(self, task):
        assert isinstance(task, Task)
        if self._lock:
            raise Exception('you can only set tasklayer reuses BEFORE Controller created.')
        self._task_reuse_scope = task.name
    
    def _set_lock(self):
        self._lock = True

    # @property
    # def task_reuse_scope(self):
    #     if self._task_reuse_scope is not None:
    #         return self._task_reuse_scope
    #     else:
    #         raise ValueError("{}: task_reuse_scope is None".format(self._name))

    # @task_reuse_scope.setter
    # def task_reuse_scope(self, scope_name):
    #     self._task_reuse_scope = str(scope_name)
    #     if self._verbose:
    #         print('{}: task_reuse_scope is set to {}'.format(self._name, self._task_reuse_scope))


X
xixiaoyao 已提交
276 277 278 279 280 281
def check_req_args(conf, name):
    assert 'reader' in conf, name+': reader is required to build TaskInstance.'
    assert 'paradigm' in conf, name+': paradigm is required to build TaskInstance.'
    assert 'train_file' in conf or 'pred_file' in conf, name+': at least train_file or pred_file should be provided to build TaskInstance.'


X
xixiaoyao 已提交
282 283
class TaskInstance(object):
    
X
xixiaoyao 已提交
284
    def __init__(self, name, id, config, verbose=True):
X
xixiaoyao 已提交
285 286 287 288
        self._name = name
        self._config = config
        self._verbose = verbose

X
xixiaoyao 已提交
289
        check_req_args(config, name)
X
xixiaoyao 已提交
290 291 292 293 294 295 296 297

        # parse Reader and Paradigm
        reader_name = config['reader']
        reader_mod = importlib.import_module(READER_DIR + '.' + reader_name)
        Reader = getattr(reader_mod, 'Reader')

        parad_name = config['paradigm']
        parad_mod = importlib.import_module(PARADIGM_DIR + '.' + parad_name)
X
xixiaoyao 已提交
298
        Paradigm = getattr(parad_mod, 'TaskType')
X
xixiaoyao 已提交
299 300 301 302

        self._Reader = Reader
        self._Paradigm = Paradigm

X
xixiaoyao 已提交
303
        self._save_infermodel_path = os.path.join(self._config['save_path'], self._name, 'infer_model')
X
xixiaoyao 已提交
304
        self._save_ckpt_path = os.path.join(self._config['save_path'], 'ckpt')
X
xixiaoyao 已提交
305
        self._save_infermodel_every_n_steps = config.get('save_infermodel_every_n_steps', -1)
X
xixiaoyao 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

        # following flags can be fetch from instance config file
        self._is_target = config.get('is_target', True)
        self._first_target = config.get('is_first_target', False)
        self._task_reuse_scope = config.get('task_reuse_scope', name)

        self._feeded_var_names = None
        self._target_vars = None

        # training process management
        self._mix_ratio = None
        self._expected_train_steps = None
        self._expected_train_epochs = None
        self._steps_pur_epoch = None
        self._cur_train_epoch = 0
        self._cur_train_step = 0
        self._train_finish = False

        # 存放不同运行阶段(train,eval,pred)的数据集reader,key为phase,value为Reader实例
        self._reader = {'train': None, 'eval': None, 'pred': None}
        self._input_layer = None
        self._inputname_to_varname = {}
        self._task_layer = {'train': None, 'eval': None, 'pred': None}
        self._pred_input_name_list = []
        self._pred_input_varname_list = []
        self._pred_fetch_name_list = []
        self._pred_fetch_var_list = []

        self._exe = fluid.Executor(fluid.CPUPlace())

        self._save_protocol = {
            'input_names': 'self._pred_input_name_list',
            'input_varnames': 'self._pred_input_varname_list',
            'fetch_list': 'self._pred_fetch_name_list'}


X
xixiaoyao 已提交
342 343
    def build_task_layer(self, net_inputs, phase, scope=""):
        output_vars = self._task_layer[phase].build(net_inputs, scope_name=scope)
X
xixiaoyao 已提交
344
        if phase == 'pred':
X
xixiaoyao 已提交
345
            if output_vars is not None:
W
wangxiao 已提交
346
                self._pred_fetch_name_list, self._pred_fetch_var_list = zip(*output_vars.items())
X
xixiaoyao 已提交
347 348 349
            else:
                self._pred_fetch_name_list = []
                self._pred_fetch_var_list = []
X
xixiaoyao 已提交
350 351 352 353 354 355 356 357 358 359 360 361
        return output_vars

    def postprocess(self, rt_outputs, phase):
        return self._task_layer[phase].postprocess(rt_outputs)

    def epoch_postprocess(self, epoch_inputs, phase):
        return self._task_layer[phase].epoch_postprocess(epoch_inputs)
    
    def save(self, suffix=''):
        dirpath = self._save_infermodel_path + suffix
        self._pred_input_varname_list = [str(i) for i in self._pred_input_varname_list]

X
xixiaoyao 已提交
362 363 364
        # fluid.io.save_inference_model(dirpath, self._pred_input_varname_list, self._pred_fetch_var_list, self._exe, export_for_deployment = True)
        prog = fluid.default_main_program().clone()
        fluid.io.save_inference_model(dirpath, self._pred_input_varname_list, self._pred_fetch_var_list, self._exe, prog)
X
xixiaoyao 已提交
365 366

        conf = {}
W
wangxiao 已提交
367
        for k, strv in self._save_protocol.items(): 
W
wangxiao 已提交
368
            d = None
W
wangxiao 已提交
369 370
            v = locals()
            exec('d={}'.format(strv), globals(), v)
W
wangxiao 已提交
371
            conf[k] = v['d']
X
xixiaoyao 已提交
372 373
        with open(os.path.join(dirpath, '__conf__'), 'w') as writer:
            writer.write(json.dumps(conf, indent=1))
X
xixiaoyao 已提交
374
        print(self._name + ': inference model saved at ' + dirpath)
X
xixiaoyao 已提交
375 376 377 378

    def load(self, infer_model_path=None):
        if infer_model_path is None:
            infer_model_path = self._save_infermodel_path
W
wangxiao 已提交
379
        for k,v in json.load(open(os.path.join(infer_model_path, '__conf__'))).items(): 
X
xixiaoyao 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
            strv = self._save_protocol[k]
            exec('{}=v'.format(strv))
        pred_prog, self._pred_input_varname_list, self._pred_fetch_var_list = \
            fluid.io.load_inference_model(infer_model_path, self._exe)
        print(self._name+': inference model loaded from ' + infer_model_path)
        return pred_prog

    @property
    def name(self):
        return self._name

    @property
    def Reader(self):
        return self._Reader

X
xixiaoyao 已提交
395 396 397 398 399 400
    # @Reader.setter
    # def Reader(self, cls):
    #     assert base_reader.__name__ == cls.__bases__[-1].__name__, \
    #         "expect: {}, receive: {}.".format(base_reader.__name__, \
    #                                           cls.__bases__[-1].__name__)
    #     self._Reader = cls
X
xixiaoyao 已提交
401 402 403 404 405

    @property
    def Paradigm(self):
        return self._Paradigm

X
xixiaoyao 已提交
406 407 408 409 410 411
    # @Paradigm.setter
    # def Paradigm(self, cls):
    #     assert base_paradigm.__name__ == cls.__bases__[-1].__name__, \
    #         "expect: {}, receive: {}.".format(base_paradigm.__name__, \
    #                                           cls.__bases__[-1].__name__)
    #     self._Paradigm = cls
X
xixiaoyao 已提交
412 413 414 415 416 417 418 419 420 421 422

    @property
    def config(self):
        return self._config

    @property
    def reader(self):
        return self._reader

    @property
    def pred_input(self):
W
wangxiao 已提交
423
        return zip(*[self._pred_input_name_list, self._pred_input_varname_list])
X
xixiaoyao 已提交
424 425 426 427 428

    @pred_input.setter
    def pred_input(self, val):
        assert isinstance(val, dict)
        self._pred_input_name_list, self._pred_input_varname_list = \
W
wangxiao 已提交
429
            zip(*[[k, v.name] for k,v in val.items()])
X
xixiaoyao 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

    @property
    def pred_fetch_list(self):
        return [self._pred_fetch_name_list, self._pred_fetch_var_list]

    @property
    def task_layer(self):
        return self._task_layer

    @property
    def is_first_target(self):
        return self._is_first_target

    @is_first_target.setter
    def is_first_target(self, value):
        self._is_first_target = bool(value)
        if self._is_first_target:
            assert self._is_target, "ERROR: only target task could be set as main task."
        if self._verbose and self._is_first_target:
            print("{}: set as main task".format(self._name))

    @property
    def is_target(self):
        if self._is_target is not None:
            return self._is_target
        else:
            raise ValueError("{}: is_target is None".format(self._name))

    @is_target.setter
    def is_target(self, value):
        self._is_target = bool(value)
        if self._verbose:
            if self._is_target:
                print('{}: set as target task.'.format(self._name))
            else:
                print('{}: set as aux task.'.format(self._name))

    @property
    def mix_ratio(self):
        if self._mix_ratio is not None:
            return self._mix_ratio
        else:
            raise ValueError("{}: mix_ratio is None".format(self._name))

    @mix_ratio.setter
    def mix_ratio(self, value):
        self._mix_ratio = float(value)
        if self._verbose:
            print('{}: mix_ratio is set to {}'.format(self._name, self._mix_ratio))

X
xixiaoyao 已提交
480 481 482 483
    @property
    def save_infermodel_every_n_steps(self):
        return self._save_infermodel_every_n_steps

X
xixiaoyao 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    @property
    def expected_train_steps(self):
        return self._expected_train_steps

    @expected_train_steps.setter
    def expected_train_steps(self, value):
        self._expected_train_steps = value
        self._expected_train_epochs = value / float(self._steps_pur_epoch)

    @property
    def expected_train_epochs(self):
        return self._expected_train_epochs

    @property
    def cur_train_epoch(self):
        return self._cur_train_epoch

    @cur_train_epoch.setter
    def cur_train_epoch(self, value):
        self._cur_train_epoch = value

    @property
    def cur_train_step(self):
        return self._cur_train_step

    @cur_train_step.setter
    def cur_train_step(self, value):
        self._cur_train_step = value
        if self._cur_train_step > self._steps_pur_epoch:
            self._cur_train_epoch += 1
            self._cur_train_step = 1
        if self._is_target and self._cur_train_step + self._cur_train_epoch * self._steps_pur_epoch >= self._expected_train_steps:
            self._train_finish = True

    @property
    def steps_pur_epoch(self):
        return self._steps_pur_epoch

    @steps_pur_epoch.setter
    def steps_pur_epoch(self, value):
        self._steps_pur_epoch = value

    @property
    def train_finish(self):
        return self._train_finish

    @property
    def task_reuse_scope(self):
        if self._task_reuse_scope is not None:
            return self._task_reuse_scope
        else:
            raise ValueError("{}: task_reuse_scope is None".format(self._name))

    @task_reuse_scope.setter
    def task_reuse_scope(self, scope_name):
        self._task_reuse_scope = str(scope_name)
        if self._verbose:
            print('{}: task_reuse_scope is set to {}'.format(self._name, self._task_reuse_scope))





        

def check_instances(insts):
    """to check ids, first_target"""
    pass

def _check_ids():
    pass

def _check_targets():
    pass

def _check_reuse_scopes():
    pass