config.py 3.1 KB
Newer Older
S
Steffy-zxf 已提交
1
#coding:utf-8
Z
Zeyu Chen 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   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.

16 17 18
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Z
Zeyu Chen 已提交
19

Z
Zeyu Chen 已提交
20
from datetime import datetime
21 22
import time

W
wuzewu 已提交
23 24
from paddlehub.finetune.strategy import DefaultStrategy
from paddlehub.common.logger import logger
25

26 27

class RunConfig(object):
28 29 30 31 32
    """ This class specifies the configurations for PaddleHub to finetune """

    def __init__(self,
                 log_interval=10,
                 eval_interval=100,
W
wuzewu 已提交
33
                 use_pyreader=False,
W
wuzewu 已提交
34
                 use_data_parallel=False,
35
                 save_ckpt_interval=None,
Z
Zeyu Chen 已提交
36
                 use_cuda=True,
37
                 checkpoint_dir=None,
Z
Zeyu Chen 已提交
38 39
                 num_epoch=1,
                 batch_size=32,
40
                 enable_memory_optim=True,
Z
Zeyu Chen 已提交
41
                 strategy=None):
42 43 44 45 46 47 48 49
        """ Construct finetune Config """
        self._log_interval = log_interval
        self._eval_interval = eval_interval
        self._save_ckpt_interval = save_ckpt_interval
        self._use_cuda = use_cuda
        self._checkpoint_dir = checkpoint_dir
        self._num_epoch = num_epoch
        self._batch_size = batch_size
W
wuzewu 已提交
50
        self._use_pyreader = use_pyreader
W
wuzewu 已提交
51
        self._use_data_parallel = use_data_parallel
Z
Zeyu Chen 已提交
52 53 54 55
        if strategy is None:
            self._strategy = DefaultStrategy()
        else:
            self._strategy = strategy
56
        self._enable_memory_optim = enable_memory_optim
W
wuzewu 已提交
57
        if checkpoint_dir is None:
58 59 60
            now = int(time.time())
            time_str = time.strftime("%Y%m%d%H%M%S", time.localtime(now))
            self._checkpoint_dir = "ckpt_" + time_str
W
wuzewu 已提交
61 62
        else:
            self._checkpoint_dir = checkpoint_dir
63
        logger.info("Checkpoint dir: {}".format(self._checkpoint_dir))
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

    @property
    def log_interval(self):
        return self._log_interval

    @property
    def eval_interval(self):
        return self._eval_interval

    @property
    def save_ckpt_interval(self):
        return self._save_ckpt_interval

    @property
    def use_cuda(self):
        return self._use_cuda

    @property
    def checkpoint_dir(self):
        return self._checkpoint_dir

    @property
    def num_epoch(self):
        return self._num_epoch

    @property
    def batch_size(self):
        return self._batch_size

    @property
Z
Zeyu Chen 已提交
94 95
    def strategy(self):
        return self._strategy
96 97 98 99

    @property
    def enable_memory_optim(self):
        return self._enable_memory_optim
W
wuzewu 已提交
100 101 102 103

    @property
    def use_pyreader(self):
        return self._use_pyreader
W
wuzewu 已提交
104 105 106 107

    @property
    def use_data_parallel(self):
        return self._use_data_parallel