图像分割 | FCN数据集制作的全流程(图像标注)

转载自:http://blog.csdn.net/u010402786/article/details/72883421
一 全卷积神经网络

文章所有代码已上传至github,觉得好用就给个star吧,谢谢

https://github.com/315386775/FCN_train

深度学习图像分割(FCN)训练自己的模型大致可以以下三步:

1.为自己的数据制作label;

2.将自己的数据分为train,val和test集;

3.仿照voc_lyaers.py编写自己的输入数据层。

其中主要是如何制作自己的数据label困扰着大家。

补充:由于图像大小的限制,这里给几个图像Resize的脚本:

(1)单张图片的resize

# coding = utf-8  
import Image  

def  convert(width,height):
    im = Image.open("C:\\xxx\\test.jpg")
    out = im.resize((width, height),Image.ANTIALIAS)
    out.save("C:\\xxx\\test.jpg")
if __name__ == '__main__':
    convert(256,256)

(2)resize整个文件夹里的图片

# coding = utf-8
import Image
import os

def convert(dir,width,height):
    file_list = os.listdir(dir)
    print(file_list)
    for filename in file_list:
        path = ''
        path = dir+filename
        im = Image.open(path)
        out = im.resize((256,256),Image.ANTIALIAS)
        print "%s has been resized!"%filename
        out.save(path)

if __name__ == '__main__':
   dir = raw_input('please input the operate dir:')
   convert(dir,256,256)

(3)按比例resize

# coding = utf-8  
import Image  

def  convert(width,height):
    im = Image.open("C:\\workspace\\PythonLearn1\\test_1.jpg")
    (x, y)= im.size
    x_s = width
    y_s = y * x_s / x
    out = im.resize((x_s, y_s), Image.ANTIALIAS)
    out.save("C:\\workspace\\PythonLearn1\\test_1_out.jpg")
if __name__ == '__main__':
    convert(256,256)

二 图像标签制作

第一步:使用github开源软件进行标注

地址:https://github.com/wkentaro/labelme

第二步:为标注出来的label.png进行着色

首先需要对照VOC分割的颜色进行着色,一定要保证颜色的准确性。Matlab代码:

function cmap = labelcolormap(N)

if nargin==0
    N=256
end
cmap = zeros(N,3);
for i=1:N
    id = i-1; r=0;g=0;b=0;
    for j=0:7
        r = bitor(r, bitshift(bitget(id,1),7 - j));
        g = bitor(g, bitshift(bitget(id,2),7 - j));
        b = bitor(b, bitshift(bitget(id,3),7 - j));
        id = bitshift(id,-3);
    end
    cmap(i,1)=r; cmap(i,2)=g; cmap(i,3)=b;
end
cmap = cmap / 255;

对应的颜色类别:

类别名称 R G B 
background 0 0 0 背景 
aeroplane 128 0 0 飞机 
bicycle 0 128 0 
bird 128 128 0 
boat 0 0 128 
bottle 128 0 128 瓶子 
bus 0 128 128 大巴 
car 128 128 128 
cat 64 0 0 猫 
chair 192 0 0 
cow 64 128 0 
diningtable 192 128 0 餐桌 
dog 64 0 128 
horse 192 0 128 
motorbike 64 128 128 
person 192 128 128 
pottedplant 0 64 0 盆栽 
sheep 128 64 0 
sofa 0 192 0 
train 128 192 0 
tvmonitor 0 64 128 显示器

然后使用python 的skimage库进行颜色填充,具体函数是skimage.color.label2rgb(),这部分代码以及颜色调整我已经完成了,由于代码太长就不贴出来了,有需要的可以私信我。

#!usr/bin/python
# -*- coding:utf-8 -*-
import PIL.Image
import numpy as np
from skimage import io,data,color
import matplotlib.pyplot as plt

img = PIL.Image.open('xxx.png')
img = np.array(img)
dst = color.label2rgb(img, bg_label=0, bg_color=(0, 0, 0))
io.imsave('xxx.png', dst)

其中skimage.color.label2rgb()的路径在:x:\Anaconda2\Lib\site-packages\skimage\color,修改如下两处,注意使用COLORS1。

DEFAULT_COLORS1 = ('maroon', 'lime', 'olive', 'navy', 'purple', 'teal',
                  'gray', 'fcncat', 'fcnchair', 'fcncow', 'fcndining',
                  'fcndog', 'fcnhorse', 'fcnmotor', 'fcnperson', 'fcnpotte',
                  'fcnsheep', 'fcnsofa', 'fcntrain', 'fcntv')

                这里写图片描述

第三步:最关键的一步

需要注意的是,label文件要是gray格式,不然会出错:scores层输出与label的数据尺寸不一致,通道问题导致的,看下面的输出是否与VOC输出一致。

In [23]: img = PIL.Image.open('F:/DL/000001_json/test/dstfcn.png')
In [24]: np.unique(img)
Out[24]: array([0, 1, 2], dtype=uint8)

其中涉及到如何把24位png图转换为8位png图,直接上代码:

dirs=dir('F:/xxx/*.png');
for n=1:numel(dirs)
     strname=strcat('F:/xxx/',dirs(n).name);
     img=imread(strname);
     [x,map]=rgb2ind(img,256);
     newname=strcat('F:/xxx/',dirs(n).name);
     imwrite(x,map,newname,'png');
end

三 FCN模型训练

推荐博客:http://www.cnblogs.com/xuanxufeng/p/6243342.html

四 测试图片结果上色

from PIL import Image
import numpy as np
from datasets import CONFIG

# The arr is a predicted result
arr = np.load('arr.npy')

print 'The shape of the image is:', arr.shape
print 'The classes in the image are:', np.unique(arr)

# Define the palette
palette = []
for i in range(256):
    palette.extend((i, i, i))

# define the color of the 21 classes(PASACAL VOC)
palette[:3*21] = CONFIG['voc12']['palette'].flatten()

assert len(palette) == 768

im = Image.fromarray(arr)
im.show()
im.putpalette(palette)
im.show()

im.save('out.png')

我的博客即将同步至腾讯云+社区,邀请大家一同入驻。

参考文章:http://blog.csdn.net/u010069760/article/details/77508864

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: FCN是Fully Convolutional Network的缩写,指的是卷积网络。FCN在遥感图像分割方面取得了不错的成果,其代码也广为流传。FCN的网络结构是由卷积层和反卷积层组成的,其中,卷积层提取特征,反卷积层将特征图还原为原图分辨率的大小。 FCN遥感图像分割代码分为两个部分,第一个是训练代码,第二个是测试代码。训练代码主要包括数据预处理,网络搭建和训练周期等部分。先进行数据预处理,包括读取数据,将图像和标签转化为合适的矩阵形式,图像进行归一化处理等。然后搭建FCN网络结构,包括多个卷积层和反卷积层,并添加参数调整和损失函数等。最后进行训练周期,将数据输入网络训练,得出参数。 测试代码则是利用训练好的模型进行遥感图像分割,与训练代码类似,也需要进行数据预处理。然后读取训练好的模型,将测试图像输入模型中进行分割,得到分割结果。再将结果进行可视化展示,以便进行验证和改进。 总的来说,FCN遥感图像分割代码比较复杂,需要一定的编程和深度学习基础,但是通过学习和实践可以掌握。 ### 回答2: FCN卷积网络)是一种深度卷积神经网络,用于图像分割。在遥感图像分割方面,它可以用来提取土地利用和土地覆盖信息,例如城市和农村地区的分类,道路、建筑物、植被和水域等。 构建FCN遥感图像分割模型通常分为以下步骤: 1.准备数据集,将遥感图像和对应的标注数据集进行处理和准备。 2.搭建FCN网络结构,通过堆积卷积层,加入池化、批处理标准化和激活函数等模块。 3.进行网络训练,使用训练数据调整网络模型的参数,以使模型输出更准确。 4.测试模型效果,使用一组独立的测试数据集对模型的精度进行评估。 其中,FCN模型的评价指标可以用像素准确度、交并比和F1-score等方面来进行评估。 在进行遥感图像分割代码实现时,可以使用开源深度学习框架TensorFlow或Pytorch等,具体实现步骤详见相关代码实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值