Browse by type
English | 中文

基于LibTorch的C++开源图像分割神经网络库.
⭐如果有用请给我一个star⭐
这个库具有以下优点:
如果你想对该开源项目有更多更详细的了解,请前往本人另一个开源项目:Libtorch教程 .
这是一个resnet34的torchscript模型,可以作为骨干网络权重。分割模型是 LibTorch 的 torch::nn::Module的派生类, 可以很容易生成:
#include "Segmentor.h"
auto model = UNet(1, /*num of classes*/
"resnet34", /*encoder name, could be resnet50 or others*/
"path to resnet34.pt"/*weight path pretrained on ImageNet, it is produced by torchscript*/
);
所有编码器均具有预训练的权重。加载预训练权重,以相同的方式训练数据,可能会获得更好的结果(更高的指标得分和更快的收敛速度)。还可以在冻结主干的同时仅训练解码器和分割头。
import torch
from torchvision import models
# resnet50 for example
model = models.resnet50(pretrained=True)
model.eval()
var=torch.ones((1,3,224,224))
traced_script_module = torch.jit.trace(model, var)
traced_script_module.save("resnet50.pt")
恭喜你! 大功告成! 现在,您可以使用自己喜欢的主干和分割框架来训练模型了。
Segmentor<FPN> segmentor;
segmentor.Initialize(0/*gpu id, -1 for cpu*/,
512/*resize width*/,
512/*resize height*/,
{"background","person"}/*class name dict, background included*/,
"resnet34"/*backbone name*/,
"your path to resnet34.pt");
segmentor.Train(0.0003/*initial leaning rate*/,
300/*training epochs*/,
4/*batch size*/,
"your path to voc_person_seg",
".jpg"/*image type*/,
"your path to save segmentor.pt");
cv::Mat image = cv::imread("your path to voc_person_seg\\val\\2007_004000.jpg");
Segmentor<FPN> segmentor;
segmentor.Initialize(0,512,512,{"background","person"},
"resnet34","your path to resnet34.pt");
segmentor.LoadWeight("segmentor.pt"/*the saved .pt path*/);
segmentor.Predict(image,"person"/*class name for showing*/);
预测结果显示如下:

Dataset
├── train
│ ├── xxx.json
│ ├── xxx.jpg
│ └......
├── val
│ ├── xxxx.json
│ ├── xxxx.jpg
│ └......
以下是该项目中受支持的编码器的列表。除resnest外,所有编码器权重都可以通过torchvision生成。选择适当的编码器,然后单击以展开表格,然后选择特定的编码器及其预训练的权重。
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 |
依赖库:
Windows:
配置libtorch 开发环境. Visual studio 和 Qt Creator已经通过libtorch1.7x release的验证.
Linux && MacOS:
安装libtorch和opencv。 对于libtorch, 按照官方教程安装。 对于opencv, 按照官方安装步骤。
如果你都配置好了他们,恭喜!!! 下载一个resnet34的预训练权重,点击下载和一个示例.pt文件,点击下载,放入weights文件夹。
更改src/main.cpp中的图片路径预训练权重和加载的segmentor权重路径。随后,build路径在终端输入:
export Torch_DIR='/path/to/libtorch'
cd build
cmake ..
make
./LibtorchSegmentation
以下是目前给予帮助的项目. - official pytorch - qubvel SMP - wkentaro labelme - nlohmann json
@misc{Chunyu:2021,
Author = {Chunyu Dong},
Title = {Libtorch Segment},
Year = {2021},
Publisher = {GitHub},
Journal = {GitHub repository},
Howpublished = {\url{https://github.com/AllentDan/SegmentationCpp}}
}
该项目以 MIT License开源,
基于libtorch,我释放了如下开源项目: - LibtorchTutorials - LibtorchSegmentation - LibtorchDetection
别忘了点赞哟
$ claude mcp add LibtorchSegmentation \
-- python -m otcore.mcp_server <graph>