Swin_T是VMamba、MLLA模型的Baseline,在CV中是经典模型,它在目标检测任务(Object Detection)上有一个专属的名字(MMdetection),也有专属的库(mmdet)。Git网址如下:
基本环境
镜像选择
coco数据下载
使用autodl的云端(足够快,推荐)
sudo apt-get install unzip
mkdir -p /root/autodl-tmp/coco/
unzip /root/autodl-pub/COCO2017/annotations_trainval2017.zip -d /root/autodl-tmp/coco/
unzip /root/autodl-pub/COCO2017/val2017.zip -d /root/autodl-tmp/coco/
unzip /root/autodl-pub/COCO2017/train2017.zip -d /root/autodl-tmp/coco/
unzip /root/autodl-pub/COCO2017/test2017.zip -d /root/autodl-tmp/coco/
环境设置
pip install mmcv-full==1.3.9 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html
pip install -U openmim
mim install mmengine
pip install timm
pip install Cython==0.29.33
pip install apex
克隆Swin-T
cd
git clone https://github.com/SwinTransformer/Swin-Transformer-Object-Detection.git
cd Swin-Transformer-Object-Detection
pip install -e.
pip install yapf==0.31
下载预训练数据
cd
wget https://github.com/SwinTransformer/storage/releases/download/v1.0.3/mask_rcnn_swin_tiny_patch4_window7_1x.pth
测试部分
修改路径
把Swin-Transformer-Object-Detection/tools/test.py得test.py文件进行插入自己的路径
插入代码如下
# 修改数据路径
new_data_root = '/root/autodl-tmp/coco/'
# 修改 data_root
cfg.data_root = new_data_root
# 更新数据加载器中引用的路径
if hasattr(cfg.data, 'train'):
cfg.data.train.ann_file = cfg.data.train.ann_file.replace('data/coco/', new_data_root)
cfg.data.train.img_prefix = cfg.data.train.img_prefix.replace('data/coco/', new_data_root)
if hasattr(cfg.data, 'val'):
cfg.data.val.ann_file = cfg.data.val.ann_file.replace('data/coco/', new_data_root)
cfg.data.val.img_prefix = cfg.data.val.img_prefix.replace('data/coco/', new_data_root)
if hasattr(cfg.data, 'test'):
cfg.data.test.ann_file = cfg.data.test.ann_file.replace('data/coco/', new_data_root)
cfg.data.test.img_prefix = cfg.data.test.img_prefix.replace('data/coco/', new_data_root)
开始测试
python /root/Swin-Transformer-Object-Detection/tools/test.py /root/Swin-Transformer-Object-Detection/configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py /root/mask_rcnn_swin_tiny_patch4_window7_1x.pth --eval bbox segm
训练部分
把Swin-Transformer-Object-Detection/tools/train.py得train.py文件进行插入自己的路径
代码如下
new_data_root = '/root/autodl-tmp/coco/'
# 更新数据集路径中的相关部分
cfg.data_root = new_data_root
# 训练集、验证集和测试集路径
if 'train' in cfg.data:
cfg.data['train']['img_prefix'] = cfg.data['train']['img_prefix'].replace('data/coco/', new_data_root)
cfg.data['train']['ann_file'] = cfg.data['train']['ann_file'].replace('data/coco/', new_data_root)
if 'val' in cfg.data:
cfg.data['val']['img_prefix'] = cfg.data['val']['img_prefix'].replace('data/coco/', new_data_root)
cfg.data['val']['ann_file'] = cfg.data['val']['ann_file'].replace('data/coco/', new_data_root)
if 'test' in cfg.data:
cfg.data['test']['img_prefix'] = cfg.data['test']['img_prefix'].replace('data/coco/', new_data_root)
cfg.data['test']['ann_file'] = cfg.data['test']['ann_file'].replace('data/coco/', new_data_root)
# 更新评估器路径中的 ann_file
if 'val_evaluator' in cfg and 'ann_file' in cfg.val_evaluator:
cfg.val_evaluator['ann_file'] = cfg.val_evaluator['ann_file'].replace('data/coco/', new_data_root)
if 'test_evaluator' in cfg and 'ann_file' in cfg.test_evaluator:
cfg.test_evaluator['ann_file'] = cfg.test_evaluator['ann_file'].replace('data/coco/', new_data_root)
if 'train_evaluator' in cfg and 'ann_file' in cfg.train_evaluator:
cfg.train_evaluator['ann_file'] = cfg.train_evaluator['ann_file'].replace('data/coco/', new_data_root)
开始训练
python /root/Swin-Transformer-Object-Detection/tools/train.py /root/Swin-Transformer-Object-Detection/configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py