前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图像分割 | FCN数据集制作的全流程(图像标注)

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

作者头像
深度学习思考者
发布2018-01-02 20:37:37
4.7K0
发布2018-01-02 20:37:37
举报

一 全卷积神经网络

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

https://github.com/315386775/FCN_train

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

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

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

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

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

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

(1)单张图片的resize

代码语言:javascript
复制
# 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整个文件夹里的图片

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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代码:

代码语言:javascript
复制
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;

对应的颜色类别:

代码语言:javascript
复制
类别名称 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(),这部分代码以及颜色调整我已经完成了,由于代码太长就不贴出来了,有需要的可以私信我。

代码语言:javascript
复制
#!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。

代码语言:javascript
复制
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输出一致。

代码语言:javascript
复制
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图,直接上代码:

代码语言:javascript
复制
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

四 测试图片结果上色

代码语言:javascript
复制
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')
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 全卷积神经网络
  • 二 图像标签制作
  • 三 FCN模型训练
  • 四 测试图片结果上色
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档