def parse_args():
parser = argparse.ArgumentParser(description='Train a recognizer')
parser.add_argument('config', help='train config file path')
parser.add_argument(
'--validate',
action='store_true',
help='whether to evaluate the checkpoint during training')
parser.add_argument(
'--test-last',
action='store_true',
help='whether to test the checkpoint after training')
parser.add_argument(
'--test-best',
action='store_true',
help='whether to test the best checkpoint (if applicable) after training')
parser.add_argument('--seed', type=int, default=None, help='random seed')
parser.add_argument(
'--deterministic',
action='store_true',
help='whether to set deterministic options for CUDNN backend.')
parser.add_argument(
'--launcher',
choices=['pytorch', 'slurm'],
default='pytorch',
help='job launcher')
parser.add_argument(
'--compile',
action='store_true',
help='whether to compile the model before training / testing (only available in pytorch 2.0)')
parser.add_argument('--local_rank', type=int, default=-1)
parser.add_argument('--local-rank', type=int, default=-1)
args = parser.parse_args()
if 'LOCAL_RANK' not in os.environ:
os.environ['LOCAL_RANK'] = str(args.local_rank)
return args
在 argparse.ArgumentParser
中使用 add_argument
方法时,参数名称前面是否加 --
有一些区别。
-
如果参数名称前面没有
--
,则该参数被视为位置参数(positional argument)。位置参数是根据参数的位置来进行解析的,命令行中的参数值与位置参数的位置相对应。例如,parser.add_argument('config', help='train config file path')
定义了一个位置参数config
,它是必需的,并且需要在命令行中以相应的顺序提供。 -
如果参数名称前面加了
--
,则该参数被视为可选参数(optional argument)。可选参数可以按照任意顺序出现在命令行中,并且可以省略不提供。例如,parser.add_argument('--validate', action='store_true', help='whether to evaluate the checkpoint during training')
定义了一个可选参数--validate
,它不需要在命令行中提供参数值,而是根据是否出现来设置为True
。
使用位置参数和可选参数的选择取决于你想要实现的命令行接口的语义。位置参数通常用于必需的参数,而可选参数用于提供更灵活的选项。
请注意,这只是一种约定,你可以根据自己的需求和命令行接口的设计选择使用位置参数或可选参数。
def parse_args():
parser = argparse.ArgumentParser(description='Train a recognizer')
parser.add_argument('--config',default='../configs/posec3d/c3d_light_gym/joint.py', help='train config file path')
parser.add_argument(
'--validate',
action='store_true',
help='whether to evaluate the checkpoint during training')
parser.add_argument(
'--test-last',
action='store_true',
help='whether to test the checkpoint after training')
parser.add_argument(
'--test-best',
action='store_true',
help='whether to test the best checkpoint (if applicable) after training')
parser.add_argument('--seed', type=int, default=None, help='random seed')
parser.add_argument(
'--deterministic',
action='store_true',
help='whether to set deterministic options for CUDNN backend.')
parser.add_argument(
'--launcher',
choices=['pytorch', 'slurm'],
default='pytorch',
help='job launcher')
# parser.add_argument('--local_rank', type=int, default=0)
args = parser.parse_args()
# if 'LOCAL_RANK' not in os.environ:
# os.environ['LOCAL_RANK'] = str(args.local_rank)
return args
参考:
windows环境下使用单GPU训练pyskl下的模型_# if 'local_rank' not in os.environ: # os.environ[-CSDN博客