From 1de07a4a4ae269eca3b879a7773dbe32f9ab330a Mon Sep 17 00:00:00 2001 From: SunGaofeng Date: Fri, 5 Jul 2019 13:24:03 +0800 Subject: [PATCH] add cuda check to show errors when run on paddle compiled with cpu only (#2732) * add cuda check to show errors when run on paddle compiled with cpu only * change use_cuda to use_gpu in the error infomation to be consistent with code --- PaddleCV/PaddleVideo/infer.py | 9 ++++++++- PaddleCV/PaddleVideo/test.py | 9 ++++++++- PaddleCV/PaddleVideo/train.py | 11 +++++++++-- PaddleCV/PaddleVideo/utils.py | 13 +++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/PaddleCV/PaddleVideo/infer.py b/PaddleCV/PaddleVideo/infer.py index 7f19dd52..41b20f14 100644 --- a/PaddleCV/PaddleVideo/infer.py +++ b/PaddleCV/PaddleVideo/infer.py @@ -17,6 +17,7 @@ import sys import time import logging import argparse +import ast import numpy as np try: import cPickle as pickle @@ -27,6 +28,7 @@ import paddle.fluid as fluid from config import * import models from datareader import get_reader +from utils import check_cuda logging.root.handlers = [] FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s' @@ -47,7 +49,10 @@ def parse_args(): default='configs/attention_cluster.txt', help='path to config file of model') parser.add_argument( - '--use_gpu', type=bool, default=True, help='default use gpu.') + '--use_gpu', + type=ast.literal_eval, + default=True, + help='default use gpu.') parser.add_argument( '--weights', type=str, @@ -155,6 +160,8 @@ def infer(args): if __name__ == "__main__": args = parse_args() + # check whether the installed paddle is compiled with GPU + check_cuda(args.use_gpu) logger.info(args) infer(args) diff --git a/PaddleCV/PaddleVideo/test.py b/PaddleCV/PaddleVideo/test.py index c178811c..10ef5355 100644 --- a/PaddleCV/PaddleVideo/test.py +++ b/PaddleCV/PaddleVideo/test.py @@ -17,6 +17,7 @@ import sys import time import logging import argparse +import ast import numpy as np import paddle.fluid as fluid @@ -24,6 +25,7 @@ from config import * import models from datareader import get_reader from metrics import get_metrics +from utils import check_cuda logging.root.handlers = [] FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s' @@ -49,7 +51,10 @@ def parse_args(): default=None, help='test batch size. None to use config file setting.') parser.add_argument( - '--use_gpu', type=bool, default=True, help='default use gpu.') + '--use_gpu', + type=ast.literal_eval, + default=True, + help='default use gpu.') parser.add_argument( '--weights', type=str, @@ -141,6 +146,8 @@ def test(args): if __name__ == "__main__": args = parse_args() + # check whether the installed paddle is compiled with GPU + check_cuda(args.use_gpu) logger.info(args) test(args) diff --git a/PaddleCV/PaddleVideo/train.py b/PaddleCV/PaddleVideo/train.py index eb2a72e5..ac42f361 100644 --- a/PaddleCV/PaddleVideo/train.py +++ b/PaddleCV/PaddleVideo/train.py @@ -16,6 +16,7 @@ import os import sys import time import argparse +import ast import logging import numpy as np import paddle.fluid as fluid @@ -25,6 +26,7 @@ import models from config import * from datareader import get_reader from metrics import get_metrics +from utils import check_cuda logging.root.handlers = [] FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s' @@ -67,7 +69,10 @@ def parse_args(): help='path to resume training based on previous checkpoints. ' 'None for not resuming any checkpoints.') parser.add_argument( - '--use_gpu', type=bool, default=True, help='default use gpu.') + '--use_gpu', + type=ast.literal_eval, + default=True, + help='default use gpu.') parser.add_argument( '--no_use_pyreader', action='store_true', @@ -100,7 +105,7 @@ def parse_args(): help='mini-batch interval to log.') parser.add_argument( '--enable_ce', - type=bool, + type=ast.literal_eval, default=False, help='If set True, enable continuous evaluation job.') args = parser.parse_args() @@ -277,6 +282,8 @@ def train(args): if __name__ == "__main__": args = parse_args() + # check whether the installed paddle is compiled with GPU + check_cuda(args.use_gpu) logger.info(args) if not os.path.exists(args.save_dir): diff --git a/PaddleCV/PaddleVideo/utils.py b/PaddleCV/PaddleVideo/utils.py index 93aa0cb7..02414386 100644 --- a/PaddleCV/PaddleVideo/utils.py +++ b/PaddleCV/PaddleVideo/utils.py @@ -14,6 +14,8 @@ import os import signal +import paddle +import paddle.fluid as fluid __all__ = ['AttrDict'] @@ -36,3 +38,14 @@ class AttrDict(dict): self.__dict__[key] = value else: self[key] = value + +def check_cuda(use_cuda, err = \ + "\nYou can not set use_gpu = True in the model because you are using paddlepaddle-cpu.\n \ + Please: 1. Install paddlepaddle-gpu to run your models on GPU or 2. Set use_gpu = False to run models on CPU.\n" + ): + try: + if use_cuda == True and fluid.is_compiled_with_cuda() == False: + print(err) + sys.exit(1) + except Exception as e: + pass -- GitLab