最近看深度学习的代码时, 大多程序运行时是通过命令行的方式实现的。简单做了的小例子:
代码:
import argparse
import os
def decode(args):
# 参数解析
print('actionName:',args.action)
print('batchSize:',args.batch_size)
print('FilePath:',args.filePath)
print('ckpt:',args.ckpt)
print('----------------------')
#将图像文件路径格式化
root=args.filePath
imgs = [] #图像文件名路径元组
for i in range(10):
img = os.path.join(root, "%03d.png" % i)
mask = os.path.join(root, "%03d_mask.png" % i)
imgs.append((img, mask))
for j in range(3):
print(imgs[j])
print(imgs[j][0])
print(imgs[j][1])
if __name__ == '__main__':
parse = argparse.ArgumentParser()
parse.add_argument("action", type=str, help="train or test")
parse.add_argument('-bat',"--batch_size", type=int, default=8) #使用第二个名称,batch_size默认值设为8
parse.add_argument("--filePath", type=str, default="D:\\")
parse.add_argument("--ckpt", type=str, help="the path of model weight file")
args = parse.parse_args() #获取解析的参数
decode(args) #args参数传给decode函数
运行命令:
python Main.py test --ckpt=weight
运行结果: