[Faster R-CNN] 项目中涉及到的python小知识(二):optparse模块

optparse是专门用来在命令行添加选项的一个模块。

from optparse import OptionParser  # 导入模块、包

parser = OptionParser() # 实例化对象parser 

#下面的代码通过parser.add_option() 作用于对象parser 
#主要参数有:dest 变量名(字典的键) default 变量的值(字典的值) help 存储帮助信息 

parser = OptionParser()
parser.add_option("-p", "--path", dest="train_path", help="Path to training data.", default="data/")
parser.add_option("-n", "--num_rois", dest="num_rois", help="Number of ROIs per iteration. Higher means more memory use.", default=32)
parser.add_option("--hf", dest="horizontal_flips", help="Augment with horizontal flips in training. (Default=true).", action="store_true", default=False)
parser.add_option("--vf", dest="vertical_flips", help="Augment with vertical flips in training. (Default=false).", action="store_true", default=False)
parser.add_option("--rot", "--rot_90", dest="rot_90", help="Augment with 90 degree rotations in training. (Default=false).", action="store_true", default=False)
parser.add_option("--num_epochs", dest="num_epochs", help="Number of epochs.", default=2000)
parser.add_option("--config_filename", dest="config_filename", help="Location to store all the metadata related to the training (to be used when testing).", default="config/config.pickle")
parser.add_option("--output_weight_path", dest="output_weight_path", help="Output path for weights.", default='model/model_frcnn.hdf5')
parser.add_option("--input_weight_path", dest="input_weight_path", help="Input path for weights. If not specified, will try to load default weights provided by keras.")

#下面通过 parser.parse_args() 可以获取键值对组成的字典,列表[]这里没涉及,可以添加的

(options, args) = parser.parse_args()
print(options) #键值对组成的字典
print(args) #列表[]
options.train_path #获取字典options 键train_path的值  
#ps:其实parser.add_option()的 "-n", "--num_rois" 可以省略"-n"

效果图如下:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41196817/article/details/82462523