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.
21 from typing import Iterable
24 def int64_array(l: Iterable):
25 return np.array(l, dtype=np.int64)
28 def float_array(l: list):
29 return np.array(l, dtype=np.float64)
32 def mark_input_bins(node, names=('weights', 'biases'), start_port: int = 1):
34 Preparing necessary attributes for edges at input ports starting from start_port.
35 It is applicable for convolution and other operations that have constant inputs which
36 are intended to be dumped as IE IR bin file.
38 for port, name in enumerate(names, start=start_port):
39 if port in node.in_nodes() and node.in_node(port).has_valid('value'):
40 node.in_edge(port)['bin'] = name
43 def assign_dims_to_weights(node, spatial, input_channel, output_channel=None, dims_number=None):
44 if spatial is not None:
45 node['spatial_dims'] = np.array(spatial, dtype=np.int64)
46 node['input_channel_dim'] = np.array(input_channel, dtype=np.int64)
47 node['output_channel_dim'] = np.array(output_channel, dtype=np.int64)
48 if 'input_channel_dim' not in node['dim_attrs']:
49 node['dim_attrs'].append('input_channel_dim')
50 node['dims_number'] = dims_number
54 return x.copy() if x is not None else None
57 def convert_tf_padding_to_str(padding):
58 mapping = {b'SAME': 'same_upper', b'VALID': 'valid'}
59 return mapping[padding.s]
62 # TODO eliminate this dependency and pass necessary function as an argument
63 def tf_window_op_pad_infer(input, window, stride, auto_pad, is_deconv=False):
64 if input is None or window is None or stride is None or auto_pad is None:
67 normalized_stride = stride
69 normalized_stride = 1 / stride
71 if auto_pad in ['same_lower', 'same_upper']:
72 if auto_pad == 'same_upper':
73 output = np.int64(np.ceil(input / normalized_stride))
75 output = np.int64(np.floor(input / normalized_stride))
76 residual = input % stride
78 full_pad = window.copy()
79 full_pad[mask] -= stride[mask]
80 mask = np.logical_not(mask) # pylint: disable=assignment-from-no-return
81 full_pad[mask] -= input[mask] % stride[mask]
82 full_pad = np.maximum(full_pad, 0) # pylint: disable=assignment-from-no-return
83 low_pad = np.int64(full_pad / 2)
84 high_pad = full_pad - low_pad
85 pad = np.array([low_pad, high_pad]).transpose()
86 elif auto_pad == 'valid':
87 output = np.int64(np.ceil((input - window + 1) / normalized_stride))
88 pad = np.zeros((len(output), 2), dtype=np.int64)
90 log.error("Unsupported padding scheme: {}".format(auto_pad))