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