示例#1
0
    def test_fused_batch_norm(self, use_cpu_only, func, backend, epsilon):
        input_shape = np.random.randint(low=1, high=4, size=4)
        attr_shape = [list(input_shape)[-1]]

        m = random_gen(shape=attr_shape, rand_min=-1.0, rand_max=1.0)
        v = random_gen(shape=attr_shape, rand_min=0.0, rand_max=10.0)
        o = random_gen(shape=attr_shape, rand_min=1.0, rand_max=10.0)
        s = random_gen(shape=attr_shape, rand_min=-1.0, rand_max=1.0)

        @make_tf_graph([input_shape])
        def build_model(x):
            return func(
                x=x,
                scale=s,
                offset=o,
                mean=m,
                variance=v,
                epsilon=epsilon,
                is_training=False,
            )[0]

        model, inputs, outputs = build_model
        input_values = [random_gen(shape=input_shape)]
        input_dict = dict(zip(inputs, input_values))

        TensorFlowBaseTest.run_compare_tf(
            model,
            input_dict,
            outputs,
            use_cpu_for_conversion=use_cpu_only,
            backend=backend,
            atol=1e-2,
            rtol=1e-3,
        )
示例#2
0
 def test_builder_eval_stress(self, rank_and_axes, epsilon):
     rank, axes = rank_and_axes
     shape = np.random.randint(low=2, high=6, size=rank)
     x_val = random_gen(shape=shape, rand_min=-100.0, rand_max=100.0)
     positive_axes = [axis + rank if axis < 0 else axis for axis in axes]
     normalized_shape = [
         shape[i] for i in range(rank) if i in positive_axes
     ]
     gamma_val = random_gen(shape=normalized_shape,
                            rand_min=-100,
                            rand_max=100)
     beta_val = random_gen(shape=normalized_shape,
                           rand_min=-100,
                           rand_max=100)
     with Function({}):
         res = mb.layer_norm(x=x_val,
                             axes=axes,
                             epsilon=epsilon,
                             gamma=gamma_val,
                             beta=beta_val)
         ref = TestNormalizationLayerNorm._np_layer_norm(x=x_val,
                                                         axes=axes,
                                                         epsilon=epsilon,
                                                         gamma=gamma_val,
                                                         beta=beta_val)
         np.testing.assert_allclose(ref, res.val, atol=1e-04, rtol=1e-05)
示例#3
0
    def test_resample(
        self,
        use_cpu_only,
        backend,
        data_warp_shapes,
    ):
        if backend[0] == "neuralnetwork":
            pytest.xfail("nn backend not supported")

        tfa = pytest.importorskip("tensorflow_addons")

        data_shape, warp_shape = data_warp_shapes

        @make_tf_graph([data_shape, warp_shape])
        def build_model(x, warp):
            return tfa.image.resampler(data=x, warp=warp)

        model, inputs, outputs = build_model
        # warp exceeding input sizes in order to test more padding modes
        input_values = [
            random_gen(data_shape, -100, 100),
            random_gen(warp_shape, -15, 15),
        ]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model,
            input_dict,
            outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#4
0
    def test_builder_to_backend_stress_numpy(self, use_cpu_only, backend,
                                             rank_and_axes, epsilon,
                                             provides_gamma_beta):

        if backend == ("mlprogram", "fp16") and not use_cpu_only:
            pytest.xfail(
                "rdar://80662357 ([GPU failures] LayerNorm FP16 tests failing on GPU with numerical errors)"
            )

        rank, axes = rank_and_axes
        shape = np.random.randint(low=2, high=6, size=rank)
        x_val = random_gen(shape=shape, rand_min=-100.0, rand_max=100.0)
        input_placeholders = {"x": mb.placeholder(shape=x_val.shape)}
        input_values = {"x": x_val}

        gamma, beta = None, None

        if provides_gamma_beta:
            positive_axes = [
                axis + rank if axis < 0 else axis for axis in axes
            ]
            normalized_shape = [
                shape[i] for i in range(rank) if i in positive_axes
            ]
            gamma = random_gen(shape=normalized_shape,
                               rand_min=-100,
                               rand_max=100)
            beta = random_gen(shape=normalized_shape,
                              rand_min=-100,
                              rand_max=100)

        def build(x):
            return [
                mb.layer_norm(x=x,
                              axes=axes,
                              epsilon=epsilon,
                              gamma=gamma,
                              beta=beta)
            ]

        output = TestNormalizationLayerNorm._np_layer_norm(x=x_val,
                                                           axes=axes,
                                                           epsilon=epsilon,
                                                           gamma=gamma,
                                                           beta=beta)
        expected_output_types = [tuple(output.shape) + (types.fp32, )]
        expected_outputs = [output]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
            atol=1e-3,
            rtol=1e-4,
        )
示例#5
0
 def test_builder_eval(self):
     x_val = random_gen(shape=(2, 2, 4), rand_min=-37, rand_max=64)
     y_val = random_gen(shape=(2, 4, 2), rand_min=-91, rand_max=84)
     v = mb.matmul(x=x_val, y=y_val)
     np.testing.assert_allclose(np.matmul(x_val, y_val),
                                v.val,
                                atol=1e-04,
                                rtol=1e-05)
示例#6
0
 def test_builder_eval(self):
     cond = np.random.randint(low=0, high=2, size=(6, 1, 7)).astype(np.bool)
     a = random_gen(shape=(6, 1, 7), rand_min=-1962.0, rand_max=0.0)
     b = random_gen(shape=(6, 1, 7), rand_min=0.0, rand_max=1964.0)
     res = mb.select(cond=cond, a=a, b=b)
     np.testing.assert_allclose(np.where(cond, a, b),
                                res.val,
                                atol=1e-04,
                                rtol=1e-05)
示例#7
0
    def test_tf(
        self,
        use_cpu_only,
        backend,
        transpose_a,
        transpose_b,
        a_is_sparse,
        b_is_sparse,
        b_is_const,
    ):
        if backend[0] == 'mlprogram':
            pytest.skip("Custom layer not supported with ML Program backend")

        rank = 2
        shape = list(np.random.randint(low=3, high=100, size=1)) * rank
        with tf.Graph().as_default() as graph:
            x = tf.placeholder(tf.float32, shape=shape)

            if b_is_const:
                y_value = random_gen(shape, rand_min=-100, rand_max=100)
                y = tf.constant(y_value, dtype=tf.float32)
            else:
                y = tf.placeholder(tf.float32, shape=shape)

            ref = tf.sparse_matmul(
                x,
                y,
                transpose_a=transpose_a,
                transpose_b=transpose_b,
                a_is_sparse=a_is_sparse,
                b_is_sparse=b_is_sparse,
            )
            mlmodel, _, _, _ = tf_graph_to_mlmodel(
                graph,
                {
                    x: random_gen(shape, rand_min=-100, rand_max=100),
                } if b_is_const else {
                    x: random_gen(shape, rand_min=-100, rand_max=100),
                    y: random_gen(shape, rand_min=-100, rand_max=100),
                },
                ref,
                backend=backend,
            )
            layers = mlmodel.get_spec().neuralNetwork.layers
            assert layers[-1].custom is not None, "Expecting a custom layer"
            assert ("SparseMatMul" == layers[-1].custom.className
                    ), "Custom Layer class name mis-match"
            assert (transpose_a == layers[-1].custom.parameters["transpose_x"].
                    boolValue), "Incorrect parameter value k"
            assert (transpose_b == layers[-1].custom.parameters["transpose_y"].
                    boolValue), "Incorrect parameter value k"
            assert (a_is_sparse == layers[-1].custom.parameters["x_is_sparse"].
                    boolValue), "Incorrect parameter value k"
            assert (b_is_sparse == layers[-1].custom.parameters["y_is_sparse"].
                    boolValue), "Incorrect parameter value k"

            assert len(layers) == 2 if b_is_const else len(layers) == 1
示例#8
0
 def test_builder_eval(self):
     x_val = random_gen(shape=(2, 2), rand_min=-37, rand_max=64)
     weight_val = random_gen(shape=(2, 2), rand_min=-91, rand_max=84)
     bias_val = random_gen(shape=(2, ), rand_min=0.0, rand_max=9.0)
     v = mb.linear(x=x_val, weight=weight_val, bias=bias_val)
     np.testing.assert_allclose(np.matmul(x_val, weight_val.T) + bias_val,
                                v.val,
                                atol=1e-04,
                                rtol=1e-05)
示例#9
0
    def test_tf_image_resize(self, use_cpu_only, backend, size, method):
        if backend[0] == "mlprogram" and not use_cpu_only:
            pytest.xfail(
                "rdar://78343225 ((MIL GPU) Core ML Tools Unit Test failures [numerical error])"
            )

        if backend[0] == "mlprogram" and size == (1, 1):
            pytest.xfail(
                "rdar://79699954 (Nearest neighbor resize numerical mismatch when output size is (1,1))"
            )

        if backend[0] == "neuralnetwork":
            pytest.xfail("nn backend not supported")

        x_shape = tuple(np.random.randint(low=1, high=3, size=4))

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.image.resize(x, size=size, method=method)

        model, inputs, outputs = build_model
        input_values = [
            random_gen(x_shape, -100, 100),
        ]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model,
            input_dict,
            outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#10
0
    def test(self, use_cpu_only, backend, transforms, interpolation, shapes):
        x_shape, output_shape = shapes
        if backend[0] == "neuralnetwork":
            pytest.xfail("nn backend not supported")

        tfa = pytest.importorskip("tensorflow_addons")

        @make_tf_graph([x_shape])
        def build_model(x):
            return tfa.image.transform(
                x,
                transforms=transforms,
                interpolation=interpolation,
                output_shape=output_shape,
            )

        model, inputs, outputs = build_model
        input_values = [
            random_gen(x_shape, -100, 100),
        ]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model,
            input_dict,
            outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#11
0
 def test_builder_eval_stress(self, rank, epsilon):
     shape = np.random.randint(low=2, high=6, size=rank)
     x_val = random_gen(shape=shape, rand_min=-1, rand_max=1)
     with Function({}):
         res = mb.l2_norm(x=x_val, epsilon=epsilon)
         ref = TestNormalizationL2Norm._compute_l2_norm(x_val, epsilon)
         np.testing.assert_allclose(ref, res.val, atol=1e-6, rtol=1e-5)
示例#12
0
    def test_tf(self, use_cpu_only, backend, rank, k):
        if backend[0] == 'mlprogram':
            pytest.xfail("Custom layer not supported with ML Program backend")

        shape = np.random.randint(low=3, high=6, size=rank)
        with tf.Graph().as_default() as graph:
            x = tf.placeholder(tf.float32, shape=shape)
            ref = tf.math.top_k(x, k=k, sorted=True)
            ref = (ref[1], ref[0])
            mlmodel, _, _, _ = tf_graph_to_mlmodel(
                graph,
                {x: random_gen(shape, rand_min=-100, rand_max=100)},
                ref,
                backend=backend,
            )
            layers = mlmodel.get_spec().neuralNetwork.layers
            assert layers[-1].custom is not None, "Expecting a custom layer"
            assert (
                "TopK" == layers[-1].custom.className
            ), "Custom Layer class name mis-match"
            assert (
                k == layers[-1].custom.parameters["k"].intValue
            ), "Incorrect parameter value k"
            assert (
                layers[-1].custom.parameters["sorted"].boolValue is True
            ), "Incorrect parameter value for Sorted"
示例#13
0
    def test_builder_to_backend_stress(self, use_cpu_only, backend, rank):
        shape = np.random.randint(low=2, high=6, size=rank)
        x_val = random_gen(shape=shape, rand_min=-10.0, rand_max=10.0)
        input_placeholders = {"x": mb.placeholder(shape=shape)}
        input_values = {"x": x_val}

        def build(x):
            return [mb.l2_norm(x=x, epsilon=1e-12)]

        # compute for the answer
        batch_dims = rank - 3
        if batch_dims == 0:
            norm = la.norm(x_val)
            output = x_val / norm
        else:
            batch_dim_prod = np.prod(shape[:batch_dims])
            reshape_x_val = np.reshape(x_val, (batch_dim_prod, -1))
            norm = la.norm(reshape_x_val, axis=1, keepdims=True)
            output = reshape_x_val / norm
            output = np.reshape(output, shape)

        expected_output_types = [list(output.shape) + [types.fp32]]
        expected_outputs = [output]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#14
0
    def test_builder_to_backend_stress(self, use_cpu_only, backend,
                                       input_shape, scale_factor,
                                       align_corners, recompute_scale_factor):
        scale_factor_height, scale_factor_width = scale_factor
        _, _, height, width = input_shape
        height = height * scale_factor_height
        width = width * scale_factor_width
        is_h_float = height - np.floor(height) > 0.001
        is_w_float = width - np.floor(width) > 0.001

        # Currently, MIL is not suporting recompute_scale_factor=False + align_corners=False
        # with fractional output size
        if not recompute_scale_factor and not align_corners and (is_h_float or
                                                                 is_w_float):
            pytest.xfail("rdar://81124053 (Support recompute_scale_factor)")

        def _get_torch_upsample_prediction(x,
                                           scale_factor=(2, 2),
                                           align_corners=False,
                                           recompute_scale_factor=True):
            x = torch.from_numpy(x)
            out = torch.nn.functional.interpolate(
                x,
                scale_factor=scale_factor,
                mode="bilinear",
                align_corners=align_corners,
                recompute_scale_factor=recompute_scale_factor,
            )
            return out.numpy()

        x = random_gen(input_shape, rand_min=-100, rand_max=100)
        torch_pred = _get_torch_upsample_prediction(
            x,
            scale_factor=scale_factor,
            align_corners=align_corners,
            recompute_scale_factor=recompute_scale_factor,
        )

        input_placeholder_dict = {"x": mb.placeholder(shape=x.shape)}
        input_value_dict = {"x": x}

        def build_upsample(x):
            return mb.upsample_bilinear(
                x=x,
                scale_factor_height=scale_factor[0],
                scale_factor_width=scale_factor[1],
                align_corners=align_corners,
            )

        expected_output_type = torch_pred.shape + (types.fp32, )
        run_compare_builder(
            build_upsample,
            input_placeholder_dict,
            input_value_dict,
            expected_output_type,
            torch_pred,
            use_cpu_only=use_cpu_only,
            frontend_only=False,
            backend=backend,
        )
示例#15
0
    def test_builder_to_backend_stress_keras(self, use_cpu_only, backend, rank_and_axes, epsilon):
        rank, axes = rank_and_axes
        shape = np.random.randint(low=2, high=6, size=rank)
        x_val = random_gen(shape=shape, rand_min=-100.0, rand_max=100.0)
        input_placeholders = {"x": mb.placeholder(shape=x_val.shape)}
        input_values = {"x": x_val}

        def build(x):
            return [
                mb.layer_norm(x=x, axes=axes, epsilon=epsilon)
            ]

        output = TestNormalizationLayerNorm._keras_layer_norm(x=x_val, axes=axes, epsilon=epsilon)
        expected_output_types = [tuple(output.shape) + (types.fp32,)]
        expected_outputs = [
            output
        ]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#16
0
    def test_builder_to_backend_stress(
        self, use_cpu_only, backend, rank, size, alpha, beta, k
    ):
        shape = np.random.randint(low=2, high=5, size=rank)
        x_val = random_gen(shape=shape)
        input_placeholders = {"x": mb.placeholder(shape=x_val.shape)}
        input_values = {"x": x_val}

        def build(x):
            return mb.local_response_norm(x=x, size=size, alpha=alpha, beta=beta, k=k)

        torch_lrn = torch.nn.LocalResponseNorm(size=size, alpha=alpha, beta=beta, k=k)
        expected_outputs = [torch_lrn(torch.as_tensor(x_val)).numpy()]
        expected_output_types = [o.shape[:] + (types.fp32,) for o in expected_outputs]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
            atol=1e-2,
            rtol=1e-3,
        )
示例#17
0
    def test_builder_to_backend_stress(self, rank, use_cpu_only, backend, epsilon):
        shape = np.random.randint(low=2, high=6, size=rank)
        x_val = random_gen(shape=shape, rand_min=-100.0, rand_max=100.0)
        input_placeholders = {"x": mb.placeholder(shape=x_val.shape)}
        input_values = {"x": x_val}

        def build(x):
            return mb.instance_norm(x=x, epsilon=epsilon)

        layer = torch.nn.InstanceNorm2d if rank == 4 else torch.nn.InstanceNorm1d
        torch_op = layer(num_features=shape[1], eps=epsilon)
        expected_outputs = [torch_op(torch.as_tensor(x_val)).numpy()]
        expected_output_types = [o.shape[:] + (types.fp32,) for o in expected_outputs]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
            atol=1e-3,
            rtol=1e-4,
            also_compare_shapes=True
        )
示例#18
0
 def test_reduce_log_sum():
     x_val = random_gen(shape=(1, 3, 4, 4),
                        rand_min=0.0,
                        rand_max=100.0)
     res = mb.reduce_log_sum(x=x_val, axes=[axis],
                             keep_dims=keep_dims).val
     ref = np.log(np.sum(x_val, axis=axis, keepdims=keep_dims))
     np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)
示例#19
0
    def test_infer_inputs_and_outputs(self):
        x_shape = (3, 4, 5)

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.nn.relu(x)

        model, inputs, outputs = build_model
        mlmodel = converter.convert(model)
        assert mlmodel is not None

        input_values = [random_gen(x_shape, -10.0, 10.0)]
        input_dict = dict(zip(inputs, input_values))
        run_compare_tf(model, input_dict, outputs)
示例#20
0
    def test_scalar_placeholder_shape(self):
        x_shape = ()  # Scalar Placeholder Shape

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.nn.relu(x)

        model, inputs, outputs = build_model
        mlmodel = converter.convert(model, source=frontend)
        assert mlmodel is not None

        input_values = [random_gen(x_shape, -10.0, 10.0)]
        input_dict = dict(zip(inputs, input_values))
        run_compare_tf(model, input_dict, outputs)
示例#21
0
    def test_infer_outputs(self):
        x_shape = (3, 4, 5)

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.nn.relu(x)

        model, inputs, outputs = build_model
        input_name = (
            inputs[0] if isinstance(inputs[0], six.string_types) else inputs[0].op.name
        )
        mlmodel = converter.convert(model, inputs=[TensorType(input_name, (3, 4, 5))])
        assert mlmodel is not None

        input_values = [random_gen(x_shape, -10.0, 10.0)]
        input_dict = dict(zip(inputs, input_values))
        run_compare_tf(model, input_dict, outputs)
示例#22
0
    def test(self, use_cpu_only, backend, rank, tf_op):
        if backend[0] == "neuralnetwork":
            pytest.skip("nn backend not supported")

        x_shape = tuple(np.random.randint(low=1, high=4, size=rank))

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf_op(x)

        model, inputs, outputs = build_model
        input_values = [
            random_gen(x_shape, -100, 100),
        ]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model, input_dict, outputs, use_cpu_for_conversion=use_cpu_only, backend=backend,
        )
示例#23
0
    def test_keras_layer(self, use_cpu_only, backend, size):
        if backend[0] == "neuralnetwork":
            pytest.skip("nn backend not supported")

        x_shape = tuple(np.random.randint(low=1, high=4, size=4))

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.keras.layers.UpSampling2D(
                size=size, interpolation="nearest",
            )(x)

        model, inputs, outputs = build_model
        input_values = [random_gen(x_shape, -100, 100)]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model, input_dict, outputs, use_cpu_for_conversion=use_cpu_only, backend=backend,
        )
示例#24
0
    def test_raw_ops(
        self,
        use_cpu_only,
        backend,
        input_shape,
        target_shape,
        align_corners,
        half_pixel_centers,
    ):
        if align_corners is True and half_pixel_centers is True:
            return

        if backend[0] == "neuralnetwork":
            # neural network backend does not support fractional scale factors for nearest neighbor upsample op
            if target_shape[-1] % input_shape[-1] != 0:
                return
            if target_shape[-2] % input_shape[-2] != 0:
                return

        if not use_cpu_only and not half_pixel_centers and backend[
                0] == "mlprogram":
            # use_cpu_only == False & half_pixel_centers == False, & backend[0] == mlprogram
            # then there are numerical errors
            pytest.xfail("rdar://78321005")

        @make_tf_graph([input_shape])
        def build_model(x):
            return tf.raw_ops.ResizeNearestNeighbor(
                images=x,
                size=target_shape,
                align_corners=align_corners,
                half_pixel_centers=half_pixel_centers,
            )

        model, inputs, outputs = build_model
        input_values = [random_gen(input_shape, -100, 100)]
        input_dict = dict(zip(inputs, input_values))
        self.run_compare_tf2(
            model,
            input_dict,
            outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#25
0
    def test_selu(self, use_cpu_only, backend, rank):
        input_shape = np.random.randint(low=1, high=6, size=rank)

        @make_tf_graph([input_shape])
        def build_model(x):
            return tf.keras.activations.selu(x)

        model, inputs, outputs = build_model

        input_values = [random_gen(input_shape, -10.0, 10.0)]
        input_dict = dict(zip(inputs, input_values))
        TensorFlowBaseTest.run_compare_tf(
            model,
            input_dict,
            outputs,
            use_cpu_only=use_cpu_only,
            frontend_only=False,
            backend=backend,
        )
示例#26
0
    def test_infer_inputs(self):
        x_shape = (3, 4, 5)

        @make_tf_graph([x_shape])
        def build_model(x):
            return tf.nn.relu(x)

        model, inputs, outputs = build_model
        if not isinstance(outputs, (tuple, list)):
            outputs = [outputs]

        output_names = [
            j if isinstance(j, six.string_types) else j.op.name for j in outputs
        ]
        mlmodel = converter.convert(model, outputs=output_names)
        assert mlmodel is not None

        input_values = [random_gen(x_shape, -10.0, 10.0)]
        input_dict = dict(zip(inputs, input_values))
        run_compare_tf(model, input_dict, outputs)
示例#27
0
    def test_builder_to_backend_stress(self, use_cpu_only, backend, rank, epsilon):
        shape = np.random.randint(low=2, high=6, size=rank)
        x_val = random_gen(shape=shape, rand_min=-1.0, rand_max=1.0)
        input_placeholders = {"x": mb.placeholder(shape=shape)}
        input_values = {"x": x_val}

        def build(x):
            return [mb.l2_norm(x=x, epsilon=epsilon)]

        output = TestNormalizationL2Norm._compute_l2_norm(x_val, epsilon)
        expected_output_types = [list(output.shape) + [types.fp32]]
        expected_outputs = [
            output
        ]

        run_compare_builder(
            build,
            input_placeholders,
            input_values,
            expected_output_types,
            expected_outputs,
            use_cpu_only=use_cpu_only,
            backend=backend,
        )
示例#28
0
    def test_shaping_utils(self):
        @make_tf_graph([(None, 4, 5)])
        def build_flexible_model(x):
            return tf.nn.relu(x)

        model, inputs, outputs = build_flexible_model
        input_name = TFConverter._get_tensor_name(inputs[0])
        output_name = TFConverter._get_tensor_name(outputs[0])

        # static-Flexible shape
        mlmodel = converter.convert(
            model, inputs=[
                # Use TF's input shapes (None, 4, 5)
                TensorType(name=input_name)],
                outputs=[output_name]
        )
        assert mlmodel is not None
        input_values = [random_gen((3, 4, 5), -10.0, 10.0)]
        input_dict = {input_name: input_values[0]}
        if _IS_MACOS:
            ret = mlmodel.predict(input_dict)
            np.allclose(ret[output_name], np.maximum(input_values[0], 0.0))

        # Enumerate shape
        inputs_shape = [
            TensorType(input_name, EnumeratedShapes(shapes=[(3, 4, 5), (4, 4, 5)]))
        ]
        mlmodel = converter.convert(model, inputs=inputs_shape, outputs=[output_name])
        assert mlmodel is not None
        input_values = [random_gen((3, 4, 5), -10.0, 10.0)]
        input_dict = {input_name: input_values[0]}
        if _IS_MACOS:
            ret = mlmodel.predict(input_dict)
            np.allclose(ret[output_name], np.maximum(input_values[0], 0.0))

        input_values = [random_gen((4, 4, 5), -10.0, 10.0)]
        input_dict = {input_name: input_values[0]}
        if _IS_MACOS:
            ret = mlmodel.predict(input_dict)
            np.allclose(ret[output_name], np.maximum(input_values[0], 0.0))

        if _IS_MACOS:
            with pytest.raises(RuntimeError) as e:
                input_values = [random_gen((5, 4, 5), -10.0, 10.0)]
                input_dict = {input_name: input_values[0]}
                ret = mlmodel.predict(input_dict)

        # Ranged shape
        inputs_shape = [TensorType(input_name, [RangeDim(3, 5), 4, 5])]
        mlmodel = converter.convert(model, inputs=inputs_shape, outputs=[output_name])
        assert mlmodel is not None
        input_values = [random_gen((3, 4, 5), -10.0, 10.0)]
        input_dict = {input_name: input_values[0]}
        if _IS_MACOS:
            ret = mlmodel.predict(input_dict)
            np.allclose(ret[output_name], np.maximum(input_values[0], 0.0))

        input_values = [random_gen((4, 4, 5), -10.0, 10.0)]
        input_dict = {input_name: input_values[0]}
        if _IS_MACOS:
            ret = mlmodel.predict(input_dict)
            np.allclose(ret[output_name], np.maximum(input_values[0], 0.0))

        if _IS_MACOS:
            with pytest.raises(RuntimeError) as e:
                input_values = [random_gen((2, 4, 5), -10.0, 10.0)]
                input_dict = {input_name: input_values[0]}
                ret = mlmodel.predict(input_dict)
示例#29
0
    def test_builder_eval(self, axis, keep_dims):
        x_val = random_gen(shape=(1, 3, 4, 4), rand_min=-100.0, rand_max=100.0)

        @ssa_fn
        def test_reduce_argmax():
            res = mb.reduce_argmax(x=x_val, axis=axis, keep_dims=keep_dims).val
            ref = np.argmax(x_val, axis=axis)
            if keep_dims:
                ref = np.expand_dims(ref, axis=axis)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_argmin():
            res = mb.reduce_argmin(x=x_val, axis=axis, keep_dims=keep_dims).val
            ref = np.argmin(x_val, axis=axis)
            if keep_dims:
                ref = np.expand_dims(ref, axis=axis)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_l1_norm():
            res = mb.reduce_l1_norm(x=x_val, axes=[axis],
                                    keep_dims=keep_dims).val
            ref = np.sum(np.abs(x_val), axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_l2_norm():
            res = mb.reduce_l2_norm(x=x_val, axes=[axis],
                                    keep_dims=keep_dims).val
            ref = np.sqrt(
                np.sum(np.square(x_val), axis=axis, keepdims=keep_dims))
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_log_sum():
            x_val = random_gen(shape=(1, 3, 4, 4),
                               rand_min=0.0,
                               rand_max=100.0)
            res = mb.reduce_log_sum(x=x_val, axes=[axis],
                                    keep_dims=keep_dims).val
            ref = np.log(np.sum(x_val, axis=axis, keepdims=keep_dims))
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_log_sum_exp():
            res = mb.reduce_log_sum_exp(x=x_val,
                                        axes=[axis],
                                        keep_dims=keep_dims).val
            ref = scipy.special.logsumexp(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_max():
            res = mb.reduce_max(x=x_val, axes=[axis], keep_dims=keep_dims).val
            ref = np.max(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_mean():
            res = mb.reduce_mean(x=x_val, axes=[axis], keep_dims=keep_dims).val
            ref = np.mean(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_min():
            res = mb.reduce_min(x=x_val, axes=[axis], keep_dims=keep_dims).val
            ref = np.min(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_prod():
            res = mb.reduce_prod(x=x_val, axes=[axis], keep_dims=keep_dims).val
            ref = np.prod(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_sum():
            res = mb.reduce_sum(x=x_val, axes=[axis], keep_dims=keep_dims).val
            ref = np.sum(x_val, axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        @ssa_fn
        def test_reduce_sum_square():
            res = mb.reduce_sum_square(x=x_val,
                                       axes=[axis],
                                       keep_dims=keep_dims).val
            ref = np.sum(np.square(x_val), axis=axis, keepdims=keep_dims)
            np.testing.assert_allclose(ref, res, atol=1e-04, rtol=1e-05)

        test_reduce_argmax()
        test_reduce_argmin()
        test_reduce_l1_norm()
        test_reduce_l2_norm()
        test_reduce_log_sum()
        test_reduce_log_sum_exp()
        test_reduce_max()
        test_reduce_mean()
        test_reduce_min()
        test_reduce_prod()
        test_reduce_sum()
        test_reduce_sum_square()