PyTorch项目实战---Unet实现道路裂纹缺陷检测

目录

1.数据集下载

2.数据准备

3.数据处理

4.Unet代码

5.训练代码

6.测试代码

1.数据集下载

下载地址:
https://github.com/cuilimeng/CrackForest-dataset

 数据集共155张图像,样本尺寸大小为320*480

2.数据准备

数据集里的groundTruth是.mat的文件格式,需要转换为.png图像格式,转换代码如下:

# -*- coding: utf-8 -*-
from os.path import isdir
from scipy import io
import os, sys
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

if __name__ == '__main__':
    file_path = './CrackForest-dataset-master/groundTruth/'
    png_img_dir = './CrackForest-dataset-master/groundTruthPngImg/'
    if not isdir(png_img_dir):
        os.makedirs(png_img_dir)
    image_path_lists = os.listdir(file_path)
    images_path = []
    for index in range(len(image_path_lists)):
        image_file = os.path.join(file_path, image_path_lists[index])
        #print(image_file)#./CrackForest-dataset-master/groundTruth/001.mat
        images_path.append(image_file)
        image_mat = io.loadmat(image_file)
        segmentation_image = image_mat['groundTruth']['Segmentation'][0]
        segmentation_image_array = np.array(segmentation_image[0])
        image = Image.fromarray((segmentation_image_array -1) * 255)
        png_image_path = os.path.join(png_img_dir, "%s.png" % image_path_lists[index][0:3])
        #print(png_image_path)#./CrackForest-dataset-master/groundTruthPngImg/001.png
        image.save(png_image_path)
        plt.figure()
        plt.imshow(image)
        #plt.pause(2)
        plt.pause(0.001)
        #plt.show()

groundTruth文件下的label是.mat的文件格式:

 groundTruthPngImg文件夹下为转换后的label是.png的图像格式:

3.数据处理

# -*- coding: utf-8 -*-
import os, sys
import numpy as np
import cv2 as cv
import torch
from torch.utils.data import Dataset,DataLoader
import matplotlib.pylab as plt

class SegmentationDataset(object):
    def __init__(self, image_dir, mask_dir):
        self.images = []
        self.masks = []
        files = os.listdir(image_dir)
        sfiles
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值