使用python内置函数dir() getattr() setattr()编写模型Config配置文件

内置函数简介

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值