Name: android-TensorFlowCloudMachineLearningEngineStylizer
Owner: Google Samples
Description: null
Created: 2018-02-21 16:36:37.0
Updated: 2018-03-20 13:59:52.0
Pushed: 2018-02-26 21:44:13.0
Homepage: null
Size: 4115
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This sample demonstrates how to use TensorFlow to apply artistic styles to photos by using Google CMLE as backend with a pre-trained TensorFlow model. It consists of a frontend Android app and a backend TensorFlow model deployed on CMLE instance. The app can take a photo, select one of the artistic styles from style carousel to send a JSON request to CMLE model, and get back a stylized photo.
This sample is a derivative of the TensorFlow Android Demo TF_Stylize app. In that sample a pre-trained TensorFlow model graph is loaded onto device and TensorFlow inference is run locally to render stylized photos on the fly.
The original TensorFlow model of image stylization is open sourced as part of the Magenta project by the Google Brain team.
The following two sections are detailed steps on how to set up your own online prediction backend service on CMLE
and how to build the frontend Andnroid app.
But first of all let's list the pre-requisites.
To set up your own online prediction service, you need to provide a SavedModel (the universal serialization format for TensorFlow models). To make it easy for developers to quickly get up and running, we have placed a SavedModel binary for this stylization model along with the corresponding variables/ directory (which stores trained values for the model parameters) in a GCS bucket. You can simply point your ML Engine?s online prediction service to that bucket deploying the model for prediction.
There are two abstractions that you need to be aware of when deploying a model to ML Engine?s online prediction service:
Next listed are steps for deployment.
First inspect current cloud configuration by running:
ud config list
Pay attention to which account and project are selected. If necessary, set current project to the desired using:
ud config set project
First, we must create a model in your project, which we can do either from the Cloud Console or using the gcloud command line tool as follows:
ud ml-engine models create <MODEL_NAME> --regions=<REGION_1>,<REGION_2>,...,<REGION_N>
For example, if we wanted to deploy the model under the name android_stylizer in the region us-central1, we would use the following command:
ud ml-engine models create android_stylizer --regions=us-central1
Next, let us deploy our SavedModel binary as a version of this model. Again, we could do so using the Cloud Console by selecting the model from the model dashboard:
and then clicking on the ?CREATE VERSION? button:
We could also use the gcloud command line tool:
ud ml-engine versions create --model=<model-name> --origin=<local-path> --runtime-version=1.2 <version> --staging-bucket=<staging bucket>
Assuming you used the android_stylizer model name, you could deploy a v1 version of this model as:
ud ml-engine versions create --model=android_stylizer --origin=gs://stylizer-model/v1 --runtime-version=1.2 v1
Once the version has been deployed, we can start stylizing. To see the service work for yourself, download gs://stylizer-model/v1/request.json locally and try:
ud ml-engine predict --model=android_stylizer --json-instances=<PATH TO>/request.json
Create service account key for authentication credentials
Save the service account key JSON file in raw folder and rename it appropriately (avoid hyphens)
Update the following in the sample code
private static final String PROJECT_ID = “
On Android Studio, click on the “Run” button. If you prefer to run on the command line, type
adlew installDebug
shell am start
example.tensorflow.cloudmachinelearningengine/.MainStylizerActivity
See LICENSE