1. 程式人生 > >影象分割 | FCN資料集製作的全流程(影象標註)

影象分割 | FCN資料集製作的全流程(影象標註)

一 全卷積神經網路

文章所有程式碼已上傳至github,覺得好用就給個star吧,謝謝

深度學習影象分割(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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

二 影象標籤製作

第一步:使用github開源軟體進行標註

第二步:為標註出來的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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

對應的顏色類別:

類別名稱 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 顯示器
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然後使用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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

其中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')
  • 1
  • 2
  • 3
  • 4

                這裡寫圖片描述

第三步:最關鍵的一步

需要注意的是,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)
  • 1
  • 2
  • 3

其中涉及到如何把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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三 FCN模型訓練

四 測試圖片結果上色

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')