直接使用
请打开基于EasyCV的视频分类示例,并点击右上角 “ 在DSW中打开” 。
import torch import os os.environ['CUDA']='cu' + torch.version.cuda.replace('.', '') os.environ['Torch']='torch'+torch.version.__version__.replace('+PAI', '') !echo $CUDA !echo $Torch
cu113 torch1.11.0+cu113
# install some python deps ! pip install --upgrade tqdm ! pip install mmcv-full==1.4.4 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.8.0/index.html ! pip install http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/third_party/nvidia_dali_cuda100-0.25.0-1535750-py3-none-manylinux2014_x86_64.whl
2、安装EasyCV算法包 注:在PAI-DSW docker中预安装了pai-easycv库,可跳过该步骤,若训练测试过程中报错,尝试用下方命令更新easycv版本
#pip install pai-easycv ! echo y | pip uninstall pai-easycv easycv !pip install pai-easycv
3、简单验证
from easycv.apis import *
[2023-02-03 15:33:22,001.001 dsw-16577-67c64db7b-kslkp:309 INFO utils.py:30] NOTICE: PAIDEBUGGER is turned off.
正式工作
数据准备
本案例我们提供了视频分类的数据集, 你可以下载链接数据。 图片文件夹结构示例如下, 文件夹路径为./kinetics400
kinetics400/ ├── val_256 ├── 0002.jpg ├── 0003.jpg |... └── test.txt └── train.txt
为了快速走通流程,我们也提供了一个小的示例数据集,执行如下命令下载解压
! wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/configs/video/kinetics400.tar.gz && tar -xpf kinetics400.tar.gz
Will not apply HSTS. The HSTS database must be a regular and non-world-writable file. ERROR: could not open HSTS store at '/root/.wget-hsts'. HSTS will be disabled. --2023-02-03 15:36:44-- http://pai-vision-data-inner.oss-cn-zhangjiakou.aliyuncs.com/yhq-hehong/dsw_example_data.tar.gz Resolving pai-vision-data-inner.oss-cn-zhangjiakou.aliyuncs.com (pai-vision-data-inner.oss-cn-zhangjiakou.aliyuncs.com)... 39.98.20.34 Connecting to pai-vision-data-inner.oss-cn-zhangjiakou.aliyuncs.com (pai-vision-data-inner.oss-cn-zhangjiakou.aliyuncs.com)|39.98.20.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 441080348 (421M) [application/x-gzip] Saving to: ‘dsw_example_data.tar.gz’ dsw_example_data.ta 100%[===================>] 420.65M 11.5MB/s in 35s 2023-02-03 15:37:19 (12.1 MB/s) - ‘dsw_example_data.tar.gz’ saved [441080348/441080348]
训练模型
这个Demo中我们采用Recognizer3D视频分类算法训练
# 下载config文件 ! wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/configs/video/x3d_xs.py
为了缩短训练时间,打开配置文件 x3d_xs.py,修改total_epoch参数为10, 每隔1次迭代打印一次日志。
# runtime settings total_epochs = 10 # log config log_config=dict(interval=1)
正式训练时,建议使用单机8卡配合该配置文件使用,如果要使用单机单卡,建议调小optimizer.lr初始学习率
# 查看easycv安装位置 import easycv print(easycv.__file__)
/opt/conda/lib/python3.7/site-packages/easycv/__init__.py
如果出现以下报错信息请返回步骤2更新pai-easycv算法包
Message: Recognizer3D is not in the model registry
!python -m easycv.tools.train x3d_xs.py --work_dir work_dir/video/x3d_xs
模型导出
# 查看训练产生的pt文件 ! ls work_dir/video/x3d_xs/*pth
work_dir/video/x3d_xs/ClsEvaluator_neck_top1_best.pth work_dir/video/x3d_xs/epoch_10.pth work_dir/video/x3d_xs/epoch_5.pth
! python -m easycv.tools.export x3d_xs.py work_dir/video/x3d_xs/ClsEvaluator_neck_top1_best.pth work_dir/video/x3d_xs/export_best.pth
video/x3d_xs.py /opt/conda/lib/python3.7/site-packages/easycv/models/loss/cross_entropy_loss.py:273: UserWarning: Default ``avg_non_ignore`` is False, if you would like to ignore the certain label and average loss over non-ignore labels, which is the same with PyTorch official cross_entropy, set ``avg_non_ignore=True``. 'Default ``avg_non_ignore`` is False, if you would like to ' load checkpoint from local path: work_dir/video/x3d_xs/ClsEvaluator_neck_top1_best.pth
from easycv.predictors.video_classifier import VideoClassificationPredictor # 修改output_ckpt指向 output_ckpt = 'work_dir/video/x3d_xs/export_best.pth' fe = VideoClassificationPredictor(output_ckpt) img_path = 'kinetics400/val_256/-P6oVaW_u9E_000097_000107.mp4' input = {'filename': img_path} results = fe([input])[0] print(results['class_name']) print(results['class_probs'])
load checkpoint from local path: work_dir/video/x3d_xs/export_best.pth ['9']