项目作者: AllentDan

项目描述 :
A c++ trainable semantic segmentation library based on libtorch (pytorch c++). Backbone: ResNet, ResNext. Architecture: FPN, U-Net, PAN, LinkNet, PSPNet, DeepLab-V3, DeepLab-V3+ by now.
高级语言: C++
项目地址: git://github.com/AllentDan/SegmentationCpp.git
创建时间: 2021-02-07T14:56:00Z
项目社区:https://github.com/AllentDan/SegmentationCpp

开源协议:MIT License

下载


English | 中文



logo
C++ library with Neural Networks for Image
Segmentation based on LibTorch.


⭐Please give a star if this project helps you.⭐

The main features of this library are:

  • High level API (just a line to create a neural network)
  • 7 models architectures for binary and multi class segmentation (including legendary Unet)
  • 15 available encoders
  • All encoders have pre-trained weights for faster and better convergence
  • 35% or more inference speed boost compared with pytorch cuda, same speed for cpu. (Unet tested in rtx 2070s).

📚 Libtorch Tutorials 📚

Visit Libtorch Tutorials Project if you want to know more about Libtorch Segment library.

📋 Table of content

  1. Quick start
  2. Examples
  3. Train your own data
  4. Models
    1. Architectures
    2. Encoders
  5. Installation
  6. Thanks
  7. To do list
  8. Citing
  9. License
  10. Related repository

" class="reference-link">⏳ Quick start

1. Create your first Segmentation model with Libtorch Segment

A resnet34 trochscript file is provided here. Segmentation model is just a LibTorch torch::nn::Module, which can be created as easy as:

  1. #include "Segmentor.h"
  2. auto model = UNet(1, /*num of classes*/
  3. "resnet34", /*encoder name, could be resnet50 or others*/
  4. "path to resnet34.pt"/*weight path pretrained on ImageNet, it is produced by torchscript*/
  5. );
  • see table with available model architectures
  • see table with available encoders and their corresponding weights

2. Generate your own pretrained weights

All encoders have pretrained weights. Preparing your data the same way as during weights pre-training may give your better results (higher metric score and faster convergence). And you can also train only the decoder and segmentation head while freeze the backbone.

  1. import torch
  2. from torchvision import models
  3. # resnet34 for example
  4. model = models.resnet34(pretrained=True)
  5. model.eval()
  6. var=torch.ones((1,3,224,224))
  7. traced_script_module = torch.jit.trace(model, var)
  8. traced_script_module.save("resnet34.pt")

Congratulations! You are done! Now you can train your model with your favorite backbone and segmentation framework.

" class="reference-link">💡 Examples

  • Training model for person segmentation using images from PASCAL VOC Dataset. “voc_person_seg” dir contains 32 json labels and their corresponding jpeg images for training and 8 json labels with corresponding images for validation.
    1. Segmentor<FPN> segmentor;
    2. segmentor.Initialize(0/*gpu id, -1 for cpu*/,
    3. 512/*resize width*/,
    4. 512/*resize height*/,
    5. {"background","person"}/*class name dict, background included*/,
    6. "resnet34"/*backbone name*/,
    7. "your path to resnet34.pt");
    8. segmentor.Train(0.0003/*initial leaning rate*/,
    9. 300/*training epochs*/,
    10. 4/*batch size*/,
    11. "your path to voc_person_seg",
    12. ".jpg"/*image type*/,
    13. "your path to save segmentor.pt");
  • Predicting test. A segmentor.pt file is provided in the project here. It is trained through a FPN with ResNet34 backbone for a few epochs. You can directly test the segmentation result through:
    1. cv::Mat image = cv::imread("your path to voc_person_seg\\val\\2007_004000.jpg");
    2. Segmentor<FPN> segmentor;
    3. segmentor.Initialize(0,512,512,{"background","person"},
    4. "resnet34","your path to resnet34.pt");
    5. segmentor.LoadWeight("segmentor.pt"/*the saved .pt path*/);
    6. segmentor.Predict(image,"person"/*class name for showing*/);
    the predicted result shows as follow:

" class="reference-link">🧑‍🚀 Train your own data

  • Create your own dataset. Using labelme through “pip install” and label your images. Split the output json files and images into folders just like below:
    1. Dataset
    2. ├── train
    3. ├── xxx.json
    4. ├── xxx.jpg
    5. └......
    6. ├── val
    7. ├── xxxx.json
    8. ├── xxxx.jpg
    9. └......
  • Training or testing. Just like the example of “voc_person_seg”, replace “voc_person_seg” with your own dataset path.
  • Refer to training tricks to improve your final training performance.

" class="reference-link">📦 Models

" class="reference-link">Architectures

" class="reference-link">Encoders

  • ResNet
  • ResNext
  • VGG

The following is a list of supported encoders in the Libtorch Segment. All the encoders weights can be generated through torchvision except resnest. Select the appropriate family of encoders and click to expand the table and select a specific encoder and its pre-trained weights.


ResNet


| Encoder | Weights | Params, M |
| ————- | :———: | :———-: |
| resnet18 | imagenet | 11M |
| resnet34 | imagenet | 21M |
| resnet50 | imagenet | 23M |
| resnet101 | imagenet | 42M |
| resnet152 | imagenet | 58M |



ResNeXt


| Encoder | Weights | Params, M |
| ———————— | :———: | :———-: |
| resnext50_32x4d | imagenet | 22M |
| resnext101_32x8d | imagenet | 86M |



ResNeSt


| Encoder | Weights | Params, M |
| ———————————- | :———: | :———-: |
| timm-resnest14d | imagenet | 8M |
| timm-resnest26d | imagenet | 15M |
| timm-resnest50d | imagenet | 25M |
| timm-resnest101e | imagenet | 46M |
| timm-resnest200e | imagenet | 68M |
| timm-resnest269e | imagenet | 108M |
| timm-resnest50d_4s2x40d | imagenet | 28M |
| timm-resnest50d_1s4x24d | imagenet | 23M |



SE-Net


| Encoder | Weights | Params, M |
| —————————- | :———: | :———-: |
| senet154 | imagenet | 113M |
| se_resnet50 | imagenet | 26M |
| se_resnet101 | imagenet | 47M |
| se_resnet152 | imagenet | 64M |
| se_resnext50_32x4d | imagenet | 25M |
| se_resnext101_32x4d | imagenet | 46M |



VGG


| Encoder | Weights | Params, M |
| ———— | :———: | :———-: |
| vgg11 | imagenet | 9M |
| vgg11_bn | imagenet | 9M |
| vgg13 | imagenet | 9M |
| vgg13_bn | imagenet | 9M |
| vgg16 | imagenet | 14M |
| vgg16_bn | imagenet | 14M |
| vgg19 | imagenet | 20M |
| vgg19_bn | imagenet | 20M |


" class="reference-link">🛠 Installation

Dependency:

Windows:

Configure the environment for libtorch development. Visual studio and Qt Creator are verified for libtorch1.7x release.

Linux && MacOS:

Install libtorch and opencv.

For libtorch, follow the official pytorch c++ tutorials here.

For opencv, follow the official opencv install steps here.

If you have already configured them both, congratulations!!! Download the pretrained weight here and a demo .pt file here into weights.

Building shared or static library -DBUILD_SHARED=:

  1. export Torch_DIR='/path/to/libtorch'
  2. cd build
  3. cmake -DBUILD_SHARED=TRUE ..
  4. make
  5. sudo make install

Building tests:

  1. cd test
  2. mkdir build && cd build
  3. cmake ..
  4. make
  5. ./resnet34 ../../voc_person_seg/val/2007_003747.jpg ../../weights/resnet34.pt ../../weights/segmentor.pt

" class="reference-link">⏳ ToDo

  • More segmentation architectures and backbones
    • UNet++ [paper]
    • ResNest
    • Se-Net
  • Data augmentations
    • Random horizontal flip
    • Random vertical flip
    • Random scale rotation
  • Training tricks
    • Combined dice and cross entropy loss
    • Freeze backbone
    • Multi step learning rate schedule

" class="reference-link">🤝 Thanks

By now, these projects helps a lot.

📝 Citing

  1. @misc{Chunyu:2021,
  2. Author = {Chunyu Dong},
  3. Title = {Libtorch Segment},
  4. Year = {2021},
  5. Publisher = {GitHub},
  6. Journal = {GitHub repository},
  7. Howpublished = {\url{https://github.com/AllentDan/SegmentationCpp}}
  8. }

" class="reference-link">🛡️ License

Project is distributed under MIT License.

Based on libtorch, I released following repositories:

Last but not least, don’t forget your star…

Feel free to commit issues or pull requests, contributors wanted.

stargazers over time