2 Copyright (c) 2018-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
23 from mo.graph.graph import Node, Graph
24 from mo.utils.error import Error
25 from mo.utils.find_inputs import find_inputs
26 from mo.utils.utils import refer_to_faq_msg
29 def get_node_top(graph: Graph, name: str):
30 node = Node(graph, name)
31 return node.out_edge()['name'] if node else None
34 def build_net(graph: Graph):
36 if not hasattr(os.environ, 'GLOG_minloglevel'):
37 os.environ['GLOG_minloglevel'] = '2'
39 log.info('Partial inference via the framework is available')
41 log.warning('pyCaffe is not available. Partial inference via the framework is not ' +
46 net = caffe.Net(graph.proto_path, graph.caffemodel_path, caffe.TEST)
47 except Exception as err:
49 'Error happened while constructing caffe.Net in the Caffe fallback function: {}. ' +
54 inputs_node_name = find_inputs(graph)
57 for i in inputs_node_name:
58 new_input_shape = graph.node[i]['shape'].astype(int)
59 top_node = get_node_top(graph, i)
60 caffe_shape = list(net.blobs[top_node].shape)
61 if not np.all(caffe_shape == new_input_shape):
62 net.blobs[top_node].reshape(*[int(x) for x in new_input_shape])
70 except KeyError as err:
71 log.error('Error happened in Caffe net.forward: {}.'.format(str(err)))
72 log.error('It may point to the known bug in pycaffe when top and name of the layer do not match.')
73 log.error('Please make sure that the latest pycaffe is used.')
74 raise Error('Cannot infer shapes due to exception in Caffe: {}. ' +
75 refer_to_faq_msg(13), str(err)) from err
76 except Exception as err:
77 raise Error('Cannot infer shapes in Caffe net.forward due to exception: {}.' +
78 refer_to_faq_msg(13), str(err)) from err
80 graph.__setattr__('caffe_net', net)
83 def get_net(graph: Graph):
87 if graph and not hasattr(graph, 'caffe_net'):
89 return getattr(graph, 'caffe_net', None)
92 def caffe_native_node_infer(node: Node):
94 Infers shape of the unknown operation via Caffe if it is available.
95 Requires graph to contain paths to both prototxt and caffemodel files.
96 When it is visited for the first time, net object is created and written to graph.
97 Next time, it just takes the built net from graph.
101 node node to infer the shape for
104 log.error("Caffe fallback is deprecated. It will be removed in future releases. Please use extensions for unsupported layers.\n" +
105 "See more information in the \"Custom Layers in the Model Optimizer\" chapter of the Model Optimizer Developer Guide",
106 extra={'is_warning': True})
107 log.info('Called "caffe_native_node_infer" for node "{}"'.format(node.id))
113 'Cannot infer shape for node "{}" because there is no Caffe available. ' +
114 'Please register python infer function for op = {} or use Caffe for shape inference. ' +
115 refer_to_faq_msg(14),
116 node.soft_get('name'),
120 for iout in range(len(node.out_nodes())):
121 output_shape = np.array(net.blobs[node.top].data.shape, dtype=np.int64)
122 node.out_node(iout).shape = output_shape