ArgumentParser中的formatter_class=argparse.ArgumentDefaultsHelpFormatter

这篇博客讲述了如何在Python的argparse模块中使用ArgumentDefaultsHelpFormatter来在帮助信息中显示参数的默认值。通过示例代码展示了启用和禁用此格式器的区别,当启用时,参数的帮助信息会明确显示其默认值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

description是一个描述信息,ArgumentDefaultsHelpFormatter自动添加默认的值的信息到每一个帮助信息的参数中。第一段代码用了ArgumentDefaultsHelpFormatter,打印出了default默认值,第二段代码没用并没有打印出默认值。

#第一段代码

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="Block_FedAvg_Simulation")

# debug attributes
parser.add_argument('-g', '--gpu', type=str, default='0', help='gpu id to use(e.g. 0,1,2,3)')

parser.print_help()



#执行结果:
usage: ipykernel_launcher.py [-h] [-g GPU]

Block_FedAvg_Simulation

optional arguments:
  -h, --help         show this help message and exit
  -g GPU, --gpu GPU  gpu id to use(e.g. 0,1,2,3) (default: 0)

可以看出打印了默认这default:0
#第二段代码

parser1 = argparse.ArgumentParser(description="Block_FedAvg_Simulation")
parser1.add_argument('-g', '--gpu', type=str, default='0', help='gpu id to use(e.g. 0,1,2,3)')
parser1.print_help()





#执行结果
usage: ipykernel_launcher.py [-h] [-g GPU]

Block_FedAvg_Simulation

optional arguments:
  -h, --help         show this help message and exit
  -g GPU, --gpu GPU  gpu id to use(e.g. 0,1,2,3)

经检查demo.py代码如下import os os.sys.path.append("../") # Add the project directory from pathlib import Path import torch from utlis import JamMa, cfg from src.utils.dataset import read_megadepth_color import argparse from loguru import logger import torch.nn.functional as F from src.utils.plotting import make_confidence_figure, make_evaluation_figure_wheel if __name__ == '__main__': parser = argparse.ArgumentParser( description='Image pair matching with JamMa', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( '--image1', type=str, default='512', help='Path to the source image') parser.add_argument( '--image2', type=str, default='512', help='Path to the target image') parser.add_argument( '--output_dir', type=str, default='output/', help='Path of the outputs') opt = parser.parse_args() Path(opt.output_dir).mkdir(exist_ok=True, parents=True) device = 'cuda' if torch.cuda.is_available() else 'cpu' jamma = JamMa(config=cfg).eval().to(device) image0, scale0, mask0, prepad_size0 = read_megadepth_color(opt.image1, 832, 16, True) image1, scale1, mask1, prepad_size1 = read_megadepth_color(opt.image2, 832, 16, True) mask0 = F.interpolate(mask0[None, None].float(), scale_factor=0.125, mode='nearest', recompute_scale_factor=False)[0].bool() mask1 = F.interpolate(mask1[None, None].float(), scale_factor=0.125, mode='nearest', recompute_scale_factor=False)[0].bool() data = { 'imagec_0': image0.to(device), 'imagec_1': image1.to(device), 'mask0': mask0.to(device), 'mask1': mask1.to(device), } logger.info(f"Matching: {opt.image1} and {opt.image2}") jamma(data) logger.info(f"Finish Matching, Visualizing") make_confidence_figure(data, path=opt.output_dir+'viz1.png', dpi=300, topk=4000) make_evaluation_figure_wheel(data, path=opt.output_dir+'viz2.png', topk=4000) logger.info(f"Done")请根据代码内容,对上面问题作出解答
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值