Detectron 的 pytorch 实现。既可从头开始训练,也可直接从预训练的 Detectron 权重中进行推理。
Detectron 的 pytorch 实现。既可从头开始训练,也可直接从预训练的 Detectron 权重中进行推理。
Use this instead: https://github.com/facebookresearch/maskrcnn-benchmark
This code follows the implementation architecture of Detectron. Only part of the functionality is supported. Check this section for more information.
With this code, you can...
This repository is originally built on jwyang/faster-rcnn.pytorch. However, after many modifications, the structure changes a lot and it's now more similar to Detectron. I deliberately make everything similar or identical to Detectron's implementation, so as to reproduce the result directly from official pretrained weight files.
This implementation has the following features:
It is pure Pytorch code. Of course, there are some CUDA code.
It supports multi-image batch training.
It supports multiple GPUs training.
It supports three pooling methods. Notice that only roi align is revised to match the implementation in Caffe2. So, use it.
It is memory efficient. For data batching, there are two techiniques available to reduce memory usage: 1) Aspect grouping: group images with similar aspect ratio in a batch 2) Aspect cropping: crop images that are too long. Aspect grouping is implemented in Detectron, so it's used for default. Aspect cropping is the idea from jwyang/faster-rcnn.pytorch, and it's not used for default.
Besides of that, I implement a customized nn.DataParallel module which enables different batch blob size on different gpus. Check My nn.DataParallel section for more details about this.
Clone the repo:
git clone https://github.com/roytseng-tw/mask-rcnn.pytorch.git
Tested under python3.
Compile the CUDA code:
cd lib # please change to this directory
sh make.sh
If your are using Volta GPUs, uncomment this line in lib/mask.sh and remember to postpend a backslash at the line above. CUDA_PATH defaults to /usr/loca/cuda. If you want to use a CUDA library on different path, change this line accordingly.
It will compile all the modules you need, including NMS, ROI_Pooing, ROI_Crop and ROI_Align. (Actually gpu nms is never used ...)
Note that, If you use CUDA_VISIBLE_DEVICES to set gpus, make sure at least one gpu is visible when compile the code.
Create a data folder under the repo,
cd {repo_root}
mkdir data
COCO: Download the coco images and annotations from coco website.
And make sure to put the files as the following structure:
coco
├── annotations
| ├── instances_minival2014.json
│ ├── instances_train2014.json
│ ├── instances_train2017.json
│ ├── instances_val2014.json
│ ├── instances_val2017.json
│ ├── instances_valminusminival2014.json
│ ├── ...
|
└── images
├── train2014
├── train2017
├── val2014
├──val2017
├── ...
Download coco mini annotations from here. Please note that minival is exactly equivalent to the recently defined 2017 val set. Similarly, the union of valminusminival and the 2014 train is exactly equivalent to the 2017 train set.
Feel free to put the dataset at any place you want, and then soft link the dataset under the data/ folder:
ln -s path/to/coco data/coco
Recommend to put the images on a SSD for possible better training performance
I use ImageNet pretrained weights from Caffe for the backbone networks.
Download them and put them into the {repo_root}/data/pretrained_model.
You can the following command to download them all:
argparse_color_formater, colorama, requestspython tools/download_imagenet_weights.py
NOTE: Caffe pretrained weights have slightly better performance than Pytorch pretrained. Suggest to use Caffe pretrained models from the above link to reproduce the results. By the way, Detectron also use pretrained weights from Caffe.
If you want to use pytorch pre-trained models, please remember to transpose images from BGR to RGB, and also use the same data preprocessing (minus mean and normalize) as used in Pytorch pretrained model.
Besides of using the pretrained weights for ResNet above, you can also use the weights from Detectron by changing the corresponding line in model config file as follows:
RESNETS:
IMAGENET_PRETRAINED_WEIGHTS: 'data/pretrained_model/R-50.pkl'
R-50-GN.pkl and R-101-GN.pkl are required for gn_baselines.
X-101-32x8d.pkl, X-101-64x4d.pkl and X-152-32x8d-IN5k.pkl are required for ResNeXt backbones.
DO NOT CHANGE anything in the provided config files(configs/**/xxxx.yml) unless you know what you are doing
Use the environment variable CUDA_VISIBLE_DEVICES to control which GPUs to use.
batch_size: NUM_GPUS x TRAIN.IMS_PER_BATCH
effective_batch_size: batch_size x iter_size
change of somethining: new value of something / old value of something
Following config options will be adjusted automatically according to actual training setups: 1) number of GPUs NUM_GPUS, 2) batch size per GPU TRAIN.IMS_PER_BATCH, 3) update period iter_size
SOLVER.BASE_LR: adjust directly propotional to the change of batch_size.SOLVER.STEPS, SOLVER.MAX_ITER: adjust inversely propotional to the change of effective_batch_size.Take mask-rcnn with res50 backbone for example.
python tools/train_net_step.py --dataset coco2017 --cfg configs/baselines/e2e_mask_rcnn_R-50-C4.yml --use_tfboard --bs {batch_size} --nw {num_workers}
Use --bs to overwrite the default batch size to a proper value that fits into your GPUs. Simliar for --nw, number of data loader threads defaults to 4 in config.py.
Specify —-use_tfboard to log the losses on Tensorboard.
NOTE: use --dataset keypoints_coco2017 when training for keypoint-rcnn.
--iter_sizeAs in Caffe, update network once (optimizer.step()) every iter_size iterations (forward + backward). This way to have a larger effective batch size for training. Notice that, step count is only increased after network update.
python tools/train_net_step.py --dataset coco2017 --cfg configs/baselines/e2e_mask_rcnn_R-50-C4.yml --bs 4 --iter_size 4
iter_size defaults to 1.
python tools/train_net_step.py ... --load_ckpt {path/to/the/checkpoint}
or using Detectron's checkpoint file
python tools/train_net_step.py ... --load_detectron {path/to/the/checkpoint}
python tools/train_net_step.py ... --load_ckpt {path/to/the/checkpoint} --resume
When resume the training, step count and optimizer state will also be restored from the checkpoint. For SGD optimizer, optimizer state contains the momentum for each trainable parameter.
NOTE: --resume is not yet supported for --load_detectron
python tools/train_net_step.py ... --no_save --set {config.name1} {value1} {config.name2} {value2} ...
python tools/train_net_step.py ... --no_save --set DEBUG True
Load less annotations to accelarate training progress. Add --no_save to avoid saving any checkpoint or logging.python train_net_step.py --help
In short, use train_net_step.py.
In train_net_step.py:
SOLVER.LR_POLICY: steps_with_decay is supported.(Deprecated) In train_net.py some config options have no effects and worth noticing:
SOLVER.LR_POLICY, SOLVER.MAX_ITER, SOLVER.STEPS,SOLVER.LRS:
For now, the training policy is controlled by these command line arguments:
--epochs: How many epochs to train. One epoch means one travel through the whole training sets. Defaults to 6.--lr_decay_epochs : Epochs to decay the learning rate on. Decay happens on the beginning of a epoch. Epoch is 0-indexed. Defaults to [4, 5].For more command line arguments, please refer to python train_net.py --help
SOLVER.WARM_UP_ITERS, SOLVER.WARM_UP_FACTOR, SOLVER.WARM_UP_METHOD:
Training warm up is not supported.
For example, test mask-rcnn on coco2017 val set
python tools/test_net.py --dataset coco2017 --cfg config/baselines/e2e_mask_rcnn_R-50-FPN_1x.yaml --load_ckpt {path/to/your/checkpoint}
Use --load_detectron to load Detectron's checkpoint. If multiple gpus are available, add --multi-gpu-testing.
Specify a different output directry, use --output_dir {...}. Defaults to {the/parent/dir/of/checkpoint}/test
python tools/infer_simple.py --dataset coco --cfg cfgs/baselines/e2e_mask_rcnn_R-50-C4.yml --load_ckpt {path/to/your/checkpoint} --image_dir {dir/of/input/images} --output_dir {dir/to/save/visualizations}
--output_dir defaults to infer_outputs.
ResNet50_conv4_body,ResNet50_conv5_body,
ResNet101_Conv4_Body,ResNet101_Conv5_Body,
ResNet152_Conv5_Body[fpn_]ResNet101_Conv4_Body,`[fpn_]ResNet101_Co暂无开放 Issues,或尚未同步最近议题。