您的位置:首页 > 其它

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

2018-01-22 12:56 615 查看
转载自: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: