Caffe2 - Python API
A deep learning, cross platform ML framework
backend_cpp_rep.py
1 ## @package onnx
2 # Module caffe2.python.onnx.backend_rep_cpp
3 
4 from __future__ import absolute_import
5 from __future__ import division
6 from __future__ import print_function
7 from __future__ import unicode_literals
8 
9 from onnx.backend.base import BackendRep, namedtupledict
10 
11 # This is a wrapper around C++ Caffe2BackendRep,
12 # mainly to handle the different input and output types for convenience of Python
13 class Caffe2CppRep(BackendRep):
14  def __init__(self, cpp_rep):
15  super(Caffe2CppRep, self).__init__()
16  self.__core = cpp_rep
17  self.__external_outputs = cpp_rep.external_outputs()
18  self.__external_inputs = cpp_rep.external_inputs()
19  self.__uninitialized_inputs = cpp_rep.uninitialized_inputs()
20 
21  def init_net(self):
22  return self.__core.init_net()
23 
24  def pred_net(self):
25  return self.__core.pred_net()
26 
27  def external_outputs(self):
28  return self.__core.external_outputs()
29 
30  def external_inputs(self):
31  return self.__core.external_inputs()
32 
33  def run(self, inputs):
34  output_values = None
35  if isinstance(inputs, dict):
36  output_values = self.__core.run(inputs)
37  elif isinstance(inputs, list) or isinstance(inputs, tuple):
38  if len(inputs) != len(self.__uninitialized_inputs):
39  raise RuntimeError('Expected {} values for uninitialized '
40  'graph inputs ({}), but got {}.'.format(
41  len(self.__uninitialized_inputs),
42  ', '.join(self.__uninitialized_inputs),
43  len(inputs)))
44  input_map = {}
45  for k, v in zip(self.__uninitialized_inputs, inputs):
46  input_map[k] = v
47  output_values = self.__core.run(input_map)
48  else:
49  # single input
50  output_values = self.__core.run([inputs])
51  return namedtupledict('Outputs', self.__external_outputs)(*output_values)
52