Skip to content

Commit 8f25506

Browse files
committed
Add pre-commit hook
1 parent 115c45d commit 8f25506

File tree

5 files changed

+175
-10
lines changed

5 files changed

+175
-10
lines changed

.copyright.hook

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from __future__ import absolute_import
2+
from __future__ import print_function
3+
from __future__ import unicode_literals
4+
5+
import argparse
6+
import io, re
7+
import sys, os
8+
import subprocess
9+
import platform
10+
11+
COPYRIGHT = '''
12+
Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
13+
14+
Licensed under the Apache License, Version 2.0 (the "License");
15+
you may not use this file except in compliance with the License.
16+
You may obtain a copy of the License at
17+
18+
http://www.apache.org/licenses/LICENSE-2.0
19+
20+
Unless required by applicable law or agreed to in writing, software
21+
distributed under the License is distributed on an "AS IS" BASIS,
22+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
See the License for the specific language governing permissions and
24+
limitations under the License.
25+
'''
26+
27+
LANG_COMMENT_MARK = None
28+
29+
NEW_LINE_MARK = None
30+
31+
COPYRIGHT_HEADER = None
32+
33+
if platform.system() == "Windows":
34+
NEW_LINE_MARK = "\r\n"
35+
else:
36+
NEW_LINE_MARK = '\n'
37+
COPYRIGHT_HEADER = COPYRIGHT.split(NEW_LINE_MARK)[1]
38+
p = re.search('(\d{4})', COPYRIGHT_HEADER).group(0)
39+
process = subprocess.Popen(["date", "+%Y"], stdout=subprocess.PIPE)
40+
date, err = process.communicate()
41+
date = date.decode("utf-8").rstrip("\n")
42+
COPYRIGHT_HEADER = COPYRIGHT_HEADER.replace(p, date)
43+
44+
45+
def generate_copyright(template, lang='C'):
46+
if lang == 'Python':
47+
LANG_COMMENT_MARK = '#'
48+
else:
49+
LANG_COMMENT_MARK = "//"
50+
51+
lines = template.split(NEW_LINE_MARK)
52+
BLANK = " "
53+
ans = LANG_COMMENT_MARK + BLANK + COPYRIGHT_HEADER + NEW_LINE_MARK
54+
for lino, line in enumerate(lines):
55+
if lino == 0 or lino == 1 or lino == len(lines) - 1: continue
56+
if len(line) == 0:
57+
BLANK = ""
58+
else:
59+
BLANK = " "
60+
ans += LANG_COMMENT_MARK + BLANK + line + NEW_LINE_MARK
61+
62+
return ans + "\n"
63+
64+
65+
def lang_type(filename):
66+
if filename.endswith(".py"):
67+
return "Python"
68+
elif filename.endswith(".h"):
69+
return "C"
70+
elif filename.endswith(".c"):
71+
return "C"
72+
elif filename.endswith(".hpp"):
73+
return "C"
74+
elif filename.endswith(".cc"):
75+
return "C"
76+
elif filename.endswith(".cpp"):
77+
return "C"
78+
elif filename.endswith(".cu"):
79+
return "C"
80+
elif filename.endswith(".cuh"):
81+
return "C"
82+
elif filename.endswith(".go"):
83+
return "C"
84+
elif filename.endswith(".proto"):
85+
return "C"
86+
else:
87+
print("Unsupported filetype %s", filename)
88+
exit(0)
89+
90+
91+
PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
92+
93+
94+
def main(argv=None):
95+
parser = argparse.ArgumentParser(
96+
description='Checker for copyright declaration.')
97+
parser.add_argument('filenames', nargs='*', help='Filenames to check')
98+
args = parser.parse_args(argv)
99+
100+
retv = 0
101+
for filename in args.filenames:
102+
fd = io.open(filename, encoding="utf-8")
103+
first_line = fd.readline()
104+
second_line = fd.readline()
105+
if "COPYRIGHT (C)" in first_line.upper(): continue
106+
if first_line.startswith("#!") or PYTHON_ENCODE.match(
107+
second_line) != None or PYTHON_ENCODE.match(first_line) != None:
108+
continue
109+
original_contents = io.open(filename, encoding="utf-8").read()
110+
new_contents = generate_copyright(
111+
COPYRIGHT, lang_type(filename)) + original_contents
112+
print('Auto Insert Copyright Header {}'.format(filename))
113+
retv = 1
114+
with io.open(filename, 'w') as output_file:
115+
output_file.write(new_contents)
116+
117+
return retv
118+
119+
120+
if __name__ == '__main__':
121+
exit(main())

.pre-commit-config.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- repo: local
2+
hooks:
3+
- id: yapf
4+
name: yapf
5+
entry: yapf
6+
language: system
7+
args: [-i, --style .style.yapf]
8+
files: \.py$
9+
10+
- repo: https://github.com/pre-commit/pre-commit-hooks
11+
sha: a11d9314b22d8f8c7556443875b731ef05965464
12+
hooks:
13+
- id: check-merge-conflict
14+
- id: check-symlinks
15+
- id: end-of-file-fixer
16+
- id: trailing-whitespace
17+
- id: detect-private-key
18+
- id: check-symlinks
19+
- id: check-added-large-files
20+
21+
- repo: local
22+
hooks:
23+
- id: flake8
24+
name: flake8
25+
entry: flake8
26+
language: system
27+
args:
28+
- --count
29+
- --select=E9,F63,F7,F82
30+
- --show-source
31+
- --statistics
32+
files: \.py$
33+
34+
- repo: local
35+
hooks:
36+
- id: copyright_checker
37+
name: copyright_checker
38+
entry: python ./.copyright.hook
39+
language: system
40+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py)$
41+
exclude: (?!.*third_party)^.*$

.style.yapf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[style]
2+
based_on_style = pep8
3+
column_limit = 80

docs/configs/dataset_group.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ DATASET Group存放所有与数据集相关的配置
6262

6363
## `VIS_FILE_LIST`
6464

65-
可视化列表,调用`pdseg/train.py`进行训练时,如果打开了--use_tbx开关,则在每次模型保存的时候,会读取该列表中的图片进行可视化
65+
可视化列表,调用`pdseg/train.py`进行训练时,如果打开了--use_tb开关,则在每次模型保存的时候,会读取该列表中的图片进行可视化
6666

6767
文件列表由多行组成,每一行的格式为
6868
```
@@ -128,4 +128,4 @@ mydata/train/image4.jpg|mydata/train/image4.label.jpg
128128

129129
### 默认值
130130

131-
255
131+
255

docs/usage.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ python pdseg/export_model.py ${FLAGS} ${OPTIONS}
2727
|--cfg|ALL|配置文件路径|None||
2828
|--use_gpu|train/eval/vis|是否使用GPU进行训练|False||
2929
|--use_mpio|train/eval|是否使用多线程进行IO处理|False|打开该开关会占用一定量的CPU内存,但是可以提高训练速度。</br> NOTE:windows平台下不支持该功能, 建议使用自定义数据初次训练时不打开,打开会导致数据读取异常不可见。 </br> |
30-
|--use_tbx|train|是否使用tensorboardX记录训练数据|False||
30+
|--use_tb|train|是否使用TensorBoard记录训练数据|False||
3131
|--log_steps|train|训练日志的打印周期(单位为step)|10||
3232
|--debug|train|是否打印debug信息|False|IOU等指标涉及到混淆矩阵的计算,会降低训练速度|
33-
|--tbx_log_dir|train|tensorboardX的日志路径|None||
33+
|--tb_log_dir|train|TensorBoard的日志路径|None||
3434
|--do_eval|train|是否在保存模型时进行效果评估|False||
3535
|--vis_dir|vis|保存可视化图片的路径|"visual"||
3636
|--also_save_raw_results|vis|是否保存原始的预测图片|False||
@@ -76,17 +76,17 @@ unzip mini_pet.zip
7676
export CUDA_VISIBLE_DEVICES=0,1
7777
python pdseg/train.py --use_gpu \
7878
--do_eval \
79-
--use_tbx \
80-
--tbx_log_dir train_log \
79+
--use_tb \
80+
--tb_log_dir train_log \
8181
--cfg configs/unet_pet.yaml \
8282
BATCH_SIZE 4 \
8383
TRAIN.PRETRAINED_MODEL unet_coco_init \
8484
DATASET.DATA_DIR mini_pet \
8585
DATASET.TEST_FILE_LIST mini_pet/file_list/test_list.txt \
8686
DATASET.TRAIN_FILE_LIST mini_pet/file_list/train_list.txt \
8787
DATASET.VAL_FILE_LIST mini_pet/file_list/val_list.txt \
88-
DATASET.VIS_FILE_LIST mini_pet/file_list/val_list.txt
89-
TRAIN.SYNC_BATCH_NORM True
88+
DATASET.VIS_FILE_LIST mini_pet/file_list/val_list.txt \
89+
TRAIN.SYNC_BATCH_NORM True \
9090
SOLVER.LR 5e-5
9191
```
9292

@@ -100,7 +100,7 @@ python pdseg/train.py --use_gpu \
100100
101101
### 训练过程可视化
102102

103-
当打开do_eval和use_tbx两个开关后,我们可以通过TensorBoard查看训练的效果
103+
当打开do_eval和use_tb两个开关后,我们可以通过TensorBoard查看训练的效果
104104
```shell
105105
tensorboard --logdir train_log --host {$HOST_IP} --port {$PORT}
106106
```
@@ -147,4 +147,4 @@ python pdseg/export_model.py --cfg configs/unet_pet.yaml \
147147
TEST.TEST_MODEL test/saved_models/unet_pet/final
148148
```
149149

150-
模型会导出到freeze_model目录,接下来就是进行模型的部署,相关步骤,请查看[模型部署](./inference/README.md)
150+
模型会导出到freeze_model目录,接下来就是进行模型的部署,相关步骤,请查看[模型部署](./inference/README.md)

0 commit comments

Comments
 (0)