argparse标准库在pytorch-grad-cam实战中的使用举例

参考链接: Python标准库Argparse使用简易指南
参考链接: jacobgil/pytorch-grad-cam

代码展示:

import torch
import argparse

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--use-cuda', action='store_true', default=False,
                        help='Use NVIDIA GPU acceleration')
    parser.add_argument('--image-path', type=str, default='./examples/both.png',
                        help='Input image path')
    args = parser.parse_args()
    args.use_cuda = args.use_cuda and torch.cuda.is_available()
    if args.use_cuda:
        print("Using GPU for acceleration")
    else:
        print("Using CPU for computation")
    return args

args = get_args()
print("打印读取的信息".center(50,'-'))
print('args.use_cuda:', args.use_cuda)
print('args.image_path:', args.image_path)

# python testArgs.py -h
# python testArgs.py
# python testArgs.py --use-cuda
# python testArgs.py --use-cuda --image-path 1.jpg 
# python testArgs.py --image-path 1.jpg --use-cuda

控制台下执行结果展示:

Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>cd C:\Users\chenxuqi\Desktop\News4cxq\test4cxq

C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py -h
usage: testArgs.py [-h] [--use-cuda] [--image-path IMAGE_PATH]

optional arguments:
  -h, --help            show this help message and exit
  --use-cuda            Use NVIDIA GPU acceleration
  --image-path IMAGE_PATH
                        Input image path

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py
Using CPU for computation
---------------------打印读取的信息----------------------
args.use_cuda: False
args.image_path: ./examples/both.png

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py --image-path 1.jpg
Using CPU for computation
---------------------打印读取的信息----------------------
args.use_cuda: False
args.image_path: 1.jpg

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py --use-cuda
Using GPU for acceleration
---------------------打印读取的信息----------------------
args.use_cuda: True
args.image_path: ./examples/both.png

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py --use-cuda --image-path 1.jpg
Using GPU for acceleration
---------------------打印读取的信息----------------------
args.use_cuda: True
args.image_path: 1.jpg

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>python testArgs.py --image-path 1.jpg --use-cuda
Using GPU for acceleration
---------------------打印读取的信息----------------------
args.use_cuda: True
args.image_path: 1.jpg

(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>
(ssd4pytorch1_2_0) C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值