添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
max_iterators: 1000000 pretrain_model_path: models/pretrain/yolo_tiny .ckpt train_dir: models/train

这是一份训练深度学习模型以实现目标检测的配置文件train.cfg,里面保存了学习率、batch_size等信息。其中[Common], [DataSet], [Net], [Solver]叫Section,Section中的内容叫option, 如Common中的Option有:image_size, batch_size, num_classes, max_objects_per_image。每一个option都会有取值。

ConfigParser模块读取配置文件

  • read(config_filename):读取配置文件内容
  • sections():列表形式返回配置文件所有section
  • options(sec):返回sec中的所有option
  • items(sec):返回sec中所有option及其取值
  • get(sec, opt):返回sec中opt的值
  • getint(sec, opt):同get(),但返回值是int型
  • getfloat(sec, opt):类似getint()
"""python
Description: a test code of reading configure file with ConfigParser library.
Author: Meringue
Date: 2018/2/7 
import ConfigParser
# create a configure object and read a configure file
conf_file = "train.cfg" 
config = ConfigParser.ConfigParser()
config.read(conf_file)
secs = config.sections()  # return a list of all section names.
print "secs = ", secs #['Common', 'DataSet', 'Net', 'Solver']
common = config.options(secs[0])
print "options in commom = ", common
common_items = config.items(secs[0])
print "items in commom = ", common_items
image_size = config.get(secs[0], "image_size")
print "image_size = ", image_size
batch_size = config.getint(secs[0], "batch_size")
print "batch_size = ", batch_size
learning_rate = config.getfloat("Solver", "learning_rate")
print "learning_rate = ", learning_rate

代码运行结果

secs =  ['Common', 'DataSet', 'Net', 'Solver']
options in commom =  ['image_size', 'batch_size', 'num_classes', 'max_objects_per_image']
items in commom =  [('image_size', '448'), ('batch_size', '16'), ('num_classes', '20'), ('max_objects_per_image', '20')]
image_size =  448
batch_size =  16
learning_rate =  1e-05
import configparser config = configparser . ConfigParser () config.read("ini", encoding="utf-8") 2、读文件、添加、删除section,删除一个配置项 import configparser config = configparser . ConfigParser () 一、 ConfigParser 简介 ConfigParser 是用来 读取 配置文件 的包。 配置文件 的格式如下: 括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。[db]db_host = 127.0.0.1db_port = 69db_user = rootdb_pass = roothost_port = 69[concurrent]thread = ... 原标题: Python 如何配置config文件? 所有程序一般都会有需要保存的变量,用来初始化程序的初始配置。我们从实战角度,说一下 配置文件 config.ini 如何使用吧,比如我们的程序 ,需要一个 url 、一个 runner 和一个 run_cnt 变量,这几个变量在程序运行之前需要配置好,我们下面来举例为大家详细说明。1、首先得新建一个 .ini 的文件,习惯性叫做 config.ini2... passwd=123456二、测试 读取 config_ini 配置文件 .py # 读取 配置数据.ini (ConfigParse类,自带的 在lib下 configparser .py),学习代码如下: import configparser #实例化对象 conf = configparser ConfigParser 模块 get官方文档解释如下:The ConfigParser class extends some methods of the Raw ConfigParser interface, adding some optional arguments. ConfigParser .get(section, option[, raw[, vars]])Get an option v...