This tutorial demonstrates how to create a new model with your own set of training images, evaluate the results and predict the classification of test image using AutoML Vision.
The tutorial uses a dataset with images of five different kinds of flowers: sunflowers, tulips, daisy, roses and dandelions. It covers training a custom model, evaluating model performance, and classifying new images using the custom model.
Prerequisites
Configure your project environment
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the AutoML Vision APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the AutoML Vision APIs.
- Install the Google Cloud CLI.
- Follow the instructions to create a service account and download a key file.
-
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path to the service account key file that you downloaded when you created the service account. For example:export GOOGLE_APPLICATION_CREDENTIALS=KEY_FILE
-
Add your new service account to the AutoML Editor IAM role with the
following commands. Replace PROJECT_ID with the name of your
Google Cloud project and replace SERVICE_ACCOUNT_NAME with the name of
your new service account, for example
service-account1@myproject.iam.gserviceaccount.com
:gcloud auth login gcloud projects add-iam-policy-binding PROJECT_ID \ --member="user:YOUR_USERID@YOUR_DOMAIN" \ --role="roles/automl.admin" gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_NAME \ --role="roles/automl.editor"
-
Allow the AutoML Vision service accounts to access your
Google Cloud project resources:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:custom-vision@appspot.gserviceaccount.com" \ --role="roles/storage.admin"
- Install the client library.
-
Set the
PROJECT_ID
andREGION_NAME
environment variables.
Replace PROJECT_ID with the Project ID of your Google Cloud project. AutoML Vision currently requires the locationus-central1
.export PROJECT_ID="PROJECT_ID" export REGION_NAME="us-central1"
-
Create a Cloud Storage bucket to store the documents that you will
use to train your custom model.
The bucket name must be in the format:$PROJECT_ID-vcm
. The following command creates a storage bucket in theus-central1
region named$PROJECT_ID-vcm
.gsutil mb -p PROJECT_ID -c regional -l us-central1 gs://PROJECT_ID-vcm/
- Set the BUCKET variable.
export BUCKET=PROJECT_ID-vcm
-
Copy the publicly available dataset of flower images from
gs://cloud-samples-data/img/flower_photos/
into your Google Cloud Storage bucket.
In your Cloud Shell session, enter:gsutil -m cp -R gs://cloud-samples-data/ai-platform/flowers/ gs://BUCKET/img/
The file copying takes about 20 minutes to complete.
This command also copies the
all_data.csv
file, which lists the original filenames and their labels. -
The sample dataset contains a CSV file with the location and labels for
each image (see Preparing your training data for
details about the required format). Update the CSV file to point to the files
in your own bucket:
gsutil cat gs://BUCKET/img/flowers/all_data.csv | sed "s:cloud-ml-data/img/flower_photos/:BUCKET/img/flowers/:" > all_data.csv
Then copy the updated CSV file into your bucket:gsutil cp all_data.csv gs://BUCKET/csv/
Source code file locations
You can download the source code from the location provided below. After downloading, you can copy the source code into your AutoML Vision project folder.
Python
The tutorial consists of these Python files:
vision_classification_create_dataset.py
– Includes functionality to create a datasetimport_dataset.py
– Includes functionality to import a datasetvision_classification_create_model.py
– Includes functionality to create a modellist_model_evaluations.py
– Includes functionality to list model evaluationsvision_classification_predict.py
– Includes functionality related to predictiondelete_model.py
- Include functionality to delete a model
Java
The tutorial consists of these Java files:
VisionClassificationCreateDataset.java
– Includes functionality to create a datasetImportDataset.java
– Includes functionality to import a datasetVisionClassificationCreateModel.java
– Includes functionality to create a modelListModelEvaluations.java
– Includes functionality to list model evaluationsVisionClassificationPredict.java
– Includes functionality related to predictionDeleteModel.java
– Includes functionality to delete a model
Node.js
The tutorial consists of these Node.js programs:
vision_classification_create_dataset.js
– Includes functionality to create a datasetimport_dataset.js
– Includes functionality to import a datasetvision_classification_create_model.js
– Includes functionality to create a modellist_model_evaluations.js
– Includes functionality to list model evaluationsvision_classification_predict.js
– Includes functionality related to predictiondelete_model.js
- Include functionality to delete a model
Running the application
Step 1: Create the Flowers dataset
The first step in creating a custom model is to create an empty dataset that will eventually hold the training data for the model. When you create a dataset, you specify the type of classification you want your custom model to perform:
- MULTICLASS assigns a single label to each classified image
- MULTILABEL allows an image to be assigned multiple labels
This tutorial creates a dataset named flowers
and uses MULTICLASS.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
Run the create_dataset
function to create an empty dataset. You must modify
the following lines of code:
- Set the
project_id
to your PROJECT_ID - Set the
display_name
for the dataset (flowers
) Change
MULTILABEL
to `MULTICLASS
Python
python3 vision_classification_create_dataset.py
Java
mvn compile exec:java -Dexec.mainClass="com.example.automl.VisionClassificationCreateDataset"
Node.js
node vision_classification_create_dataset.js
Response
The response includes the details of the newly created dataset, including the
Dataset ID that you'll use to reference the dataset in future requests. We
recommend that you set an environment variable DATASET_ID
to the returned
Dataset ID value.
Dataset name: projects/216065747626/locations/us-central1/datasets/ICN7372141011130533778 Dataset id: ICN7372141011130533778 Dataset display name: flowers Image classification dataset specification: classification_type: MULTICLASS Dataset example count: 0 Dataset create time: seconds: 1530251987 nanos: 216586000
Step 2: Import images into the dataset
The next step is to populate the dataset with training images labeled using the target labels.
The import_data
function interface takes as input a CSV file that lists the
locations of all training images and the proper label for each one. (See
Prepare your data for details about the required
format.) For this tutorial, we will use the labeled images that you copied into
your Cloud Storage bucket, which are listed in
gs://$PROJECT_ID-vcm/csv/all_data.csv
.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
Run the import_data
function to import the training content. The first
piece of code to change is the Dataset ID from the previous step and the second
is the URI of all_data.csv
. You must modify
the following lines of code:
- Set the
project_id
to your PROJECT_ID - Set the
dataset_id
for the dataset (from the output of the previous step) Set the
path
which is the URI of the (gs://YOUR_PROJECT_ID-vcm/csv/all_data.csv
)python3 import_dataset.py {Python}
mvn compile exec:java -Dexec.mainClass="com.example.automl.ImportDataset" {Java}
node import_dataset.js {Node.js}
Response
Processing import... Dataset imported.
Step 3: Create (train) the model
Now that you have a dataset of labeled training images, you can train a new model.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
Call the create_model
function to create a model. The Dataset ID is from the
previous steps. You must modify the following lines of code:
- Set the
project_id
to your PROJECT_ID - Set the
dataset_id
for the dataset (from the output of the previous step) Set the
display_name
for your model (flowers_model
)python3 vision_classification_create_model.py {Python}
mvn compile exec:java -Dexec.mainClass="com.example.automl.VisionClassificationCreateModel" {Java}
node vision_classification_create_model.js {Node.js}
Response
The create_model
function starts a training operation and prints the
operation name. Training happens asynchronously and can take a while to
complete, so you can use the operation ID to check
training status. When training is
complete, create_model
returns the Model ID. As with the Dataset ID, you might
want to set an environment variable MODEL_ID
to the returned Model ID value.
Training operation name: projects/216065747626/locations/us-central1/operations/ICN3007727620979824033 Training started... Model name: projects/216065747626/locations/us-central1/models/ICN7683346839371803263 Model id: ICN7683346839371803263 Model display name: flowers_model Image classification model metadata: Training budget: 1 Training cost: 1 Stop reason: Base model id: Model create time: seconds: 1529649600 nanos: 966000000 Model deployment state: deployed
Step 4: Evaluate the model
After training, you can evaluate your model's readiness by reviewing its precision, recall, and F1 score.
The display_evaluation
function takes the Model ID as a parameter.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
Make a request to display the overall evaluation performance of the model by executing the following request. You must modify the following lines of code:
- Set the
project_id
to your PROJECT_ID Set the
model_id
to your model's idpython3 list_model_evaluations.py {Python}
mvn compile exec:java -Dexec.mainClass="com.example.automl.ListModelEvaluations" {Java}
node list_model_evaluations.js {Node.js}
Response
If the precision and recall scores are too low, you can strengthen the training dataset and re-train your model. For more information, see Evaluating models.
Precision and recall are based on a score threshold of 0.5 Model Precision: 96.3% Model Recall: 95.7% Model F1 score: 96.0% Model Precision@1: 96.33% Model Recall@1: 95.74% Model F1 score@1: 96.04%
Step 5: Use a model to make a prediction
When your custom model meets your quality standards, you can use it to classify new flower images.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
For the predict
function you must modify the following lines of code:
- Set the
project_id
to your PROJECT_ID - Set the
model_id
to your model's id Set the
file_path
to the downloaded file ("resources/test.png")python3 vision_classification_predict.py {Python}
mvn compile exec:java -Dexec.mainClass="com.example.automl.VisionClassificationPredict" {Java}
node vision_classification_predict.js {Node.js}
Response
The function returns the classification score for how well the image matches each category, exceeding the stated confidence threshold of 0.7.
Prediction results: Predicted class name: dandelion Predicted class score: 0.9702693223953247
Step 6: Delete the model
When you are done using this sample model, you can delete it permanently. You will no longer be able to use the model for prediction.
Copy the Code
Python
Java
Node.js
For more information, see the AutoML Vision Node.js API reference documentation.
To authenticate to AutoML Vision, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Request
Make a request with operation type delete_model
to delete a model you created
you must modify the following lines of code:
- Set the
project_id
to your PROJECT_ID Set the
model_id
to your model's idpython3 delete_model.py {Python}
mvn compile exec:java -Dexec.mainClass="com.example.automl.DeleteModel" {Java}
node delete_model.js {Node.js}
Response
Model deleted.