Пример #1
0
        def __init__(self, model_dir, skip_preprocessing):
            self.model_dir = model_dir

            session, signature = mlprediction.load_model(model_dir)
            client = mlprediction.SessionClient(session, signature)
            self.model = mlprediction.create_model(
                client, model_dir, skip_preprocessing=skip_preprocessing)
Пример #2
0
 def testUserPostprocessOnly(self):
     create_version_json = """
     {
       "version": {
         "processor_class": "google.cloud.ml.prediction.testdata.user_custom_python.user_model.TfUserPostprocessor"
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         model = mlprediction.create_model(self._mock_client,
                                           self._model_path)
         # Verify the default TensorFlowModel is instantiated.
         self.assertIsInstance(model, mlprediction.TensorFlowModel)
         # Verify postprocessing (which divides values by 2) is applied to the
         # predicted results.
         self.assertEqual(model.predict(self._instances, **self._kwargs),
                          (self._instances, [{
                              "c": 5
                          }, {
                              "c": 10
                          }]))
         # Verify no preprocessing performed.
         self._mock_client.predict.assert_has_calls([
             mock.call({
                 "a": [1, 2],
                 "b": [2, 4]
             },
                       stats=mock.ANY,
                       signature_name=mock.ANY)
         ])
Пример #3
0
 def __init__(self,
              model_dir,
              tags,
              framework=mlprediction.TENSORFLOW_FRAMEWORK_NAME):
   self.model_dir = model_dir
   client = mlprediction.create_client(framework, model_dir, tags)
   self.model = mlprediction.create_model(client, model_dir, framework)
Пример #4
0
 def testMissingPredictAndProcessMethod(self):
     create_version_json = """
     {
       "version": {
         "model_class": "google.cloud.ml.prediction.testdata.user_custom_python.user_model.MissingPredict"
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         with self.assertRaises(mlprediction.PredictionError) as error:
             client = None
             dummy_model_path = "gs://dummy/model/path"
             mlprediction.create_model(client, dummy_model_path)
     self.assertEqual(
         error.exception.error_detail,
         ("The provided model class, MissingPredict, is missing "
          "the required predict method."))
Пример #5
0
 def testUserModelTooFewPredictArgs(self):
     create_version_json = """
     {
       "version": {
         "model_class": "google.cloud.ml.prediction.testdata.user_custom_python.user_model.PredictMethodTooFewArgs"
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         with self.assertRaises(mlprediction.PredictionError) as error:
             client = None
             dummy_model_path = "gs://dummy/model/path"
             mlprediction.create_model(client, dummy_model_path)
     self.assertEqual(
         error.exception.error_detail,
         ("The provided model class, PredictMethodTooFewArgs, has "
          "a predict method with an invalid signature. "
          "Expected signature: ['self', 'instances'] "
          "User signature: ['self']"))
Пример #6
0
 def testMissingUserModelClassModule(self):
     create_version_json = """
     {
       "version": {
         "model_class": "wrong_module.UserModel",
         "package_uris": ["gs://test_package"]
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         with self.assertRaises(mlprediction.PredictionError) as error:
             client = None
             dummy_model_path = "gs://dummy/model/path"
             mlprediction.create_model(client, dummy_model_path)
     self.assertEqual(
         error.exception.error_detail,
         "wrong_module.UserModel cannot be found. "
         "Please make sure (1) model_class is the fully qualified "
         "function name, and (2) model_class uses the correct "
         "package name as provided by the package_uris: "
         "['gs://test_package']")
Пример #7
0
 def testUserModelMissingCustomMethod(self):
     create_version_json = """
     {
       "version": {
         "model_class": "google.cloud.ml.prediction.testdata.user_custom_python.wrong_user_model",
         "package_uris": ["gs://test_package"]
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         with self.assertRaises(mlprediction.PredictionError) as error:
             client = None
             dummy_model_path = "gs://dummy/model/path"
             mlprediction.create_model(client, dummy_model_path)
     self.assertEqual(
         error.exception.error_detail,
         ("google.cloud.ml.prediction.testdata.user_custom_python."
          "wrong_user_model cannot be found. Please make "
          "sure (1) model_class is the fully qualified function "
          "name, and (2) model_class uses the correct package name "
          "as provided by the package_uris: ['gs://test_package']"))
Пример #8
0
 def testModelCreationNoCustomCode(self):
     create_version_json = """
    {
      "version": {}
    }
    """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         client = mock.Mock()
         client.signature_map = {"serving_default": None}
         dummy_model_path = "gs://dummy/model/path"
         model = mlprediction.create_model(client, dummy_model_path)
         self.assertIsInstance(model, mlprediction.TensorFlowModel)
Пример #9
0
 def testUserModelCreationFromClassMethod(self):
     create_version_json = """
     {
       "version": {
         "model_class": "google.cloud.ml.prediction.testdata.user_custom_python.user_model.UserModel"
       }
     }
     """
     env_map = {"create_version_request": create_version_json}
     with mock.patch.dict("os.environ", env_map):
         client = None
         dummy_model_path = "gs://dummy/model/path"
         model = mlprediction.create_model(client, dummy_model_path)
         self.assertIsInstance(model, user_model.UserModel)
Пример #10
0
 def testNoUserProcessor(self):
     model = mlprediction.create_model(self._mock_client, self._model_path)
     self.assertIsInstance(model, mlprediction.TensorFlowModel)
     self.assertEqual(model.predict(self._instances, **self._kwargs),
                      (self._instances, [{
                          "c": 10
                      }, {
                          "c": 20
                      }]))
     self._mock_client.predict.assert_has_calls([
         mock.call({
             "a": [1, 2],
             "b": [2, 4]
         },
                   stats=mock.ANY,
                   signature_name=mock.ANY)
     ])
Пример #11
0
    def testModelWithBytesBasedOutput(self):
        mock_client = mock.Mock()
        mock_client.predict.return_value = {"x_bytes": "to_encode"}
        signature_def = meta_graph_pb2.SignatureDef()
        signature_def.outputs["x_bytes"].dtype = types_pb2.DT_STRING
        signature_def.inputs["input_key"].dtype = types_pb2.DT_STRING
        mock_client.signature_map = {
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            signature_def
        }

        model = mlprediction.create_model(mock_client, "gs://tmp/foo")
        _, predictions = model.predict({"input_key": "foo"})
        self.assertEqual(predictions, [{
            "x_bytes": {
                "b64": base64.b64encode("to_encode")
            }
        }])
Пример #12
0
 def testTensorFlowModelCreationNoCreateVersionRequest(self):
     client = mock.Mock()
     client.signature_map = {"serving_default": None}
     dummy_model_path = "gs://dummy/model/path"
     model = mlprediction.create_model(client, dummy_model_path)
     self.assertIsInstance(model, mlprediction.TensorFlowModel)