Train a multi-label image classification model
Use PAI EasyVision to train multi-label image classification models on large-scale datasets. Unlike single-label classification, multi-label classification assigns multiple labels to each image — for example, an image can be tagged as both "outdoors" and "sunset" simultaneously.
Prerequisites
Before you begin, ensure that you have:
-
An Object Storage Service (OSS) bucket containing training and evaluation data in TFRecord format
-
Resource Access Management (RAM) authorization for PAI to access OSS. To get your Alibaba Cloud Resource Name (ARN), go to the PAI console, open the All Product Dependencies page, find the Designer section, and click View Authorization Information in the Actions column. For details, see Cloud product dependencies and authorization: Designer
-
Access to a SQL Script component, MaxCompute client, or DataWorks developer node to invoke PAI commands. See Connect using a local client (odpscmd) or Create and manage MaxCompute nodes for setup instructions
Training commands
All examples use pai -name easy_vision_ext with -Dcmd train. Set --model_type in param_config to select the classification mode.
Single-label classification on a single server
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 Classification --backbone inception_v4 --num_classes 10 --num_epochs 1 --model_dir oss://examplebucket/test/cifar_inception_v4 --use_pretrained_model true --train_data oss://examplebucket/data/test/cifar10/*.tfrecord --test_data oss://examplebucket/data/test/cifar10/*.tfrecord --num_test_example 20 --train_batch_size 32 --test_batch_size=32 --image_size 299 --initial_learning_rate 0.01 --staircase true'
Single-label classification on multiple servers
Distributed training splits work across a parameter server and multiple workers. The example below uses one parameter server and three GPU workers.
pai -name easy_vision_ext
-Dbuckets='oss://{bucket_name}.{oss_host}/{path}'
-Darn='acs:ram::*********:role/aliyunodpspaidefaultrole'
-DossHost='{oss_host}'
-Dcmd train
-Dcluster='{
"ps": {
"count" : 1,
"cpu" : 600
},
"worker" : {
"count" : 3,
"cpu" : 800,
"gpu" : 100
}
}'
-Dparam_config='--model_type Classification --backbone inception_v4 --num_classes 10 --num_epochs 1 --model_dir oss://examplebucket/test/cifar_inception_v4_dis --use_pretrained_model true --train_data oss://examplebucket/data/test/cifar10/*.tfrecord --test_data oss://examplebucket/data/test/cifar10/*.tfrecord --num_test_example 20 --train_batch_size 32 --test_batch_size=32 --image_size 299 --initial_learning_rate 0.01 --staircase true'
Multi-label classification on a single server
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 MultiLabelClassification --backbone inception_v4 --num_classes 10 --num_epochs 1 --model_dir oss://examplebucket/test/cifar_inception_v4 --use_pretrained_model true --train_data oss://examplebucket/data/test/cifar10/*.tfrecord --test_data oss://examplebucket/data/test/cifar10/*.tfrecord --num_test_example 20 --train_batch_size 32 --test_batch_size=32 --image_size 299 --initial_learning_rate 0.01 --staircase true'
Multi-label classification on multiple servers (distributed training)
Distributed training splits work across a parameter server and multiple workers. Use this setup when single-server training is too slow for your dataset size. The example below uses one parameter server and three GPU workers.
pai -name easy_vision_ext
-Dbuckets='oss://{bucket_name}.{oss_host}/{path}'
-Darn='acs:ram::*********:role/aliyunodpspaidefaultrole'
-DossHost='{oss_host}'
-Dcmd train
-Dcluster='{
"ps": {
"count" : 1,
"cpu" : 600
},
"worker" : {
"count" : 3,
"cpu" : 800,
"gpu" : 100
}
}'
-Dparam_config='--model_type MultiLabelClassification --backbone inception_v4 --num_classes 10 --num_epochs 1 --model_dir oss://examplebucket/test/cifar_inception_v4_dis --use_pretrained_model true --train_data oss://examplebucket/data/test/cifar10/*.tfrecord --test_data oss://examplebucket/data/test/cifar10/*.tfrecord --num_test_example 20 --train_batch_size 32 --test_batch_size=32 --image_size 299 --initial_learning_rate 0.01 --staircase true'
Do not enclose string parameter values in quotation marks.
PAI command parameters
| Parameter | Required | Description | Format | Default |
|---|---|---|---|---|
buckets |
Yes | OSS bucket address. Must end with a forward slash (/). |
oss://{bucket_name}.{oss_host}/{path} |
— |
arn |
Yes | RAM authorization for OSS access. See Prerequisites for how to get this value. | acs:ram::*:role/AliyunODPSPAIDefaultRole |
— |
ossHost |
No | OSS endpoint. For valid values, see Regions and endpoints. Defaults to the value extracted from buckets. |
oss-{region}.aliyuncs.com |
Extracted from buckets |
cluster |
No | Distributed training configuration as a JSON string. Omit for single-server training. | JSON string | "" |
gpuRequired |
No | GPU resources per worker. 100 = 1 GPU; 200 = 2 GPUs per worker. |
Integer | 100 |
cmd |
Yes | EasyVision task type. Set to train for model training. |
train |
— |
param_config |
Yes | Model training parameters in Python Argparser format. See param_config parameters for details. | String | — |
param_config parameters
Pass param_config as a single string in Python Argparser format:
-Dparam_config = '--model_type MultiLabelClassification --backbone inception_v4 --num_classes 200 --model_dir oss://your/bucket/exp_dir'
Required parameters
| Parameter | Description | Format |
|---|---|---|
model_type |
Model type. Set to MultiLabelClassification for multi-label classification, or Classification for single-label. |
String |
num_classes |
Number of label classes. | Integer. Example: 10 |
train_data |
OSS path to training data files. | oss://path/to/train_*.tfrecord |
test_data |
OSS path to evaluation data. | oss://path/to/test_*.tfrecord |
train_batch_size |
Batch size for training. | Integer. Example: 32 |
test_batch_size |
Batch size for evaluation. | Integer. Example: 32 |
model_dir |
OSS directory where training output is saved. | oss://path/to/model |
num_epochs |
Number of training epochs. One epoch processes the entire training dataset once. | Integer. Example: 40 |
Network architecture
Use backbone to select the feature extraction network. The default is inception_v4.
| Network family | Supported values | Notes |
|---|---|---|
| LeNet / CifarNet / AlexNet | lenetcifarnetalexnet |
Lightweight networks for simple tasks |
| VGG | vgg_16, vgg_19 |
Deep convolutional networks |
| Inception (default) | inception_v1, inception_v2, inception_v3, inception_v4 |
inception_v4 is the default; works well for most classification tasks |
| MobileNet | mobilenet_v1, mobilenet_v2 |
Lightweight; suitable for resource-constrained deployments |
| ResNet | resnet_v1_50, resnet_v1_101, resnet_v1_152 |
Residual networks; good for deep feature extraction |
Learning rate schedule
Set lr_type to control how the learning rate changes during training. The default is exponential_decay.
lr_type value |
Behavior | Related parameters |
|---|---|---|
exponential_decay |
Exponentially decays the learning rate. decay_epochs maps to decay_steps in tf.train.exponential_decay based on total training data — typically set to half of total epochs (e.g., 10). |
decay_epochs, decay_factor (default: 0.95), staircase (default: true) |
polynomial_decay |
Polynomial decay. Automatically sets num_steps to total training iterations and end_learning_rate to 1/1000 of initial_learning_rate. |
power (default: 0.9) |
manual_step |
Manually specify the learning rate at chosen epochs. Set decay_epochs to the target epochs (e.g., 16 18) and learning_rates to the corresponding rates. Typically set transition points at 8/10 and 9/10 of total epochs. |
decay_epochs, learning_rates |
cosine_decay |
Cosine curve-based schedule. See SGDR: Stochastic Gradient Descent with Warm Restarts for details. | — |
Example — manual step decay:
If decay_epochs is 20 40, set learning_rates to 0.001 0.0001 to apply a rate of 0.001 at epoch 20 and 0.0001 at epoch 40. A useful rule of thumb is to reduce by 1/10, 1/100, and 1/1000 of the initial rate at each step.
Optional parameters
| Parameter | Description | Format | Default |
|---|---|---|---|
image_size |
Target image size in pixels after resizing. | Integer | 224 |
use_crop |
Specifies whether to apply cropping for data augmentation. | Boolean | true |
eval_each_category |
Specifies whether to evaluate accuracy per category separately. | Boolean | false |
optimizer |
Optimizer algorithm. momentum uses SGD with momentum; adam uses the Adam optimizer. |
momentum | adam |
momentum |
initial_learning_rate |
Starting learning rate. | Float | 0.01 |
train_num_readers |
Number of concurrent threads for reading training data. | Integer | 4 |
pretrained_model |
OSS path to a pre-trained model checkpoint. When set, training fine-tunes this model instead of training from scratch. | oss://pai-vision-data-sh/pretrained_models/inception_v4.ckpt |
"" |
use_pretrained_model |
Specifies whether to load a pre-trained model for fine-tuning. | Boolean | true |
num_test_example |
Number of evaluation samples during training. -1 uses all test data. |
Integer | -1 |
num_visualizations |
Number of samples to visualize during evaluation. | Integer | 10 |
save_checkpoint_epochs |
Checkpoint save frequency in epochs. 1 saves after each epoch. |
Integer | 1 |
num_train_images |
Total number of training samples. Required when using custom TFRecord files. | Integer | 0 |
label_map_path |
Path to the .pbtxt file that maps label IDs to label names. Required when using custom TFRecord files. |
String | "" |
What's next
In multi-label classification, classes are not mutually exclusive. The model outputs all detected classes meeting the specified probability threshold. After training completes, deploy the model to Elastic Algorithm Service (EAS) for online inference. For deployment instructions, see Custom deployment.