自制DeepLabV3Plus框架训练语义分割的VOC格式数据集

DeepLabV3Plus框架训练语义分割的数据集是VOC格式,单通道的彩色图(Pillow中的一种色彩模式)。

小常识:OpenCV打开图片默认是三通道格式,显示和保存也是只支持三通道的图像。即cv::imshow()只能显示三通道的图片,cv::imwrite()、cv::VideoWriter如果要保存灰度图,必须将调用cv::cvtColor()先将图像转换成三通道的。

我是做二分类,只用得上下面代码中colors_map前面两种颜色,已有的数据集的mask是24位三通道的,要转换成VOC格式。代码如下:
# coding=utf-8

import os
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageOps


# 颜色映射表,当把颜色表的颜色种类增加到17种时(17>2^4),图片的位深度就会变成8。
colors_map = [
    (0, 0, 0),  # 黑色(背景像素) 0
    (128, 0, 0),  # 暗红色 1
    (255, 0, 0),  # 红色 2
    (0, 255, 0),  # 绿色 3
    (0, 0, 255),  # 蓝色 4
    (255, 255, 0),  # 黄色 5
    (0, 255, 255),  # 青色 6
    (255, 0, 255),  # 深红色 7
    (255, 192, 203),  # 粉红色 8
    (0, 199, 140),  # 玉色 9
    (128, 42, 42),  # 棕色 10
    (255, 97, 0),  # 橙色 11
    (238, 130, 238),  # 紫罗兰 12
    (128, 0, 128),  # 紫色 13
    (0, 0, 139),  # 深蓝色 14
    (0, 0, 128),  # 海军蓝 15
    (211, 242, 231),  # 水绿色 16
    (0, 139, 139),  # 深青色 17
    (0, 100, 0)  # 深绿色 18
]


def rgb_to_mask(image_file, out_dir):
    image = Image.open(image_file)
    image_gray = ImageOps.grayscale(image)

    width = image.size[0]
    height = image.size[1]

    for x in range(0, width):
        for y in range(0, height):
            pix = image_gray.getpixel((x, y))
            if pix != 0:
                image_gray.putpixel((x, y), 1)

    image_p = image_gray.convert('P')  # P代表索引图片,
    palette = np.array(colors_map).reshape(-1).tolist()
    image_p.putpalette(palette)

    (filename, extension) = os.path.splitext(image_file)
    filename = os.path.basename(filename)
    image_p_name = os.path.join(out_dir, filename + ".png")
    image_p.save(image_p_name)
    # image_p.show()


if __name__ == '__main__':
    input_path = 'D:\\BaiduNetdiskDownload\\CelebAMask-HQ\\CelebA-HQ-mask\\'
    output_path = 'D:\\BaiduNetdiskDownload\\CelebAMask-HQ\\CelebA-HQ-mask\\result\\'
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    files = os.listdir(input_path)
    image_files = list(filter(lambda x: '.png' in x, files))
    image_files = tqdm(image_files)
    for image_file in image_files:
        image_file = input_path + image_file
        rgb_to_mask(image_file, output_path)

以下是一个采用ONNX格式deeplabv3+网络进行语义分割的C++代码示例: ```c++ #include <iostream> #include <sstream> #include <fstream> #include <algorithm> #include <vector> #include <opencv2/opencv.hpp> #include <opencv2/dnn.hpp> using namespace std; using namespace cv; using namespace cv::dnn; int main(int argc, char** argv) { // 加载模型文件 Net net = readNetFromONNX("deeplabv3plus.onnx"); // 设置计算后端和目标设备 net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableTarget(DNN_TARGET_CPU); // 加载标签文件 vector<string> labels; ifstream ifs("labels.txt"); string line; while (getline(ifs, line)) labels.push_back(line); // 加载测试图像 Mat image = imread("test.jpg"); // 进行前向推理 Mat blob = blobFromImage(image, 1.0 / 255, Size(513, 513), Scalar(0, 0, 0), true, false); net.setInput(blob); Mat result = net.forward(); // 解析输出结果 Mat segMap(result.size[2], result.size[3], CV_32F, result.ptr<float>()); Mat segMap8u; segMap.convertTo(segMap8u, CV_8U); Mat colored = Mat::zeros(segMap.size(), CV_8UC3); for (int i = 0; i < segMap.rows; ++i) for (int j = 0; j < segMap.cols; ++j) { int index = segMap.at<uint8_t>(i, j); if (index >= labels.size()) index = 0; colored.at<Vec3b>(i, j) = Vec3b( uchar(255 * colors[index][0]), uchar(255 * colors[index][1]), uchar(255 * colors[index][2])); } // 显示结果 imshow("image", image); imshow("segmentation", colored); waitKey(0); return 0; } ``` 其中,`deeplabv3plus.onnx` 是ONNX格式deeplabv3+模型文件,`labels.txt` 包含了语义分割标签,`test.jpg` 是待测试的图像文件。该示例代码使用OpenCV的DNN模块进行前向推理,并将分割结果可视化显示出来。注意需要自己定义颜色映射表 colors。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值