This is a warehouse for SwinTransformerV2-UNet using pytorch framework, can be used to train your segmentation datasets.
For other segmentation tasks, please refer this github warehouse
├── datasets: Load datasets
├── mydataset.py: Build the train dataloader and valid dataloader
├── ext_transforms.py: Additional data augmentation methods
├── models: SwiftFormer Model
├── build_models.py: Construct SwinUNet models
├── cbam.py: Construct "CBAM: Convolutional Block Attention Module" module.
├── swintransformerv2.py: Construct "swintransformerv2" module.
├── swinv2UNet.py: Construct "SwinTransformerV2UNet" module.
├── util:
├── losses.py: Construct "DiceScore", "DiceBCELoss" and "Dice_th_pred" modules.
├── scheduler.py: Construct a lr_scheduler.
├── engine.py: Function code for a training/validation process.
├── model_configs.py: Define config parameters.
└── train_gpu.py: Training model startup file
Before you use the code to train your own data set, please first enter the train_gpu.py file and modify the data_root, batch_size, num_workers and nb_classes parameters. If you want to predict, you only need to set the predict parameter to True.
1. nproc_per_node: <The number of GPUs you want to use on each node (machine/server)>
2. CUDA_VISIBLE_DEVICES: <Specify the index of the GPU corresponding to a single node (machine/server) (starting from 0)>
3. nnodes: <number of nodes (machine/server)>
4. node_rank: <node (machine/server) serial number>
5. master_addr: <master node (machine/server) IP address>
6. master_port: <master node (machine/server) port number>
Step 1: Download the pretrained-weights
Step 2: Write the pre-training weight path into the args.finetune in string format. Adjust args.input_size parameter based on the model pre-trained on images of different sizes.
Step 3: Modify the args.freeze_layers according to your own GPU memory. If you don't have enough memory, you can set this to True to freeze the weights of the remaining layers except the last layer of classification-head without updating the parameters. If you have enough memory, you can set this to False and not freeze the model weights.
If you want to use multiple GPU for training, whether it is a single machine with multiple GPUs or multiple machines with multiple GPUs, each GPU will divide the batch_size equally. For example, batch_size=4 in my train_gpu.py. If I want to use 2 GPUs for training, it means that the batch_size on each GPU is 4. Do not let batch_size=1 on each GPU, otherwise BN layer maybe report an error.
python train_gpu.py
python -m torch.distributed.run --nproc_per_node=8 train_gpu.py
(using a specified part of the GPUs: for example, I want to use the second and fourth GPUs)
CUDA_VISIBLE_DEVICES=1,3 python -m torch.distributed.run --nproc_per_node=2 train_gpu.py
(For the specific number of GPUs on each machine, modify the value of --nproc_per_node. If you want to specify a certain GPU, just add CUDA_VISIBLE_DEVICES= to specify the index number of the GPU before each command. The principle is the same as single-machine multi-GPU training)
python -m torch.distributed.run --nproc_per_node=1 --nnodes=2 --node_rank=0 --master_addr=<Master node IP address> --master_port=<Master node port number> train_gpu.py
python -m torch.distributed.run --nproc_per_node=1 --nnodes=2 --node_rank=1 --master_addr=<Master node IP address> --master_port=<Master node port number> train_gpu.py
@inproceedings{cao2022swin,
title={Swin-unet: Unet-like pure transformer for medical image segmentation},
author={Cao, Hu and Wang, Yueyue and Chen, Joy and Jiang, Dongsheng and Zhang, Xiaopeng and Tian, Qi and Wang, Manning},
booktitle={European conference on computer vision},
pages={205--218},
year={2022},
organization={Springer}
}
@article{liu2021swin,
title={Swin Transformer V2: Scaling Up Capacity and Resolution},
author={Liu, Ze and Hu, Han and Lin, Yutong and Yao, Zhuliang and Xie, Zhenda and Wei, Yixuan and Ning, Jia and Cao, Yue and Zhang, Zheng and Dong, Li and others},
journal={arXiv preprint arXiv:2111.09883},
year={2021}
}