image_all_name = [image_file.replace(‘\’, ‘/’).split(‘/’)[-1].split(‘.’)[0] for image_file in image_all]
image_SegmentationClass = glob.glob(‘data/VOCdevkit/VOC2012/SegmentationClass/*.png’)
image_se_name= [image_file.replace(‘\’, ‘/’).split(‘/’)[-1].split(‘.’)[0] for image_file in image_SegmentationClass]
image_other=list(set(image_all_name) - set(image_se_name))
print(image_other)
for image_name in image_other:
os.remove(‘data/VOCdevkit/VOC2012/JPEGImages/{}.jpg’.format(image_name))
===============================================================
其他的代码也有很多,这篇比较好理解!
其实还有个比较好的图像分割库:https://github.com/qubvel/segmentation_models.pytorch
这个图像分割集合由俄罗斯的程序员小哥Pavel Yakubovskiy一手打造。在后面的文章,我也会使用这个库演示。
===============================================================
├── src: 模型的backbone以及FCN的搭建
├── train_utils: 训练、验证以及多GPU训练相关模块
├── my_dataset.py: 自定义dataset用于读取VOC数据集
├── train.py: 以fcn_resnet50(这里使用了Dilated/Atrous Convolution)进行训练
├── predict.py: 简易的预测脚本,使用训练好的权重进行预测测试
├── validation.py: 利用训练好的权重验证/测试数据的mIoU等指标,并生成record_mAP.txt文件
└── pascal_voc_classes.json: pascal_voc标签文件
由于代码很多不能一一讲解,所以,接下来对重要的代码做剖析。
===================================================================
my_dataset.py自定义数据读取的方法,代码如下:
import os
import torch.utils.data as data
from PIL import Image
class VOCSegmentation(data.Dataset):
def init(self, voc_root, year=“2012”, transforms=None, txt_name: str = “train.txt”):
super(VOCSegmentation, self).init()
assert year in [“2007”, “2012”], “year must be in [‘2007’, ‘2012’]”
root = os.path.join(voc_root, “VOCdevkit”, f"VOC{year}")
root=root.replace(‘\’,‘/’)
assert os.path.exists(root), “path ‘{}’ does not exist.”.format(root)
image_dir = os.path.join(root, ‘JPEGImages’)
mask_dir = os.path.join(root, ‘SegmentationClass’)
txt_path = os.path.join(root, “ImageSets”, “Segmentation”, txt_name)
txt_path=txt_path.replace(‘\’,‘/’)
a