From d46b37bb10389ecdca13d724430b08092a82b53f Mon Sep 17 00:00:00 2001 From: FDInSky <48318485+FDInSky@users.noreply.github.com> Date: Fri, 5 Jul 2019 19:31:02 +0800 Subject: [PATCH] add check gpu for deeplabe3+, icnet, rcnn (#2738) * add check gpu for deeplabe3+, icnet, rcnn * modify based on review * modify based on review --- PaddleCV/deeplabv3+/eval.py | 1 + PaddleCV/deeplabv3+/train.py | 1 + PaddleCV/deeplabv3+/utility.py | 20 ++++++++++++++++++++ PaddleCV/icnet/eval.py | 3 ++- PaddleCV/icnet/infer.py | 3 ++- PaddleCV/icnet/train.py | 3 ++- PaddleCV/icnet/utils.py | 20 ++++++++++++++++++++ PaddleCV/rcnn/eval_coco_map.py | 3 ++- PaddleCV/rcnn/infer.py | 3 ++- PaddleCV/rcnn/train.py | 3 ++- PaddleCV/rcnn/utility.py | 20 ++++++++++++++++++++ 11 files changed, 74 insertions(+), 6 deletions(-) diff --git a/PaddleCV/deeplabv3+/eval.py b/PaddleCV/deeplabv3+/eval.py index 94d82e70..99e49e11 100644 --- a/PaddleCV/deeplabv3+/eval.py +++ b/PaddleCV/deeplabv3+/eval.py @@ -55,6 +55,7 @@ def load_model(): CityscapeDataset = reader.CityscapeDataset args = parser.parse_args() +utility.check_gpu(args.use_gpu) models.clean() models.is_train = False diff --git a/PaddleCV/deeplabv3+/train.py b/PaddleCV/deeplabv3+/train.py index 2e3ba9cb..c8a1b26b 100755 --- a/PaddleCV/deeplabv3+/train.py +++ b/PaddleCV/deeplabv3+/train.py @@ -116,6 +116,7 @@ def loss(logit, label): args = parser.parse_args() utility.print_arguments(args) +utility.check_gpu(args.use_gpu) models.clean() models.bn_momentum = 0.9997 diff --git a/PaddleCV/deeplabv3+/utility.py b/PaddleCV/deeplabv3+/utility.py index aebb9acb..8d9bcfb3 100644 --- a/PaddleCV/deeplabv3+/utility.py +++ b/PaddleCV/deeplabv3+/utility.py @@ -17,6 +17,7 @@ from __future__ import division from __future__ import print_function import distutils.util import six +import paddle.fluid as fluid def print_arguments(args): @@ -58,3 +59,22 @@ def add_arguments(argname, type, default, help, argparser, **kwargs): type=type, help=help + ' Default: %(default)s.', **kwargs) + + +def check_gpu(use_gpu): + """ + Log error and exit when set use_gpu=true in paddlepaddle + cpu version. + """ + err = "Config use_gpu cannot be set as true while you are " \ + "using paddlepaddle cpu version ! \nPlease try: \n" \ + "\t1. Install paddlepaddle-gpu to run model on GPU \n" \ + "\t2. Set use_gpu as false in config file to run " \ + "model on CPU" + + try: + if use_gpu and not fluid.is_compiled_with_cuda(): + logger.error(err) + sys.exit(1) + except Exception as e: + pass diff --git a/PaddleCV/icnet/eval.py b/PaddleCV/icnet/eval.py index dc2f5743..bae05895 100644 --- a/PaddleCV/icnet/eval.py +++ b/PaddleCV/icnet/eval.py @@ -1,7 +1,7 @@ """Evaluator for ICNet model.""" import paddle.fluid as fluid import numpy as np -from utils import add_arguments, print_arguments, get_feeder_data +from utils import add_arguments, print_arguments, get_feeder_data, check_gpu from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.initializer import init_on_cpu from icnet import icnet @@ -89,6 +89,7 @@ def eval(args): def main(): args = parser.parse_args() print_arguments(args) + check_gpu(args.use_gpu) eval(args) diff --git a/PaddleCV/icnet/infer.py b/PaddleCV/icnet/infer.py index 1686adcc..b1145203 100644 --- a/PaddleCV/icnet/infer.py +++ b/PaddleCV/icnet/infer.py @@ -10,7 +10,7 @@ import cv2 import paddle.fluid as fluid import paddle from icnet import icnet -from utils import add_arguments, print_arguments, get_feeder_data +from utils import add_arguments, print_arguments, get_feeder_data, check_gpu from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.initializer import init_on_cpu import numpy as np @@ -128,6 +128,7 @@ def infer(args): def main(): args = parser.parse_args() print_arguments(args) + check_gpu(args.use_gpu) infer(args) diff --git a/PaddleCV/icnet/train.py b/PaddleCV/icnet/train.py index 07efe4c6..cdb8e96c 100644 --- a/PaddleCV/icnet/train.py +++ b/PaddleCV/icnet/train.py @@ -11,7 +11,7 @@ import os import time import paddle.fluid as fluid import numpy as np -from utils import add_arguments, print_arguments, get_feeder_data +from utils import add_arguments, print_arguments, get_feeder_data, check_gpu from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.initializer import init_on_cpu @@ -148,6 +148,7 @@ def train(args): def main(): args = parser.parse_args() print_arguments(args) + check_gpu(args.use_gpu) train(args) diff --git a/PaddleCV/icnet/utils.py b/PaddleCV/icnet/utils.py index 18172b33..34e28602 100644 --- a/PaddleCV/icnet/utils.py +++ b/PaddleCV/icnet/utils.py @@ -21,6 +21,7 @@ import numpy as np import six import paddle.fluid as fluid + def print_arguments(args): """Print argparse's arguments. @@ -112,3 +113,22 @@ def get_feeder_data(data, place, for_test=False): feed_dict["mask"] = mask_t return feed_dict + + +def check_gpu(use_gpu): + """ + Log error and exit when set use_gpu=true in paddlepaddle + cpu version. + """ + err = "Config use_gpu cannot be set as true while you are " \ + "using paddlepaddle cpu version ! \nPlease try: \n" \ + "\t1. Install paddlepaddle-gpu to run model on GPU \n" \ + "\t2. Set use_gpu as false in config file to run " \ + "model on CPU" + + try: + if use_gpu and not fluid.is_compiled_with_cuda(): + logger.error(err) + sys.exit(1) + except Exception as e: + pass diff --git a/PaddleCV/rcnn/eval_coco_map.py b/PaddleCV/rcnn/eval_coco_map.py index 6b01da55..e4d8bb4a 100644 --- a/PaddleCV/rcnn/eval_coco_map.py +++ b/PaddleCV/rcnn/eval_coco_map.py @@ -22,7 +22,7 @@ from eval_helper import * import paddle import paddle.fluid as fluid import reader -from utility import print_arguments, parse_args +from utility import print_arguments, parse_args, check_gpu import models.model_builder as model_builder import models.resnet as resnet import json @@ -139,4 +139,5 @@ def eval(): if __name__ == '__main__': args = parse_args() print_arguments(args) + check_gpu(args.use_gpu) eval() diff --git a/PaddleCV/rcnn/infer.py b/PaddleCV/rcnn/infer.py index 0c66ec08..b37d6c2c 100644 --- a/PaddleCV/rcnn/infer.py +++ b/PaddleCV/rcnn/infer.py @@ -5,7 +5,7 @@ from eval_helper import * import paddle import paddle.fluid as fluid import reader -from utility import print_arguments, parse_args +from utility import print_arguments, parse_args, check_gpu import models.model_builder as model_builder import models.resnet as resnet from config import cfg @@ -92,4 +92,5 @@ def infer(): if __name__ == '__main__': args = parse_args() print_arguments(args) + check_gpu(args.use_gpu) infer() diff --git a/PaddleCV/rcnn/train.py b/PaddleCV/rcnn/train.py index 49fcb71d..74e86840 100644 --- a/PaddleCV/rcnn/train.py +++ b/PaddleCV/rcnn/train.py @@ -35,7 +35,7 @@ import sys import numpy as np import time import shutil -from utility import parse_args, print_arguments, SmoothedValue, TrainingStats, now_time +from utility import parse_args, print_arguments, SmoothedValue, TrainingStats, now_time, check_gpu import collections import paddle @@ -260,4 +260,5 @@ def train(): if __name__ == '__main__': args = parse_args() print_arguments(args) + check_gpu(args.use_gpu) train() diff --git a/PaddleCV/rcnn/utility.py b/PaddleCV/rcnn/utility.py index fcad5566..9df7f78b 100644 --- a/PaddleCV/rcnn/utility.py +++ b/PaddleCV/rcnn/utility.py @@ -29,6 +29,7 @@ from paddle.fluid import core import argparse import functools from config import * +import paddle.fluid as fluid def print_arguments(args): @@ -172,3 +173,22 @@ def parse_args(): else: merge_cfg_from_args(args, 'val') return args + + +def check_gpu(use_gpu): + """ + Log error and exit when set use_gpu=true in paddlepaddle + cpu version. + """ + err = "Config use_gpu cannot be set as true while you are " \ + "using paddlepaddle cpu version ! \nPlease try: \n" \ + "\t1. Install paddlepaddle-gpu to run model on GPU \n" \ + "\t2. Set use_gpu as false in config file to run " \ + "model on CPU" + + try: + if use_gpu and not fluid.is_compiled_with_cuda(): + logger.error(err) + sys.exit(1) + except Exception as e: + pass -- GitLab