Skip to content

Commit b758129

Browse files
authored
check gpu for ppdet/yolov3 and update ppdet MODEL_ZOO (PaddlePaddle#2730)
* check gpu for ppdet/yolov3 * add random shape descp in MODEL_ZOO * refine doc * not use core
1 parent 1de07a4 commit b758129

File tree

12 files changed

+102
-7
lines changed

12 files changed

+102
-7
lines changed

PaddleCV/PaddleDetection/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Advanced Features:
5656
- [x] **Modulated Deformable Convolution**: pretrained models to be released.
5757
- [x] **Deformable PSRoI Pooling**: pretrained models to be released.
5858

59+
**NOTE:** Synchronized batch normalization can only be used on multiple GPU devices, can not be used on CPU devices or single GPU device.
60+
5961

6062
## Model zoo
6163

PaddleCV/PaddleDetection/docs/MODEL_ZOO.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ The backbone models pretrained on ImageNet are available. All backbone models ar
6767
| ResNet34 | 416 | 8 | 270e | 34.3 | [model](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r34.tar) |
6868
| ResNet34 | 320 | 8 | 270e | 31.4 | [model](https://paddlemodels.bj.bcebos.com/object_detection/yolov3_r34.tar) |
6969

70-
**NOTE**: Yolo v3 trained in 8 GPU with total batch size as 64 and trained 270 epoches. Yolo v3 training data augmentations: mixup,
71-
randomly color distortion, randomly cropping, randomly expansion, randomly interpolation method, randomly flippling.
70+
**NOTE**: Yolo v3 is trained in 8 GPU with total batch size as 64 and trained 270 epoches. Yolo v3 training data augmentations: mixup,
71+
randomly color distortion, randomly cropping, randomly expansion, randomly interpolation method, randomly flippling. Yolo v3 used randomly
72+
reshaped minibatch in training, inferences can be performed on different image sizes with the same model weights, and we provided evaluation
73+
results of image size 608/416/320 above.
7274

7375
### RetinaNet
7476

@@ -85,5 +87,5 @@ randomly color distortion, randomly cropping, randomly expansion, randomly inter
8587
| :----------- | :--: | :-----: | :-----: | :----: | :-------: |
8688
| MobileNet v1 | 300 | 32 | 120e | 73.2 | [model](https://paddlemodels.bj.bcebos.com/object_detection/ssd_mobilenet_v1_voc.tar) |
8789

88-
**NOTE**: SSD trained in 2 GPU with totoal batch size as 64 and trained 120 epoches. SSD training data augmentations: randomly color distortion,
90+
**NOTE**: SSD is trained in 2 GPU with totoal batch size as 64 and trained 120 epoches. SSD training data augmentations: randomly color distortion,
8991
randomly cropping, randomly expansion, randomly flipping.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import absolute_import
16+
from __future__ import division
17+
from __future__ import print_function
18+
from __future__ import unicode_literals
19+
20+
import sys
21+
22+
import paddle.fluid as fluid
23+
24+
import logging
25+
logger = logging.getLogger(__name__)
26+
27+
__all__ = ['check_gpu']
28+
29+
30+
def check_gpu(use_gpu):
31+
"""
32+
Log error and exit when set use_gpu=true in paddlepaddle
33+
cpu version.
34+
"""
35+
err = "Config use_gpu cannot be set as true while you are " \
36+
"using paddlepaddle cpu version ! \nPlease try: \n" \
37+
"\t1. Install paddlepaddle-gpu to run model on GPU \n" \
38+
"\t2. Set use_gpu as false in config file to run " \
39+
"model on CPU"
40+
41+
try:
42+
if use_gpu and not fluid.is_compiled_with_cuda():
43+
logger.error(err)
44+
sys.exit(1)
45+
except Exception as e:
46+
pass
47+

PaddleCV/PaddleDetection/tools/eval.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ppdet.utils.eval_utils import parse_fetches, eval_run, eval_results
2525
import ppdet.utils.checkpoint as checkpoint
2626
from ppdet.utils.cli import ArgsParser
27+
from ppdet.utils.check import check_gpu
2728
from ppdet.modeling.model_input import create_feed
2829
from ppdet.data.data_feed import create_reader
2930
from ppdet.core.workspace import load_config, merge_config, create
@@ -46,6 +47,9 @@ def main():
4647

4748
merge_config(FLAGS.opt)
4849

50+
# check if set use_gpu=True in paddlepaddle cpu version
51+
check_gpu(cfg.use_gpu)
52+
4953
if cfg.use_gpu:
5054
devices_num = fluid.core.get_cuda_device_count()
5155
else:

PaddleCV/PaddleDetection/tools/infer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from ppdet.utils.eval_utils import parse_fetches
3232
from ppdet.utils.cli import ArgsParser
33+
from ppdet.utils.check import check_gpu
3334
from ppdet.utils.visualizer import visualize_results
3435
import ppdet.utils.checkpoint as checkpoint
3536

@@ -109,6 +110,9 @@ def main():
109110

110111
merge_config(FLAGS.opt)
111112

113+
# check if set use_gpu=True in paddlepaddle cpu version
114+
check_gpu(cfg.use_gpu)
115+
112116
if 'test_feed' not in cfg:
113117
test_feed = create(main_arch + 'TestFeed')
114118
else:

PaddleCV/PaddleDetection/tools/train.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def set_paddle_flags(**kwargs):
4343
from ppdet.utils.eval_utils import parse_fetches, eval_run, eval_results
4444
from ppdet.utils.stats import TrainingStats
4545
from ppdet.utils.cli import ArgsParser
46+
from ppdet.utils.check import check_gpu
4647
import ppdet.utils.checkpoint as checkpoint
4748
from ppdet.modeling.model_input import create_feed
4849

@@ -62,6 +63,9 @@ def main():
6263

6364
merge_config(FLAGS.opt)
6465

66+
# check if set use_gpu=True in paddlepaddle cpu version
67+
check_gpu(cfg.use_gpu)
68+
6569
if cfg.use_gpu:
6670
devices_num = fluid.core.get_cuda_device_count()
6771
else:

PaddleCV/yolov3/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ dataset/coco/
111111
* 学习率采用warmup算法,前4000轮学习率从0.0线性增加至0.001。在400000,450000轮时使用0.1,0.01乘子进行学习率衰减,最大训练500000轮。
112112
* 通过设置`--syncbn=True`可以开启Synchronized batch normalization,该模式下精度会提高
113113

114+
**注意:** Synchronized batch normalization只能用于多GPU训练,不能用于CPU训练和单GPU训练。
115+
114116
下图为模型训练结果:
115117
<p align="center">
116118
<img src="image/train_loss.png" height="400" width="550" hspace="10"/><br />

PaddleCV/yolov3/README_en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Please make sure that pre-trained model is downloaded and loaded correctly, othe
112112
* In first 4000 iteration, the learning rate increases linearly from 0.0 to 0.001. Then lr is decayed at 400000, 450000 iteration with multiplier 0.1, 0.01. The maximum iteration is 500200.
113113
* Synchronized batch normalization can be set by `--syncbn=True`, which can produce a higher performance.
114114

115+
**NOTE:** Synchronized batch normalization can only be used on multiple GPU devices, can not be used on CPU devices or single GPU device.
116+
115117
Training losses is shown as below:
116118
<p align="center">
117119
<img src="image/train_loss.png" height="500" width="650" hspace="10"/><br />

PaddleCV/yolov3/eval.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
import paddle.fluid as fluid
2424
import reader
2525
from models.yolov3 import YOLOv3
26-
from utility import print_arguments, parse_args
26+
from utility import print_arguments, parse_args, check_gpu
2727
from pycocotools.coco import COCO
2828
from pycocotools.cocoeval import COCOeval, Params
2929
from config import cfg
3030

3131

3232
def eval():
33+
# check if set use_gpu=True in paddlepaddle cpu version
34+
check_gpu(cfg.use_gpu)
35+
3336
if '2014' in cfg.dataset:
3437
test_list = 'annotations/instances_val2014.json'
3538
elif '2017' in cfg.dataset:

PaddleCV/yolov3/infer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import paddle.fluid as fluid
2020
import box_utils
2121
import reader
22-
from utility import print_arguments, parse_args
22+
from utility import print_arguments, parse_args, check_gpu
2323
from models.yolov3 import YOLOv3
2424
from pycocotools.coco import COCO
2525
from pycocotools.cocoeval import COCOeval, Params
@@ -28,6 +28,9 @@
2828

2929
def infer():
3030

31+
# check if set use_gpu=True in paddlepaddle cpu version
32+
check_gpu(cfg.use_gpu)
33+
3134
if not os.path.exists('output'):
3235
os.mkdir('output')
3336

PaddleCV/yolov3/train.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def set_paddle_flags(flags):
3535
import random
3636
import time
3737
import shutil
38-
from utility import parse_args, print_arguments, SmoothedValue
38+
from utility import (parse_args, print_arguments,
39+
SmoothedValue, check_gpu)
3940

4041
import paddle
4142
import paddle.fluid as fluid
@@ -61,6 +62,9 @@ def get_device_num():
6162

6263
def train():
6364

65+
# check if set use_gpu=True in paddlepaddle cpu version
66+
check_gpu(cfg.use_gpu)
67+
6468
if cfg.debug or args.enable_ce:
6569
fluid.default_startup_program().random_seed = 1000
6670
fluid.default_main_program().random_seed = 1000

PaddleCV/yolov3/utility.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import numpy as np
2424
import six
2525
from collections import deque
26-
from paddle.fluid import core
26+
import paddle.fluid as fluid
2727
import argparse
2828
import functools
2929
from config import *
@@ -87,6 +87,24 @@ def get_mean_value(self):
8787
return self.loss_sum / self.iter_cnt
8888

8989

90+
def check_gpu(use_gpu):
91+
"""
92+
Log error and exit when set use_gpu=True in paddlepaddle
93+
cpu version.
94+
"""
95+
err = "Config use_gpu cannot be set as True while you are " \
96+
"using paddlepaddle cpu version ! \nPlease try: \n" \
97+
"\t1. Install paddlepaddle-gpu to run model on GPU \n" \
98+
"\t2. Set --use_gpu=False to run model on CPU"
99+
100+
try:
101+
if use_gpu and not fluid.is_compiled_with_cuda():
102+
print(err)
103+
sys.exit(1)
104+
except Exception as e:
105+
pass
106+
107+
90108
def parse_args():
91109
"""return all args
92110
"""

0 commit comments

Comments
 (0)