def extract(cls, node):
        dst_type = lambda x: mo_array(x)

        scale = onnx_attr(node,
                          'scale',
                          'f',
                          default=mo_array(1.0),
                          dst_type=dst_type)
        bias = onnx_attr(node,
                         'bias',
                         'floats',
                         default=None,
                         dst_type=dst_type)

        # Expand dims for bias in case if it is not scalar
        if bias.ndim != 0:
            broadcast_dims_cnt = 2 if node.graph.graph[
                'layout'] == 'NCHW' else 0
            for idx in range(broadcast_dims_cnt):
                bias = np.expand_dims(bias, axis=-1)

        node['scale'] = scale
        node['bias'] = bias

        return cls.enabled
Exemple #2
0
    def extract(cls, node):
        mode = onnx_attr(node,
                         'mode',
                         's',
                         default='constant',
                         dst_type=lambda x: x.decode())
        # Pytorch 1.3 while converting to opset 11, creates Pad from older opset.
        # To be able to convert such models we have to check if pads attribute exists.
        pads = onnx_attr(node, 'pads', 'ints', dst_type=int64_array)
        if get_onnx_opset_version(node) < 11 or pads is not None:
            value = onnx_attr(node, 'value', 'f', default=0.)

            assert pads is not None, 'pads is required attribute for Pad operation'

            # MO Pad op and ONNX Pad op have different format for pads values
            # MO Pad has Dx2 where D is the total number of dimensions
            # ONNX Pad pads flat layout, so need to reshape and transpose

            pads = np.transpose(pads.reshape([2, -1]))

            AttributedPad.update_node_stat(node, {
                'mode': mode,
                'pads': pads,
                'fill_value': value
            })
        else:
            ONNXPad.update_node_stat(node, {'mode': mode})
        return cls.enabled
Exemple #3
0
    def extract(cls, node):
        # borders: leftBorder, topBorder, rightBorder, bottomBordes
        borders = onnx_attr(node,
                            'border',
                            'ints',
                            default=None,
                            dst_type=int64_array)
        scale = onnx_attr(node,
                          'scale',
                          'ints',
                          default=None,
                          dst_type=int64_array)

        # Crop reference: https://github.com/onnx/onnx/blob/master/docs/Operators.md#Crop
        if len(borders) != 4:
            log.error(
                'ONNX Crop layer {} should take exactly 4 borders instead of {}'
                .format(node.name, len(borders)))
            return False

        attrs = {'axis': int64_array([2, 3])}
        if scale is not None:
            attrs.update({
                'dim': scale,
                'offset': int64_array([borders[1], borders[0]])
            })
        else:
            attrs.update({
                'crop_begin': int64_array([borders[1], borders[0]]),
                'crop_end': int64_array([borders[3], borders[2]])
            })

        Crop.update_node_stat(node, attrs)
        return CropFrontExtractor.enabled
Exemple #4
0
 def extract(cls, node: Node):
     onnx_opset_version = get_onnx_opset_version(node)
     if onnx_opset_version is not None and onnx_opset_version >= 11:
         mode = onnx_attr(node, 'mode', 's', default=b'nearest').decode()
         transformation_mode = onnx_attr(node,
                                         'coordinate_transformation_mode',
                                         's',
                                         default=b'half_pixel').decode()
         nearest_mode = onnx_attr(node,
                                  'nearest_mode',
                                  's',
                                  default=b'round_prefer_floor').decode()
         cubic_coeff_a = onnx_attr(node,
                                   'cubic_coeff_a',
                                   'f',
                                   default=-0.75)
         attrs = {
             'mode': mode,
             'coordinate_transformation_mode': transformation_mode,
             'nearest_mode': nearest_mode,
             'cube_coeff': cubic_coeff_a
         }
         ONNXResize11Op.update_node_stat(node, attrs)
     else:
         mode = onnx_attr(node, 'mode', 's', default=b'nearest').decode()
         ONNXResize10.update_node_stat(node, {'mode': mode})
     return cls.enabled
 def extract(cls, node):
     attrs = {
         'eps': mo_array(onnx_attr(node, 'eps', 'f', default=1e-6), dtype=np.float),
         'num_groups': int64_array(onnx_attr(node, 'num_groups', 'i', default=1)),
     }
     GroupNorm.update_node_stat(node, attrs)
     return cls.enabled
Exemple #6
0
 def extract(cls, node):
     attrs = dict(h=onnx_attr(node, 'h', 'i', 0),
                  w=onnx_attr(node, 'w', 'i', 0),
                  stride_x=onnx_attr(node, 'stride_x', 'f', 0),
                  stride_y=onnx_attr(node, 'stride_y', 'f', 0),
                  flatten=onnx_attr(node, 'flatten', 'i', 1))
     ExperimentalDetectronPriorGridGenerator.update_node_stat(node, attrs)
     return cls.enabled
Exemple #7
0
 def extract(cls, node):
     exclusive = onnx_attr(node, 'exclusive', 'i', 0)
     reverse = onnx_attr(node, 'reverse', 'i', 0)
     CumSum.update_node_stat(node, {
         'exclusive': exclusive,
         'reverse': reverse
     })
     return cls.enabled
    def replace_op(self, graph: Graph, node: Node):
        alpha = onnx_attr(node, 'alpha', 'f', default=0.2)
        beta = onnx_attr(node, 'beta', 'f', default=0.5)

        hard_sigmoid = create_op_with_const_inputs(graph, HardSigmoid, {1: np.array(alpha), 2: np.array(beta)},
                                                   {'name': node.name + '/HardSigmoid_'})

        node.in_port(0).get_connection().set_destination(hard_sigmoid.in_port(0))
        return [hard_sigmoid.id]
 def extract(cls, node):
     attrs = dict(min_size=onnx_attr(node, 'min_size', 'f', 0.0),
                  nms_threshold=onnx_attr(node, 'nms_threshold', 'f', 0.7),
                  post_nms_count=onnx_attr(node, 'post_nms_count', 'i',
                                           1000),
                  pre_nms_count=onnx_attr(node, 'pre_nms_count', 'i', 1000))
     ExperimentalDetectronGenerateProposalsSingleImage.update_node_stat(
         node, attrs)
     return cls.enabled
Exemple #10
0
 def extract(cls, node):
     attrs = {
         'alpha': onnx_attr(node, 'alpha', 'f', 1e-4),
         'beta': onnx_attr(node, 'beta', 'f', 0.75),
         'bias': onnx_attr(node, 'bias', 'f', 1.0),
         'local_size': onnx_attr(node, 'size', 'i', None),
     }
     AttributedLRN.update_node_stat(node, attrs)
     return cls.enabled
Exemple #11
0
    def extract(cls, node):
        batch_axis = onnx_attr(node, 'batch_axis', 'i', default=1)
        time_axis = onnx_attr(node, 'time_axis', 'i', default=0)

        attrs = {
            'batch_axis': batch_axis,
            'seq_axis': time_axis,
        }
        ReverseSequence.update_node_stat(node, attrs)
        return cls.enabled
    def extract(cls, node):
        onnx_opset_version = get_onnx_opset_version(node)
        if onnx_opset_version is not None and onnx_opset_version >= 9:
            mode = onnx_attr(node,
                             'mode',
                             's',
                             default='nearest',
                             dst_type=lambda x: x.decode())
            ONNXResize10.update_node_stat(node, {'mode': mode})
        else:
            mode = onnx_attr(node,
                             'mode',
                             's',
                             default='nearest',
                             dst_type=lambda x: x.decode())
            scales = onnx_attr(
                node,
                'scales',
                'floats',
                dst_type=lambda x: np.array(x, dtype=np.float32))
            width_scale = onnx_attr(node, 'width_scale', 'f')
            height_scale = onnx_attr(node, 'height_scale', 'f')

            supported_modes = ['nearest', 'linear']
            if mode not in supported_modes:
                raise Error(
                    'Error decoding Upsample node {}, mode = {} is not in the list of supported modes {}.',
                    node.name, mode, supported_modes)

            if scales is not None:
                if scales.shape != (4, ):
                    raise Error(
                        'Upsample scales attribute is wrong for node {}. Only 4D scales are supported.',
                        node.name)
                if math.fabs(scales[0] - 1) > 1e-5 or math.fabs(scales[1] -
                                                                1) > 1e-5:
                    raise Error(
                        'Upsampling of batch and feature dimensions is not supported for node {}.',
                        node.name)
                height_scale = scales[2]
                width_scale = scales[3]

            if (width_scale is None
                    or height_scale is None) and len(node.in_nodes()) != 2:
                raise Error(
                    'One/both of widths_scale = {} and height_scale = {} is not defined for Upsample node {}.',
                    width_scale, height_scale, node.name)

            UpsampleOp.update_node_stat(
                node, {
                    'mode': mode,
                    'height_scale': height_scale,
                    'width_scale': width_scale
                })
        return cls.enabled
Exemple #13
0
    def extract(cls, node):
        dst_type = lambda x: np.array(x)

        scale = onnx_attr(node, 'alpha', 'f', default=None, dst_type=dst_type)
        bias = onnx_attr(node, 'beta', 'f', default=None, dst_type=dst_type)

        node['scale'] = scale
        node['bias'] = bias
        node['op'] = 'ImageScaler'

        return cls.enabled
Exemple #14
0
 def extract(cls, node: Node):
     shape = onnx_attr(node, 'shape', 'ints', default=None, dst_type=int64_array)
     out_type = get_onnx_datatype_as_numpy(onnx_attr(node, 'dtype', 'i', default=1))
     seed = onnx_attr(node, 'seed', 'f', default=0.0)
     min_val = onnx_attr(node, 'low', 'f', default=0.0)
     max_val = onnx_attr(node, 'high', 'f', default=1.0)
     AttributedRandomUniform.update_node_stat(node, {'shape': shape,
                                                     'output_type': out_type,
                                                     'seed': seed,
                                                     'min_val': out_type(min_val),
                                                     'max_val': out_type(max_val)})
     return cls.enabled
Exemple #15
0
    def extract(cls, node):
        attrs = {
            'p': onnx_attr(node, 'p', 'i', 2),
            'axis': onnx_attr(node, 'axis', 'i', -1),
            'eps_mode': 'add',  # TODO check ONNX implementation
            'eps': 1e-6,  # TODO check ONNX implementation
        }
        if attrs['p'] == 1:
            log.debug('The node {} has unsupported attribute "p" = {}'.format(node.soft_get('name'), attrs['p']))
            return False

        NormalizeL2Op.update_node_stat(node, attrs)
        return cls.enabled
Exemple #16
0
    def extract(cls, node):
        keepdims = onnx_attr(node, 'keepdims', 'i', default=1)
        axis = onnx_attr(node, 'axis', 'i', default=0)

        attrs = {
            'axis': axis,
            'top_k': 1,
            'keepdims': keepdims,
            'remove_values_output': True
        }

        ArgMinOp.update_node_stat(node, attrs)
        return cls.enabled
Exemple #17
0
    def extract(cls, node):
        across_spatial = onnx_attr(node, 'across_spatial', 'i', default=0)
        channel_shared = onnx_attr(node, 'channel_shared', 'i', default=0)
        eps = onnx_attr(node, 'eps', 'f', default=0)
        
        attrs = {'across_spatial': bool(across_spatial),
                 'channel_shared': bool(channel_shared),
                 'eps': eps,
                 'layout': 'NCHW'}

        # update the attributes of the node
        NormalizeOp.update_node_stat(node, attrs)
        return cls.enabled
Exemple #18
0
    def extract(cls, node):
        # update the attributes of the node
        node_name = node.soft_get('name', node.id)
        block_size = onnx_attr(node, 'blocksize', 'i', default=None)
        assert block_size is not None, \
            'DepthToSpace should have "blocksize" attribute specified for node {}'.format(node_name)
        onnx_mode = onnx_attr(node, 'mode', 's', default=b'DCR').decode()
        assert onnx_mode in ['DCR', 'CRD'], 'Unrecognized mode provided for DepthToSpace node {}'.format(node_name)
        if onnx_mode == 'DCR':
            mode = 'blocks_first'
        else:
            mode = 'depth_first'

        DepthToSpaceOp.update_node_stat(node, {'block_size': block_size, 'mode': mode})
        return cls.enabled
Exemple #19
0
 def extract(cls, node):
     attrs = {
         'merge_repeated':
         bool(onnx_attr(node, 'merge_repeated', 'i', default=1)),
     }
     CTCGreedyDecoderSeqLenOp.update_node_stat(node, attrs)
     return cls.enabled
Exemple #20
0
 def extract(cls, einsum_node):
     einsum_name = einsum_node.soft_get('name', einsum_node.id)
     equation = onnx_attr(einsum_node, 'equation',
                          's').decode(encoding="utf-8")
     normalized_equation = Einsum.normalize_equation(einsum_name, equation)
     Einsum.update_node_stat(einsum_node, {'equation': normalized_equation})
     return cls.enabled
Exemple #21
0
 def extract(cls, node):
     attr_dict = {
         'data_format': 'NCHW',
         'eps': onnx_attr(node, 'epsilon', 'f', 1e-5),
     }
     BatchNormInference.update_node_stat(node, attr_dict)
     return cls.enabled
Exemple #22
0
    def extract(cls, node):
        mode = onnx_attr(node, 'mode', 's', default=b'avg').decode()
        output_height = onnx_attr(node, 'output_height', 'i', default=1)
        output_width = onnx_attr(node, 'output_width', 'i', default=1)
        sampling_ratio = onnx_attr(node, 'sampling_ratio', 'i', default=0)
        spatial_scale = onnx_attr(node, 'spatial_scale', 'f', default=1.0)

        ROIAlign.update_node_stat(
            node, {
                'pooled_h': output_height,
                'pooled_w': output_width,
                'sampling_ratio': sampling_ratio,
                'spatial_scale': spatial_scale,
                'mode': mode
            })
        return cls.enabled
 def extract(cls, node):
     attrs = {}
     if get_onnx_opset_version(node) >= 13:
         axis = onnx_attr(node, 'axis', 'i', default=None)
         attrs.update(axis=axis)
     QuantizeLinear.update_node_stat(node, attrs)
     return cls.enabled
 def extract(cls, node):
     encoding_map = {0: 'corner', 1: 'center'}
     center_point_box = onnx_attr(node, 'center_point_box', 'i', default=0)
     NonMaxSuppression.update_node_stat(node, {'sort_result_descending': 0,
                                               'output_type': np.int64,
                                               'box_encoding': encoding_map[center_point_box]})
     return cls.enabled
    def extract(cls, node):
        attrs = {
            'axis':
            np.array(onnx_attr(node, 'axis', 'i', default=0), dtype=np.int64)
        }

        AttributedGather.update_node_stat(node, attrs)
        return cls.enabled
Exemple #26
0
 def extract(cls, node):
     dim = onnx_attr(node, 'shape', 'ints', None)
     if dim is not None:
         dim = int64_array(dim)
         Reshape.update_node_stat(node, {'dim': dim})
     else:
         Reshape.update_node_stat(node)
     return cls.enabled
Exemple #27
0
    def extract(cls, node):
        """
        TopK-1 (k as attribute, required)
        TopK-10 (k as input, no sorting manipulations)
        TopK-11 (k as input, sorting manipulations through `sorted` and `largest` attrs)
        """
        attrs = {
            'axis': onnx_attr(node, 'axis', 'i', default=-1),
            'index_element_type': np.int64
        }
        if onnx_node_has_attr(node, 'k'):
            attrs['k'] = onnx_attr(node, 'k', 'i')
        attrs['sort'] = 'value' if onnx_attr(node, 'sorted', 'i', default=1) else 'none'
        attrs['mode'] = 'max' if onnx_attr(node, 'largest', 'i', default=1) else 'min'

        TopK.update_node_stat(node, attrs)
        return cls.enabled
Exemple #28
0
 def extract(cls, node: Node):
     scale = onnx_attr(node,
                       'scale',
                       'f',
                       default=mo_array(1.0),
                       dst_type=lambda x: mo_array(x))
     AttributedPower.update_node_stat(node, {'scale': scale})
     return cls.enabled
Exemple #29
0
    def extract(cls, node):
        axis = int64_array(onnx_attr(node, 'axes', 'ints', default=[]))

        attrs = {'squeeze_dims': axis if len(axis) != 0 else None}

        # update the attributes of the node
        Squeeze.update_node_stat(node, attrs)
        return cls.enabled
Exemple #30
0
 def extract(cls, node):
     negative_slope = onnx_attr(node, 'alpha', 'f', default=1.0)
     if negative_slope == 0:
         ReLU.update_node_stat(node)
     else:
         LeakyReLU.update_node_stat(node,
                                    {'negative_slope': negative_slope})
     return cls.enabled