Пример #1
0
def test_max_pool_non_zero_pads():
    rt = get_runtime()

    # array([[[[ 0.5,  1.5,  2.5,  3.5],
    #          [ 4.5,  5.5,  6.5,  7.5],
    #          [ 8.5,  9.5, 10.5, 11.5],
    #          [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
    data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
    strides = [1, 1]
    dilations = [1, 1]
    pads_begin = [1, 1]
    pads_end = [1, 1]
    #  0   0  ,  0  ,  0  ,  0,    0
    #  0 [ 0.5,  1.5,  2.5,  3.5], 0,
    #  0 [ 4.5,  5.5,  6.5,  7.5], 0,
    #  0 [ 8.5,  9.5, 10.5, 11.5], 0,
    #  0 [12.5, 13.5, 14.5, 15.5], 0
    #  0   0  ,  0  ,  0  ,  0,    0
    kernel_shape = [2, 2]
    rounding_type = "floor"
    auto_pad = None
    index_et = "i32"

    data_node = ov.parameter(data.shape, name="A", dtype=np.float32)
    maxpool_node = ov.max_pool(
        data_node,
        strides,
        dilations,
        pads_begin,
        pads_end,
        kernel_shape,
        rounding_type,
        auto_pad,
        index_et,
    )
    comp = rt.computation(maxpool_node, data_node)
    result = comp(data)

    expected = np.array(
        [[[
            [0.5, 1.5, 2.5, 3.5, 3.5],
            [4.5, 5.5, 6.5, 7.5, 7.5],
            [8.5, 9.5, 10.5, 11.5, 11.5],
            [12.5, 13.5, 14.5, 15.5, 15.5],
            [12.5, 13.5, 14.5, 15.5, 15.5],
        ]]],
        dtype=np.float32,
    )
    expected_idx = np.array(
        [[[
            [0, 1, 2, 3, 3],
            [4, 5, 6, 7, 7],
            [8, 9, 10, 11, 11],
            [12, 13, 14, 15, 15],
            [12, 13, 14, 15, 15],
        ]]],
        dtype=np.int32,
    )
    assert np.allclose(result[0], expected)
    assert np.allclose(result[1], expected_idx)
Пример #2
0
def test_max_pool_kernel_shape3x3():
    rt = get_runtime()

    # array([[[[ 0.5,  1.5,  2.5,  3.5],
    #          [ 4.5,  5.5,  6.5,  7.5],
    #          [ 8.5,  9.5, 10.5, 11.5],
    #          [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
    data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
    strides = [1, 1]
    dilations = [1, 1]
    pads_begin = [0, 0]
    pads_end = [0, 0]
    kernel_shape = [3, 3]
    rounding_type = "floor"
    auto_pad = None
    index_et = "i32"

    data_node = ov.parameter(data.shape, name="A", dtype=np.float32)
    maxpool_node = ov.max_pool(
        data_node,
        strides,
        dilations,
        pads_begin,
        pads_end,
        kernel_shape,
        rounding_type,
        auto_pad,
        index_et,
    )
    comp = rt.computation(maxpool_node, data_node)
    result = comp(data)

    expected = np.array([[[[10.5, 11.5], [14.5, 15.5]]]], dtype=np.float32)
    assert np.allclose(result[0], expected)
Пример #3
0
    def __init__(self, model_adapter, configuration=None, preload=False):
        super().__init__(model_adapter, configuration, preload=False)
        self.pooled_heatmaps_blob_name = 'pooled_heatmaps'
        self.heatmaps_blob_name = 'heatmaps'
        self.pafs_blob_name = 'pafs'

        function = self.model_adapter.model
        paf = function.get_output_op(0)
        paf_shape = paf.output(0).get_shape()
        heatmap = function.get_output_op(1)

        heatmap_shape = heatmap.output(0).get_shape()
        if len(paf_shape) != 4 and len(heatmap_shape) != 4:
            raise RuntimeError('OpenPose outputs must be 4-dimensional')
        if paf_shape[2] != heatmap_shape[2] and paf_shape[3] != heatmap_shape[3]:
            raise RuntimeError('Last two dimensions of OpenPose outputs must match')
        if paf_shape[1] * 2 == heatmap_shape[1]:
            paf, heatmap = heatmap, paf
        elif paf_shape[1] != heatmap_shape[1] * 2:
            raise RuntimeError('Size of second dimension of OpenPose of one output must be two times larger then size '
                'of second dimension of another output')

        paf = paf.inputs()[0].get_source_output().get_node()
        paf.get_output_tensor(0).set_names({self.pafs_blob_name})
        heatmap = heatmap.inputs()[0].get_source_output().get_node()

        heatmap.get_output_tensor(0).set_names({self.heatmaps_blob_name})

        # Add keypoints NMS to the network.
        # Heuristic NMS kernel size adjustment depending on the feature maps upsampling ratio.
        p = int(np.round(6 / 7 * self.upsample_ratio))
        k = 2 * p + 1
        pooled_heatmap = opset8.max_pool(heatmap, kernel_shape=(k, k), dilations=(1, 1), pads_begin=(p, p), pads_end=(p, p),
                                     strides=(1, 1), name=self.pooled_heatmaps_blob_name)
        pooled_heatmap.output(0).get_tensor().set_names({self.pooled_heatmaps_blob_name})
        self.model_adapter.model.add_outputs([pooled_heatmap.output(0)])

        self.inputs = self.model_adapter.get_input_layers()
        self.outputs = self.model_adapter.get_output_layers()

        self.output_scale = self.inputs[self.image_blob_name].shape[-2] / self.outputs[self.heatmaps_blob_name].shape[-2]

        if self.target_size is None:
            self.target_size = self.inputs[self.image_blob_name].shape[-2]
        self.h = (self.target_size + self.size_divisor - 1) // self.size_divisor * self.size_divisor
        input_width = round(self.target_size * self.aspect_ratio)
        self.w = (input_width + self.size_divisor - 1) // self.size_divisor * self.size_divisor
        default_input_shape = self.inputs[self.image_blob_name].shape
        input_shape = {self.image_blob_name: (default_input_shape[:-2] + [self.h, self.w])}
        self.logger.debug('\tReshape model from {} to {}'.format(default_input_shape, input_shape[self.image_blob_name]))
        super().reshape(input_shape)

        if preload:
            self.load()

        num_joints = self.outputs[self.heatmaps_blob_name].shape[1] - 1  # The last channel is for background
        self.decoder = OpenPoseDecoder(num_joints, score_threshold=self.confidence_threshold)
Пример #4
0
def test_max_pool():
    # test 1d
    element_type = Type.f32
    shape = Shape([1, 1, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10])
    window_shape = [3]

    strides = [1] * len(window_shape)
    dilations = [1] * len(window_shape)
    pads_begin = [0] * len(window_shape)
    pads_end = [0] * len(window_shape)
    rounding_type = "floor"
    auto_pad = "explicit"
    idx_elem_type = "i32"

    model = ov.max_pool(
        A,
        strides,
        dilations,
        pads_begin,
        pads_end,
        window_shape,
        rounding_type,
        auto_pad,
        idx_elem_type,
    )
    function = Model([model], parameter_list, "test")

    runtime = get_runtime()
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = (np.arange(8) + 2).reshape(1, 1, 8)
    assert np.allclose(result, expected)

    # test 1d with strides
    strides = [2]
    pads_begin = [0] * len(window_shape)
    pads_end = [0] * len(window_shape)

    model = ov.max_pool(
        A,
        strides,
        dilations,
        pads_begin,
        pads_end,
        window_shape,
        rounding_type,
        auto_pad,
        idx_elem_type,
    )
    function = Model([model], parameter_list, "test")

    size = 4
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
    assert np.allclose(result, expected)

    # test 2d
    element_type = Type.f32
    shape = Shape([1, 1, 10, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    window_shape = [3, 3]

    strides = [1, 1]
    dilations = [1, 1]
    pads_begin = [0, 0]
    pads_end = [0, 0]

    model = ov.max_pool(
        A,
        strides,
        dilations,
        pads_begin,
        pads_end,
        window_shape,
        rounding_type,
        auto_pad,
        idx_elem_type,
    )
    function = Model([model], parameter_list, "test")

    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8)
    assert np.allclose(result, expected)

    # test 2d with strides
    strides = [2, 2]
    dilations = [1, 1]
    pads_begin = [0, 0]
    pads_end = [0, 0]

    model = ov.max_pool(
        A,
        strides,
        dilations,
        pads_begin,
        pads_end,
        window_shape,
        rounding_type,
        auto_pad,
        idx_elem_type,
    )
    function = Model([model], parameter_list, "test")
    computation = runtime.computation(function, *parameter_list)
    result = computation(input_arr)[0]

    size = 4
    expected = ((np.arange(100).reshape(10,
                                        10))[2::2,
                                             2::2]).reshape(1, 1, size, size)
    assert np.allclose(result, expected)