内置函数简介
dir()函数:列出模块定义的标识符(函数,类或者变量)
getattr(object, name, default):取得object的name成员的value。
如果name成员不存在就返回default的value;
如果default未给出则raise AttributeError.
setattr(object, name, value):新建/修改object的name成员的值为value。
配置文件Config
config.py
#directory to save weights file
CHECKPOINT_PATH = 'checkpoint'
#total training epoches
EPOCH = 200
MILESTONES = [60, 120, 160]
#initial learning rate
#INIT_LR = 0.1
LOW_RES = 64
SUP_SCALE = 2
HIGH_RES = 128
#tensorboard log dir
LOG_DIR = 'runs'
#save weights file per SAVE_EPOCH epoch
SAVE_EPOCH = 10
调用配置文件
read_config.py
import config as _settings
Class Settings:
def __init__(self, settings_module):
for attr in dir(settings_module):
if attr.isupper():
setattr(self, attr, getattr(settings_module, attr))
settings = Settings(_settings)
文件夹直接调用
创建一个文件夹 命名为 conf 存放__init__.py 和config.py两个文件
init.py
import config as _settings
Class Settings:
def __init__(self, settings_module):
for attr in dir(settings_module):
if attr.isupper():
setattr(self, attr, getattr(settings_module, attr))
settings = Settings(_settings)
那么在模型训练文件中可以通过:
from conf import settings
直接调用config里面(函数,类或者变量)的值,setting.HIGH_RES #128