1. 根据官方 github 改编的一个人像分割例子
(1) UNet-Pytorch Demo
网址:https://github.com/leijue222/portrait-matting-unet-flask(分割人像)
https://cloud.tencent.com/developer/article/1658426
第一步:
$ git clone https://github.com/leijue222/portrait-matting-unet-flask.git
注:clone 下来已经包含 MODEL.pth
第二步:
$ cd portrait-matting-unet-flask/
$ python predict.py -i image.jpg -o output.jpg
注:image.jpg 是自己放在 portrait-matting-unet-flask 文件夹下的一张包含有人的
图像,Output.jpg 是输出。
若报错:No module named‘xxx’如下:
ModuleNotFoundError: No module named 'torchsummary'
解决方法:pip 安装相应模块即可。
(2) UNet-Pytorch 训练数据集
第一步:下载数据集
百度网盘:http://pan.baidu.com/s/1dE14537
密码:
ndg8
第二步:数据处理
将 training 中的图像数据包括图像及其 masks,部分如下
将其分开,把全部图像放在新建的一个 imgs 文件夹中,全部 masks 放在一个新
建的 masks 文件夹中。再把 imgs、masks 文件夹复制到项目 data 文件夹下。(原
data 中无数据,删除其包含文件夹)
第三步:训练
$ python train.py -e 20 -b 1 -l 0.1 -s 0.5 -v 15.0
(3) UNet-Pytorch 训练自己的数据集
第一步:处理自己的数据集
注意自己的数据集命名规范,最好按照一次递增一一对应。
imgs:
Masks:
注:后缀.png 和.jpg 都可以使用。批量命名方式:https://www.kafan.cn/A/732kj9m43l.html
第二步:训练
$ python train.py -e 20 -b 1 -l 0.1 -s 0.5 -v 15.0
-e 表示 epoch 数
-b 表示 batch size
-l 表示学习率
-s 表示 scale
-v 表示 验证集所占的百分比
注:相关参数可参照 github 自行修改。
若报错:AssertionError: Either no mask or multiple masks found for the ID
可能的原因:https://github.com/milesial/Pytorch-UNet/issues/202
原因总结:
图像通道问题(输入 3 通道,输出单通道);
多分类 or 单分类(https://www.pianshen.com/article/68871053160/);
数据集命名问题(未一一对应)
2. 官方 github 主页:
https://github.com/milesial/Pytorch-UNet(分割车辆,2.7k Star)
预训练模型下载说明:('MODEL.pth')
https://github.com/milesial/Pytorch-UNet/issues/189(此模型 demo 预测时成功)
https://github.com/milesial/Pytorch-UNet/blob/e2e46ce509382a45b1db4e1f639aeed568f6cb3e/MODEL.pth(此模
型 demo 预测时报错)
https://github.com/milesial/Pytorch-UNet/issues/189#issuecomment-666034521
数据集说明:
https://www.kaggle.com/c/carvana-image-masking-challenge/data
Demo 实现:同上,只是 MODEL.pth 需要自己下载。
训练数据集:同上。
3. 参考网址:
https://www.aiuai.cn/aifarm416.html
https://github.com/milesial/Pytorch-UNet/issues/202
https://cloud.tencent.com/developer/article/1658426
https://github.com/leijue222/portrait-matting-unet-flask
https://www.pianshen.com/article/68871053160/
https://www.kafan.cn/A/732kj9m43l.html