(1)查看cuda版本:
nvcc -V
gcc --version
(2) 安裝 PyTorch and torchvision
conda install pytorch cudatoolkit=10.1 torchvision -c pytorch
安裝完成之后驗證安裝的torch是否支持GPU加速:
torch.cuda.is_available()
判斷cudnn是否可用:
-
from torch.backends import cudnn
-
cudnn.is_available()
如果電腦只是cpu,沒有安裝cuda,則使用以下命令安裝
conda install pytorch torchvision torchaudio cpuonly -c pytorch
(3)通過mim安裝mmdetection
pip install openmim mim install mmdet
使用這種方式安裝時,如果要修改類別數導致不能訓練成功,則去修改python目錄下的第三方包mmdete里的相應的文件:coco.py、evaluation/里面的文件
(4)訓練
nohop python tools/train.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py --gpus 1 --work_dir work_dirs > test.log 2>&1 &
(5)測試python tools/train.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py work_dirs/faster_rcnn_r50_fpn_1x_coco.pth
在使用result=inference_detector(model,img)測試單張圖片時,返回對應類別數長度的list,其中元素是每一類的bbox,五個值分別代表框的左上角、右下角坐標,以及置信度。
(6)后台執行命令:&后台執行,nohup服務器鏈接斷了還能運行程序
nohup python test.py > test.log 2>&1 &
查看后台正在運行的程序:jobs
重新登陸之后查看后台運行程序(以python為例): ps aux|grep python
將后台運行的進程調用到前台: fg %1
暫停正在運行的進程: bg %1
(7)訓練前需要修改的地方
faster_rcnn:
基於resnet50的backbone
一、運行前要修改的地方:
(1)定義數據種類, 修改mmdet/datasets/coco.py
(2)test時會用到,mmdet/core/evaluation/class_names.py修改coco_classes數據集類別
(3)修改配置文件,修改圖片尺寸以及種類數、學習率、圖片路徑
roi_head里的num_classes
當gpu數量為8時,lr=0.02;當gpu數量為4時,lr=0.01;我只要一個gpu,所以設置lr=0.0025
如果不是完全按照coco格式,需要修改加載圖片路徑 data里的train、val、test都需要修改
二:訓練模型
python tools/train.py configs/yolox/yolox_l_8x8_300e_coco.py --resume-from *.pth
--resume-from: 恢復繼續訓練
nohup python tools/train.py configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py --resume-from work_dirs/mask_rcnn_r50_fpn_1x_coco/latest.pth &
python tools/train.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py &
訓練過程中每個epoch包含了多個batch,每個batch訓練兩張圖片
三:檢測模型
(1)mmdetection自帶的log分析工具:
python tools/analyze_logs.py plot_curve log.json --keys loss_cls loss_reg --out losses.pdf
python tools/analysis_tools/analyze_logs.py plot_curve work_dirs/yolof_r50_c5_8x8_1x_coco/20211007_192929.log.json --keys loss_cls
python tools/analysis_tools/analyze_logs.py plot_curve work_dirs/mask_rcnn_r50_fpn_1x_coco/20211008_164249.log.json --keys loss_cls loss_reg
(2)對訓練結果做定量分析
python tools/test.py configs/your_confige.py work_dirs/your_model_.pth --out ./result/fastrcnn/result_100.pkl --eval bbox --show-dir 目錄 --show-score-thr 數值 -options
--out: 檢測結果以pkl格式進行存儲,
--eval: 評價指標,COCO數據集包括proposl_fast、proposal、bbox和segm
--show-dir 將圖片檢測結果放到該目錄下
--show-score-thr 低於此數值的結果將不顯示
--options
(3)展示測試結果中評分前幾的物體
python tools/analysis_tools/analyze_results.py \
${CONFIG} \
${PREDICTION_PATH} \
${SHOW_DIR} \
[--show] \
python tools/analysis_tools/analyze_results.py work_dirs/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco.py
(3)可視化數據集
python tools/misc/browse_dataset.py ${ CONFIG }
四:檢測單張圖片時用到以下方法:
model = init_detector(config_file,checkpoint_file)
result=inference_detector(model,img)
model.show_result(img, result, model.CLASSES, out_file='testOut.jpg')