Caffe2 - Python API
A deep learning, cross platform ML framework
functional.py
1 from __future__ import absolute_import
2 from __future__ import division
3 from __future__ import print_function
4 from __future__ import unicode_literals
5 
6 from caffe2.python import core, workspace
7 from collections import namedtuple
8 from six import string_types
9 
10 OpSchema = workspace.C.OpSchema
11 
12 
13 def namedtupledict(typename, field_names, *args, **kwargs):
14  field_names_map = {n: i for i, n in enumerate(field_names)}
15  # Some output names are invalid python identifier, e.g. "0"
16  kwargs.setdefault('rename', True)
17  data = namedtuple(typename, field_names, *args, **kwargs)
18 
19  def getitem(self, key):
20  if isinstance(key, string_types):
21  key = field_names_map[key]
22  return super(type(self), self).__getitem__(key)
23 
24  data.__getitem__ = getitem
25  return data
26 
27 
28 class _Functional(object):
29  def __getattribute__(self, op_type):
30  def op_func(*inputs, **args):
31  ws = workspace.C.Workspace()
32  schema = OpSchema.get(op_type)
33  input_prefix = 'input_'
34  output_prefix = 'output_'
35 
36  def get_name_list(prefix, num, max_num):
37  return [prefix + str(x) for x in range(min(num, max_num))]
38 
39  input_names, output_names = [], []
40  input_names = get_name_list(
41  input_prefix, len(inputs), schema.max_input
42  )
43  # verify the length of input name is in range
44  # of schema
45  num_input = len(input_names)
46  if num_input > schema.max_input or num_input < \
47  schema.min_input or not schema.num_inputs_allowed(num_input):
48  raise ValueError(
49  "Functional C2: Number of inputs not in \
50  range: {} - {} or not allowed."
51  .format(schema.min_input, schema.max_input)
52  )
53 
54  if 'num_output' in args:
55  num_output = args['num_output']
56  if num_output > schema.max_output or \
57  num_output < schema.min_output or \
58  not schema.num_outputs_allowed(num_output) or \
59  not schema.num_inputs_outputs_allowed(num_input,
60  num_output):
61  raise ValueError(
62  "Functional C2: Number of output \
63  not in range: {} - {} or not allowed"
64  .format(schema.min_output, schema.max_output)
65  )
66  output_names = get_name_list(
67  output_prefix, num_output, schema.max_output
68  )
69  args.pop('num_output')
70  calculated = schema.CalculateOutput(num_input)
71  if not output_names and calculated != -1:
72  output_names = get_name_list(
73  output_prefix, calculated, schema.max_output
74  )
75 
76  if not output_names:
77  max_output = schema.max_output
78  # For an op with max_output == inf
79  # and no Output defined in schema
80  # user should pass output_size explicitly
81  if schema.inf == max_output:
82  raise ValueError(
83  "For operators with max_output == inf,\
84  user should pass num_output explicity."
85  )
86  output_names = get_name_list(
87  output_prefix, max_output, max_output
88  )
89  for i, input_blob in enumerate(inputs):
90  ws.create_blob(input_names[i]).feed(input_blob)
91 
92  op = core.CreateOperator(
93  op_type, input_names, output_names, **args
94  )
95  ws._run_operator(op.SerializeToString())
96  # RunOperator
97  output_values = [ws.fetch_blob(x) for x in output_names]
98  return namedtupledict('output', output_names)(*output_values)
99 
100  return op_func
101 
102 
103 Functional = _Functional()