Image Classification using Flowers dataset

This tutorial uses the Flowers dataset to build a customized image classification model via transfer learning and the existent Inception-v3 model in order to correctly label different types of flowers using Cloud Machine Learning Engine.

The sample code you will walk through and results you will monitor consist of four parts: data preprocessing, model training with the transformed data, model deployment, and prediction request steps. All parts will be completed in the cloud.

What you will build

You will run sample code in order to preprocess data with Cloud Dataflow and then use that transformed data to train a model with Cloud ML Engine. You will then deploy the trained model to Cloud ML Engine and test the model by sending a prediction request to it.

In this sample dataset you only have a small set of images (~3,600). Without more data it isn’t possible to use machine learning techniques to adequately train an accurate classification model from scratch. Instead, you’ll use an approach called transfer learning. In transfer learning you use a pre-trained model to extract image features that you will use to train a new classifier. In this tutorial in particular you’ll use a pre-trained model called Inception.

Objectives

In this introductory, end-to-end walkthrough you will use python code to:

  • Perform data preprocessing in the cloud, reading original data files and using Cloud Dataflow to convert them into TFRecords format for training.
  • Run training using Cloud Machine Learning Engine to obtain optimal model specifications.
  • Deploy the trained model.
  • Request a prediction from the trained model and observe the resulting accuracy.

Costs

This walkthrough uses billable components of Google Cloud Platform, including:

  • Cloud Dataflow for:
    • Preprocessing data
  • Cloud Machine Learning Engine for:
    • Training
    • Making a prediction request
  • Google Cloud Storage for:
    • Storing input data for training
    • Staging the trainer package
    • Writing training artifacts

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

New Cloud Platform users might be eligible for a free trial.

Before you begin

Download the sample from the GitHub repository

macOS

  1. Download and extract the Cloud ML Engine sample zip file.

  2. Open a terminal window and navigate to the directory that contains the extracted cloudml-samples-master directory.

  3. Navigate to the cloudml-samples-master > flowers directory. The commands in this walkthrough must be run from the flowers directory.

    cd cloudml-samples-master/flowers
    

Cloud Shell

  1. Download the Cloud ML Engine sample zip file.

    wget https://github.com/GoogleCloudPlatform/cloudml-samples/archive/master.zip
    
  2. Unzip the file to extract the cloudml-samples-master directory.

    unzip master.zip
    
  3. Navigate to the cloudml-samples-master > flowers directory. The commands in this walkthrough must be run from the flowers directory.

    cd cloudml-samples-master/flowers
    

Install dependencies

The sample provides a requirements.txt file that you can use to install the dependencies required by the project.

sudo pip install -r requirements.txt

Set up and test your Cloud environment

Complete the following steps to set up a GCP account with the Cloud ML Engine API activated, install and activate the Cloud SDK, and install and test TensorFlow.

Set up your GCP project

  1. Google アカウントへのログイン

    Google アカウントをまだお持ちでない場合は、新しいアカウントを登録します。

  2. Cloud Platform Console プロジェクトを選択または作成します。

    [プロジェクト] ページに移動

  3. プロジェクトの課金を有効にします。

    課金の有効化

  4. Cloud Machine Learning Engine and Compute Engine API(複数)を有効にする。

    Enable the APIs

Set up your environment

Choose one of the options below to set up your environment locally on macOS or in a remote environment on Cloud Shell.

For macOS users, we recommend that you set up your environment using the MACOS tab below. Cloud Shell, shown on the CLOUD SHELL tab, is available on macOS, Linux, and Windows. Cloud Shell provides a quick way to try Cloud Machine Learning Engine, but isn’t suitable for ongoing development work.

macOS

  1. Install a virtual environment to create an isolated Python development environment for this guide. For example, the following commands install virtualenv and activate an environment named cmle-env.

    virtualenv cmle-env
    source cmle-env/bin/activate
  2. Confirm that you have Python installed and, if necessary, install it.

    python -V
  3. Install the Google Cloud SDK for macOS by following the instructions in the before you begin section of the Cloud SDK Quickstart.

  4. Initialize the Cloud SDK.

    gcloud init

    For more information about the initialization process, see the Cloud SDK documentation.

  5. Install pip, Python’s package manager. Check if you already have pip installed by running pip --version. Ensure that you have the latest version of pip and if not, upgrade it using the following command:

    pip install -U pip
  6. Install TensorFlow:

    pip install --upgrade tensorflow

    For more information about installing TensorFlow, see the TensorFlow documentation.

  7. Create application default credentials:

    gcloud auth application-default login

Cloud Shell

  1. Open the Google Cloud Platform Console.

    Google Cloud Platform Console

  2. Click the Activate Google Cloud Shell button at the top of the console window.

    Activate Google Cloud Shell

    A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt. It can take a few seconds for the shell session to be initialized.

    Cloud Shell session

    Your Cloud Shell session is ready to use.

  3. Configure the gcloud command-line tool to use your selected project.

            gcloud config set project [selected-project-id]
            

    where [selected-project-id] is your project ID. (Omit the enclosing brackets.)

Verify the Google Cloud SDK components

To verify that the Google Cloud SDK components are installed:

  1. List your models:

    gcloud ml-engine models list
  2. Verify that the command returns an empty list:

    Listed 0 items.

    After you start creating models, you can see them listed by using this command.

Run a simple TensorFlow Python program

Run a simple Python program to test your installation of TensorFlow. If you are using Cloud Shell, note that TensorFlow is already installed.

  1. Python の対話型シェルを起動します。

    python
  2. TensorFlow をインポートします。

    >>> import tensorflow as tf
  3. 文字列を格納する定数を作成します。

    >>> hello = tf.constant('Hello, TensorFlow!')
  4. TensorFlow セッションを作成します。

    >>> sess = tf.Session()

    特定の命令を使用するための TensorFlow ライブラリがコンパイルされていないという警告は無視してかまいません。

  5. hello の値を表示します。

    >>> print(sess.run(hello))

    成功した場合は、次のように出力されます。

    Hello, TensorFlow!
  6. Python の対話型シェルを停止します。

    >>> exit()

TensorFlow のインストール方法の詳細については、TensorFlow のサイトにある TensorFlow のインストール方法の説明をご覧ください。

Set up your Cloud Storage bucket

Cloud ML Engine のサービスは、モデルのトレーニングやバッチ予測の実行中にデータを読み書きするために Cloud Storage 上の場所にアクセスする必要があります。このセクションでは、新しいバケットを作成する方法を説明します。既存のバケットを使用することもできますが、Cloud ML Engine の実行に使用するプロジェクトに属していないバケットの場合は、明示的に Cloud ML Engine サービス アカウントにアクセス権を付与する必要があります。

モデル トレーニングやバッチ予測の実行中にデータを読み書きするための Google Cloud Storage バケットを作成します。

  1. 新しいバケットに名前を設定します。

    プロジェクト名に -mlengine を付加したものを使用したい場合は、次のようにプロジェクト名を取得して -mlengine を付加します。

    PROJECT_ID=$(gcloud config list project --format "value(core.project)")
    BUCKET_NAME=${PROJECT_ID}-mlengine

    それ以外の場合は、任意の名前を使用できますが、Cloud Storage のすべてのバケットの中で一意であることが必要です。

    BUCKET_NAME="your_bucket_name"
  2. 作成したバケット名を確認します。

    echo $BUCKET_NAME
  3. バケットのリージョンを選択し、`REGION` 環境変数を設定します。

    警告: バケットには、マルチリージョンのロケーション(us-central1 など)ではなく、1 つのリージョン(us など)を指定する必要があります。詳細は開発環境の概要をご覧ください。たとえば、次のコードは `REGION` を作成して `us-central1` に設定します。

    REGION=us-central1
  4. 新しいバケットを作成します。

    gsutil mb -l $REGION gs://$BUCKET_NAME

    注: 使用するリージョンは、Cloud ML Engine ジョブを実行するリージョンと同一にしてください。この例では us-central1 を使用していますが、クイックスタートの手順でこのリージョンが使用されているためです。

Declaring variables

Start by declaring all variables and making them read-only.

 declare -r BUCKET_NAME="gs://${your_bucket_name}"
 declare -r REGION=“your_valid_region”
 declare -r PROJECT_ID=$(gcloud config list project --format "value(core.project)")
 declare -r JOB_NAME="flowers_${USER}_$(date +%Y%m%d_%H%M%S)"
 declare -r GCS_PATH="${BUCKET_NAME}/${USER}/${JOB_NAME}"
 declare -r DICT_FILE=gs://cloud-ml-data/img/flower_photos/dict.txt

 declare -r MODEL_NAME=flowers
 declare -r VERSION_NAME=v1

 echo
 echo "Using job id: " $JOB_NAME
 set -v -e

Note that the BUCKET_NAME and REGION variables are user specific, so you must explicitly fill in the name of your own project's bucket and region. For help with choosing a region, see the guide to available regions for Cloud ML Engine services.

Preprocessing training and evaluation data in the cloud

For the sake of this tutorial the original dataset of labeled flower images has been randomly split into training and evaluation datasets. Of the original data, 90% is reserved for training and 10% is reserved for evaluation. You start with these two separate files which are stored in a Google-owned Cloud Storage bucket. You then preprocess these two Google-hosted datasets to extract the image features from the bottleneck layer (typically the penultimate layer just before the final output layer that actually does the classification) of the Inception network. You save the output of this preprocessing in your own Cloud Storage bucket and use these files for training.

Preprocessing takes about 60 minutes per dataset to complete.

Evaluation data preprocessing

Start with the evaluation data preprocessing.

python trainer/preprocess.py \
    --input_dict "$DICT_FILE" \
    --input_path "gs://cloud-ml-data/img/flower_photos/eval_set.csv" \
    --output_path "${GCS_PATH}/preproc/eval" \
    --cloud

Monitor preprocessing

To monitor preprocessing progress you can either use the command line tool for text log entries or Dataflow's web-based monitoring user interface in the console which provides a graphical representation of each pipeline and details about your job's status and execution.

Print log entries with the following commands:

gcloud dataflow jobs list
export JOB_ID="{corresponding_id}"
gcloud beta dataflow logs list $JOB_ID --importance=detailed

Or navigate to Dataflow's monitoring user interface:

Cloud Dataflow Monitoring UI

Training data preprocessing

Preprocess training data in the same manner as the evaluation data.

python trainer/preprocess.py \
    --input_dict "$DICT_FILE" \
    --input_path "gs://cloud-ml-data/img/flower_photos/train_set.csv" \
    --output_path "${GCS_PATH}/preproc/train" \
    --cloud

Monitoring progress is the same as the evaluation data.

Run model training in the cloud

Once preprocessing on Cloud ML Engine has fully completed you are ready to train a simple classifier. Verify that preprocessing has finished by navigating to the Dataflow console page and checking the job status.

Cloud Dataflow Monitoring UI

Run model training in the cloud with the following command:

gcloud ml-engine jobs submit training "$JOB_NAME" \
    --stream-logs \
    --module-name trainer.task \
    --package-path trainer \
    --staging-bucket "$BUCKET_NAME" \
    --region "$REGION" \
    --runtime-version=1.2\
    -- \
    --output_path "${GCS_PATH}/training" \
    --eval_data_paths "${GCS_PATH}/preproc/eval*" \
    --train_data_paths "${GCS_PATH}/preproc/train*"

Monitor training progress

Similar to preprocessing monitoring, you can monitor training progress via simple text logs in the command line or by launching TensorBoard visualization tool and going to the summary logs produced during training.

Stream logs with the command line:

gcloud ml-engine jobs stream-logs "$JOB_NAME"

Or open TensorBoard visualization tool:

macOS

  1. Launch TensorBoard:

    OUTPUT_PATH = "${GCS_PATH}/training"

    python -m tensorflow.tensorboard --logdir=$OUTPUT_PATH

  2. Once you start running TensorBoard, you can access it in your browser at http://localhost:6006

Cloud Shell

  1. Launch TensorBoard:

    OUTPUT_PATH = "${GCS_PATH}/training"
    
    python -m tensorflow.tensorboard --logdir=$OUTPUT_PATH --port=8080
    
  2. Select "Preview on port 8080" from the Web Preview menu at the top of the command line.

You can shut down TensorBoard at any time by typing ctrl+c on the command line.

Deploying and using the model for prediction

You now have a SavedModel stored on Google Cloud Storage ready to be exported. Think of the "model" that is being created with this command as a container for the optimal computation artifact (Tensorflow graphs) that were created during training.

  1. Use the gcloud ml-engine models create command to name and export the SavedModel to Cloud ML Engine.

    gcloud ml-engine models create "$MODEL_NAME" \
      --regions "$REGION"
    
  2. Create the first version of the model. Creating a version actually deploys your uniquely defined model to a Cloud instance, and gets it ready to serve (predict).

    gcloud ml-engine versions create "$VERSION_NAME" \
      --model "$MODEL_NAME" \
      --origin "${GCS_PATH}/training/model" \
      --runtime-version=1.2
    

Cloud ML Engine automatically sets the first version of your model to the default.

Send a prediction request to the trained model

After you deploy your model you are able to test its online prediction capability. You do this by downloading an example flower (daisy) image from the eval set and then send a request message.

  1. Copy the image to your local disk.

      gsutil cp \
        gs://cloud-ml-data/img/flower_photos/daisy/100080576_f52e8ee070_n.jpg \
        daisy.jpg
    

    In order to make a prediction request the above JPEG file must be encoded (using base64 encoding) in a JSON string.

  2. Create a request message locally in JSON format from the downloaded daisy JPEG file.

      python -c 'import base64, sys, json; img = base64.b64encode(open(sys.argv[1], "rb").read()); print json.dumps({"key":"0", "image_bytes": {"b64": img}})' daisy.jpg &> request.json
    

    The above code is Cloud ML Engine online prediction in action.

    If you are deploying a new version of your model you might have to wait for your request to be fulfilled. This could take up to 10 minutes. To check the status of your deployed version navigate to ML Engine > Models on Google Cloud Platform Console.

    After a few minutes, the model will become available for use via Cloud ML Engine’s prediction API. When it does use the predict command to send a prediction request to Cloud ML Engine for the given instance.

  3. Call the prediction service API to get classifications.

      gcloud ml-engine predict --model ${MODEL_NAME} --json-instances request.json
    

    The response should look something like this, where prediction number corresponds to flower type (daisy - 0, dandelion - 1, roses - 2, sunflowers - 3, tulips - 4):

KEY  PREDICTION                SCORES
0      0                    [0.9980067610740662, 0.00011650333908619359,
0.00028453863342292607, 0.0006193328299559653, 0.0009433324448764324,
2.9501752578653395e-05]

The prediction accuracy after running this code should be approximately 99.8%.

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. Cloud Platform Console で、[プロジェクト] ページに移動します。

    [プロジェクト] ページに移動する

  2. プロジェクト名の右側にある、ゴミ箱アイコンをクリックします。

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

  1. Open a terminal window (if not already open).

  2. Use the gsutil rm command with the -r flag to delete the directory that contains your most recent job:

    gsutil rm -r gs://$BUCKET_NAME/$JOB_NAME
    

If successful, the command returns a message similar to:

Removing gs://my-awesome-bucket/just-a-folder/cloud-storage.logo.png#1456530077282000...
Removing gs://my-awesome-bucket/...

Repeat the command for any other directories that you created for this sample.

Alternately, if you have no other data stored in the bucket, you can run the gsutil rm -r command on the bucket itself.

What's next

You've now completed a walkthrough of a Cloud ML Engine code sample that uses flower image data for preprocessing, training, deployment and prediction. You ran code to preprocess the image data using Cloud Dataflow, performed training in the cloud, then deployed a model and used it to get an online prediction.

The following resources can help you continue learning about Cloud ML Engine.

フィードバックを送信...

Cloud Machine Learning Engine (Cloud ML Engine)