yolov5在v7.0的版本中加入了对图像实例分割的全面支持,这里主要就是想基于v7.0的分支来开发构建裸土分割模型,其实在实际计算的时候模型是可以连带着检测任务一起输出结果的,这里我从结果形式上面直观来看应该就是在推理阶段直接基于分割的结果计算得到的检测框吧,还没有具体去看这块的具体逻辑,但是猜测应该是这样的。
首先来看下效果图:
这里我直接使用的是官方v7.0分支的代码,项目地址在这里,如下所示:
如果不会使用可以参考我的教程:
《基于yolov5-v7.0开发实践实例分割模型超详细教程》
这里就不再赘述了。
简单看下数据集:
标注数据实例如下所示:
0 0.37676635514018686 0.6521515867310259 0.2720934579439252 0.6739755571531272 0.18517757009345792 0.6842456608811749 0.08611214953271028 0.707353394269282 0.0038691588785046633 0.7022183424052583 0.024429906542056075 0.8382972168018897 0.08611214953271028 0.8382972168018897 0.1384485981308411 0.8370134538358837 0.19078504672897195 0.8100544315497586 0.2440560747663551 0.7741090685015919 0.28611214953271025 0.7497175721474786 0.3384485981308411 0.7587039129095203 0.35900934579439253 0.7741090685015919 0.38891588785046727 0.7882304611276574 0.4253644859813083 0.7933655129916812 0.43657943925233644 0.7856629351956453 0.4393831775700934 0.7484338091814727 0.48891588785046725 0.7445825202834548 0.49826168224299067 0.7227585498613536 0.43751401869158874 0.6675567423230974 0.41134579439252333 0.6675567423230974 0.4141495327102804 0.7035021053712642 0.41975700934579435 0.7278936017253774 0.4029345794392523 0.7368799424874191 0.36928971962616824 0.6957995275752284
0 0.5982616822429906 0.6316113792749306 0.6375140186915887 0.6316113792749306 0.7085420560747663 0.6290438533429188 0.7646168224299065 0.6290438533429188 0.827233644859813 0.6097874088528293 0.916018691588785 0.5738420458046626 0.998 0.6126373626373627 0.998 0.7445054945054945 0.9496635514018692 0.7445825202834548 0.9337757009345794 0.7445825202834548 0.8748971962616822 0.7278936017253774 0.8122803738317755 0.7112046831672999 0.7786355140186916 0.7112046831672999 0.6973271028037383 0.7086371572352881 0.6365794392523364 0.7060696313032762 0.6057383177570094 0.6893807127451987 0.5833084112149532 0.6637054534250796
0 0.5814392523364486 0.7510013351134847 0.6319065420560748 0.7625552018075382 0.7141495327102804 0.7676902536715621 0.7571401869158878 0.7689740166375679 0.8010654205607476 0.7818116462976276 0.8150841121495327 0.7946492759576871 0.8197570093457943 0.8267433501078361 0.810411214953271 0.8575536612919791 0.7758317757009345 0.8780938687480743 0.7571401869158878 0.8806613946800863 0.7487289719626168 0.856269898325973 0.7309719626168223 0.8280271130738419 0.7150841121495326 0.8164732463797884 0.5991962616822429 0.8100544315497586 0.5515327102803738 0.8113381945157645 0.5403177570093458 0.8126219574817706 0.5496635514018691 0.7869466981616514 0.5674205607476636 0.7651227277395501
我这里只有一个类别,所以index都是0.
这里我使用的是yolov5n轻量级的分割模型,对应的yaml 修改如下:
#Parameters
nc: 1 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.25 # layer channel multiple
anchors:- [10,13, 16,30, 33,23] # P3/8- [30,61, 62,45, 59,119] # P4/16- [116,90, 156,198, 373,326] # P5/32#Backbone
backbone:# [from, number, module, args][[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2[-1, 1, Conv, [128, 3, 2]], # 1-P2/4[-1, 3, C3, [128]],[-1, 1, Conv, [256, 3, 2]], # 3-P3/8[-1, 6, C3, [256]],[-1, 1, Conv, [512, 3, 2]], # 5-P4/16[-1, 9, C3, [512]],[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32[-1, 3, C3, [1024]],[-1, 1, SPPF, [1024, 5]], # 9]#Head
head:[[-1, 1, Conv, [512, 1, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[[-1, 6], 1, Concat, [1]], # cat backbone P4[-1, 3, C3, [512, False]], # 13[-1, 1, Conv, [256, 1, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[[-1, 4], 1, Concat, [1]], # cat backbone P3[-1, 3, C3, [256, False]], # 17 (P3/8-small)[-1, 1, Conv, [256, 3, 2]],[[-1, 14], 1, Concat, [1]], # cat head P4[-1, 3, C3, [512, False]], # 20 (P4/16-medium)[-1, 1, Conv, [512, 3, 2]],[[-1, 10], 1, Concat, [1]], # cat head P5[-1, 3, C3, [1024, False]], # 23 (P5/32-large)[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)]
主要就是修改nc=1
在data目录下编写baresoil.yaml,如下:
内容如下:
#Dataset
path: ./datasets/baresoil-seg
train: images/train
val: images/train
test: images/train # Classes
names:0: baresoil
最后修改一下train.py,如下:
红框中是我自己修改的部分,可以根据自己的实际需求进行修改即可。
如果不修改--workers的话可能会报错:
AttributeError: 'NoneType' object has no attribute 'python_exit_status'
所以建议修改为0.
默认执行100次epoch的迭代计算,日志输出如下所示:
可以看到:这里分别输出了box和mask的各种指标,可以看到模型是在同时完成检测和分割两种任务的计算。
训练完成后结果目录如下所示:
详情如下:
混淆矩阵:
检测实例: