Skip to content

Commit f9d93bf

Browse files
committed
Add document to random crop operator
1 parent 1af0b28 commit f9d93bf

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

paddle/fluid/operators/random_crop_op.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class RandomCropOpMaker : public framework::OpProtoAndCheckerMaker {
3636
AddInput("Seed", "The random seed.");
3737
AddOutput("Out", "The cropped instance batch.");
3838
AddOutput("SeedOut", "The random seed after random cropping.")
39-
.AsDispensable();
39+
.AsIntermediate();
4040
AddAttr<std::vector<int>>("shape", "The shape of a cropped instance.");
4141
AddComment(R"DOC(
42-
This operator takes a batch of instance, and do random cropping on each instance.
43-
It means that cropping positions differs on each instance, which is determined
42+
This operator takes a batch of instance, and do random cropping on each instance.
43+
It means that cropping positions differs on each instance, which is determined
4444
by an uniform random generator. All cropped instances have the same shape, which
4545
is determined by the operator's attribute 'shape'.
4646
)DOC");

python/paddle/fluid/layers/layer_function_generator.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
import cStringIO
1616
import functools
1717
import warnings
18+
import string
1819

1920
from ..proto import framework_pb2
2021
from ..framework import OpProtoHolder, Variable
2122
from ..layer_helper import LayerHelper
2223

23-
__all__ = [
24-
'deprecated',
25-
'generate_layer_fn',
26-
'autodoc',
27-
]
24+
__all__ = ['deprecated', 'generate_layer_fn', 'autodoc', 'templatedoc']
2825

2926

3027
def _convert_(name):
@@ -43,6 +40,10 @@ def _convert_(name):
4340
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
4441

4542

43+
def _type_to_str_(tp):
44+
return framework_pb2.AttrType.Name(tp)
45+
46+
4647
def _generate_doc_string_(op_proto):
4748
"""
4849
Generate docstring by OpProto
@@ -54,9 +55,6 @@ def _generate_doc_string_(op_proto):
5455
str: the document string
5556
"""
5657

57-
def _type_to_str_(tp):
58-
return framework_pb2.AttrType.Name(tp)
59-
6058
if not isinstance(op_proto, framework_pb2.OpProto):
6159
raise TypeError("OpProto should be `framework_pb2.OpProto`")
6260

@@ -220,3 +218,42 @@ def __impl__(func):
220218
return func
221219

222220
return __impl__
221+
222+
223+
def templatedoc():
224+
"""
225+
Decorator of layer function. It will use the docstring from the layer
226+
function as the template. The template arguments are:
227+
228+
* ${comment}: The operator comment written in CPP.
229+
* ${{name}_comment}: The comment of ${name} written with AddAttr, AddOutput,
230+
and AddInput. The ${name} is Python snake style. i.e., xxx_xxx.
231+
* ${{name}_type}: The type of ${name}.
232+
233+
234+
Returns:
235+
Decorated funciton.
236+
"""
237+
238+
def __impl__(func):
239+
op_proto = OpProtoHolder.instance().get_op_proto(func.__name__)
240+
tmpl = string.Template(func.__doc__)
241+
args = {"comment": " ".join(op_proto.comment.split())}
242+
for each_input in op_proto.inputs:
243+
input_name = _convert_(each_input.name)
244+
args["{0}_comment".format(input_name)] = each_input.comment
245+
args["{0}_type".format(input_name)] = "Variable"
246+
for each_attr in op_proto.attrs:
247+
input_name = _convert_(each_attr.name)
248+
args["{0}_comment".format(input_name)] = each_attr.comment
249+
args["{0}_type".format(input_name)] = _type_to_str_(each_attr.type)
250+
251+
for each_opt in op_proto.outputs:
252+
output_name = _convert_(each_opt.name)
253+
args["{0}_comment".format(output_name)] = each_opt.comment
254+
args["{0}_type".format(output_name)] = "Variable"
255+
256+
func.__doc__ = tmpl.substitute(args)
257+
return func
258+
259+
return __impl__

python/paddle/fluid/layers/nn.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
from ..initializer import Normal, Constant
2020
from ..framework import Variable
2121
from ..param_attr import ParamAttr
22-
from layer_function_generator import autodoc
22+
from layer_function_generator import autodoc, templatedoc
2323
from tensor import concat
2424
import utils
25+
import random
2526

2627
__all__ = [
2728
'fc',
@@ -3992,10 +3993,34 @@ def _is_list_or_turple_(data):
39923993
return out
39933994

39943995

3995-
def random_crop(input, shape, seed=1):
3996+
@templatedoc()
3997+
def random_crop(x, shape, seed=None):
3998+
"""
3999+
**Random crop operator**
4000+
4001+
${comment}
4002+
4003+
Examples:
4004+
>>> img = fluid.layers.data("img", [3, 256, 256])
4005+
>>> cropped_img = fluid.layers.random_crop(img, shape=[3, 224, 224])
4006+
4007+
Args:
4008+
x(${x_type}): ${x_comment}
4009+
shape(${shape_type}): ${shape_comment}
4010+
seed(int|${seed_type}|None): ${seed_comment} By default, the seed will
4011+
get from `random.randint(-65536, 65535)`.
4012+
4013+
Returns:
4014+
${out_comment}
4015+
4016+
"""
4017+
39964018
helper = LayerHelper("random_crop", **locals())
39974019
dtype = helper.input_dtype()
39984020
out = helper.create_tmp_variable(dtype)
4021+
if seed is None:
4022+
seed = random.randint(-65536, 65535)
4023+
39994024
if isinstance(seed, int):
40004025
seed_value = seed
40014026
seed = helper.create_tmp_variable(dtype="int64")

0 commit comments

Comments
 (0)