Part II: Train, Store, and Deploy a Machine Model (link)

A: Train model a model artifact

Run file TrainModel/main.py in code and export as a pickle file.

B: Move to Cloud Storage bucket

  • Create a storage bucket in Terminal
    BUCKET_NAME=< PROJECT_ID >-aiplatform
    REGION=us-east1
    gsutil mb -l $REGION gs://$BUCKET_NAME
  • Train your model and export (Python code).
    with open('model.pkl', 'wb') as model_file:
      pickle.dump(classifier, model_file)
  • Upload model to bucket in Terminal
    TRAIN_MODEL_DIR="/Users/meninder/Google Drive/Mikey/Code/nyu/TrainModel/"
    gsutil cp < from full model path, including file > < to storage bucket >

C: Deploy Model and Versions

AI Platform - terminal, can also use console

  • Create a model resource: gcloud ai-platform models create regression_insurance_model --region=us-east1
  • Create a model version
    MODEL_DIR="gs://$BUCKET_NAME"
    VERSION_NAME="version1"
    MODEL_NAME="regression_insurance_model"
    FRAMEWORK="SCIKIT_LEARN"
    gcloud ai-platform versions create $VERSION_NAME --model=$MODEL_NAME --origin=$MODEL_DIR --runtime-version=2.5 --framework=$FRAMEWORK --python-version=3.7 --region=us-east1
    Confirm it is deployed: gcloud ai-platform versions describe $VERSION_NAME --model=$MODEL_NAME

D: Making predictions

  • Create input.json file: INPUT_FILE="$TRAIN_MODEL_DIR"input.json
  • Calling local prediction to local model in Terminal (DON'T FORGET: use path to pickle file, not specific 'model.pkl' file). For this to work, you need tensor flow installed (pip install tensorflow).
    gcloud ai-platform local predict --model-dir "$TRAIN_MODEL_DIR" --json-instances "$TRAIN_MODEL_DIR"input.json --framework scikit-learn
  • Calling local to stored model in Terminal. Make prediction:
    gcloud ai-platform predict --model $MODEL_NAME --version $VERSION_NAME --json-instances "$INPUT_FILE"
  • Calling local to stored model in Python. Make prediction.
    • See file code TrainModel/call_prediction .py
      pip install google-api-python-client
    • API enablement (link): AI Platform Training & Prediction, Compute Engine APIs
    • Setting up service accounts so you can call from Python API, here
      • Create service account, role: AI Platform Admin & Storage Object Admin.
      • Create key, download.