记录::图像分割数据集制作过程

图像分割数据集制作,主要分为4步,用于语义分割还是实例分割,分不清楚,只是针对代码制作的自己的数据集。

数据整体差别不大,用复制加速了一下。

1、cmd:labelme,先画一个json作为模板

2、制作label

通过代码批量复制json,每个图片都复制同样的label,然后在此基础上修改能稍快一些

然后labelme手动调整

主要是json图片数据那块,需要转换一下

import os
import glob
import PIL.Image
from labelme.logger import logger
from labelme import PY2
from labelme import QT4
import io
import json
import os.path as osp
from scipy import ndimage
import base64
from labelme import utils


path =r"图像文件夹位置"
jsonpath = r"json文件夹位置"
jsonone = r"1370559239.json"#将被复制的模板

#这块很重要
def load_image_file(filename):
    try:
        image_pil = PIL.Image.open(filename)
    except IOError:
        logger.error('Failed opening image file: {}'.format(filename))
        return

    # apply orientation to image according to exif
    image_pil = utils.apply_exif_orientation(image_pil)

    with io.BytesIO() as f:
        ext = osp.splitext(filename)[1].lower()
        if PY2 and QT4:
            format = 'PNG'
        elif ext in ['.jpg', '.jpeg']:
            format = 'JPEG'
        else:
            format = 'PNG'
        image_pil.save(f, format=format)
        f.seek(0)
        return f.read()


def newjson(jsonone,path,jsonpath):
    data = json.load(open(jsonone))
    imagePath = data["imagePath"]
    imageps = imagePath.split("\\")

    imgpath =os.listdir(path)
    for j in range(2,len(imgpath)):
        imgp = imgpath[j]
        print(imgp)
        imageps[-1] = imgp
        imagePathnew=imageps[0]
        for i in range(1,len(imageps)):
            imagePathnew = imagePathnew+"\\"+imageps[i]
        data["imagePath"] = imagePathnew

        #这块很重要
        imageData = load_image_file(path+"\\"+imgp)
        imageData = base64.b64encode(imageData).decode('utf-8')
        data["imageData"] = imageData

        image = PIL.Image.open(path+"\\"+imgp)

        data["imageHeight"] = image.height
        data["imageWidth"] = image.width

        jsonnew = jsonpath + "\\" + imgp.split('.')[0] + '.json'
        json.dump(data, open(jsonnew, 'w'))

3、将json转换成png图片

顺带也分了一下训练集和测试集

import base64
import json
import os
import os.path as osp
import PIL.Image
from labelme import utils
import random

def main():

    jsonpath = r"json文件夹"#应该是从json里面直接读的图片数据
    outpath = r"png文件夹"
    ratio = 0.8
    if not osp.exists(outpath):
        os.mkdir(outpath)

    namelist=[]
    paths = os.listdir(jsonpath)
    for onepath in paths:
        name = onepath.split(".")[0]
        json_file = jsonpath+"\\"+onepath
        outfile = outpath+"\\"+name+".png"
        namelist.append(name)

        data = json.load(open(json_file))
        imageData = data.get("imageData")

        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
            with open(imagePath, "rb") as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode("utf-8")
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {"_background_": 0}
        for shape in sorted(data["shapes"], key=lambda x: x["label"]):
            label_name = shape["label"]
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl, _ = utils.shapes_to_label(
            img.shape, data["shapes"], label_name_to_value
        )
        utils.lblsave(outfile, lbl)

    random.shuffle(namelist)
    n_total = len(namelist)
    offset = int(n_total * ratio)
    train = namelist[:offset]
    val = namelist[offset:]

    with open(outpath+"\\train.txt","w") as f:
        for i in range(len(train)):
            f.write(str(train[i])+"\n")
    f.close()
    with open(outpath+"\\val.txt","w") as f:
        for i in range(len(val)):
            f.write(str(val[i])+"\n")
    f.close()


if __name__ == "__main__":
    main()

png结果图片

4、制作训练集

我其实训练的时候只用到了,原图和3生成的png,将数据分为训练和测试写入txt,就可以了

训练模型调别人的:GitHub - bubbliiiing/deeplabv3-plus-pytorch: 这是一个deeplabv3-plus-pytorch的源码,可以用于训练自己的模型。

5、测试c++

为了让c++调用,将pth转成带网络结构的pt文件

    weight = torch.load(“*.pth”)
    model.load_state_dict(weight, strict=True)
    model = model.eval()
    # 注意模型输入的尺寸
    example = torch.rand(1, 3, 512, 512)
    traced_script_module = torch.jit.trace(model, example)
    name = lp.replace(".pth",".pt")
    traced_script_module.save(name)

opencv调用,Python版


    image = cv2.imread("img_path")
    orininal_h,orininal_w,_ = image.shape
    image=cv2.resize(image, (512, 512))
    #image.convertTo(image,cv2.CV_32F, 1.0 / 255, 0)
    image = image/255.0
    image_data  = np.expand_dims(np.transpose((np.array(image, np.float32)), (2, 0, 1)), 0)
    img_tensor = torch.from_numpy(image_data)
    pr = module.forward(img_tensor)[0]
    pr = F.softmax(pr.permute(1, 2, 0), dim=-1).cpu().detach().numpy()
    # cv2.imshow("pr",pr)
    # cv2.waitKey(0)
    pr = cv2.resize(pr, (orininal_w, orininal_h), interpolation=cv2.INTER_LINEAR)
    pr = pr.argmax(axis=-1)

    colors = [(0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128),
                   (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),
                   (64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128),
                   (128, 64, 12)]
    seg_img = np.reshape(np.array(colors, np.uint8)[np.reshape(pr, [-1])], [orininal_h, orininal_w, -1])
    image = Image.fromarray(np.uint8(seg_img))
    image.save("")

### 获取适用于遥感图像语义分割数据集 #### 使用现有公开数据集 对于希望快速启动项目的研究者来说,可以利用现有的公开数据集。一个典型例子是由Landsat 8 提供的场景图像及其手动提取的地物真实标签,专门用于云检测任务。此数据集中包含了38幅原始图像,经过预处理后被裁剪成了尺寸为384×384的小图斑块,总计有8400个训练样本和9201个测试样本[^1]。 #### 自定义创建数据集 当现成的数据无法满足具体应用需求时,则需自行构建定制化的数据集。这通常涉及到以下几个方面的工作: - **选择合适的遥感源**:根据研究目标选取适当类型的传感器数据作为基础素材。例如,如果关注植被分析,那么可能更倾向于使用包含可见光谱段以及近红外波段的信息丰富的多光谱影像。 - **采集高质量地面真值**:为了确保模型能够准确理解并分类不同类别的地表覆盖类型,必须精心准备详尽而精确的人工标注信息。这部分工作往往依赖专业的地理信息系统软件来完成,比如ArcGIS Pro这样的工具可以帮助高效地标记感兴趣区域,并生成所需的矢量文件格式以便后续处理[^2]。 - **前处理与增强**:收集到足够的原始资料之后,还需要对其进行一系列标准化操作,如辐射校正、几何配准等;另外也可以考虑采用数据扩增技术增加多样性,从而提高最终模型泛化能力。 #### 利用Google Earth Engine (GEE) 平台 作为一种便捷的选择,研究人员还可以借助像Google Earth Engine这样强大的云端服务平台来进行大规模时空序列数据分析。通过简单的脚本编程即可访问海量历史存档资源,并能轻松实现自动化批量下载功能。特别是针对那些需要长时间跨度或者大范围空间覆盖的应用场合而言,这种方法尤为适用[^3]。 ```javascript // JavaScript code snippet to load Landsat 8 data from GEE var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') .filterDate('2020-01-01', '2020-12-31'); print(dataset); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值