Train a video classification model
Train video classification models on large-scale short video datasets and deploy them to Elastic Algorithm Service (EAS) for inference.
Prerequisites
Before you begin, ensure that you have:
An Object Storage Service (OSS) bucket in the same region as your PAI project
A Resource Access Management (RAM) role with OSS access permissions. To get the ARN, log in to the PAI console, go to Dependent Services, and click View Authorization in the Designer row.
Training videos uploaded to OSS, organized by category
The test dataset, pre-trained model, and configuration file downloaded from Download resources for video classification
How it works
The workflow has three stages:
Prepare data — Organize videos by category and create a CSV label file that maps each video to its label.
Convert to TFRecord — Run the
easy_vision_extPAI command to convert raw.avior.mp4files into TFRecord format, which accelerates training.Train the model — Run the
ev_video_classification_extPAI command with a ResNet-3D-50 backbone. The trained model is saved in TensorFlow SaveModel format to OSS.
Submit all PAI commands through one of these tools:
SQL Script component in PAI Designer
MaxCompute client. See Connect using the local client (odpscmd).
ODPS SQL node in DataWorks. See Develop an ODPS SQL task.
Prepare your data
The algorithm supports .avi and .mp4 video files. Organize your videos in OSS by category:
oss://<bucket-name>/data/
|-- eyemakeup/
|-- video_001.avi
|-- video_002.avi
|-- lipsmakeup/
|-- video_003.avi
|-- video_004.aviEach category folder name becomes a class label. File names within each folder can be any value.
Create a CSV label file that lists each video's OSS path and its category label:
#Data ID,Raw Data,Label
1,"{""tfspath"": ""oss://examplebucket/data/eye/public_v_ApplyEyeMakeup_g01_c01.avi""}","{""option"": ""ApplyEyeMakeup""}"
2,"{""tfspath"": ""oss://examplebucket/data/eye/public_v_ApplyEyeMakeup_g02_c03.avi""}","{""option"": ""ApplyEyeMakeup""}"
3,"{""tfspath"": ""oss://examplebucket/data/eye/public_v_ApplyEyeMakeup_g02_c04.avi""}","{""option"": ""ApplyEyeMakeup""}"
4,"{""tfspath"": ""oss://examplebucket/data/eye/public_v_ApplyEyeMakeup_g03_c01.avi""}","{""option"": ""ApplyEyeMakeup""}"
5,"{""tfspath"": ""oss://examplebucket/data/eye/public_v_ApplyEyeMakeup_g04_c01.avi""}","{""option"": ""ApplyEyeMakeup""}"
6,"{""tfspath"": ""oss://examplebucket/data/lips/public_v_ApplyLipstick_g04_c02.avi""}","{""option"": ""ApplyLipstick""}"
7,"{""tfspath"": ""oss://examplebucket/data/lips/public_v_ApplyLipstick_g05_c01.avi""}","{""option"": ""ApplyLipstick""}"
8,"{""tfspath"": ""oss://examplebucket/data/lips/public_v_ApplyLipstick_g07_c04.avi""}","{""option"": ""ApplyLipstick""}"
9,"{""tfspath"": ""oss://examplebucket/data/lips/public_v_ApplyLipstick_g01_c02.avi""}","{""option"": ""ApplyLipstick""}"Upload the CSV file to your OSS bucket and note the path — you'll specify it as label_file during data conversion.
Convert data to TFRecord format
Run the easy_vision_ext command to convert your videos to TFRecord format. The following example converts two categories of makeup videos:
pai -name easy_vision_ext
-project algo_public
-Dbuckets='oss://examplebucket.oss-cn-beijing-internal.aliyuncs.com/vip/'
-Darn='acs:ram::******:role/aliyunodpspaidefaultrole'
-DossHost='oss-cn-beijing-internal.aliyuncs.com'
-Dcmd convert
-Dconvert_config='oss://examplebucket.oss-cn-beijing-internal.aliyuncs.com/vip/ucf101_qince.config'
-Dlabel_file='oss://examplebucket.oss-cn-beijing-internal.aliyuncs.com/vip/vip.csv'
-Doutput_tfrecord='oss://examplebucket.oss-cn-beijing-internal.aliyuncs.com/vip/'Parameters
| Parameter | Description | Required | Valid values / Notes |
|---|---|---|---|
buckets | OSS root directory for input data. | Required | Must end with a forward slash (/). |
arn | ARN of the RAM role with OSS access permissions. | Required | Get from the PAI console under Dependent Services > Designer > View Authorization. |
ossHost | OSS endpoint for the bucket region. | Required | See Regions and endpoints. |
convert_config | OSS path to the configuration file that defines video categories. | Required | Download from the resources page. See Configuration file format below. |
label_file | OSS path to the CSV file mapping videos to their labels. | Required | See Prepare your data for the CSV format. |
output_tfrecord | OSS path where converted TFRecord files are saved. | Required | — |
Configuration file format
The configuration file defines the category labels and conversion settings:
class_map {
label_name: "ApplyEyeMakeup"
}
class_map {
label_name: "ApplyLipstick"
}
model_type: VIDEO_CLASSIFICATION
converter_class: "QinceConverter"
write_thread_num: 8
part_record_num: 64
test_ratio: 0.0Add one class_map block for each video category. The label_name values must match the labels in your CSV file exactly.
After conversion, the output directory contains the TFRecord files and a data_label_map.pbtxt file — you'll need both for training.
Train the model
Run the ev_video_classification_ext command with your converted TFRecord data. The following example trains a two-category classifier with the ResNet-3D-50 backbone, using transfer learning from a pre-trained checkpoint:
pai -name ev_video_classification_ext
-project algo_public
-Dbackbone='resnet_3d_50'
-Dnum_epochs=50
-Ddecay_epochs=5
-Dsave_checkpoints_epochs=1
-Dmodel_dir='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/model/'
-Duse_pretrained_model=true
-Dpretrained_model='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/model/resent_3d_50_model.ckpt'
-Dtrain_data='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/vip/data_train_0_0.tfrecord'
-Dtest_data='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/vip/data_train_0_0.tfrecord'
-Dlabel_map_path='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/vip/data_label_map.pbtxt'
-Dnum_test_example=10
-Dtrain_batch_size=2
-Dtest_batch_size=2
-Dbuckets='oss://demo-yuze.oss-cn-beijing-internal.aliyuncs.com/vip/'
-Darn='acs:ram::********:role/aliyunodpspaidefaultrole'
-DossHost='oss-cn-beijing-internal.aliyuncs.com'
-Dinitial_learning_rate=0.0001
-Dstaircase=false
-DgpuRequired=100
-Dnum_classes=2Parameters
| Parameter | Description | Required | Default / Valid values |
|---|---|---|---|
backbone | Network architecture for feature extraction. | Required | resnet_3d_50 |
num_classes | Number of video categories. Must match the number of class_map entries in your configuration file. | Required | Positive integer |
num_epochs | Total number of training epochs. | Required | Positive integer. Example: 50 |
model_dir | OSS path where the trained model is saved. | Required | OSS path ending with / |
train_data | OSS path to the TFRecord file for training. | Required | Generated by the convert step |
test_data | OSS path to the TFRecord file for evaluation. | Required | Generated by the convert step |
label_map_path | OSS path to the .pbtxt label map file. | Required | Generated by the convert step as data_label_map.pbtxt |
buckets | OSS root directory. | Required | OSS path ending with / |
arn | ARN of the RAM role with OSS access permissions. | Required | Get from PAI console under Dependent Services > Designer > View Authorization |
ossHost | OSS endpoint for the bucket region. | Required | See Regions and endpoints |
num_test_example | Number of samples used for evaluation. | Optional | Positive integer. Example: 10 |
train_batch_size | Number of samples per training batch. | Optional | Positive integer. Example: 2 |
test_batch_size | Number of samples per evaluation batch. | Optional | Positive integer. Example: 2 |
use_pretrained_model | Whether to initialize from a pre-trained model checkpoint. Set to true for transfer learning. | Optional | true / false |
pretrained_model | OSS path to the pre-trained model checkpoint (.ckpt). Required when use_pretrained_model=true. | Conditional | Download from the resources page |
initial_learning_rate | Starting learning rate. Works together with decay_epochs and staircase to control learning rate scheduling. | Optional | Float. Example: 0.0001 |
decay_epochs | Number of epochs between each learning rate decay step. Works together with initial_learning_rate. | Optional | Positive integer. Must be less than num_epochs. Example: 5 |
save_checkpoints_epochs | Frequency (in epochs) at which model checkpoints are saved. | Optional | Positive integer. Must not exceed num_epochs. Example: 1 |
staircase | Whether to apply step-wise (staircase) learning rate decay. When false, decay is applied smoothly. | Optional | true / false |
gpuRequired | GPU resources allocated for training, expressed as a percentage. | Optional | Integer. Example: 100 |
When training completes, the model is saved in TensorFlow SaveModel format to the model_dir directory.
What's next
Deploy the trained model to EAS for online inference. See Custom deployment.