運行環(huán)境:ubuntu18.04, CUDA10,conda
硬件:2080Ti
參考:
PolarMask pytorch代碼實現(xiàn)記錄(碎碎念事無巨細版)_Amberrr的博客-CSDN博客_polarmask訓(xùn)練自己的數(shù)據(jù)集
PolarMask是基于mmdetection的。
1.下載文件
新建PolarMask文件夾,在其中下載PolarMask源碼文件夾,并下載cocoapi文件夾,并列放置
2.安裝依賴
進入PolarMask文件夾,修改PolarMask/mmdet/datasets/loader/sampler.py文件中:
from mmcv.runner.utils import get_dist_info
為:
from mmcv.runner import get_dist_info
然后開始進行配置,參考安裝的指令install.md:
執(zhí)行如下命令:
conda create -n your_venv_name python=3.7 -y #自己設(shè)置虛擬環(huán)境名字,和python版本,為了匹配matlib,建議選擇3.7 conda activate your_venv_name #激活你建立的虛擬環(huán)境 conda install pytorch=1.3.1 cudatoolkit=10.0 torchvision=0.4.2 -c pytorch #安裝此版本的pytorch和cuda開發(fā)組件
如果下載沒問題(使用教育網(wǎng))跳過此步,但是如果執(zhí)行conda install pytorch=1.3.1 cudatoolkit=10.0 torchvision=0.4.2 -c pytorch,下載緩慢,則可以嘗試使用pip命令安裝pip install torch===1.3.1 torchvision===0.4.2 -f https://download.pytorch.org/whl/torch_stable.html
安裝成功后,進入cocoapi文件夾中安裝coco依賴,再進入PolarMask文件夾中安裝PolarMask依賴,代碼如下:
cd /cocoapi/PythonAPI #進入cocoapi文件夾 python setup.py build_ext install #安裝cocoapi依賴 cd /PolarMask python setup.py develop #安裝PolarMask依賴
過程中如果有報錯no module named 'xxxxx' ,可使用conda install xxxx或者pip install xxxxx,命令安裝即可。
安裝好之后,開始訓(xùn)練dataset
3.制作自己的數(shù)據(jù)集并訓(xùn)練
其中coco數(shù)據(jù)集我使用的是2017格式:
data文件夾放在PolarMask代碼包里面,布局如下:
data
|? ? ? ?|---coco
|? ? ? ?|? ? ? ? ? ? |---annotations
|? ? ? ?|? ? ? ? ? ? |---train2017
|? ? ? ?|? ? ? ? ? ? |---val2017
設(shè)置好數(shù)據(jù)集格式和位置后,在configs/polarmask/4gpu/polar_768_1x_r50.py中修改為自己的數(shù)據(jù)集路徑(data_root=data/coco/),修改類別數(shù)量num_classes=自己數(shù)據(jù)集中的類別數(shù)量,學(xué)習(xí)率設(shè)置為0.025(單塊GPU要設(shè)置?。?,可自定義修改工作路徑“work_dir=xxxxx”,保存訓(xùn)練過程中的權(quán)重文件和訓(xùn)練信息;device_ids = range(4)變?yōu)閐evices_ids = range(1)。
接下來開始訓(xùn)練,訓(xùn)練命令:
python tools/train.py configs/polarmask/4gpu/polar_768_1x_r50.py
執(zhí)行該命令后可能出現(xiàn)cuda版本的相關(guān)信息,此種情況可能是自己的cuda版本與顯卡不支持,或者是mmcv/mmdetection的版本不支持,如果這樣,請在安裝PolarMask依賴之前首先下載mmdetection和mmcv相應(yīng)版本的代碼包,按照:
安裝正確的mmdetection和mmcv版本,之后再安裝Polarmask依賴,即可解決該問題,
實際測試中 cuda10,pytorch1.3.1,python3.7, 2080Ti,ubuntu18.04,mmdetection1.0.0, mmcv0.5.3支持運行。
訓(xùn)練完成后相應(yīng)權(quán)重保存在自己設(shè)置的工作路徑中。
4.test和inference
測試命令,將訓(xùn)練結(jié)果輸出保存為pkl文件:
#test的格式?
python tools/test.py configs/polarmask/4gpu/polar_768_1x_r50.py [YOUR_CHECKPOINT_DIR] --out [OUT_DIR] # eg:python tools/test.py configs/polarmask/4gpu/polar_768_1x_r101.py /home/wh/weights/polarmask_r101_1x.pth --out work_dirs/polar101.pkl?
#自己的?
python tools/test.py configs/polarmask/4gpu/polar_768_1x_r50.py work_dirs/trash/epoch_12.pth --out /mnt/lxr/Polarmask/PolarMaskmaster/work_dirs/out/results.pkl
Inference過程
/demo/visual.ipynb改成python腳本visual.py即可
在demo/visual.py里修改cofig路徑、模型存儲路徑和測試圖片路徑

修改/mmdet/api/infrence.py文件中的函數(shù)show_result_pyplot(),在最后加上plt.savefig(“result.jpg”),可以添加參數(shù)設(shè)置指定目錄,可以重新寫一個腳本進行批處理。