Python影像辨識筆記(二十八):BalancedMetaSoftmax — Instance Segmentation

開發環境安裝

# PyTorch 1.6版本(CUDA 9.2版)pip install torch==1.6.0+cu92 torchvision==0.7.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html# detectron2 (必須安裝0.2.1版本否則會錯誤)python -m pip install detectron2==0.2.1 -f \
https://dl.fbaipublicfiles.com/detectron2/wheels/cu92/torch1.6/index.html
# lvis api (dataset設定)
pip install git+https://github.com/lvis-dataset/lvis-api.git
# higher
pip install higher
# fvcore(避免這個問題)
pip install fvcore==0.1.1.post20200716
# tensorboard
pip install tensorboard==1.15
# 程式專案
git clone https://github.com/Majiker/BalancedMetaSoftmax-InstanceSeg

取得 dataset

Note: LVIS uses the COCO 2017 train, validation, and test image sets. If you have already downloaded the COCO images, you only need to download the LVIS annotations. LVIS val set contains images from COCO 2017 train in addition to the COCO 2017 val split.

lvis是採用coco2017作為其dataset;lvis作為annotation

根據Expected dataset structure for LVIS instance segmentation,我們必須要有以下結構的dataset分布。若要使用預設值路徑,請將coco和lvis資料夾都放在balancedmetasoftmax-instanceseg/datasets/裡面。

coco/
{train,val,test}2017/
lvis/
lvis_v0.5_{train,val}.json
lvis_v0.5_image_info_test.json
lvis_v1_{train,val}.json
lvis_v1_image_info_test{,_challenge}.json
# 下載coco dataset
wget https://raw.githubusercontent.com/ultralytics/yolov3/master/data/scripts/get_coco.sh
sh get_coco.sh#下載完成後,請將coco/images中的train2017和val2017移動到coco/。# 下載lvis annotationwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v0.5_train.json.zipwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_train.json.zipwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v0.5_val.json.zipwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_val.json.zipwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_image_info_test_challenge.json.zipwget https://s3-us-west-2.amazonaws.com/dl.fbaipublicfiles.com/LVIS/lvis_v1_image_info_test_dev.json.zip

下載 pre-trained weight並進行訓練

作者提供的程式碼可能有一些BUG存在,在進行訓練的時候,我遇到了Checkpoint not found的錯誤,Detectron2找不到Detectron Model Zoo提供的pre-trained weight去進行訓練。

  1. 下載weight到指定目錄(請自行建立pretrains資料夾)

(如BalancedMetaSoftmax-InstanceSeg/pretrains/)

wget https://dl.fbaipublicfiles.com/detectron/ImageNetPretrained/MSRA/R-50.pkl

2.開啟defaults.py檔案的第314行,發現問題的癥結點:

#修改BalancedMetaSoftmax-InstanceSeg/detectron2/engine/defaults.py當中314行的:
checkpoint=self.checkpointer.resume_or_load(self.cfg.MODEL.WEIGHTS, resume=resume)
要改成:
checkpoint =self.checkpointer.resume_or_load("/root/notebooks/nfs/work/yanwei.liu/BalancedMetaSoftmax-InstanceSeg/pretrains/R-50.pkl", resume=resume)
--------------------------------------------------------------------
--------------------------------------------------------------------
或是使用以下指令,即可讓程式讀取到R-50.pkl的pre-trained weight(其實就是在--config-file後面補上MODEL.WEIGHTS的參數),這個方式是在defaults.py的65行發現的:python ./projects/BALMS/train_net.py --num-gpus 4 --config-file XXXX.yaml MODEL.WEIGHTS ./pretrains/R-50.pkl

3. 進行訓練

# Baseline 訓練
python ./projects/BALMS/train_net.py --num-gpus 4 --config-file ./projects/BALMS/configs/feature/sigmoid_resampling_mask_rcnn_R_50_FPN_1x.yaml MODEL.WEIGHTS ./pretrains/R-50.pkl
# BALMS_decouple訓練
# 將baseline訓練好的weight放到pretrains資料夾當中,並且設定.yaml檔案中的weight路徑到訓練完成weight的檔名,即可接續balms_decouple的訓練
python ./projects/BALMS/train_net.py --config-file ./projects/BALMS/configs/classifier/balms_decouple_resampling_mask_rcnn_R_50_FPN_1x.yaml --num-gpus 4# BalancedSoftmax 訓練
python ./projects/BALMS/train_net.py --config-file ./projects/BALMS/configs/classifier/balanced_softmax_decouple_resampling_mask_rcnn_R_50_FPN_1x.yaml --num-gpus 4
Image for post
Image for post
Image for post
Image for post
使用TESLA V100 GPU的訓練結果

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store