def test_multiarray_to_image_input_util_HWC_format(self): H, W, C = 1, 1, 3 input_features = [("data", datatypes.Array(H, W, C))] output_features = [("out", datatypes.Array(H, W, C))] builder = NeuralNetworkBuilder( input_features, output_features, disable_rank5_shape_mapping=True ) builder.add_activation("linear", "LINEAR", "data", "out") spec = builder.spec mlmodel = MLModel(spec) mlmodel = make_image_input( mlmodel, "data", red_bias=-5, green_bias=-6, blue_bias=-2.5, scale=10.0, image_format="NHWC", ) x = np.array([4, 2, 5], dtype=np.uint8) x = np.reshape(x, (H, W, C)) pil_img = PIL.Image.fromarray(x) y = mlmodel.predict({"data": pil_img}, useCPUOnly=True)["out"] self.assertEqual(y.shape, (H, W, C)) np.testing.assert_almost_equal(y.flatten(), [35.0, 14.0, 47.5])
def test_rename_image_input(self): input_features = [("data", datatypes.Array(3, 1, 1))] output_features = [("out", datatypes.Array(3, 1, 1))] builder = NeuralNetworkBuilder( input_features, output_features, disable_rank5_shape_mapping=True ) builder.add_activation("linear", "LINEAR", "data", "out") spec = builder.spec # make an image input mlmodel = make_image_input(MLModel(spec), "data", image_format="NCHW", scale=2.0) # rename the input spec = mlmodel.get_spec() rename_feature(spec, "data", "new_input_name") mlmodel = MLModel(spec) # test x = np.array([4, 5, 6], dtype=np.uint8).reshape(1, 1, 3) pil_img = PIL.Image.fromarray(x) out = mlmodel.predict({"new_input_name": pil_img}, useCPUOnly=True)['out'] np.testing.assert_equal(out, np.array([8.0, 10.0, 12.0]).reshape(3, 1, 1))