every blog every motto: Just live your life cause we don’t live twice.
0. 前言
因爲遇到一個任務,需要自己製作標籤。
題外話: 像mnist或是fashion_minst數據集,樣本的標籤是是一個數字(或是one-hot)或是字符串。對於搭建的模型最後一般用softmax調整下概率,最後交叉熵計算就行。剛開始製作標籤的就一根筋的想着怎麼把樣本映射到一個數字上,擦,,,,浪費了不少時間。原來是遇到一個新的任務------圖像分割(語義分割),minst那些是圖像分類,兩個不是一碼事!!!!
這裏的標籤也是一個圖片!!!
這裏的標籤也是一個圖片!!!
這裏的標籤也是一個圖片!!!
1. 正文
1.1 安裝與使用
控制檯輸入
pip install labelme
打開軟件,控制檯輸入
labelme
如下圖,
open: 打開單個文件
open dir : 打開文件夾(內含多個文件)
打開多個圖片,右下角顯示文件夾中含有的圖片,如下圖。
進行標註,如下圖(create polygons,首尾點合併就能輸入標籤名稱。
保存,如下圖(會在文件下生成對應的json文件)
接着進行下一張圖片的標註,點擊next image,如下圖(圖片僅爲演示)
1.2 (批量)轉成可用與訓練的樣本和標籤
第一步,將上面生成的若干個json文件放在同一個文件夾下(文件夾內只含有json文件),如下圖。
路徑:
D:\Data_saved\Desktop\deep_learning_data\Sample\json
第二步,找到 json_to_dataset.py
在如下圖路徑下。
將json_to_dataset.py中的代碼替換爲以下。代碼來源
import argparse
import json
import os
import os.path as osp
import warnings
import PIL.Image
import yaml
from labelme import utils
import base64
def main():
warnings.warn("This script is aimed to demonstrate how to convert the\n"
"JSON file to a single image dataset, and not to handle\n"
"multiple JSON files to generate a real-use dataset.")
parser = argparse.ArgumentParser()
parser.add_argument('json_file')
parser.add_argument('-o', '--out', default=None)
args = parser.parse_args()
json_file = args.json_file
if args.out is None:
out_dir = osp.basename(json_file).replace('.', '_')
out_dir = osp.join(osp.dirname(json_file), out_dir)
else:
out_dir = args.out
if not osp.exists(out_dir):
os.mkdir(out_dir)
count = os.listdir(json_file)
for i in range(0, len(count)):
path = os.path.join(json_file, count[i])
if os.path.isfile(path):
data = json.load(open(path))
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
with open(imagePath, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
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
# label_values must be dense
label_values, label_names = [], []
for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
label_values.append(lv)
label_names.append(ln)
assert label_values == list(range(len(label_values)))
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
captions = ['{}: {}'.format(lv, ln)
for ln, lv in label_name_to_value.items()]
lbl_viz = utils.draw_label(lbl, img, captions)
out_dir = osp.basename(count[i]).replace('.', '_')
out_dir = osp.join(osp.dirname(count[i]), out_dir)
if not osp.exists(out_dir):
os.mkdir(out_dir)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
#PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in label_names:
f.write(lbl_name + '\n')
warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=label_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
print('Saved to: %s' % out_dir)
if __name__ == '__main__':
main()
第三步,切換到Scripts,路徑如下
(其實可以不用切換,可以在任意路徑下,不過後續生成的文件會在此文件夾下,因爲是用的這個文件夾下的labelme_json_to_dataset.exe)
第四步,運行命令
命令格式:
labelme_json_to_dataset.exe + json文件的路徑
我的圖片標註後生成的json文件同一放在如下路徑(1.1 中有提到),如下:
D:\Data_saved\Desktop\deep_learning_data\Sample\json
所以,完成的命令如下:
labelme_json_to_dataset.exe D:\Data_saved\Desktop\deep_learning_data\Sample\json
具體文件如下圖
參考文獻
[1] https://blog.csdn.net/yql_617540298/article/details/81110685