Skip to content

Commit ddeb925

Browse files
authored
Update API usage according to 1.8 recommendations (#4657)
1 parent 1bf7264 commit ddeb925

File tree

8 files changed

+54
-54
lines changed

8 files changed

+54
-54
lines changed

PaddleCV/rrpn/README.md

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

1313
## 安装
1414

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

1717

1818
## 简介
@@ -27,16 +27,23 @@ RRPN是在Faster RCNN基础上拓展出的两阶段目标检测器,可用于
2727

2828
### 编译自定义OP
2929

30+
**注意:** 通过pip方式安装的PaddlePaddle由GCC 4.8编译得到,由于GCC 4.8和GCC 5以上C++11 ABI不兼容,您编写的自定义OP,需要通过GCC 4.8编译。若是GCC 5及以上的环境上使用自定义OP,推荐使用Docker安装PaddlePaddle,使得编Paddle和编译自定义OP的GCC版本相同。
31+
3032
自定义OP编译方式如下:
3133

3234
进入 `models/ext_op/src` 目录,执行编译脚本
3335
```
3436
cd models/ext_op/src
3537
sh make.sh ${cuda_path} ${cudnn_path} ${nccl_path}
36-
'''
38+
```
3739
其中${cuda_path}、$cudnn_path}和{nccl_path}分别为cuda、cudnn、nccl的安装路径,需通过命令行进行指定
38-
成功编译后,`ext_op/src` 目录下将会生成 `rrpn_lib.so`
39-
40+
成功编译后,`ext_op/src` 目录下将会生成 `rrpn_lib.so`。
41+
需要将`rrpn_lib.so`所在路径以及libpaddle_framework.so路径(即paddle.sysconfig.get_lib()得到路径)设置到环境变量LD_LIBRARY_PATH中:
42+
```
43+
# 假如rrpn_lib.so路径是:`rrpn/models/ext_op/src/`,对于Linux环境设置:
44+
export LD_LIBRARY_PATH=rrpn/models/ext_op/src/:$( python -c 'import paddle; print(paddle.sysconfig.get_lib())'):$LD_LIBRARY_PATH
45+
```
46+
4047
## 数据准备
4148
### 公开数据集
4249
[ICDAR2015数据集](https://rrc.cvc.uab.es/?ch=4&com=downloads)上进行训练,数据集需进入官网进行注册后方可下载。
@@ -58,8 +65,8 @@ dataset/icdar2015/
5865
│ ├── img_112.jpg
5966
| ...
6067
├── ch4_test_localization_transcription_gt
61-
│ ├── img_111.jpg
62-
│ ├── img_112.jpg
68+
│ ├── img_111.txt
69+
│ ├── img_112.txt
6370
| ...
6471
```
6572
### 自定义数据
@@ -88,7 +95,7 @@ x1, y1, x2, y2, x3, y3, x4, y4, class_name
8895
python train.py \
8996
--model_save_dir=output/ \
9097
--pretrained_model=${path_to_pretrain_model} \
91-
--data_dir=${path_to_data} \
98+
--data_dir=${path_to_icdar2015} \
9299
```
93100
94101
@@ -126,7 +133,7 @@ x1, y1, x2, y2, x3, y3, x4, y4, class_name
126133
127134
```
128135
python eval.py \
129-
--dataset=icdar2015 \
136+
--data_dir=${path_to_icdar2015} \
130137
--pretrained_model=${path_to_trained_model}
131138
```
132139
@@ -143,10 +150,6 @@ RRPN
143150
| [RRPN](https://paddleseg.bj.bcebos.com/deploy/temp/model_final.tar) |8 | 17500 | 0.8048 |
144151
145152
146-
147-
148-
149-
150153
## 模型推断及可视化
151154
152155
模型推断可以获取图像中的物体及其对应的类别,`infer.py`是主要执行程序,调用示例如下:

PaddleCV/rrpn/checkpoint.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def _load_state(path):
4141
return state
4242

4343

44+
def _strip_postfix(path):
45+
path, ext = os.path.splitext(path)
46+
assert ext in ['', '.pdparams', '.pdopt', '.pdmodel'], \
47+
"Unknown postfix {} from weights".format(ext)
48+
return path
49+
50+
4451
def load_params(exe, prog, path):
4552
"""
4653
Load model from the given path.
@@ -50,20 +57,33 @@ def load_params(exe, prog, path):
5057
path (string): URL string or loca model path.
5158
"""
5259

53-
if not os.path.exists(path):
60+
path = _strip_postfix(path)
61+
if not (os.path.isdir(path) or os.path.exists(path + '.pdparams')):
5462
raise ValueError("Model pretrain path {} does not "
5563
"exists.".format(path))
5664

5765
logger.info('Loading parameters from {}...'.format(path))
5866

59-
def _if_exist(var):
60-
param_exist = os.path.exists(os.path.join(path, var.name))
61-
do_load = param_exist
62-
if do_load:
63-
logger.debug('load weight {}'.format(var.name))
64-
return do_load
67+
ignore_set = set()
68+
state = _load_state(path)
6569

66-
fluid.io.load_vars(exe, path, prog, predicate=_if_exist)
70+
# ignore the parameter which mismatch the shape
71+
# between the model and pretrain weight.
72+
all_var_shape = {}
73+
for block in prog.blocks:
74+
for param in block.all_parameters():
75+
all_var_shape[param.name] = param.shape
76+
ignore_set.update([
77+
name for name, shape in all_var_shape.items()
78+
if name in state and shape != state[name].shape
79+
])
80+
81+
if len(ignore_set) > 0:
82+
for k in ignore_set:
83+
if k in state:
84+
logger.warning('variable {} not used'.format(k))
85+
del state[k]
86+
fluid.io.set_program_state(prog, state)
6787

6888

6989
def save(exe, prog, path):
@@ -83,6 +103,7 @@ def save(exe, prog, path):
83103
def load_and_fusebn(exe, prog, path):
84104
"""
85105
Fuse params of batch norm to scale and bias.
106+
86107
Args:
87108
exe (fluid.Executor): The fluid.Executor object.
88109
prog (fluid.Program): save weight from which Program object.
@@ -104,19 +125,12 @@ def load_and_fusebn(exe, prog, path):
104125
# x is any prefix
105126
mean_variances = set()
106127
bn_vars = []
107-
108-
state = None
109-
if os.path.exists(path + '.pdparams'):
110-
state = _load_state(path)
128+
state = _load_state(path)
111129

112130
def check_mean_and_bias(prefix):
113131
m = prefix + 'mean'
114132
v = prefix + 'variance'
115-
if state:
116-
return v in state and m in state
117-
else:
118-
return (os.path.exists(os.path.join(path, m)) and
119-
os.path.exists(os.path.join(path, v)))
133+
return v in state and m in state
120134

121135
has_mean_bias = True
122136

@@ -156,16 +170,14 @@ def check_mean_and_bias(prefix):
156170
bn_vars.append(
157171
[scale_name, bias_name, mean_name, variance_name])
158172

159-
if state:
160-
fluid.io.set_program_state(prog, state)
161-
else:
162-
load_params(exe, prog, path)
163173
if not has_mean_bias:
174+
fluid.io.set_program_state(prog, state)
164175
logger.warning(
165176
"There is no paramters of batch norm in model {}. "
166177
"Skip to fuse batch norm. And load paramters done.".format(path))
167178
return
168179

180+
fluid.load(prog, path, exe)
169181
eps = 1e-5
170182
for names in bn_vars:
171183
scale_name, bias_name, mean_name, var_name = names

PaddleCV/rrpn/models/ext_op/rrpn_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import paddle.fluid as fluid
1616
from paddle.fluid.layer_helper import LayerHelper
1717
from paddle.fluid.framework import Variable
18-
fluid.load_op_library('models/ext_op/src/rrpn_lib.so')
18+
fluid.load_op_library('rrpn_lib.so')
1919

2020

2121
def rrpn_target_assign(bbox_pred,

PaddleCV/rrpn/models/ext_op/src/make.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ git clone https://github.com/NVlabs/cub.git
2727

2828
nvcc rrpn_generate_proposals_op.cu -c -o rrpn_generate_proposals_op.cu.o -ccbin cc -DPADDLE_WITH_MKLDNN -DPADDLE_WITH_CUDA -DEIGEN_USE_GPU -DPADDLE_USE_DSO -Xcompiler -fPIC -std=c++11 -Xcompiler -fPIC -w --expt-relaxed-constexpr -O3 -DNVCC \
2929
-I ${include_dir} \
30-
-I ${include_dir}/third_party \
30+
-I ${include_dir}/third_party \
3131
-I ${CUDA}/include \
3232
-I ${CUDNN}/include \
3333
-I ${NCCL}/include \

PaddleCV/rrpn/models/ext_op/src/rrpn_rotated_roi_align_op.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ class RRPNRotatedROIAlignGradMaker : public framework::SingleGradOpMaker<T> {
165165
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
166166

167167
protected:
168-
std::unique_ptr<T> Apply() const override {
169-
std::unique_ptr<T> op(new T);
168+
void Apply(GradOpPtr<T> op) const override {
170169
op->SetType("rrpn_rotated_roi_align_grad");
171170
op->SetInput("X", this->Input("X"));
172171
op->SetInput("ROIs", this->Input("ROIs"));
@@ -175,12 +174,11 @@ class RRPNRotatedROIAlignGradMaker : public framework::SingleGradOpMaker<T> {
175174
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
176175
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
177176
op->SetAttrMap(this->Attrs());
178-
return op;
179177
}
180178
};
181179

182-
DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(
183-
RRPNRotatedRoiAlignGradNoNeedBufVarsInferer, "X");
180+
DECLARE_NO_NEED_BUFFER_VARS_INFERER(RRPNRotatedRoiAlignGradNoNeedBufVarsInferer,
181+
"X");
184182

185183
} // namespace operators
186184
} // namespace paddle

PaddleCV/rrpn/reader.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def reader():
9696
continue
9797
batch_out.append(datas)
9898
end = time.time()
99-
#print('reader time:', end - start)
10099
if len(batch_out) == batch_size:
101100
yield batch_out
102101
count += 1

PaddleCV/rrpn/roidbs.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ def get_roidb(self):
101101
elif edge2 >= edge1:
102102
width = edge2
103103
height = edge1
104-
# print pt2[0], pt3[0]
105104
if pt2[0] - pt3[0] != 0:
106105
angle = -np.arctan(
107106
float(pt2[1] - pt3[1]) /
@@ -160,7 +159,6 @@ def get_roidb(self):
160159
else:
161160
hard_boxes.append([x_ctr, y_ctr, width, height, angle])
162161

163-
#print(easy_boxes)
164162
if self.mode == 'train':
165163
boxes.extend(easy_boxes)
166164
# hard box only get 1/3 for train
@@ -173,8 +171,6 @@ def get_roidb(self):
173171
is_difficult = [0] * len(easy_boxes)
174172
is_difficult.extend([1] * int(len(hard_boxes)))
175173
len_of_bboxes = len(boxes)
176-
#is_difficult = [0] * len(easy_boxes)
177-
#is_difficult.extend([1] * int(len(hard_boxes)))
178174
is_difficult = np.array(is_difficult).reshape(
179175
1, len_of_bboxes).astype(np.int32)
180176
if self.mode == 'train':
@@ -221,11 +217,9 @@ class ICDAR2017Dataset(object):
221217
def __init__(self, mode):
222218
print('Creating: {}'.format(cfg.dataset))
223219
self.name = cfg.data_dir
224-
#print('**************', self.name)
225220
self.mode = mode
226221
data_path = DatasetPath(mode, self.name)
227222
data_dir = data_path.get_data_dir()
228-
#print("&**************", data_dir)
229223
file_list = data_path.get_file_list()
230224
self.image_dir = data_dir
231225
self.gt_dir = file_list
@@ -245,15 +239,12 @@ def get_roidb(self):
245239
labels_map = get_labels_maps()
246240
for image in image_list:
247241
prefix = image[:-4]
248-
#print(image)
249242

250243
if image.split('.')[-1] not in post_fix:
251244
continue
252245
img_name = os.path.join(self.image_dir, image)
253246
gt_name = os.path.join(self.gt_dir, 'gt_' + prefix + '.txt')
254247
gt_classes = []
255-
#boxes = []
256-
#hard_boxes = []
257248
boxes = []
258249
gt_obj = open(gt_name, 'r', encoding='UTF-8-sig')
259250
gt_txt = gt_obj.read()
@@ -293,7 +284,6 @@ def get_roidb(self):
293284
elif edge2 >= edge1:
294285
width = edge2
295286
height = edge1
296-
# print pt2[0], pt3[0]
297287
if pt2[0] - pt3[0] != 0:
298288
angle = -np.arctan(
299289
float(pt2[1] - pt3[1]) /
@@ -312,7 +302,6 @@ def get_roidb(self):
312302
else:
313303
boxes.append([x_ctr, y_ctr, width, height, angle])
314304
len_of_bboxes = len(boxes)
315-
#print(len_of_bboxes)
316305
is_difficult = np.zeros((len_of_bboxes, 1), dtype=np.int32)
317306
if self.mode == 'train':
318307
gt_boxes = np.zeros((len_of_bboxes, 5), dtype=np.int32)
@@ -332,7 +321,6 @@ def get_roidb(self):
332321
boxes[idx][3], boxes[idx][4], boxes[idx][5],
333322
boxes[idx][6], boxes[idx][7]
334323
]
335-
#gt_classes[idx] = 1
336324
if gt_boxes.shape[0] <= 0:
337325
continue
338326
gt_boxes = gt_boxes.astype(np.float64)

PaddleCV/rrpn/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def parse_args():
154154
add_arg('pixel_means', float, [0.485, 0.456, 0.406], "pixel mean")
155155
add_arg('nms_thresh', float, 0.3, "NMS threshold.")
156156
add_arg('score_thresh', float, 0.01, "score threshold for NMS.")
157-
add_arg('snapshot_stride', int, 1000, "save model every snapshot stride.")
157+
add_arg('snapshot_iter', int, 1000, "save model every snapshot iter.")
158158
# SINGLE EVAL AND DRAW
159159
add_arg('draw_threshold', float, 0.8, "Confidence threshold to draw bbox.")
160160
add_arg('image_path', str, 'ICDAR2015/tmp/', "The image path used to inference and visualize.")

0 commit comments

Comments
 (0)