Skip to content

Commit 659f240

Browse files
authored
Merge pull request #22 from PaddlePaddle/develop
update
2 parents 52e4139 + d44d5c6 commit 659f240

File tree

19 files changed

+283
-133
lines changed

19 files changed

+283
-133
lines changed

fluid/PaddleCV/gan/cycle_gan/train.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,22 @@ def init_model():
147147
init_model()
148148
losses = [[], []]
149149
t_time = 0
150+
build_strategy = fluid.BuildStrategy()
151+
build_strategy.enable_inplace = False
152+
build_strategy.memory_optimize = False
150153

151154
g_A_trainer_program = fluid.CompiledProgram(
152155
g_A_trainer.program).with_data_parallel(
153-
loss_name=g_A_trainer.g_loss_A.name)
156+
loss_name=g_A_trainer.g_loss_A.name, build_strategy=build_strategy)
154157
g_B_trainer_program = fluid.CompiledProgram(
155158
g_B_trainer.program).with_data_parallel(
156-
loss_name=g_B_trainer.g_loss_B.name)
159+
loss_name=g_B_trainer.g_loss_B.name, build_strategy=build_strategy)
157160
d_B_trainer_program = fluid.CompiledProgram(
158161
d_B_trainer.program).with_data_parallel(
159-
loss_name=d_B_trainer.d_loss_B.name)
162+
loss_name=d_B_trainer.d_loss_B.name, build_strategy=build_strategy)
160163
d_A_trainer_program = fluid.CompiledProgram(
161164
d_A_trainer.program).with_data_parallel(
162-
loss_name=d_A_trainer.d_loss_A.name)
165+
loss_name=d_A_trainer.d_loss_A.name, build_strategy=build_strategy)
163166
for epoch in range(args.epoch):
164167
batch_id = 0
165168
for i in range(max_images_num):

fluid/PaddleCV/gan/cycle_gan/trainer.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def __init__(self, input_A, input_B):
1313
self.program = fluid.default_main_program().clone()
1414
with fluid.program_guard(self.program):
1515
self.fake_B = build_generator_resnet_9blocks(input_A, name="g_A")
16-
#FIXME set persistable explicitly to pass CE
17-
self.fake_B.persistable = True
1816
self.fake_A = build_generator_resnet_9blocks(input_B, name="g_B")
1917
self.cyc_A = build_generator_resnet_9blocks(self.fake_B, "g_B")
2018
self.cyc_B = build_generator_resnet_9blocks(self.fake_A, "g_A")
@@ -60,8 +58,6 @@ def __init__(self, input_A, input_B):
6058
with fluid.program_guard(self.program):
6159
self.fake_B = build_generator_resnet_9blocks(input_A, name="g_A")
6260
self.fake_A = build_generator_resnet_9blocks(input_B, name="g_B")
63-
#FIXME set persistable explicitly to pass CE
64-
self.fake_A.persistable = True
6561
self.cyc_A = build_generator_resnet_9blocks(self.fake_B, "g_B")
6662
self.cyc_B = build_generator_resnet_9blocks(self.fake_A, "g_A")
6763
self.infer_program = self.program.clone()

fluid/PaddleCV/rcnn/.run_ce.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export OMP_NUM_THREADS=1
77
cudaid=${face_detection:=0} # use 0-th card as default
88
export CUDA_VISIBLE_DEVICES=$cudaid
99

10-
FLAGS_benchmark=true python train.py --model_save_dir=output/ --data_dir=dataset/coco/ --max_iter=10 --enable_ce --pretrained_model=./imagenet_resnet50_fusebn | python _ce.py
10+
FLAGS_benchmark=true python train.py --model_save_dir=output/ --data_dir=dataset/coco/ --max_iter=100 --enable_ce --pretrained_model=./imagenet_resnet50_fusebn | python _ce.py
1111

1212

1313
cudaid=${face_detection_m:=0,1,2,3} # use 0,1,2,3 card as default
1414
export CUDA_VISIBLE_DEVICES=$cudaid
1515

16-
FLAGS_benchmark=true python train.py --model_save_dir=output/ --data_dir=dataset/coco/ --max_iter=10 --enable_ce --pretrained_model=./imagenet_resnet50_fusebn | python _ce.py
16+
FLAGS_benchmark=true python train.py --model_save_dir=output/ --data_dir=dataset/coco/ --max_iter=100 --enable_ce --pretrained_model=./imagenet_resnet50_fusebn | python _ce.py
1717

fluid/PaddleCV/rcnn/README.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## Installation
1414

15-
Running sample code in this directory requires PaddelPaddle Fluid v.1.0.0 and later. If the PaddlePaddle on your device is lower than this version, please follow the instructions in [installation document](http://www.paddlepaddle.org/documentation/docs/zh/0.15.0/beginners_guide/install/install_doc.html#paddlepaddle) and make an update.
15+
Running sample code in this directory requires PaddelPaddle Fluid v.1.3.0 and later. If the PaddlePaddle on your device is lower than this version, please follow the instructions in [installation document](http://paddlepaddle.org/documentation/docs/en/1.3/beginners_guide/install/index_en.html) and make an update.
1616

1717
## Introduction
1818

@@ -37,6 +37,25 @@ Train the model on [MS-COCO dataset](http://cocodataset.org/#download), download
3737
cd dataset/coco
3838
./download.sh
3939

40+
The data catalog structure is as follows:
41+
42+
```
43+
data/coco/
44+
├── annotations
45+
│   ├── instances_train2014.json
46+
│   ├── instances_train2017.json
47+
│   ├── instances_val2014.json
48+
│   ├── instances_val2017.json
49+
| ...
50+
├── train2017
51+
│   ├── 000000000009.jpg
52+
│   ├── 000000580008.jpg
53+
| ...
54+
├── val2017
55+
│   ├── 000000000139.jpg
56+
│   ├── 000000000285.jpg
57+
| ...
58+
```
4059

4160
## Training
4261

@@ -51,9 +70,8 @@ Please make sure that pretrained_model is downloaded and loaded correctly, other
5170

5271
To train the model, [cocoapi](https://github.com/cocodataset/cocoapi) is needed. Install the cocoapi:
5372

54-
# COCOAPI=/path/to/clone/cocoapi
55-
git clone https://github.com/cocodataset/cocoapi.git $COCOAPI
56-
cd $COCOAPI/PythonAPI
73+
git clone https://github.com/cocodataset/cocoapi.git
74+
cd cocoapi/PythonAPI
5775
# if cython is not installed
5876
pip install Cython
5977
# Install into global site-packages
@@ -66,25 +84,29 @@ After data preparation, one can start the training step by:
6684

6785
- Faster RCNN
6886

87+
```
6988
python train.py \
7089
--model_save_dir=output/ \
7190
--pretrained_model=${path_to_pretrain_model} \
7291
--data_dir=${path_to_data} \
7392
--MASK_ON=False
93+
```
7494
7595
- Mask RCNN
7696
97+
```
7798
python train.py \
7899
--model_save_dir=output/ \
79100
--pretrained_model=${path_to_pretrain_model} \
80101
--data_dir=${path_to_data} \
81102
--MASK_ON=True
103+
```
82104
83-
- Set ```export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7``` to specifiy 8 GPU to train.
84-
- Set ```MASK_ON``` to choose Faster RCNN or Mask RCNN model.
85-
- For more help on arguments:
105+
- Set ```export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7``` to specifiy 8 GPU to train.
106+
- Set ```MASK_ON``` to choose Faster RCNN or Mask RCNN model.
107+
- For more help on arguments:
86108
87-
python train.py --help
109+
python train.py --help
88110
89111
**data reader introduction:**
90112
@@ -116,20 +138,25 @@ Evaluation is to evaluate the performance of a trained model. This sample provid
116138
117139
- Faster RCNN
118140
141+
```
119142
python eval_coco_map.py \
120143
--dataset=coco2017 \
121-
--pretrained_model=${path_to_pretrain_model} \
144+
--pretrained_model=${path_to_trained_model} \
122145
--MASK_ON=False
146+
```
123147
124148
- Mask RCNN
125149
150+
```
126151
python eval_coco_map.py \
127152
--dataset=coco2017 \
128-
--pretrained_model=${path_to_pretrain_model} \
153+
--pretrained_model=${path_to_trainde_model} \
129154
--MASK_ON=True
155+
```
130156
131-
- Set ```export CUDA_VISIBLE_DEVICES=0``` to specifiy one GPU to eval.
132-
- Set ```MASK_ON``` to choose Faster RCNN or Mask RCNN model.
157+
- Set ```--pretrained_model=${path_to_trained_model}``` to specifiy the trained model, not the initialized model.
158+
- Set ```export CUDA_VISIBLE_DEVICES=0``` to specifiy one GPU to eval.
159+
- Set ```MASK_ON``` to choose Faster RCNN or Mask RCNN model.
133160
134161
Evalutaion result is shown as below:
135162
@@ -159,12 +186,14 @@ Mask RCNN:
159186
160187
Inference is used to get prediction score or image features based on trained models. `infer.py` is the main executor for inference, one can start infer step by:
161188
162-
python infer.py \
163-
--dataset=coco2017 \
164-
--pretrained_model=${path_to_pretrain_model} \
165-
--image_path=dataset/coco/val2017/ \
166-
--image_name=000000000139.jpg \
167-
--draw_threshold=0.6
189+
```
190+
python infer.py \
191+
--pretrained_model=${path_to_trained_model} \
192+
--image_path=dataset/coco/val2017/000000000139.jpg \
193+
--draw_threshold=0.6
194+
```
195+
196+
Please set the model path and image path correctly. GPU device is used by default, you can set `--use_gpu=False` to switch to CPU device. And you can set `draw_threshold` to tune score threshold to control the number of output detection boxes.
168197
169198
Visualization of infer result is shown as below:
170199
<p align="center">

fluid/PaddleCV/rcnn/README_cn.md

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## 安装
1414

15-
在当前目录下运行样例代码需要PadddlePaddle Fluid的v.1.0.0或以上的版本。如果你的运行环境中的PaddlePaddle低于此版本,请根据[安装文档](http://www.paddlepaddle.org/documentation/docs/zh/0.15.0/beginners_guide/install/install_doc.html#paddlepaddle)中的说明来更新PaddlePaddle。
15+
在当前目录下运行样例代码需要PadddlePaddle Fluid的v.1.3.0或以上的版本。如果你的运行环境中的PaddlePaddle低于此版本,请根据[安装文档](http://www.paddlepaddle.org/)中的说明来更新PaddlePaddle。
1616

1717
## 简介
1818
区域卷积神经网络(RCNN)系列模型为两阶段目标检测器。通过对图像生成候选区域,提取特征,判别特征类别并修正候选框位置。
@@ -37,6 +37,27 @@ Mask RCNN同样为两阶段框架,第一阶段扫描图像生成候选框;
3737
cd dataset/coco
3838
./download.sh
3939

40+
数据目录结构如下:
41+
42+
```
43+
data/coco/
44+
├── annotations
45+
│   ├── instances_train2014.json
46+
│   ├── instances_train2017.json
47+
│   ├── instances_val2014.json
48+
│   ├── instances_val2017.json
49+
| ...
50+
├── train2017
51+
│   ├── 000000000009.jpg
52+
│   ├── 000000580008.jpg
53+
| ...
54+
├── val2017
55+
│   ├── 000000000139.jpg
56+
│   ├── 000000000285.jpg
57+
| ...
58+
59+
```
60+
4061
## 模型训练
4162

4263
**下载预训练模型:** 本示例提供Resnet-50预训练模型,该模性转换自Caffe,并对批标准化层(Batch Normalization Layer)进行参数融合。采用如下命令下载预训练模型:
@@ -50,9 +71,8 @@ Mask RCNN同样为两阶段框架,第一阶段扫描图像生成候选框;
5071

5172
训练前需要首先下载[cocoapi](https://github.com/cocodataset/cocoapi)
5273

53-
# COCOAPI=/path/to/clone/cocoapi
54-
git clone https://github.com/cocodataset/cocoapi.git $COCOAPI
55-
cd $COCOAPI/PythonAPI
74+
git clone https://github.com/cocodataset/cocoapi.git
75+
cd cocoapi/PythonAPI
5676
# if cython is not installed
5777
pip install Cython
5878
# Install into global site-packages
@@ -65,25 +85,29 @@ Mask RCNN同样为两阶段框架,第一阶段扫描图像生成候选框;
6585

6686
- Faster RCNN
6787

88+
```
6889
python train.py \
6990
--model_save_dir=output/ \
7091
--pretrained_model=${path_to_pretrain_model} \
7192
--data_dir=${path_to_data} \
7293
--MASK_ON=False
94+
```
7395
7496
- Mask RCNN
7597
98+
```
7699
python train.py \
77100
--model_save_dir=output/ \
78101
--pretrained_model=${path_to_pretrain_model} \
79102
--data_dir=${path_to_data} \
80103
--MASK_ON=True
104+
```
81105
82-
- 通过设置export CUDA\_VISIBLE\_DEVICES=0,1,2,3,4,5,6,7指定8卡GPU训练。
83-
- 通过设置```MASK_ON```选择Faster RCNN和Mask RCNN模型。
84-
- 可选参数见:
106+
- 通过设置export CUDA\_VISIBLE\_DEVICES=0,1,2,3,4,5,6,7指定8卡GPU训练。
107+
- 通过设置```MASK_ON```选择Faster RCNN和Mask RCNN模型。
108+
- 可选参数见:
85109
86-
python train.py --help
110+
python train.py --help
87111
88112
**数据读取器说明:** 数据读取器定义在reader.py中。所有图像将短边等比例缩放至`scales`,若长边大于`max_size`, 则再次将长边等比例缩放至`max_size`。在训练阶段,对图像采用水平翻转。支持将同一个batch内的图像padding为相同尺寸。
89113
@@ -110,20 +134,25 @@ Mask RCNN同样为两阶段框架,第一阶段扫描图像生成候选框;
110134
111135
- Faster RCNN
112136
137+
```
113138
python eval_coco_map.py \
114139
--dataset=coco2017 \
115-
--pretrained_model=${path_to_pretrain_model} \
140+
--pretrained_model=${path_to_trained_model} \
116141
--MASK_ON=False
142+
```
117143
118144
- Mask RCNN
119145
146+
```
120147
python eval_coco_map.py \
121148
--dataset=coco2017 \
122-
--pretrained_model=${path_to_pretrain_model} \
149+
--pretrained_model=${path_to_trained_model} \
123150
--MASK_ON=True
151+
```
124152
125-
- 通过设置export CUDA\_VISIBLE\_DEVICES=0指定单卡GPU评估。
126-
- 通过设置```MASK_ON```选择Faster RCNN和Mask RCNN模型。
153+
- 通过设置`--pretrained_model=${path_to_trained_model}`指定训练好的模型,注意不是初始化的模型。
154+
- 通过设置`export CUDA\_VISIBLE\_DEVICES=0`指定单卡GPU评估。
155+
- 通过设置```MASK_ON```选择Faster RCNN和Mask RCNN模型。
127156
128157
下表为模型评估结果:
129158
@@ -155,12 +184,14 @@ Mask RCNN:
155184
156185
模型推断可以获取图像中的物体及其对应的类别,`infer.py`是主要执行程序,调用示例如下:
157186
158-
python infer.py \
159-
--dataset=coco2017 \
160-
--pretrained_model=${path_to_pretrain_model} \
161-
--image_path=dataset/coco/val2017/ \
162-
--image_name=000000000139.jpg \
163-
--draw_threshold=0.6
187+
```
188+
python infer.py \
189+
--pretrained_model=${path_to_trained_model} \
190+
--image_path=dataset/coco/val2017/000000000139.jpg \
191+
--draw_threshold=0.6
192+
```
193+
194+
注意,请正确设置模型路径`${path_to_trained_model}`和预测图片路径。默认使用GPU设备,也可通过设置`--use_gpu=False`使用CPU设备。可通过设置`draw_threshold`调节得分阈值控制检测框的个数。
164195
165196
下图为模型可视化预测结果:
166197
<p align="center">

fluid/PaddleCV/rcnn/data_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
import os
3232

3333

34+
class DatasetPath(object):
35+
def __init__(self, mode):
36+
self.mode = mode
37+
mode_name = 'train' if mode == 'train' else 'val'
38+
if cfg.dataset != 'coco2014' and cfg.dataset != 'coco2017':
39+
raise NotImplementedError('Dataset {} not supported'.format(
40+
cfg.dataset))
41+
self.sub_name = mode_name + cfg.dataset[-4:]
42+
43+
def get_data_dir(self):
44+
return os.path.join(cfg.data_dir, self.sub_name)
45+
46+
def get_file_list(self):
47+
sfile_list = 'annotations/instances_' + self.sub_name + '.json'
48+
return os.path.join(cfg.data_dir, sfile_list)
49+
50+
3451
def get_image_blob(roidb, mode):
3552
"""Builds an input blob from the images in the roidb at the specified
3653
scales.

fluid/PaddleCV/rcnn/eval_coco_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pycocotools.coco import COCO
3030
from pycocotools.cocoeval import COCOeval, Params
3131
from config import cfg
32-
from roidbs import DatasetPath
32+
from data_utils import DatasetPath
3333

3434

3535
def eval():

0 commit comments

Comments
 (0)