論文][Transformer][預(yù)訓(xùn)練模型]精讀NAS-BERT">[NAS論文][Transformer][預(yù)訓(xùn)練模型]精讀NAS-BERT
1895
2022-05-25
Win10實現(xiàn)Swin-Transformer 圖像分割
這篇博文是關(guān)于Swin-Transformer 圖像分割的應(yīng)用實戰(zhàn),包括環(huán)境搭建、訓(xùn)練和測試。數(shù)據(jù)集采用ADE鏈接:http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip。
Swin-Transformer 圖像分割github地址:https://github.com/SwinTransformer/Swin-Transformer-Semantic-Segmentation
這篇文章分三個部分:
第一部分介紹環(huán)境的搭建,分為Win10和Ubuntu20.04.
第二部分介紹了如何配置訓(xùn)練參數(shù)。
第三部分教大家如何配置推理參數(shù)的配置和推理結(jié)果的展示。
配置環(huán)境
win10環(huán)境配置
#VS2017
#pytorch 1.7.1
#CUDA 11.3
1、創(chuàng)建虛擬環(huán)境
conda create -n swinseg python=3.7 conda activate swinseg
2、給cl.exe添加系統(tǒng)變量
3、安裝所需要的庫
pip install cython matplotlib opencv-python pip install mmcv-full==1.3.13 pip install mmsegmentation
安裝上面的庫。mmcv的版本不要太高,高版本中有些參數(shù)改變了,運行的時候會有一些問題。
4、下載代碼
github地址:https://github.com/SwinTransformer/Swin-Transformer-Semantic-Segmentation
Ubuntu20.04環(huán)境配置
Ubuntu的環(huán)境配置相對簡單一些,
1、創(chuàng)建虛擬環(huán)境
conda create -n open-mmlab python=3.7 conda activate open-mmlab
2、安裝pytorch
根據(jù)電腦的cuda版本選擇pytorch,我試了1.6.0版本的可以。其他的版本在安裝mmcv的時候有可能會出現(xiàn)問題。
3、安裝mmcv-full
pip install -U torch==1.6.0+cu101 torchvision==0.7.0+cu102 -f https://download.pytorch.org/whl/torch_stable.html
4、下載并安裝Swin-Transformer-Semantic-Segmentation
git clone https://github.com/SwinTransformer/Swin-Transformer-Semantic-Segmentation cd Swin-Transformer-Semantic-Segmentation pip install -e . #或者 pyhton setup.py develop。注意-e后面還有個. 不要丟掉。
測試環(huán)境
1、下載預(yù)訓(xùn)練模型
ADE20K
百度網(wǎng)盤的提取碼是:swin
下載完后復(fù)制到項目的根目錄。
2、修改./demo/image_demo.py
修改配置參數(shù)img、config、checkpoint、palette。
from argparse import ArgumentParser from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot from mmseg.core.evaluation import get_palette def main(): parser = ArgumentParser() parser.add_argument('--img', default='demo.png', help='Image file') parser.add_argument('--config', default='../configs/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k.py', help='Config file') parser.add_argument('--checkpoint', default='../upernet_swin_tiny_patch4_window7_512x512.pth', help='Checkpoint file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--palette', default='ade20k', help='Color palette used for segmentation map') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_segmentor(args.config, args.checkpoint, device=args.device) # test a single image result = inference_segmentor(model, args.img) # show the results show_result_pyplot(model, args.img, result, get_palette(args.palette)) if __name__ == '__main__': main()
修改完成后運行image_demo.py
出現(xiàn)上面的圖說明環(huán)境沒有問題了。
如果出現(xiàn)找不到color150.mat,百度搜索尋找一個,非常好找到。
訓(xùn)練
數(shù)據(jù)集配置
下載數(shù)據(jù)集然后放到./tools/data/ade、下面然后解壓。
數(shù)據(jù)集路徑配置在./configs/base/datasets/ade20k.py。如果不要按照我配置的路徑配置,可以在這里修改路徑。
模型配置
修改config/base/models文件夾下對應(yīng)的upernet_swin.py將norm_cfg參數(shù)中的type由 SyncBN修改為BN 。將num_classes修改為150(可以不修改,我沒有修改也沒有出什么問題)。
修改config/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k.py的參數(shù)。
修改_base_,如下圖:
_base_ = [ '../_base_/models/upernet_swin.py', '../_base_/datasets/ade20k.py', '../_base_/default_runtime.py', '../_base_/schedules/schedule_160k.py' ]
默認是ade20k,如果選用其他的數(shù)據(jù)集,則修改對應(yīng)的py腳本、比如pascal_voc12數(shù)據(jù)集
_base_ = [ '../_base_/models/upernet_swin.py', '../_base_/datasets/pascal_voc12.py', '../_base_/default_runtime.py', '../_base_/schedules/schedule_160k.py' ]
修改所有num_classes,ade的類別是150。
修改data[‘samples_per_gpu’],這個就是batchsize,不能小于2。
修改train.py
通過from mmseg import __version__這句話找到mmesg適用的版本,然后將其MAX修改為1.3.13。不然會有版本的問題
參照下面的配置參數(shù)修改:
def parse_args(): parser = argparse.ArgumentParser(description='Train a segmentor') parser.add_argument('--config',default='../configs/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k.py', help='train config file path') parser.add_argument('--work-dir',default='output', help='the dir to save logs and models') parser.add_argument( '--load-from',default='../upernet_swin_tiny_patch4_window7_512x512.pth', help='the checkpoint file to load weights from') parser.add_argument( '--resume-from', help='the checkpoint file to resume from') parser.add_argument( '--no-validate', action='store_true', help='whether not to evaluate the checkpoint during training') group_gpus = parser.add_mutually_exclusive_group() group_gpus.add_argument( '--gpus', type=int, help='number of gpus to use ' '(only applicable to non-distributed training)') group_gpus.add_argument( '--gpu-ids', type=int, nargs='+', help='ids of gpus to use ' '(only applicable to non-distributed training)') parser.add_argument('--seed', type=int, default=None, help='random seed') parser.add_argument( '--deterministic', action='store_true', help='whether to set deterministic options for CUDNN backend.') parser.add_argument( '--options', nargs='+', action=DictAction, help='custom options') parser.add_argument( '--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) args = parser.parse_args() if 'LOCAL_RANK' not in os.environ: os.environ['LOCAL_RANK'] = str(args.local_rank) return args
然后運行train.py
測試
修改./demo/image_demo.py 中的checkpoint路徑,然后運行即可。
參考:
Win10配置Swin-Transformer-Semantic-Segmentation并訓(xùn)練自己數(shù)據(jù)集_嗶哩嗶哩_bilibili
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。