Train an object detection model

Updated at:

Train SSD, Faster R-CNN, or R-FCN object detection models with EasyVision on single or multiple GPUs.

Supported models

EasyVision supports these object detection models:

Model

model_type value

FPN support

Faster R-CNN

FasterRCNN

Yes

R-FCN

RFCN

No

SSD

SSD

With ResNet backbones only

Supported model-backbone combinations

Model

Backbone

FPN support

Faster R-CNN

resnet_v1_50, resnet_v1_101, resnet_v1a_18, resnet_v1a_34, resnet_v1d_50, resnet_v1d_101, resnet_v1d_152

Yes

R-FCN

resnet_v1_50, resnet_v1_101, resnet_v1a_18, resnet_v1a_34, resnet_v1d_50, resnet_v1d_101, resnet_v1d_152

No

SSD

resnet_v1_50, resnet_v1d_50

Yes

SSD

vgg16_reduce_fc

No

SSD

mobilenet_v1

No

Prerequisites

  • PAI workspace with easy_vision_ext algorithm access

  • OSS bucket for training data, checkpoints, and model output

  • RAM role with OSS access permissions. For ARN format, see I/O parameters in Parameters of PAI-TensorFlow tasks.

  • Training and test data in TFRecord format, uploaded to OSS

Single GPU training

This example trains an SSD model with resnet_v1_50 backbone on a single GPU.

pai -name easy_vision_ext
    -Dbuckets='oss://{bucket_name}.{oss_host}/{path}'
    -Darn='acs:ram::*********:role/aliyunodpspaidefaultrole'
    -DgpuRequired=100
    -Dcmd train
    -Dparam_config '
      --model_type SSD
      --backbone  resnet_v1_50
      --num_classes 20
      --model_dir oss://YOUR_BUCKET_NAME/test/ssd_fpn_resnet50
      --train_data oss://pai-vision-data-sh/data/voc0712_tfrecord/voc0712_part_*.tfrecord
      --test_data oss://pai-vision-data-sh/data/voc0712_tfrecord/VOC2007_test.tfrecord
      --num_test_example 2
      --train_batch_size 32
      --test_batch_size 1
      --image_sizes 300
      --lr_type exponential_decay
      --initial_learning_rate 0.001
      --decay_epochs 20
      --staircase true
    '

Key parameters:

Parameter

Value

Purpose

gpuRequired

100

Allocates 1 GPU (100 = 1 GPU)

model_type

SSD

Single-stage detection model

backbone

resnet_v1_50

Feature extraction network

num_classes

20

PASCAL VOC dataset (20 classes)

image_sizes

300

Input image dimensions (SSD only)

Multi-GPU training

Use -Dcluster to define parameter server and worker configuration for distributed training. When -Dcluster is specified, -DgpuRequired is ignored.

pai -name easy_vision_ext
    -Dbuckets='oss://{bucket_name}.{oss_host}/{path}'
    -Darn='acs:ram::*********:role/aliyunodpspaidefaultrole'
    -Dcmd train
    -Dcluster='{
      "ps": {
          "count" : 1,
          "cpu" : 600
      },
      "worker" : {
          "count" : 3,
          "cpu" : 800,
          "gpu" : 100
      }
    }'
    -Dparam_config '
      --model_type SSD
      --backbone  resnet_v1_50
      --num_classes 20
      --model_dir oss://YOUR_BUCKET_NAME/test/ssd_fpn_resnet50
      --train_data oss://pai-vision-data-sh/data/voc0712_tfrecord/voc0712_part_*.tfrecord
      --test_data oss://pai-vision-data-sh/data/voc0712_tfrecord/VOC2007_test.tfrecord
      --num_test_example 2
      --train_batch_size 32
      --test_batch_size 1
      --image_sizes 300
      --lr_type exponential_decay
      --initial_learning_rate 0.001
      --decay_epochs 20
      --staircase true
    '

Cluster configuration:

Node type

Field

Value

Description

Parameter server (ps)

count

1

Parameter servers

Parameter server (ps)

cpu

600

CPU allocation (600 = 6 cores)

Worker (worker)

count

3

Workers for parallel training

Worker (worker)

cpu

800

CPU allocation per worker (800 = 8 cores)

Worker (worker)

gpu

100

GPU allocation per worker (100 = 1 GPU)

PAI command parameters

Control job configuration with these top-level parameters.

Parameter

Required

Description

Format or example

Default

buckets

Yes

OSS path for data access

oss://{bucket_name}.{oss_host}/{path}

N/A

arn

Yes

ARN of the RAM role with OSS access permissions. See I/O parameters in Parameters of PAI-TensorFlow tasks.

acs:ram::*:role/aliyunodpspaidefaultrole

N/A

cluster

No

Distributed training configuration as JSON. Defines parameter servers and workers with CPU and GPU allocations.

JSON string

""

gpuRequired

No

GPUs per worker in standalone mode. Each unit of 100 represents one GPU. Set to 0 for CPU-only training. Ignored when cluster is specified.

100

100

cmd

Yes

EasyVision task type. Set to train.

train

N/A

param_config

Yes

Model training configuration. Uses Python ArgumentParser format. See Training parameters.

String

N/A

Training parameters

param_config accepts training configuration in Python ArgumentParser format:

-Dparam_config = '
--backbone resnet_v1_50
--num_classes 200
--model_dir oss://your/bucket/exp_dir
'
Important

String values in param_config must not use quotation marks.

Model parameters

Parameter

Required

Description

Default

model_type

Yes

Detection model architecture: SSD, FasterRCNN, RFCN

N/A

backbone

Yes

Backbone network for feature extraction: mobilenet_v1, resnet_v1_50, resnet_v1_101, resnet_v1a_18, resnet_v1a_34, resnet_v1d_50, resnet_v1d_101, vgg16_reduce_fc.

N/A

use_fpn

No

Enables FPN for multi-scale feature extraction. Supported by Faster R-CNN (with ResNet backbones) and SSD (with resnet_v1_50 or resnet_v1d_50 backbones)

false

num_classes

No

Object classes excluding background. PASCAL VOC has 20 classes

21

weight_decay

No

L2 regularization coefficient

1e-4

Anchor parameters

Parameter

Required

Description

Default

anchor_scales

No

Anchor box sizes in pixels relative to resized input. Behavior varies by model

Varies by model (see below)

anchor_ratios

No

Width-to-height ratios of anchor boxes

0.5 1 2

anchor_scales behavior by model:

Configuration

Behavior

Default

SSD

Automatically determined. Do not set

0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 0.96, or 1.0 times the input image size

Faster R-CNN with FPN

Set a single base scale for the highest-resolution feature layer. FPN uses five layers, each doubling anchor size. For example, 32 produces scales of 32, 64, 128, 256, and 512

32

Faster R-CNN / R-FCN without FPN

Specifies multiple anchor scales directly (such as 128 256 512)

128 256 512

Image size parameters

Parameter

Required

Applies to

Description

Default

image_sizes

No

SSD only

Height and width to resize input images to

300 300

image_min_sizes

No

Faster R-CNN, R-FCN

Target shorter side length after resizing. For multi-scale training, specify multiple values. The last value is used for evaluation; others are randomly selected per training iteration

600

image_max_sizes

No

Faster R-CNN, R-FCN

Target longer side length after resizing. For multiple values, the last one is used for evaluation and others for training

1024

Training hyperparameters

Parameter

Required

Description

Default

optimizer

No

Optimization algorithm: momentum (SGD with momentum), adam

momentum

num_epochs

Yes

Training epochs. One epoch processes the full training dataset once

N/A

lr_type

No

Learning rate schedule: exponential_decay, polynomial_decay, manual_step, cosine_decay. See Learning rate schedules.

exponential_decay

initial_learning_rate

No

Initial learning rate

0.01

decay_epochs

No

Epoch interval for learning rate adjustment. Behavior depends on lr_type. For exponential_decay, specifies epoch interval (converted to decay_steps). For manual_step, specifies epochs to adjust learning rate. See Learning rate schedules.

20

decay_factor

No

Decay multiplier for learning rate. Equivalent to the decay_factor parameter in tf.train.exponential.decay

0.95

staircase

No

Applies step-wise (staircase) decay instead of continuous decay. Equivalent to the staircase parameter in tf.train.exponential.decay

true

power

No

Exponent for polynomial decay. Equivalent to the power parameter in tf.train.polynomial.decay. Applies only when lr_type is polynomial_decay

0.9

learning_rates

No

Learning rates for each epoch in decay_epochs. Required when lr_type is manual_step. Values must match epochs in decay_epochs. For example, if decay_epochs is 20 40, set to 0.001 0.0001. Recommended: adjust to 1/10, 1/100, 1/1000 of the initial rate in sequence

N/A

lr_warmup

No

Enables learning rate warmup

false

lr_warm_up_epochs

No

Epochs over which to linearly warm up learning rate

1

train_batch_size

Yes

Images per training batch

N/A

test_batch_size

Yes

Images per evaluation batch

N/A

Data parameters

Parameter

Required

Description

Default

train_data

Yes

OSS path to training data in TFRecord format. Supports wildcards such as *.tfrecord

N/A

test_data

Yes

OSS path to evaluation data in TFRecord format

N/A

train_num_readers

No

Concurrent threads for reading training data

4

num_test_example

No

Examples to evaluate during training. Set to -1 to evaluate all

-1

num_train_images

No

Total training images. Required for custom TFRecord files

0

label_map_path

No

Label map file path mapping class IDs to names. Required for custom TFRecord files

""

Checkpoint and logging parameters

Parameter

Required

Description

Default

model_dir

Yes

OSS path for model checkpoints and training artifacts

N/A

pretrained_model

No

OSS path to pretrained checkpoint for fine-tuning. Example: oss://pai-vision-data-sh/pretrained_models/inception_v4.ckpt

""

use_pretrained_model

No

Initialize from pretrained checkpoint

true

save_checkpoint_epochs

No

Checkpoint save frequency in epochs. 1 saves after every epoch

1

save_summary_epochs

No

TensorFlow summary save frequency as fraction of epoch. 0.01 saves after every 1% of training data

0.01

num_visualizations

No

Evaluation examples to visualize

10

Learning rate schedules

lr_type controls learning rate changes during training.

Schedule

Description

Key parameters

exponential_decay (default)

Decays learning rate by decay_factor every decay_epochs epochs. When staircase is true, the rate drops in discrete steps; otherwise continuous

decay_factor, decay_epochs, staircase

polynomial_decay

Decays learning rate following polynomial curve. num_steps is automatically set to total training iterations, with end learning rate at 1/1000 of initial_learning_rate

power

manual_step

Adjusts learning rate at specific epochs. Set decay_epochs to specify when (such as 16 18), and learning_rates for corresponding rates (such as 0.001 0.0001). Common strategy for N epochs: set decay points at 0.8N and 0.9N with rates at 1/10 and 1/100 of the initial rate

decay_epochs, learning_rates

cosine_decay

Adjusts learning rate following the cosine curve described in SGDR: Stochastic Gradient Descent with Warm Restarts

decay_epochs

Next steps