Skip to content

Commit 6605498

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into add-dot_product_attention
2 parents 9bcb2d2 + 38c6105 commit 6605498

File tree

399 files changed

+5570
-125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

399 files changed

+5570
-125
lines changed

.copyright.hook

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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) 2016 PaddlePaddle Authors. All Rights Reserve.
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+
ans = LANG_COMMENT_MARK + COPYRIGHT_HEADER + NEW_LINE_MARK
53+
for lino, line in enumerate(lines):
54+
if lino == 0 or lino == 1 or lino == len(lines) - 1: continue
55+
ans += LANG_COMMENT_MARK + line + NEW_LINE_MARK
56+
57+
return ans
58+
59+
60+
def lang_type(filename):
61+
if filename.endswith(".py"):
62+
return "Python"
63+
elif filename.endswith(".h"):
64+
return "C"
65+
elif filename.endswith(".hpp"):
66+
return "C"
67+
elif filename.endswith(".cc"):
68+
return "C"
69+
elif filename.endswith(".cpp"):
70+
return "C"
71+
elif filename.endswith(".cu"):
72+
return "C"
73+
elif filename.endswith(".cuh"):
74+
return "C"
75+
elif filename.endswith(".go"):
76+
return "C"
77+
elif filename.endswith(".proto"):
78+
return "C"
79+
else:
80+
print("Unsupported filetype")
81+
exit(0)
82+
83+
84+
def main(argv=None):
85+
parser = argparse.ArgumentParser(
86+
description='Checker for copyright declaration.')
87+
parser.add_argument('filenames', nargs='*', help='Filenames to check')
88+
args = parser.parse_args(argv)
89+
90+
retv = 0
91+
for filename in args.filenames:
92+
first_line = io.open(filename).readline()
93+
if "COPYRIGHT" in first_line.upper() : continue
94+
original_contents = io.open(filename).read()
95+
new_contents = generate_copyright(
96+
COPYRIGHT, lang_type(filename)) + original_contents
97+
print('Auto Insert Copyright Header {}'.format(filename))
98+
retv = 1
99+
with io.open(filename, 'w') as output_file:
100+
output_file.write(new_contents)
101+
102+
return retv
103+
104+
105+
if __name__ == '__main__':
106+
exit(main())

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@
3131
- id: go-fmt
3232
types:
3333
- go
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)^.*$ | (?!.*book)^.*$

adversarial/advbox/attacks/base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
"""
215
The base model of the model.
316
"""

adversarial/advbox/attacks/gradientsign.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
"""
215
This module provide the attack method for FGSM's implement.
316
"""
@@ -36,3 +49,39 @@ def _apply(self, image_label, epsilons=1000):
3649

3750

3851
FGSM = GradientSignAttack
52+
53+
54+
class IteratorGradientSignAttack(Attack):
55+
"""
56+
This attack was originally implemented by Alexey Kurakin(Google Brain).
57+
Paper link: https://arxiv.org/pdf/1607.02533.pdf
58+
"""
59+
60+
def _apply(self, image_label, epsilons=100, steps=10):
61+
"""
62+
Apply the iterative gradient sign attack.
63+
Args:
64+
image_label(list): The image and label tuple list of one element.
65+
epsilons(list|tuple|int): The epsilon (input variation parameter).
66+
steps(int): The number of iterator steps.
67+
Return:
68+
numpy.ndarray: The adversarail sample generated by the algorithm.
69+
"""
70+
assert len(image_label) == 1
71+
pre_label = np.argmax(self.model.predict(image_label))
72+
gradient = self.model.gradient(image_label)
73+
min_, max_ = self.model.bounds()
74+
75+
if not isinstance(epsilons, Iterable):
76+
epsilons = np.linspace(0, 1, num=epsilons + 1)
77+
78+
for epsilon in epsilons:
79+
adv_img = image_label[0][0].reshape(gradient.shape)
80+
for _ in range(steps):
81+
gradient = self.model.gradient([(adv_img, image_label[0][1])])
82+
gradient_sign = np.sign(gradient) * (max_ - min_)
83+
adv_img = adv_img + epsilon * gradient_sign
84+
adv_img = np.clip(adv_img, min_, max_)
85+
adv_label = np.argmax(self.model.predict([(adv_img, 0)]))
86+
if pre_label != adv_label:
87+
return adv_img

adversarial/advbox/models/base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
"""
215
The base model of the model.
316
"""

adversarial/advbox/models/paddle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
from __future__ import absolute_import
215

316
import numpy as np

adversarial/fluid_mnist.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
"""
215
CNN on mnist data using fluid api of paddlepaddle
316
"""

adversarial/mnist_tutorial_fgsm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
"""
215
FGSM demos on mnist using advbox tool.
316
"""

benchmark/paddle/image/alexnet.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
#!/usr/bin/env python
215

316
from paddle.trainer_config_helpers import *

benchmark/paddle/image/googlenet.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.
114
#!/usr/bin/env python
215
from paddle.trainer_config_helpers import *
316

0 commit comments

Comments
 (0)