本指南說明如何使用 Google 預先建立的搜尋空間,以及以 TF-vision 為基礎的預先建立訓練程式碼,為 MnasNet 和 SpineNet 執行 Vertex AI 神經架構搜尋工作。如需端對端範例,請參閱 MnasNet 分類 Notebook 和 SpineNet 物件偵測 Notebook。
為預先建構的訓練工具準備資料
神經架構搜尋預先建構的訓練器要求資料採用 TFRecord
格式,並包含 tf.train.Example
。tf.train.Example
必須包含下列欄位:
'image/encoded': tf.FixedLenFeature(tf.string)
'image/height': tf.FixedLenFeature(tf.int64)
'image/width': tf.FixedLenFeature(tf.int64)
# For image classification only.
'image/class/label': tf.FixedLenFeature(tf.int64)
# For object detection only.
'image/object/bbox/xmin': tf.VarLenFeature(tf.float32)
'image/object/bbox/xmax': tf.VarLenFeature(tf.float32)
'image/object/bbox/ymin': tf.VarLenFeature(tf.float32)
'image/object/bbox/ymax': tf.VarLenFeature(tf.float32)
'image/object/class/label': tf.VarLenFeature(tf.int64)
您可以按照這裡的操作說明準備 ImageNet 資料。
如要轉換自訂資料,請使用下載的範例程式碼和公用程式所附帶的剖析指令碼。如要自訂資料剖析,請修改 tf_vision/dataloaders/*_input.py 檔案。
進一步瞭解 TFRecord
和 tf.train.Example
。
定義實驗環境變數
在執行實驗前,您需要定義幾個環境變數,包括:
- TRAINER_DOCKER_ID:
${USER}_nas_experiment
(建議格式) 實驗將使用的訓練和驗證資料集 Cloud Storage 位置。舉例來說 (CoCo 用於偵測):
gs://cloud-samples-data/ai-platform/built-in/image/coco/train*
gs://cloud-samples-data/ai-platform/built-in/image/coco/val*
實驗輸出的 Cloud Storage 位置。建議格式:
gs://${USER}_nas_experiment
REGION:應與實驗輸出值區所在區域相同的區域。例如
us-central1
。PARAM_OVERRIDE:.yaml 檔案,用於覆寫預先建構訓練工具的參數。神經架構搜尋提供一些可用的預設設定:
PROJECT_ID=PROJECT_ID
TRAINER_DOCKER_ID=TRAINER_DOCKER_ID
LATENCY_CALCULATOR_DOCKER_ID=LATENCY_CALCULATOR_DOCKER_ID
GCS_ROOT_DIR=OUTPUT_DIR
REGION=REGION
PARAM_OVERRIDE=tf_vision/configs/experiments/spinenet_search_gpu.yaml
TRAINING_DATA_PATH=gs://PATH_TO_TRAINING_DATA
VALIDATION_DATA_PATH=gs://PATH_TO_VALIDATION_DATA
您可能需要選取及/或修改符合訓練需求的覆寫檔案。請考量下列事項:
- 您可以設定
--accelerator_type
,選擇 GPU 或 CPU。如要只執行幾個 epoch,以便使用 CPU 進行快速測試,您可以設定標記--accelerator_type=""
,並使用設定檔tf_vision/test_files/fast_nas_detection_spinenet_search_for_testing.yaml
。 - 訓練週期數
- 訓練執行時間
- 超參數,例如學習率
如需控制訓練工作的所有參數清單,請參閱 tf_vision/configs/
。以下是主要參數:
task:
train_data:
global_batch_size: 80
validation_data:
global_batch_size: 16
init_checkpoint: null
trainer:
train_steps: 16634
steps_per_loop: 1386
optimizer_config:
learning_rate:
cosine:
initial_learning_rate: 0.16
decay_steps: 16634
type: 'cosine'
warmup:
type: 'linear'
linear:
warmup_learning_rate: 0.0067
warmup_steps: 1386
建立 Cloud Storage 值區,讓神經架構搜尋功能儲存工作輸出內容 (也就是檢查點):
gcloud storage buckets create $GCS_ROOT_DIR
建構訓練器容器和延遲計算器容器
下列指令會使用下列 URI 在 Google Cloud 中建構訓練器映像檔:gcr.io/PROJECT_ID/TRAINER_DOCKER_ID
,這會在下一個步驟的神經網路架構搜尋工作中使用。
python3 vertex_nas_cli.py build \
--project_id=PROJECT_ID \
--trainer_docker_id=TRAINER_DOCKER_ID \
--latency_calculator_docker_id=LATENCY_CALCULATOR_DOCKER_ID \
--trainer_docker_file=tf_vision/nas_multi_trial.Dockerfile \
--latency_calculator_docker_file=tf_vision/latency_computation_using_saved_model.Dockerfile
如要變更搜尋空間和獎勵,請在 Python 檔案中更新這些項目,然後重新建構 Docker 映像檔。
在本機測試訓練程式
由於在 Google Cloud 服務中啟動工作需要花費數分鐘的時間,因此在本機測試訓練器 Docker 可能會更方便,例如驗證 TFRecord 格式。以 spinenet
搜尋空間為例,您可以在本機執行搜尋作業 (模型會隨機抽樣):
# Define the local job output dir.
JOB_DIR="/tmp/iod_${search_space}"
python3 vertex_nas_cli.py search_in_local \
--project_id=PROJECT_ID \
--trainer_docker_id=TRAINER_DOCKER_ID \
--prebuilt_search_space=spinenet \
--use_prebuilt_trainer=True \
--local_output_dir=${JOB_DIR} \
--search_docker_flags \
params_override="tf_vision/test_files/fast_nas_detection_spinenet_search_for_testing.yaml" \
training_data_path=TEST_COCO_TF_RECORD \
validation_data_path=TEST_COCO_TF_RECORD \
model=retinanet
training_data_path
和 validation_data_path
是 TFRecords 的路徑。
在 Google Cloud上啟動第 1 階段搜尋,接著啟動第 2 階段訓練工作
如需端對端範例,請參閱 MnasNet 分類 Notebook 和 SpineNet 物件偵測 Notebook。
您可以設定旗標
--max_parallel_nas_trial
和--max_nas_trial
來自訂。類神經架構搜尋會並行啟動max_parallel_nas_trial
試驗,並在max_nas_trial
試驗結束後完成。如果設定
--target_device_latency_ms
標記,系統會啟動個別的latency calculator
工作,並使用由--target_device_type
標記指定的加速器。神經架構搜尋控制器會透過 FLAG
--nas_params_str
,為每個測試提供新的候選架構建議。每個試驗都會根據 FLAG
nas_params_str
的值建立圖表,並啟動訓練工作。每個測試也會將其值儲存至 JSON 檔案 (位於os.path.join(nas_job_dir, str(trial_id), "nas_params_str.json")
)。
獎勵延遲時間限制
MnasNet 分類 Notebook 提供雲端 CPU 裝置的延遲限制搜尋範例。
如要搜尋有延遲限制的模型,訓練程式可以將獎勵回報為準確度和延遲的函式。
在共用原始碼中,獎勵的計算方式如下:
def compute_reward(target_latency, accuracy, inference_latency, weight=0.07):
"""Compute reward from accuracy and latency."""
speed_ratio = target_latency / inference_latency
return accuracy * (speed_ratio**weight)
您可以使用 mnasnet 論文第 3 頁的 reward
計算方式的其他變化版本。
target_device_type
會指定 Google Cloud 中支援的目標裝置類型,例如NVIDIA_TESLA_P100
。use_prebuilt_latency_calculator
會使用預先建構的延遲計算機tf_vision/latency_computation_using_saved_model.py
。target_device_latency_ms
可指定目標裝置的延遲時間。
如要瞭解如何自訂延遲時間計算函式,請參閱 tf_vision/latency_computation_using_saved_model.py
。
監控神經架構搜尋工作進度
在 Google Cloud 控制台的工作頁面中,圖表會顯示 reward vs. trial number
,而表格則會顯示每次試驗的獎勵。您可以找到獎勵最高的熱門試用。
繪製第 2 階段訓練曲線
在第 2 階段訓練後,您可以使用 Cloud Shell 或 Google Cloud
TensorBoard
指向工作目錄,繪製訓練曲線:
部署所選模型
如要建立 SavedModel,您可以使用 export_saved_model.py
指令碼搭配 params_override=${GCS_ROOT_DIR}/${TRIAL_ID}/params.yaml
。