上周我尝试用自制数据集训练了一下FCN网络,特此记录。首先,要感谢我的几位师弟帮忙对图片进行标注,这部分工作量还是蛮大的,非常考验细心和耐心。
一、拍摄图片
首先我们拍摄了300张图片,同一类三种不同外观和形状的物体,每一种100张,拍摄的图片力求背景要多样化,而且物体也要分布在图片的不同位置。当然,这些图片的数量还是太少了,本次实验主要是走通FCN训练的流程。
二、Labelme标注图片
然后,我们仿照PASCAL-VOC数据集对图片进行标注,通常大家都是利用Labelme软件完成此项工作,Labelme软件的使用比较简单,在此不做介绍。分割完成后,Labelme软件会生成.json文件,然后如下命令将.json转换为FCN网络可识别的原图和标注图。
labelme_json_to_dataset xxx.json
一共会生成5个文件,其中只有原图(24位的RGB图)和标注图(着色的8位单通道图像)对我们来说是有用的。网上有很多关于FCN训练自制数据集的教程,大部分都会提到用程序生成色盘的步骤,这部分不需要去做,因为我们得到的标签图本身就是8位的图像,没必要进一步去手动着色。但是,他们的工作也给了我很多的启发,我大概明白了PASCAL-VOC数据集颜色定义的来源,这个色盘colormap可以说是一种标准吧。在此,本文也记录下来:
https://blog.csdn.net/xiamentingtao/article/details/78696276
import numpy as np
# Get the specified bit value
def bitget(byteval, idx):
return ((byteval & (1 << idx)) != 0)
# Create label-color map, label --- [R G B]
# 0 --- [ 0 0 0], 1 --- [128 0 0], 2 --- [ 0 128 0]
# 3 --- [128 128 0], 4 --- [ 0 0 128], 5 --- [128 0 128]
# 6 --- [ 0 128 128], 7 --- [128 128 128], 8 --- [ 64 0 0]
# 9 --- [192 0 0], 10 --- [ 64 128 0], 11 --- [192 128 0]
# 12 --- [ 64 0 128], 13 --- [192 0 128], 14 --- [ 64 128 128]
# 15 --- [192 128 128], 16 --- [ 0 64 0], 17 --- [128 64 0]
# 18 --- [ 0 192 0], 19 --- [128 192 0], 20 --- [ 0 64 128]
def labelcolormap(N=256):
color_map = np.zeros((N, 3))
for n in xrange(N):
id_num = n
r, g, b = 0, 0, 0
for pos in xrange(8):
r = np.bitwise_or(r, (bitget(id_num, 0) << (7-pos)))
g = np.bitwise_or(g, (bitget(id_num, 1) << (7-pos)))
b = np.bitwise_or(b, (bitget(id_num, 2) << (7-pos)))
id_num = (id_num >> 3)
color_map[n, 0] = r
color_map[n, 1] = g
color_map[n, 2] = b
return color_map/255
if __name__=="__main__":
color_map=labelcolormap(21)
print color_map
上面会生成如下的矩阵,以python的结果为例:
[[ 0. 0. 0. ]
[ 0.50196078 0. 0. ]
[ 0. 0.50196078 0. ]
[ 0.50196078 0.50196078 0. ]
[ 0. 0. 0.50196078]
[ 0.50196078 0. 0.50196078]
[ 0. 0.50196078 0.50196078]
[ 0.50196078 0.50196078 0.50196078]
[ 0.25098039 0. 0. ]
[ 0.75294118 0. 0. ]
[ 0.25098039 0.50196078 0. ]
[ 0.75294118 0.50196078 0. ]
[ 0.25098039 0. 0.50196078]
[ 0.75294118 0. 0.50196078]
[ 0.25098039 0.50196078 0.50196078]
[ 0.75294118 0.50196078 0.50196078]
[ 0. 0.25098039 0. ]
[ 0.50196078 0.25098039 0. ]
[ 0. 0.75294118 0. ]
[ 0.50196078 0.75294118 0. ]
[ 0. 0.25098039 0.50196078]]
分别对应着Pascal-VOC的colormap:
background