Train a model for semantic image segmentation

Updated at:

Train a DeepLabV3 semantic segmentation model with EasyVision using TFRecord datasets stored in OSS.

Prerequisites

Procedure

DeepLabV3 is a semantic image segmentation model. For more information, see Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation.

Run PAI commands through the SQL Script component, MaxCompute client, or DataWorks SQL nodes. For more information, see MaxCompute client (odpscmd) or Develop a MaxCompute SQL node.

The following example trains a semantic image segmentation model on a single GPU.

pai -name easy_vision_ext
           -Dbuckets='oss://{bucket_name}.{oss_host}/{path}'
           -Darn='acs:ram::*********:role/aliyunodpspaidefaultrole'
           -DossHost='{oss_host}'
           -DgpuRequired=100
           -Dcmd train
           -Dparam_config '--model_type DeeplabV3 --backbone  resnet_v1_50 --backbone_feature_stride 16 --bn_trainable true --num_classes 21 --num_epochs 1 --model_dir oss://YOUR_BUCKET_NAME/test/test_deeplabv3 --train_data oss://YOUR_BUCKET_NAME/data/test/pascal_voc_seg_aug/voc_ev_train.tfrecord --test_data oss://YOUR_BUCKET_NAME/data/test/pascal_voc_seg_aug/voc_ev_train.tfrecord --num_test_example 2 --train_batch_size 6 --test_batch_size 1 --image_crop_size 513 --lr_type polynomial_decay --initial_learning_rate 0.007 --power 0.9'

Parameters

Parameter

Required

Description

Format

Default value

buckets

Yes

OSS bucket URL. Must end with a forward slash (/).

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

N/A

arn

Yes

ARN of the RAM role with OSS bucket permissions. To get the ARN, log on to the PAI console and go to the Dependent Services page. In the Designer section, find OSS, click View Authorization in the Actions column, and copy the Role Name value. For more information, see Grant permissions to use Machine Learning Designer.

acs:ram::*:role/AliyunODPSPAIDefaultRole

N/A

ossHost

No

OSS bucket domain name. For more information, see Regions and endpoints.

oss-{region}.aliyuncs.com

By default, specified by buckets.

cluster

No

Distributed training configuration.

JSON string

""

gpuRequired

No

GPUs per worker. By default, each worker uses one GPU. A value of 200 allocates two GPUs per worker.

100

100

cmd

Yes

Job type to run on EasyVision. For training, set to train.

train

N/A

param_config

Yes

Model training configuration. Uses the same format as Python's ArgumentParser() object. For more information, see param_config.

STRING

N/A

param_config

The param_config parameter specifies model training parameters in command-line format for Python's argparse module. Example:

-Dparam_config = '--backbone resnet_v1_50 --num_classes 200 --model_dir oss://YOUR_BUCKET_NAME/exp_dir'
Note

Do not enclose string parameter values in double quotation marks (") or single quotation marks (').

Architecture

Parameter

Required

Description

Format

Default value

model_type

Yes

Model type to train. For semantic image segmentation, set to DeeplabV3.

STRING

N/A

backbone

Yes

Backbone network name. Valid values: resnet_v1_50, resnet_v1_101, resnet_v1a_18, resnet_v1a_34, resnet_v1d_50, resnet_v1d_101, xception_41, xception_65, and xception_71.

STRING

N/A

backbone_feature_stride

No

Feature downsampling stride of the backbone network.

INT

16

bn_trainable

No

Whether to train the batch normalization (BN) layer. Set to true if train_batch_size exceeds 8.

BOOL

true

weight_decay

No

Weight decay coefficient (L2 regularization).

FLOAT

1e-4

pretrained_model

No

OSS path of a pretrained model. If specified, training fine-tunes the pretrained model.

oss://examplebucket/pretrained_models/inception_v4.ckpt

""

use_pretrained_model

No

Whether to use a pretrained model.

BOOL

true

Data

Parameter

Required

Description

Format

Default value

num_classes

Yes

Number of segmentation classes, including background.

21

N/A

train_data

Yes

OSS path of training data.

oss://path/to/train_*.tfrecord

N/A

test_data

Yes

OSS path of evaluation data.

oss://path/to/test_*.tfrecord

N/A

image_crop_size

No

Image size after cropping.

INT

513

num_train_images

No

Total number of training samples. Required for custom TFRecord files.

INT

0

label_map_path

No

.pbtxt file that maps label IDs to label names. Required for custom TFRecord files.

STRING

""

train_num_readers

No

Concurrent threads for reading training samples.

INT

4

Training

Parameter

Required

Description

Format

Default value

num_epochs

Yes

Number of training epochs. A value of 1 processes each training sample once.

INT

N/A

train_batch_size

Yes

Batch size for training.

INT

N/A

test_batch_size

Yes

Batch size for evaluation.

INT

N/A

num_test_example

No

Number of samples for model evaluation. A value of -1 evaluates on the entire training dataset.

INT

-1

optimizer

No

Optimizer type. Valid values: momentum (SGD with momentum) and adam.

STRING

momentum

lr_type

No

Learning rate adjustment policy. Valid values: exponential_decay: Exponential decay. polynomial_decay: Polynomial decay. When set to polynomial_decay, num_steps is automatically set to total training iterations, and end_learning_rate is automatically set to 0.001× initial_learning_rate. manual_step: Manual adjustment per epoch. When set to manual_step, configure decay_epochs to specify adjustment epochs and learning_rates to specify the learning rate for each epoch. cosine_decay: Cosine curve adjustment. For more information, see SGDR: Stochastic Gradient Descent with Warm Restarts. When set to cosine_decay, configure decay_epochs to specify adjustment epochs.

STRING

exponential_decay

initial_learning_rate

No

Initial learning rate.

FLOAT

0.01

decay_epochs

No

For exponential_decay: Equivalent to the decay_steps parameter of tf.train.exponential_decay. The system calculates decay_steps based on total training samples. Set to half the total epochs. Example: 10. For manual_step: Specifies epochs for learning rate adjustment. For example, a value of 16 18 adjusts the learning rate in epochs 16 and 18. A general guideline is 0.8×N and 0.9×N, where N is the total number of epochs.

INTEGER list

20

decay_factor

No

Learning rate decay factor. Equivalent to the decay_factor parameter of tf.train.exponential_decay.

FLOAT

0.95

staircase

No

Whether to decrease the learning rate at discrete intervals. Equivalent to the staircase parameter of tf.train.exponential_decay.

BOOL

true

power

No

Polynomial power. Equivalent to the power parameter of tf.train.polynomial_decay. Valid only when lr_type is set to polynomial_decay.

FLOAT

0.9

learning_rates

No

Learning rate for each specified epoch. Required when lr_type is set to manual_step. If adjusting for two epochs, specify two learning rates. For example, if decay_epochs is set to 20 40, set learning_rates to 0.001 0.0001. This adjusts the learning rate to 0.001 at epoch 20 and 0.0001 at epoch 40. Recommended: Adjust to 0.1×, 0.01×, and 0.001× the initial learning rate in sequence.

FLOAT list

N/A

lr_warmup

No

Whether to warm up the learning rate before training.

BOOL

false

lr_warm_up_epochs

No

Number of epochs for learning rate warmup.

FLOAT

1

Output

Parameter

Required

Description

Format

Default value

model_dir

Yes

OSS path for storing the trained model.

oss://path/to/model

N/A

num_visualizations

No

Number of samples to visualize during model evaluation.

INT

10

save_checkpoint_epochs

No

Checkpoint save interval in epochs. A value of 1 saves a checkpoint after each epoch.

INT

1

save_summary_epochs

No

Summary save interval in epochs. A value of 0.01 saves a summary after every 1% of training data.

FLOAT

0.01