1 # ******************************************************************************
2 # Copyright 2017-2020 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.
15 # ******************************************************************************
17 """Factory functions for all ngraph ops."""
18 from typing import Callable, Iterable, List, Optional, Set, Union
21 from functools import partial
23 from ngraph.impl import Node, Shape
24 from ngraph.impl.op import Constant, Parameter
25 from ngraph.opset_utils import _get_node_factory
26 from ngraph.utils.decorators import binary_op, nameable_op, unary_op
27 from ngraph.utils.input_validation import (
29 check_valid_attributes,
30 is_non_negative_value,
33 from ngraph.utils.node_factory import NodeFactory
34 from ngraph.utils.tensor_iterator_types import (
36 TensorIteratorSliceInputDesc,
37 TensorIteratorMergedInputDesc,
38 TensorIteratorInvariantInputDesc,
39 TensorIteratorBodyOutputDesc,
40 TensorIteratorConcatOutputDesc,
42 from ngraph.utils.types import (
56 _get_node_factory_opset4 = partial(_get_node_factory, "opset4")
58 # -------------------------------------------- ops ------------------------------------------------
64 logit_length: NodeInput,
66 label_length: NodeInput,
67 blank_index: Optional[NodeInput] = None,
68 preprocess_collapse_repeated: bool = False,
69 ctc_merge_repeated: bool = True,
71 name: Optional[str] = None,
73 """Return a node which performs CTCLoss.
75 :param logits: 3-D tensor of logits.
76 :param logit_length: 1-D tensor of lengths for each object from a batch.
77 :param labels: 2-D tensor of labels for which likelihood is estimated using logits.
78 :param label_length: 1-D tensor of length for each label sequence.
79 :param blank_index: Scalar used to mark a blank index.
80 :param preprocess_collapse_repeated: Flag for preprocessing labels before loss calculation.
81 :param ctc_merge_repeated: Flag for merging repeated characters in a potential alignment.
82 :param unique: Flag to find unique elements in a target.
83 :return: The new node which performs CTCLoss
85 if blank_index is not None:
86 inputs = as_nodes(logits, logit_length, labels, label_length, blank_index)
88 inputs = as_nodes(logits, logit_length, labels, label_length)
91 "preprocess_collapse_repeated": preprocess_collapse_repeated,
92 "ctc_merge_repeated": ctc_merge_repeated,
96 return _get_node_factory_opset4().create("CTCLoss", inputs, attributes)
100 def non_max_suppression(
103 max_output_boxes_per_class: Optional[NodeInput] = None,
104 iou_threshold: Optional[NodeInput] = None,
105 score_threshold: Optional[NodeInput] = None,
106 box_encoding: str = "corner",
107 sort_result_descending: bool = True,
108 output_type: str = "i64",
109 name: Optional[str] = None,
111 """Return a node which performs NonMaxSuppression.
113 :param boxes: Tensor with box coordinates.
114 :param scores: Tensor with box scores.
115 :param max_output_boxes_per_class: Tensor Specifying maximum number of boxes
116 to be selected per class.
117 :param iou_threshold: Tensor specifying intersection over union threshold
118 :param score_threshold: Tensor specifying minimum score to consider box for the processing.
119 :param box_encoding: Format of boxes data encoding.
120 :param sort_result_descending: Flag that specifies whenever it is necessary to sort selected
121 boxes across batches or not.
122 :param output_type: Output element type.
123 :return: The new node which performs NonMaxSuppression
125 if max_output_boxes_per_class is None:
126 max_output_boxes_per_class = make_constant_node(0, np.int64)
127 if iou_threshold is None:
128 iou_threshold = make_constant_node(0, np.float32)
129 if score_threshold is None:
130 score_threshold = make_constant_node(0, np.float32)
132 inputs = as_nodes(boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold)
134 "box_encoding": box_encoding,
135 "sort_result_descending": sort_result_descending,
136 "output_type": output_type,
139 return _get_node_factory_opset4().create("NonMaxSuppression", inputs, attributes)
143 def softplus(data: NodeInput, name: Optional[str] = None) -> Node:
144 """Apply SoftPlus operation on each element of input tensor.
146 :param data: The tensor providing input data.
147 :return: The new node with SoftPlus operation applied on each element.
149 return _get_node_factory_opset4().create("SoftPlus", as_nodes(data), {})
153 def mish(data: NodeInput, name: Optional[str] = None,) -> Node:
154 """Return a node which performs Mish.
156 :param data: Tensor with input data floating point type.
157 :return: The new node which performs Mish
159 return _get_node_factory_opset4().create("Mish", as_nodes(data), {})
163 def hswish(data: NodeInput, name: Optional[str] = None,) -> Node:
164 """Return a node which performs HSwish (hard version of Swish).
166 :param data: Tensor with input data floating point type.
167 :return: The new node which performs HSwish
169 return _get_node_factory_opset4().create("HSwish", as_nodes(data), {})
175 beta: Optional[NodeInput] = None,
176 name: Optional[str] = None,
178 """Return a node which performing Swish activation function Swish(x, beta=1.0) = x * sigmoid(x * beta)).
180 :param data: Tensor with input data floating point type.
181 :return: The new node which performs Swish
184 beta = make_constant_node(1.0, np.float32)
185 return _get_node_factory_opset4().create("Swish", as_nodes(data, beta), {})
189 def acosh(node: NodeInput, name: Optional[str] = None) -> Node:
190 """Apply hyperbolic inverse cosine function on the input node element-wise.
192 :param node: One of: input node, array or scalar.
193 :param name: Optional new name for output node.
194 :return: New node with arccosh operation applied on it.
196 return _get_node_factory_opset4().create("Acosh", [node])
200 def asinh(node: NodeInput, name: Optional[str] = None) -> Node:
201 """Apply hyperbolic inverse sinus function on the input node element-wise.
203 :param node: One of: input node, array or scalar.
204 :param name: Optional new name for output node.
205 :return: New node with arcsinh operation applied on it.
207 return _get_node_factory_opset4().create("Asinh", [node])
211 def atanh(node: NodeInput, name: Optional[str] = None) -> Node:
212 """Apply hyperbolic inverse tangent function on the input node element-wise.
214 :param node: One of: input node, array or scalar.
215 :param name: Optional new name for output node.
216 :return: New node with arctanh operation applied on it.
218 return _get_node_factory_opset4().create("Atanh", [node])
225 image_shape: NodeInput,
227 name: Optional[str] = None,
229 """Filter bounding boxes and outputs only those with the highest prediction confidence.
231 :param class_probs: 4D input floating point tensor with class prediction scores.
232 :param bbox_deltas: 4D input floating point tensor with corrected predictions of bounding boxes
233 :param image_shape: The 1D input tensor with 3 or 4 elements describing image shape.
234 :param attrs: The dictionary containing key, value pairs for attributes.
235 :param name: Optional name for the output node.
236 * base_size The size of the anchor to which scale and ratio attributes are applied.
237 Range of values: a positive unsigned integer number
240 * pre_nms_topn The number of bounding boxes before the NMS operation.
241 Range of values: a positive unsigned integer number
244 * post_nms_topn The number of bounding boxes after the NMS operation.
245 Range of values: a positive unsigned integer number
248 * nms_thresh The minimum value of the proposal to be taken into consideration.
249 Range of values: a positive floating-point number
252 * feat_stride The step size to slide over boxes (in pixels).
253 Range of values: a positive unsigned integer
256 * min_size The minimum size of box to be taken into consideration.
257 Range of values: a positive unsigned integer number
260 * ratio The ratios for anchor generation.
261 Range of values: a list of floating-point numbers
264 * scale The scales for anchor generation.
265 Range of values: a list of floating-point numbers
268 * clip_before_nms The flag that specifies whether to perform clip bounding boxes before
269 non-maximum suppression or not.
270 Range of values: True or False
273 * clip_after_nms The flag that specifies whether to perform clip bounding boxes after
274 non-maximum suppression or not.
275 Range of values: True or False
278 * normalize The flag that specifies whether to perform normalization of output boxes to
279 [0,1] interval or not.
280 Range of values: True or False
283 * box_size_scale Specifies the scale factor applied to logits of box sizes before decoding.
284 Range of values: a positive floating-point number
287 * box_coordinate_scale Specifies the scale factor applied to logits of box coordinates
289 Range of values: a positive floating-point number
292 * framework Specifies how the box coordinates are calculated.
293 Range of values: "" (empty string) - calculate box coordinates like in Caffe*
294 tensorflow - calculate box coordinates like in the TensorFlow*
295 Object Detection API models
296 Default value: "" (empty string)
298 Example of attribute dictionary:
299 .. code-block:: python
308 'ratio': [0.1, 1.5, 2.0, 2.5],
309 'scale': [2, 3, 3, 4],
311 Optional attributes which are absent from dictionary will be set with corresponding default.
312 :return: Node representing Proposal operation.
315 ("base_size", True, np.unsignedinteger, is_positive_value),
316 ("pre_nms_topn", True, np.unsignedinteger, is_positive_value),
317 ("post_nms_topn", True, np.unsignedinteger, is_positive_value),
318 ("nms_thresh", True, np.floating, is_positive_value),
319 ("feat_stride", True, np.unsignedinteger, is_positive_value),
320 ("min_size", True, np.unsignedinteger, is_positive_value),
321 ("ratio", True, np.floating, None),
322 ("scale", True, np.floating, None),
323 ("clip_before_nms", False, np.bool_, None),
324 ("clip_after_nms", False, np.bool_, None),
325 ("normalize", False, np.bool_, None),
326 ("box_size_scale", False, np.floating, is_positive_value),
327 ("box_coordinate_scale", False, np.floating, is_positive_value),
328 ("framework", False, np.str_, None),
331 check_valid_attributes("Proposal", attrs, requirements)
333 return _get_node_factory_opset4().create(
334 "Proposal", [class_probs, bbox_deltas, as_node(image_shape)], attrs
340 node: NodeInput, reduction_axes: NodeInput, keep_dims: bool = False, name: Optional[str] = None
342 """L1-reduction operation on input tensor, eliminating the specified reduction axes.
344 :param node: The tensor we want to mean-reduce.
345 :param reduction_axes: The axes to eliminate through mean operation.
346 :param keep_dims: If set to True it holds axes that are used for reduction
347 :param name: Optional name for output node.
348 :return: The new node performing mean-reduction operation.
350 return _get_node_factory_opset4().create(
351 "ReduceL1", as_nodes(node, reduction_axes), {"keep_dims": keep_dims}
357 node: NodeInput, reduction_axes: NodeInput, keep_dims: bool = False, name: Optional[str] = None
359 """L2-reduction operation on input tensor, eliminating the specified reduction axes.
361 :param node: The tensor we want to mean-reduce.
362 :param reduction_axes: The axes to eliminate through mean operation.
363 :param keep_dims: If set to True it holds axes that are used for reduction
364 :param name: Optional name for output node.
365 :return: The new node performing mean-reduction operation.
367 return _get_node_factory_opset4().create(
368 "ReduceL2", as_nodes(node, reduction_axes), {"keep_dims": keep_dims}
375 initial_hidden_state: NodeInput,
376 initial_cell_state: NodeInput,
381 activations: List[str] = None,
382 activations_alpha: List[float] = None,
383 activations_beta: List[float] = None,
385 name: Optional[str] = None,
387 """Return a node which performs LSTMCell operation.
389 :param X: The input tensor with shape: [batch_size, input_size].
390 :param initial_hidden_state: The hidden state tensor with shape: [batch_size, hidden_size].
391 :param initial_cell_state: The cell state tensor with shape: [batch_size, hidden_size].
392 :param W: The weight tensor with shape: [4*hidden_size, input_size].
393 :param R: The recurrence weight tensor with shape: [4*hidden_size, hidden_size].
394 :param B: The bias tensor for gates with shape: [4*hidden_size].
395 :param hidden_size: Specifies hidden state size.
396 :param activations: The list of three activation functions for gates.
397 :param activations_alpha: The list of alpha parameters for activation functions.
398 :param activations_beta: The list of beta parameters for activation functions.
399 :param clip: Specifies bound values [-C, C] for tensor clipping performed before activations.
400 :param name: An optional name of the output node.
402 :return: The new node represents LSTMCell. Node outputs count: 2.
404 if activations is None:
405 activations = ["sigmoid", "tanh", "tanh"]
406 if activations_alpha is None:
407 activations_alpha = []
408 if activations_beta is None:
409 activations_beta = []
411 node_inputs = as_nodes(X, initial_hidden_state, initial_cell_state, W, R, B)
414 "hidden_size": hidden_size,
415 "activations": activations,
416 "activations_alpha": activations_alpha,
417 "activations_beta": activations_beta,
420 return _get_node_factory_opset4().create("LSTMCell", node_inputs, attributes)