Swin-Ttransformer Object Detection environment configuration and training
Source code address: https://github.com/SwinTransformer/Swin-Transformer-Object-Detection
Reference directory:
https://www.bilibili.com/video/BV1eu41197qK/?spm_id_from=333.788
https://www.bilibili.com /video/BV1Y3411676J?spm_id_from=333.337.search-card.all.click
Simply record the steps of installing, running, and GPU training swin-t source code.
Environment configuration
- First get the source code from the official website: https://github.com/SwinTransformer/Swin-Transformer-Object-Detection
git clone https://github.com/SwinTransformer/Swin-Transformer-Object-Detection.git
-
Create a new conda environment and install pytorch. I use python==3.6.13 and pytorch==1.7.1 and torchvision==0.8.2. There are
many tutorials for installing pytorch, so I won’t go into details here. Pay attention to the matching of the GPU version with the cuda driver . Let me put the verification method here:
$ python
enter the python command line
import torch
and it is ok if there is no error.
If it is the GPU version and you need to entertorch.cuda.is_available()
True, it is successful. (available() is followed by parentheses )
-
Install timm according to the official website
pip install timm==0.4.12
- The installation of mmcv-full failed directly
pip install mmcv-full==1.4.0
, but the installation with mim was successful (the success rate of mim installation is still quite high). I saw other bloggers say that the 1.4.5 version is easy to step on the pit.
pip install openmim
mim install mmcv-full==1.4.0 #(这部分会下载opencv-python,就不需要单独装了)
- Next, install according to the official website
pip install -r requirements.txt
python setup.py develop
- It’s okay not to install apex for the time being. If you only use cpu, you don’t need to install apex at all. But I installed it directly:
git clone https://github.com/NVIDIA/apex #(git clone的时候可能会失败,多试几次就好了。)
cd apex
pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./ #(这句失败)
pip install -v --no-cache-dir ./ #(我是用这句成功的,在GPU上这句的成功率高)
At this point the installation is over.
demo
Try running demo.py
:
command:
python demo/image_demo.py demo/demo.jpg configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py ./mask_rcnn_swin_tiny_patch4_window7_1x.pth
Explain, where:
configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py
is the configuration file
./mask_rcnn_swin_tiny_patch4_window7_1x.pth
is the pre-training model parameters, I put it in the current directory, and write it wherever the path is placed.
The pre-training model is downloaded from the official website, and I downloaded it
Running results: It can be seen that the detection accuracy is very high, and the edge is accurately grasped.
COCO training
Download COCO dataset: Official website address
But the official website is very difficult to download, and the webpage can’t be opened.
The following address can be downloaded directly by copying to the webpage:
http://images.cocodataset.org/zips/train2017.zip
http://images. cocodataset.org/annotations/annotations_trainval2017.zip
http://images.cocodataset.org/zips/val2017.zip
http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
http://images.cocodataset.org/zips /test2017.zip
http://images.cocodataset.org/annotations/image_info_test2017.zip
Generally, you only need to download the first three, including training and verification images and labels.
Next, we need to make some small modifications to the code, mainly ① the path of the data set, and ② if the GPU cannot be moved, the scaling size can be modified. The above two points are enough, and other debugging can be done later.
- Open the configuration file
swin-T-DE/configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py
(I modified Swin-Transformer-Object-Detection to swin-T-DE) and
you can see that the coco_instance.py file is called. This is the target segmentation. It doesn't matter. Let's use this first. Or students with obsessive-compulsive disorder can also modify it to coco_detection.py.
Open whichever file is called ( coco_instance.py
or coco_detection.py
), you can see the second line, the data set path data_root, and change it to the path where you store the data set
The following is the path to find the training and verification sets, refer to your own path. All of mine are placed in the COCO folder, and the annotations_trainval2017.zip is decompressed and the file name is annotations.
OK, you can already train here, if you are afraid that the GPU will not be able to carry it, then look at the second point
coco_instance.py
①As in 1, modify ( or ) in which file to callcoco_detection.py
, and change the zoom scale to (256, 256). There are two places in total, which should be the eighth and nineteenth rows. ② Modify the three scales in
the configuration file used in 1 .mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py
It has been circled in the picture.
Here's what I'm doing halfway through my training:
The training will take about two days.
Finally, I wish you all success in scientific research, good health, and success in everything~