提交 435fbef6 编写于 作者: K Kentaro Wada

Add example of semantic_segmentation

上级 076066ac
# Semantic Segmentation Example
## Annotation
```bash
labelme data_annotated --labels="$(tr '\n' , < labels.txt)" --nodata
```
![](.readme/annotation.jpg)
## Convert to VOC-like Dataset
```bash
# It generates:
# - data_dataset_voc/JPEGImages
# - data_dataset_voc/SegmentationClass
# - data_dataset_voc/SegmentationClassVisualization
./labelme2voc.py labels.txt data_annotated data_dataset_voc
```
<img src="data_dataset/JPEGImages/2011_000003.jpg" width="33%" /> <img src="data_dataset/SegmentationClass/2011_000003.png" width="33%" /> <img src="data_dataset/SegmentationClassVisualization/2011_000003.jpg" width="33%" />
Fig 1. JPEG image (left), PNG label (center), JPEG label visualization (right)
*Note that the reason why the label file is mostly black is it contains only very low label values (ex. `-1, 0, 4, 14`).*
{
"shapes": [
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
250.8142292490119,
107.33596837944665
],
[
229.8142292490119,
119.33596837944665
],
[
221.8142292490119,
135.33596837944665
],
[
223.8142292490119,
148.33596837944665
],
[
217.8142292490119,
161.33596837944665
],
[
202.8142292490119,
168.33596837944665
],
[
192.8142292490119,
200.33596837944665
],
[
194.8142292490119,
222.33596837944665
],
[
199.8142292490119,
227.33596837944665
],
[
191.8142292490119,
234.33596837944665
],
[
197.8142292490119,
264.3359683794467
],
[
213.8142292490119,
295.3359683794467
],
[
214.8142292490119,
320.3359683794467
],
[
221.8142292490119,
327.3359683794467
],
[
235.8142292490119,
326.3359683794467
],
[
240.8142292490119,
323.3359683794467
],
[
235.8142292490119,
298.3359683794467
],
[
238.8142292490119,
287.3359683794467
],
[
234.8142292490119,
268.3359683794467
],
[
257.81422924901193,
258.3359683794467
],
[
264.81422924901193,
264.3359683794467
],
[
256.81422924901193,
273.3359683794467
],
[
259.81422924901193,
282.3359683794467
],
[
284.81422924901193,
288.3359683794467
],
[
297.81422924901193,
278.3359683794467
],
[
288.81422924901193,
270.3359683794467
],
[
281.81422924901193,
270.3359683794467
],
[
283.81422924901193,
264.3359683794467
],
[
292.81422924901193,
261.3359683794467
],
[
308.81422924901193,
236.33596837944665
],
[
313.81422924901193,
217.33596837944665
],
[
309.81422924901193,
208.33596837944665
],
[
312.81422924901193,
202.33596837944665
],
[
308.81422924901193,
185.33596837944665
],
[
291.81422924901193,
173.33596837944665
],
[
269.81422924901193,
159.33596837944665
],
[
261.81422924901193,
154.33596837944665
],
[
264.81422924901193,
142.33596837944665
],
[
273.81422924901193,
137.33596837944665
],
[
278.81422924901193,
130.33596837944665
],
[
270.81422924901193,
121.33596837944665
]
]
},
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
482.81422924901193,
85.33596837944665
],
[
468.81422924901193,
90.33596837944665
],
[
460.81422924901193,
110.33596837944665
],
[
460.81422924901193,
127.33596837944665
],
[
444.81422924901193,
137.33596837944665
],
[
419.81422924901193,
153.33596837944665
],
[
410.81422924901193,
163.33596837944665
],
[
403.81422924901193,
168.33596837944665
],
[
394.81422924901193,
170.33596837944665
],
[
386.81422924901193,
168.33596837944665
],
[
386.81422924901193,
184.33596837944665
],
[
392.81422924901193,
182.33596837944665
],
[
410.81422924901193,
187.33596837944665
],
[
414.81422924901193,
192.33596837944665
],
[
437.81422924901193,
189.33596837944665
],
[
434.81422924901193,
204.33596837944665
],
[
390.81422924901193,
195.33596837944665
],
[
386.81422924901193,
195.33596837944665
],
[
387.81422924901193,
208.33596837944665
],
[
381.81422924901193,
212.33596837944665
],
[
372.81422924901193,
212.33596837944665
],
[
372.81422924901193,
216.33596837944665
],
[
400.81422924901193,
270.3359683794467
],
[
389.81422924901193,
272.3359683794467
],
[
389.81422924901193,
274.3359683794467
],
[
403.81422924901193,
282.3359683794467
],
[
444.81422924901193,
283.3359683794467
],
[
443.81422924901193,
259.3359683794467
],
[
426.81422924901193,
244.33596837944665
],
[
462.81422924901193,
256.3359683794467
],
[
474.81422924901193,
270.3359683794467
],
[
477.81422924901193,
280.3359683794467
],
[
473.81422924901193,
289.3359683794467
],
[
471.81422924901193,
296.3359683794467
],
[
472.81422924901193,
317.3359683794467
],
[
480.81422924901193,
332.3359683794467
],
[
494.81422924901193,
335.3359683794467
],
[
498.81422924901193,
329.3359683794467
],
[
494.81422924901193,
308.3359683794467
],
[
499.81422924901193,
297.3359683794467
],
[
499.81422924901193,
90.33596837944665
]
]
},
{
"label": "bottle",
"line_color": null,
"fill_color": null,
"points": [
[
374.81422924901193,
159.33596837944665
],
[
369.81422924901193,
170.33596837944665
],
[
369.81422924901193,
210.33596837944665
],
[
375.81422924901193,
212.33596837944665
],
[
387.81422924901193,
209.33596837944665
],
[
385.81422924901193,
185.33596837944665
],
[
385.81422924901193,
168.33596837944665
],
[
385.81422924901193,
165.33596837944665
],
[
382.81422924901193,
159.33596837944665
]
]
},
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
370.81422924901193,
170.33596837944665
],
[
366.81422924901193,
173.33596837944665
],
[
365.81422924901193,
182.33596837944665
],
[
368.81422924901193,
185.33596837944665
]
]
},
{
"label": "__ignore__",
"line_color": null,
"fill_color": null,
"points": [
[
338.81422924901193,
266.3359683794467
],
[
313.81422924901193,
269.3359683794467
],
[
297.81422924901193,
277.3359683794467
],
[
282.81422924901193,
288.3359683794467
],
[
273.81422924901193,
302.3359683794467
],
[
272.81422924901193,
320.3359683794467
],
[
279.81422924901193,
337.3359683794467
],
[
428.81422924901193,
337.3359683794467
],
[
432.81422924901193,
316.3359683794467
],
[
423.81422924901193,
296.3359683794467
],
[
403.81422924901193,
283.3359683794467
],
[
370.81422924901193,
270.3359683794467
]
]
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "2011_000003.jpg",
"imageData": null
}
\ No newline at end of file
{
"shapes": [
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
204.936170212766,
108.56382978723406
],
[
183.936170212766,
141.56382978723406
],
[
166.936170212766,
150.56382978723406
],
[
108.93617021276599,
203.56382978723406
],
[
92.93617021276599,
228.56382978723406
],
[
95.93617021276599,
244.56382978723406
],
[
105.93617021276599,
244.56382978723406
],
[
116.93617021276599,
223.56382978723406
],
[
163.936170212766,
187.56382978723406
],
[
147.936170212766,
212.56382978723406
],
[
117.93617021276599,
222.56382978723406
],
[
108.93617021276599,
243.56382978723406
],
[
100.93617021276599,
325.56382978723406
],
[
135.936170212766,
329.56382978723406
],
[
148.936170212766,
319.56382978723406
],
[
150.936170212766,
295.56382978723406
],
[
169.936170212766,
272.56382978723406
],
[
171.936170212766,
249.56382978723406
],
[
178.936170212766,
246.56382978723406
],
[
186.936170212766,
225.56382978723406
],
[
214.936170212766,
219.56382978723406
],
[
242.936170212766,
157.56382978723406
],
[
228.936170212766,
146.56382978723406
],
[
228.936170212766,
125.56382978723406
],
[
216.936170212766,
112.56382978723406
]
]
},
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
271.936170212766,
109.56382978723406
],
[
249.936170212766,
110.56382978723406
],
[
244.936170212766,
150.56382978723406
],
[
215.936170212766,
219.56382978723406
],
[
208.936170212766,
245.56382978723406
],
[
214.936170212766,
220.56382978723406
],
[
188.936170212766,
227.56382978723406
],
[
170.936170212766,
246.56382978723406
],
[
170.936170212766,
275.56382978723406
],
[
221.936170212766,
278.56382978723406
],
[
233.936170212766,
259.56382978723406
],
[
246.936170212766,
253.56382978723406
],
[
245.936170212766,
256.56382978723406
],
[
242.936170212766,
251.56382978723406
],
[
262.936170212766,
256.56382978723406
],
[
304.936170212766,
226.56382978723406
],
[
297.936170212766,
199.56382978723406
],
[
308.936170212766,
164.56382978723406
],
[
296.936170212766,
148.56382978723406
]
]
},
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
308.936170212766,
115.56382978723406
],
[
298.936170212766,
145.56382978723406
],
[
309.936170212766,
166.56382978723406
],
[
297.936170212766,
200.56382978723406
],
[
305.936170212766,
228.56382978723406
],
[
262.936170212766,
258.56382978723406
],
[
252.936170212766,
284.56382978723406
],
[
272.936170212766,
291.56382978723406
],
[
281.936170212766,
250.56382978723406
],
[
326.936170212766,
235.56382978723406
],
[
351.936170212766,
239.56382978723406
],
[
365.936170212766,
223.56382978723406
],
[
371.936170212766,
187.56382978723406
],
[
353.936170212766,
168.56382978723406
],
[
344.936170212766,
143.56382978723406
],
[
336.936170212766,
115.56382978723406
]
]
},
{
"label": "chair",
"line_color": null,
"fill_color": null,
"points": [
[
308.936170212766,
242.56382978723406
],
[
281.936170212766,
251.56382978723406
],
[
270.936170212766,
287.56382978723406
],
[
174.936170212766,
275.56382978723406
],
[
148.936170212766,
296.56382978723406
],
[
150.936170212766,
319.56382978723406
],
[
159.936170212766,
328.56382978723406
],
[
164.77327127659578,
375.0
],
[
485.936170212766,
373.56382978723406
],
[
497.936170212766,
336.56382978723406
],
[
497.936170212766,
202.56382978723406
],
[
453.936170212766,
193.56382978723406
],
[
434.936170212766,
212.56382978723406
],
[
367.936170212766,
224.56382978723406
],
[
350.936170212766,
241.56382978723406
]
]
},
{
"label": "person",
"line_color": null,
"fill_color": null,
"points": [
[
425.936170212766,
82.56382978723406
],
[
404.936170212766,
109.56382978723406
],
[
400.936170212766,
114.56382978723406
],
[
437.936170212766,
114.56382978723406
],
[
448.936170212766,
102.56382978723406
],
[
446.936170212766,
91.56382978723406
]
]
},
{
"label": "__ignore__",
"line_color": null,
"fill_color": null,
"points": [
[
457.936170212766,
85.56382978723406
],
[
439.936170212766,
117.56382978723406
],
[
477.936170212766,
117.56382978723406
],
[
474.936170212766,
87.56382978723406
]
]
},
{
"label": "sofa",
"line_color": null,
"fill_color": null,
"points": [
[
183.936170212766,
140.56382978723406
],
[
125.93617021276599,
140.56382978723406
],
[
110.93617021276599,
187.56382978723406
],
[
22.936170212765987,
199.56382978723406
],
[
18.936170212765987,
218.56382978723406
],
[
22.936170212765987,
234.56382978723406
],
[
93.93617021276599,
239.56382978723406
],
[
91.93617021276599,
229.56382978723406
],
[
110.93617021276599,
203.56382978723406
]
]
},
{
"label": "sofa",
"line_color": null,
"fill_color": null,
"points": [
[
103.93617021276599,
290.56382978723406
],
[
58.93617021276599,
303.56382978723406
],
[
97.93617021276599,
311.56382978723406
]
]
},
{
"label": "sofa",
"line_color": null,
"fill_color": null,
"points": [
[
348.936170212766,
146.56382978723406
],
[
472.936170212766,
149.56382978723406
],
[
477.936170212766,
162.56382978723406
],
[
471.936170212766,
196.56382978723406
],
[
453.936170212766,
192.56382978723406
],
[
434.936170212766,
213.56382978723406
],
[
368.936170212766,
226.56382978723406
],
[
375.936170212766,
187.56382978723406
],
[
353.936170212766,
164.56382978723406
]
]
},
{
"label": "sofa",
"line_color": null,
"fill_color": null,
"points": [
[
246.936170212766,
252.56382978723406
],
[
219.936170212766,
277.56382978723406
],
[
254.936170212766,
287.56382978723406
],
[
261.936170212766,
256.56382978723406
]
]
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "2011_000006.jpg",
"imageData": null
}
\ No newline at end of file
{
"shapes": [
{
"label": "bus",
"line_color": null,
"fill_color": null,
"points": [
[
260.936170212766,
22.563829787234056
],
[
193.936170212766,
19.563829787234056
],
[
124.93617021276599,
39.563829787234056
],
[
89.93617021276599,
101.56382978723406
],
[
81.93617021276599,
150.56382978723406
],
[
108.93617021276599,
145.56382978723406
],
[
88.93617021276599,
244.56382978723406
],
[
89.93617021276599,
322.56382978723406
],
[
116.93617021276599,
367.56382978723406
],
[
158.936170212766,
368.56382978723406
],
[
165.936170212766,
337.56382978723406
],
[
347.936170212766,
335.56382978723406
],
[
349.936170212766,
369.56382978723406
],
[
391.936170212766,
373.56382978723406
],
[
403.936170212766,
335.56382978723406
],
[
425.936170212766,
332.56382978723406
],
[
421.936170212766,
281.56382978723406
],
[
428.936170212766,
252.56382978723406
],
[
428.936170212766,
236.56382978723406
],
[
409.936170212766,
220.56382978723406
],
[
409.936170212766,
150.56382978723406
],
[
430.936170212766,
143.56382978723406
],
[
433.936170212766,
112.56382978723406
],
[
431.936170212766,
96.56382978723406
],
[
408.936170212766,
90.56382978723406
],
[
395.936170212766,
50.563829787234056
],
[
338.936170212766,
25.563829787234056
]
]
},
{
"label": "bus",
"line_color": null,
"fill_color": null,
"points": [
[
88.93617021276599,
115.56382978723406
],
[
0.9361702127659877,
96.56382978723406
],
[
0.0,
251.968085106388
],
[
0.9361702127659877,
265.56382978723406
],
[
27.936170212765987,
265.56382978723406
],
[
29.936170212765987,
283.56382978723406
],
[
63.93617021276599,
281.56382978723406
],
[
89.93617021276599,
252.56382978723406
],
[
100.93617021276599,
183.56382978723406
],
[
108.93617021276599,
145.56382978723406
],
[
81.93617021276599,
151.56382978723406
]
]
},
{
"label": "car",
"line_color": null,
"fill_color": null,
"points": [
[
413.936170212766,
168.56382978723406
],
[
497.936170212766,
168.56382978723406
],
[
497.936170212766,
256.56382978723406
],
[
431.936170212766,
258.56382978723406
],
[
430.936170212766,
236.56382978723406
],
[
408.936170212766,
218.56382978723406
]
]
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "2011_000025.jpg",
"imageData": null
}
\ No newline at end of file
_background_
aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
potted plant
sheep
sofa
train
tv/monitor
\ No newline at end of file
#!/usr/bin/env python
from __future__ import print_function
import argparse
import glob
import io
import json
import os
import os.path as osp
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
import PIL.ImagePalette
import skimage.color
import skimage.io
import labelme
from labelme.utils import label2rgb
from labelme.utils import label_colormap
# TODO(wkentaro): Move to labelme/utils.py
# contrib
# -----------------------------------------------------------------------------
def labelme_shapes_to_label(img_shape, shapes, label_name_to_value):
lbl = np.zeros(img_shape[:2], dtype=np.int32)
for shape in shapes:
polygons = shape['points']
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
mask = labelme.utils.polygons_to_mask(img_shape[:2], polygons)
lbl[mask] = label_value
return lbl
def draw_label(label, img, label_names, colormap=None):
plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
wspace=0, hspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
if colormap is None:
colormap = label_colormap(len(label_names))
label_viz = label2rgb(
label, img, n_labels=len(label_names), alpha=.5)
plt.imshow(label_viz)
plt.axis('off')
plt_handlers = []
plt_titles = []
for label_value, label_name in enumerate(label_names):
if label_value not in label:
continue
if label_name.startswith('_'):
continue
fc = colormap[label_value]
p = plt.Rectangle((0, 0), 1, 1, fc=fc)
plt_handlers.append(p)
plt_titles.append(label_name)
plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=.5)
f = io.BytesIO()
plt.savefig(f, bbox_inches='tight', pad_inches=0)
plt.cla()
plt.close()
out_size = (img.shape[1], img.shape[0])
out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
out = np.asarray(out)
return out
# -----------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('labels_file')
parser.add_argument('in_dir')
parser.add_argument('out_dir')
args = parser.parse_args()
if osp.exists(args.out_dir):
print('Output directory already exists:', args.out_dir)
quit(1)
os.makedirs(args.out_dir)
os.makedirs(osp.join(args.out_dir, 'JPEGImages'))
os.makedirs(osp.join(args.out_dir, 'SegmentationClass'))
os.makedirs(osp.join(args.out_dir, 'SegmentationClassVisualization'))
print('Creating dataset:', args.out_dir)
class_names = []
class_name_to_id = {}
for i, line in enumerate(open(args.labels_file).readlines()):
class_id = i - 1 # starts with -1
class_name = line.strip()
class_name_to_id[class_name] = class_id
if class_id == -1:
assert class_name == '__ignore__'
continue
elif class_id == 0:
assert class_name == '_background_'
class_names.append(class_name)
class_names = tuple(class_names)
print('class_names:', class_names)
out_class_names_file = osp.join(args.out_dir, 'class_names.txt')
with open(out_class_names_file, 'w') as f:
f.writelines('\n'.join(class_names))
print('Saved class_names:', out_class_names_file)
colormap = labelme.utils.label_colormap(255)
for label_file in glob.glob(osp.join(args.in_dir, '*.json')):
print('Generating dataset from:', label_file)
with open(label_file) as f:
base = osp.splitext(osp.basename(label_file))[0]
out_img_file = osp.join(
args.out_dir, 'JPEGImages', base + '.jpg')
out_lbl_file = osp.join(
args.out_dir, 'SegmentationClass', base + '.png')
out_viz_file = osp.join(
args.out_dir, 'SegmentationClassVisualization', base + '.jpg')
data = json.load(f)
img_file = osp.join(osp.dirname(label_file), data['imagePath'])
img = skimage.io.imread(img_file)
skimage.io.imsave(out_img_file, img)
lbl = labelme_shapes_to_label(
img_shape=img.shape,
shapes=data['shapes'],
label_name_to_value=class_name_to_id,
)
lbl_pil = PIL.Image.fromarray(lbl)
# Only works with uint8 label
# lbl_pil = PIL.Image.fromarray(lbl, mode='P')
# lbl_pil.putpalette((colormap * 255).flatten())
lbl_pil.save(out_lbl_file)
viz = draw_label(
lbl, img, class_names, colormap=colormap)
skimage.io.imsave(out_viz_file, viz)
if __name__ == '__main__':
main()
__ignore__
_background_
aeroplane
bicycle
bird
boat
bottle
bus
car
cat
chair
cow
diningtable
dog
horse
motorbike
person
potted plant
sheep
sofa
train
tv/monitor
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册