寫在前面:
本文參照楊帆老師的博客,根據(jù)自己的需要進(jìn)行制作,楊帆老師博客原文鏈接如下:
自定義ava數(shù)據(jù)集及訓(xùn)練與測(cè)試 完整版 時(shí)空動(dòng)作/行為 視頻數(shù)據(jù)集制作 yolov5, deep sort, VIA MMAction, SlowFast-CSDN博客文章瀏覽閱讀2.2w次,點(diǎn)贊31次,收藏165次。前言這一篇博客應(yīng)該是我花時(shí)間最多的一次了,從2022年1月底至2022年4月底。我已經(jīng)將這篇博客的內(nèi)容寫為論文,上傳至arxiv:https://arxiv.org/pdf/2204.10160.pdf歡迎大家指出我論文中的問(wèn)題,特別是語(yǔ)法與用詞問(wèn)題在github上,我也上傳了完整的項(xiàng)目:https://github.com/Whiffe/Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset關(guān)于自定義ava數(shù)據(jù)集,也是后臺(tái)_ava數(shù)據(jù)集https://blog.csdn.net/WhiffeYF/article/details/124358725
一、環(huán)境準(zhǔn)備
1、數(shù)據(jù)集制作環(huán)境
conda create -n your-env-name python=3.8 -y
conda activate your-env-name
數(shù)據(jù)集制作環(huán)境只需要YOLOv5和torch,下面命令是安裝cuda11.1版本的torch的torchvision。
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
pip install opencv-python-headless==4.1.2.30
提前下載好YOLOv5源碼,cd到Y(jié)OLOv5根目錄,使用如下命令安裝YOLOv5的依賴。
pip install -r requirements.txt
2、訓(xùn)練環(huán)境
訓(xùn)練環(huán)境本文嚴(yán)格依照MMaction2框架的安裝環(huán)境,如有需要可參考如下鏈接:
安裝 — MMAction2 1.2.0 文檔https://mmaction2.readthedocs.io/zh-cn/latest/get_started/installation.html
2.1創(chuàng)建訓(xùn)練環(huán)境
conda create -n your-env-name python=3.8 -y
conda activate your-env-name
?2.2安裝Pytorch和對(duì)應(yīng)版本的torchvision
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
2.3安裝MMaction2依賴(使用mim安裝命令)
使用mim命令可以自動(dòng)解決版本問(wèn)題
pip install -U openmim
mim install mmengine
mim install mmcv
mim install mmdet
mim install mmpose
2.4從源碼構(gòu)建MMaction2
git clone https://github.com/open-mmlab/mmaction2.git
cd mmaction2
pip install -v -e .
2.5驗(yàn)證安裝
請(qǐng)參考其他博客,進(jìn)行MMaction2的安裝驗(yàn)證
Win11系統(tǒng)下使用SlowFast訓(xùn)練AVA數(shù)據(jù)集-CSDN博客文章瀏覽閱讀237次。本文主要講述如何在Win11系統(tǒng)使用MMaction2框架下SlowFast網(wǎng)絡(luò)進(jìn)行AVA數(shù)據(jù)集的訓(xùn)練。https://blog.csdn.net/Yayisheng/article/details/135116428?spm=1001.2014.3001.5501
二、數(shù)據(jù)集制作(以下內(nèi)容請(qǐng)?jiān)谝?1創(chuàng)建的環(huán)境下運(yùn)行)
請(qǐng)先下載楊帆老師的裁剪程序包
git clone https://gitee.com/YFwinston/Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset.git
1、視頻裁剪
本文以11秒視頻為例,讀者可以根據(jù)自己的需求變更視頻長(zhǎng)度,建議每個(gè)視頻里都有動(dòng)作并且持續(xù)11秒時(shí)間,裁剪軟件可以使用剪映,ffmpeg裁剪出來(lái)的視頻長(zhǎng)度有出入,一定務(wù)必保證裁剪出來(lái)的視頻為11:00:00,不能多一毫秒?。。。?/p>
2、視頻抽幀
2.1每秒30幀抽取
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset下運(yùn)行如下命令
sh cut_video.sh
生成的結(jié)果保存在 Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/frames文件夾下
?2.2整合和縮減幀
每秒一幀,整合在一個(gè)文件夾中的原因是方便yolov5檢測(cè),初步得到打標(biāo)框 ,在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset下運(yùn)行如下命令
python choose_frames_all.py 10 0
運(yùn)行結(jié)果保存在 Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/choose_frames_all下
2.3不整合縮減
不整合主要是方便via的標(biāo)注,文件結(jié)構(gòu)會(huì)根據(jù)對(duì)應(yīng)的視頻 ,在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset下運(yùn)行如下命令
python choose_frames.py 10 0
結(jié)果保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/choose_frames下
3、YOLOv5對(duì)幀進(jìn)行檢測(cè)
推薦使用YOLOv5x作為檢測(cè)的預(yù)訓(xùn)練權(quán)重,雖然模型較大,但是檢測(cè)性能較好,如有需要可以在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/yolov5/detect.py修改對(duì)應(yīng)代碼
3.1對(duì)choose_frames_all進(jìn)行檢測(cè)
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort下運(yùn)行如下代碼
python ./yolov5/detect.py --source ../Dataset/choose_frames_all/ --save-txt --save-conf
結(jié)果會(huì)保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/yolov5/runs/detect/exp,請(qǐng)挨個(gè)檢查是否每張圖片都有人的標(biāo)注框,如果沒有,請(qǐng)先將label里的txt文件轉(zhuǎn)成xml后,使用labelimg標(biāo)注,之后再將xml轉(zhuǎn)為txt
3.2 生成dense_proposals_train.pkl
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/mywork下運(yùn)行如下代碼
python dense_proposals_train.py ../yolov5/runs/detect/exp/labels ./dense_proposals_train.pkl show
4、 使用VIA工具
4.1choose_frames_all_middle
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset下運(yùn)行如下代碼
python choose_frames_middle.py
4.2自定義動(dòng)作
請(qǐng)?jiān)贑ustom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/mywork/dense_proposals_train_to_via.py 中修改自己的動(dòng)作類別(注意:動(dòng)作類別請(qǐng)盡量使用英文,中文動(dòng)作可能在訓(xùn)練階段會(huì)報(bào)編碼錯(cuò)誤!)
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/mywork下運(yùn)行如下代碼
python dense_proposals_train_to_via.py ./dense_proposals_train.pkl ../../Dataset/choose_frames_middle/
結(jié)果保存在 Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/choose_frames_middle文件夾中的json文件
4.3 去掉其他多余的標(biāo)簽
由于YOLOv5的預(yù)訓(xùn)練權(quán)重包含了80種物品類別,我們只需要人這一種類別,所以我們需要?jiǎng)h除其他類別
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset下運(yùn)行如下代碼
python chang_via_json.py
結(jié)果保存在 Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/choose_frames_middle文件夾中的_s.json文件
4.4 VIA標(biāo)注
via標(biāo)注工具下載鏈接:https://www.robots.ox.ac.uk/~vgg/software/via/downloads/via3/via-3.0.11.ziphttps://www.robots.ox.ac.uk/~vgg/software/via/downloads/via3/via-3.0.11.zip點(diǎn)擊 via_image_annotator.html
點(diǎn)擊1,導(dǎo)入choose_frames_middle的圖片文件,點(diǎn)擊2,導(dǎo)入_s.json文件
?標(biāo)注完之后點(diǎn)擊保存,會(huì)下載一個(gè)json文件,請(qǐng)按照1_finish.json,2_finish.json重命名文件之后,放在choose_frames_middle對(duì)應(yīng)的文件夾下
4.5VIA標(biāo)注信息提取和整合
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/下執(zhí)行
python json_extract.py
結(jié)果保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/train_without_personID.csv文件內(nèi)
5、使用deepsort進(jìn)行人物追蹤
請(qǐng)?zhí)崆跋螺ddeepsort的權(quán)重文件ckpt.t7,梯子下載過(guò)慢可以使用下面的百度網(wǎng)盤鏈接:
鏈接:https://pan.baidu.com/s/1zPBlLgJ8o3i5R5H539p_QQ
提取碼:ww9c
將權(quán)重文件放在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/deep_sort_pytorch/deep_sort/deep/checkpoint文件夾內(nèi)
5.1生成train_personID.csv
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/執(zhí)行(注意:請(qǐng)修改source的路徑)
python yolov5_to_deepsort.py --source /path/to/your/Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/frames
結(jié)果保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/train_personID.csv
5.2融合兩個(gè)csv文件
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/下執(zhí)行
python train_temp.py
結(jié)果保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/train_temp.csv
5.3修正文件
會(huì)發(fā)現(xiàn)有些ID是-1,這些-1是deepsort未檢測(cè)出來(lái)的數(shù)據(jù),原因是人首次出現(xiàn)或者出現(xiàn)時(shí)間過(guò)短,deepsort未檢測(cè)出ID
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/下執(zhí)行
python train.py
結(jié)果保存在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/annotations/train.csv
6、其余標(biāo)注文件
6.1生成train_excluded_timestamps.csv
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/annotations執(zhí)行
type nul > train_excluded_timestamps.csv
type nul > included_timestamps.txt
然后在included_timestamps.txt 中寫入
02
03
04
05
06
07
08
?6.2創(chuàng)建動(dòng)作文件
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/annotations執(zhí)行
type nul > action_list.pbtxt
在文件內(nèi)寫入你需要的動(dòng)作
item {
name: "your action1"
id: 1
}
item {
name: "your action2"
id: 2
}
item {
name: "your action3"
id: 3
}
6.3復(fù)制dense_proposals_train.pkl
文件
將3.2生成的dense_proposals_train.pkl
文件復(fù)制到Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/annotations下
6.4創(chuàng)建val.csv
由于本文使用的數(shù)據(jù)集較小,所以不區(qū)分訓(xùn)練集和驗(yàn)證集,使用訓(xùn)練集作為驗(yàn)證集,如果有需要區(qū)分的讀者,驗(yàn)證集的制作過(guò)程需要重復(fù)本文以上的所有操作
6.5創(chuàng)建val_excluded_timestamps.csv
復(fù)制6.1生成的文件即可
7、視頻幀重命名
請(qǐng)將Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/frames內(nèi)的所有文件,復(fù)制到Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/Dataset/rawframes中
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/mywork下運(yùn)行如下代碼
python change_raw_frames.py
8、標(biāo)注文件修正
在Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset/yolovDeepsort/mywork下運(yùn)行如下代碼
python change_dense_proposals_train.py
python change_dense_proposals_val.py
9、所有標(biāo)注文件一覽
10、AVA2.1數(shù)據(jù)集所有文件一覽
Dataset
├─annotations
│ ├─dense_proposals_train.pkl
│ ├─dense_proposals_val.pkl
│ ├─dense_proposals_test.pkl
│ ├─train.csv
│ ├─val.csv
│ ├─included_timestamps.csv
│ ├─train_excluded_timestamps.csv
│ ├─val_excluded_timestamps.csv
│ └─action_list.pbtxt
├─videos
│ ├─1.mp4
│ ├─2.mp4
│ └─...
├─video_crop
│ ├─1.mp4
│ ├─2.mp4
│ └─...
└─rawframes
├─1
│ ├─img_00001.jpg
│ ├─img_00002.jpg
│ └─...
├─2
│ ├─img_00001.jpg
│ ├─img_00002.jpg
│ └─...
└─...
三、模型訓(xùn)練
請(qǐng)參考下面的博客,對(duì)代碼進(jìn)行修改
Win11系統(tǒng)下使用SlowFast訓(xùn)練AVA數(shù)據(jù)集-CSDN博客文章瀏覽閱讀240次。本文主要講述如何在Win11系統(tǒng)使用MMaction2框架下SlowFast網(wǎng)絡(luò)進(jìn)行AVA數(shù)據(jù)集的訓(xùn)練。https://blog.csdn.net/Yayisheng/article/details/135116428?spm=1001.2014.3001.5502修改的地方如下所示
使用如下命令啟動(dòng)訓(xùn)練
python tools/train.py path/to/your/config
模型權(quán)重保存在work_dir文件下
四、模型測(cè)試
使用如下命令進(jìn)行測(cè)試
python tools/test.py path/to/your/config path/to/your/weights
得到如下結(jié)果
五、使用本地視頻進(jìn)行推理
在MMaction2的根目錄下創(chuàng)建Checkpoints文件夾
1、下載slowfast_r50_8x8x1_256e_kinetics400_rgb_20200716-73547d2b.pth
將slowfast_r50_8x8x1_256e_kinetics400_rgb_20200716-73547d2b.pth文件放入Checkpoints/mmaction文件夾內(nèi)
2、下載faster_rcnn_r50_fpn_2x_coco_bbox_mAP-0.384_20200504_210434-a5d8aa15.pth
?將faster_rcnn_r50_fpn_2x_coco_bbox_mAP-0.384_20200504_210434-a5d8aa15.pth放入Checkpoints/mmdetection文件夾內(nèi)
以上文件的百度網(wǎng)盤鏈接如下:
鏈接:https://pan.baidu.com/s/1gUs3Ec_Ux2rfEBmY3AWRjw
提取碼:8cx2
3、創(chuàng)建新的label_map
在MMaction2/tools/data/ava文件夾下,創(chuàng)建my_label_map.txt,里面放入自己的動(dòng)作類別,前后順序嚴(yán)格按照4.2的順序
4、視頻推理
使用如下命令進(jìn)行視頻推理(請(qǐng)根據(jù)自己的需求修改命令行的路徑)
python demo/demo_spatiotemporal_det.py --config path/to/your/config --checkpoint path/to/your/weights --det-config demo/demo_configs/faster-rcnn_r50_fpn_2x_coco_infer.py --det-checkpoint Checkpoints/mmdetection/faster_rcnn_r50_fpn_2x_coco_bbox_mAP-0.384_20200504_210434-a5d8aa15.pth --video path/to/your/input/video --out_filename demo/det_1.mp4 --det-score-thr 0.5 --action-score-thr 0.5 --output-stepsize 4 --output-fps 6 --label-map path/to/your/labelmap
視頻保存在demo/det_1.mp4
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-827420.html
六、寫在最后
如果在任何一步出現(xiàn)問(wèn)題,可以在評(píng)論區(qū)進(jìn)行討論,謝謝大家的關(guān)注文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-827420.html
到了這里,關(guān)于win11系統(tǒng)AVA2.1數(shù)據(jù)集制作、訓(xùn)練、測(cè)試、本地視頻驗(yàn)證(完整已跑通)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!