Part 2 - Google BigQuery ML predicting cardiovascular disease risk using binary classification

Part 2 - Google BigQuery ML predicting cardiovascular disease risk using binary classification

Having created, tweaked and tested our heart disease model from this previous article:

it's time to actually leverage the benefit of all that work - real time prediction! But first, we need to get our model out of BigQuery and into GCPs AI Platform.

For that, we'll use a GCS storage bucket as the intermediary, and the cloud shell as our entry point. From your BigQuery project, open up cloudshell, and run the bq extract command

james_matson@cloudshell:~ (gcp-project)$ bq extract -m cardiovascular_disease.binary_model_05 gs://dev-ml/model_05        

As you can see above, I've extracted version 5 of my model (the latest one) to a folder of my dev-ml storage bucket in the same project as my BigQuery engine.

Once the extraction is completed, you can check the folder in your storage bucket and find the following artefacts:

No alt text provided for this image

The most important file above is our saved_model.pb. That's our TensorFlow protobuf file, and contains our actual trained model (weights included).

Back to our cloud shell, let's now create our model within the AI Platform service (I've already enabled the required APIs to allow this to take place, if you get any warnings, you might need to enable some first). Use the following to create your model.

gcloud ai-platform models create CARDIO_MODEL        

You'll be asked to choose a region, then you can create your model. At the moment, this is just a placeholder, you still need to create a version to actually use the model. To create our first version, we're going to use the model we trained and extracted out of BigQuery.

gcloud ai-platform versions create v1 --model=CARDIO_MODEL --origin=gs://dev-ml/model_05 --runtime-version=1.15 --framework=TENSORFLOW        

Here, we're creating v1, attaching it to our CARDIO_MODEL, and specifying the origin of the model as our stored model in GCS. We're also specifying tensorflow as our framework and version 1.15 of the AI platform itself.

Again, you'll choose the region, then kick off the process. Now this will take some time.

A lot of time.

So, you know, go have a cuppa and maybe hunt around the kitchen cupboard for that last Tim Tam you just absolutely-for-sure know you haven't already eaten......

(Many minutes later and minus one Tim Tam....)

Okay, Cloud Shell says we're finished, so let's navigate to the AI Platform service in the GCP console, from the dashboard you'll be able to see your v1 model in the model versions pane:

No alt text provided for this image

If you select this version, you'll be able to see the model details themselves.

No alt text provided for this image

Now let's have a go at real time prediction from the console. Select your v1 version from the model details screen, then click on 'test & use'.

No alt text provided for this image

This is where you'll be able to try out predictions on your model by feeding in a JSON representation of the features used in your model prediction. The sample shown in the screenshot above gives you an idea of how to format your prediction values. Essentially, you'll wrap your JSON values inside the instances array. For our model, you'll end up with something like the below:

???{?"instances":?[
????????{
????????????"age":?30,
????????????"gender":?0,
????????????"height":?186.0,
????????????"weight":?220.0,
????????????"ap_hi":?130.0,
????????????"ap_lo":?80.0,
????????????"cholesterol":?0,
????????????"gluc":?0,
????????????"smoke":?0,
????????????"alco":?0,
????????????"active":?0
????????}
????]
}        

As you can see, I've included a label and value for each data point we originally trained our model on. Let's run with the above sample by pasting it into the sample data box and clicking 'test'. The results are in!

??{"predictions":?[
????{
??????"predicted_risk":?[
????????"1"
??????],
??????"risk_values":?[
????????"1",
????????"0"
??????],
??????"risk_probs":?[
????????0.5476831342859715,
????????0.4523168657140285
??????]
????}
??]
}        

We get back the risk probabilities for both 1 and 0 (true and false, remembering this is a binary classifier) the possible risk values, and finally our predicted risk of 1 (meaning yes, this patient is at risk of heart disease). Let's adjust our values by reducing the 'patient' weight and run the test again:

? ?{ "instances": [
? ? ? ? {
? ? ? ? ? ? "age": 30,
? ? ? ? ? ? "gender": 0,
? ? ? ? ? ? "height": 186.0,
? ? ? ? ? ? "weight": 95.0,
? ? ? ? ? ? "ap_hi": 130.0,
? ? ? ? ? ? "ap_lo": 80.0,
? ? ? ? ? ? "cholesterol": 0,
? ? ? ? ? ? "gluc": 0,
? ? ? ? ? ? "smoke": 0,
? ? ? ? ? ? "alco": 0,
? ? ? ? ? ? "active": 0
? ? ? ? }
? ? ]
}        

And the results show that there's a correlation between weighing less and being less at risk of heart disease, with the risk factor now coming back at 0 (false).

??{"predictions":?[
????{
??????"risk_values":?[
????????"1",
????????"0"
??????],
??????"risk_probs":?[
????????0.18683777619372433,
????????0.8131622238062757
??????],
??????"predicted_risk":?[
????????"0"
??????]
????}
??]
}        

The best part about having deployed our model to the AI Platform however, is that GCP has actually already provisioned a HTTPS API endpoint for us to utilise for real-time inference, the tricky part - at least while I was figuring all this out - was getting the right URL and the right way to access the API. Let's start with the URL. The bit I missed out on initially, is that you need to include the region your model is deployed to in the URL you use. The full format is:

https://<region>-ml.googleapis.com/v1/projects/<project id>d/models/<model name>:predict

So for my model, it became:

https://australia-southeast1-ml.googleapis.com/v1/projects/gcp-model-poc/models/CARDIO_MODEL:predict

Next up, is access. In order to call the API, we're going to need an access token, which we'll provide as a parameter when calling the API, as follows:

https://australia-southeast1-ml.googleapis.com/v1/projects/gcp-model-poc/models/CARDIO_MODEL:predict?access_token=abcd1234

We could just use our principal user account to generate the required token, but instead we'll go through the process of creating a service account to use instead. So do the following from the IAM console of GCP.

  • Create a new service account.
  • Create a custom role to attach to the service account with the following 3 permissions:

  1. ml.versions.get
  2. ml.versions.list
  3. ml.versions.predict

  • Attach the role to the service account.

Because I'm going to use my standard user account [email protected] to impersonate the service account to generate the access token, I need to make sure my user account has the Service Account Token Creator permission on the service account itself.

gcloud iam service-accounts add-iam-policy-binding [email protected] --member=user:[email protected] --role=roles/iam.serviceAccountTokenCreator        

Now that we've completed these steps, even though I'm signed into gcloud using my user account, I can impersonate the service account in order to generate my temporary access token to call the API:

gcloud auth print-access-token --impersonate-service-account=gcp-ml-prediction@gcp-model-poc.iam.gserviceaccount.com        

(Side note: If you do this, and your access token comes back with a bazillion dots on the end of it, don't freak out. Just remain calm. Do not adjust your television set. After a quick Google search, apparently these are a normal and valid part of the access token, because Google are testing a future extension of the byte length of the access tokens.)

Now if the above worked, you should have an access token you can now add to your API call, and make a real time inference call! Here's mine below in POSTMAN (using the URL we discussed earlier and the JSON format from our console based testing)

No alt text provided for this image

Woohoo! Success. Awesome. So from this we can see that while there is a bit of work involved in getting towards a usable model, once you have on, the steps to take that model to a production endpoint in GCP are super easy. I hope you've enjoyed this look at one of the many (many) AI/ML workflows in GCP as much as I enjoyed experimenting with it, and I'll be sure to put up some more tutorials like this in the coming weeks as I explore other ways to leverage the power of AI in GCP!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了