Train a video classification model

Updated at:

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:

  1. Prepare data — Organize videos by category and create a CSV label file that maps each video to its label.

  2. Convert to TFRecord — Run the easy_vision_ext PAI command to convert raw .avi or .mp4 files into TFRecord format, which accelerates training.

  3. Train the model — Run the ev_video_classification_ext PAI 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:

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.avi

Each 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

ParameterDescriptionRequiredValid values / Notes
bucketsOSS root directory for input data.RequiredMust end with a forward slash (/).
arnARN of the RAM role with OSS access permissions.RequiredGet from the PAI console under Dependent Services > Designer > View Authorization.
ossHostOSS endpoint for the bucket region.RequiredSee Regions and endpoints.
convert_configOSS path to the configuration file that defines video categories.RequiredDownload from the resources page. See Configuration file format below.
label_fileOSS path to the CSV file mapping videos to their labels.RequiredSee Prepare your data for the CSV format.
output_tfrecordOSS 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.0

Add 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=2

Parameters

ParameterDescriptionRequiredDefault / Valid values
backboneNetwork architecture for feature extraction.Requiredresnet_3d_50
num_classesNumber of video categories. Must match the number of class_map entries in your configuration file.RequiredPositive integer
num_epochsTotal number of training epochs.RequiredPositive integer. Example: 50
model_dirOSS path where the trained model is saved.RequiredOSS path ending with /
train_dataOSS path to the TFRecord file for training.RequiredGenerated by the convert step
test_dataOSS path to the TFRecord file for evaluation.RequiredGenerated by the convert step
label_map_pathOSS path to the .pbtxt label map file.RequiredGenerated by the convert step as data_label_map.pbtxt
bucketsOSS root directory.RequiredOSS path ending with /
arnARN of the RAM role with OSS access permissions.RequiredGet from PAI console under Dependent Services > Designer > View Authorization
ossHostOSS endpoint for the bucket region.RequiredSee Regions and endpoints
num_test_exampleNumber of samples used for evaluation.OptionalPositive integer. Example: 10
train_batch_sizeNumber of samples per training batch.OptionalPositive integer. Example: 2
test_batch_sizeNumber of samples per evaluation batch.OptionalPositive integer. Example: 2
use_pretrained_modelWhether to initialize from a pre-trained model checkpoint. Set to true for transfer learning.Optionaltrue / false
pretrained_modelOSS path to the pre-trained model checkpoint (.ckpt). Required when use_pretrained_model=true.ConditionalDownload from the resources page
initial_learning_rateStarting learning rate. Works together with decay_epochs and staircase to control learning rate scheduling.OptionalFloat. Example: 0.0001
decay_epochsNumber of epochs between each learning rate decay step. Works together with initial_learning_rate.OptionalPositive integer. Must be less than num_epochs. Example: 5
save_checkpoints_epochsFrequency (in epochs) at which model checkpoints are saved.OptionalPositive integer. Must not exceed num_epochs. Example: 1
staircaseWhether to apply step-wise (staircase) learning rate decay. When false, decay is applied smoothly.Optionaltrue / false
gpuRequiredGPU resources allocated for training, expressed as a percentage.OptionalInteger. 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.