labelme标注遇到的常见问题

本文记录了使用labelme进行多分类图像分割时遇到的问题,包括批量转换json文件生成黑色label、图像位深问题以及相同类别label颜色不一致等。作者提供了修改后的代码示例,解决了这些问题,并提醒注意labelme版本差异。此外,还提到了可能出现的导入错误及其解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用labelme做多分类分割数据集(遇到的各种问题和解决办法)

这两天为了给医学图像做多分类分割又折腾了很久的labelme,因为之前做的是简单的二分类问题,而且也是瞎捣鼓的能解决当时的问题就行了。这次却遇到了很多之前没遇到的问题,也了解了一些东西,特此记录下来因为我感觉我睡一晚上就会忘了。

关于labelme的版本问题

关于labelme安装过程,这里就不详细讲了基本上所有相关博客都有讲。3.xx和4.xx的版本主要在标注的显示和生成的json文件上有差别。
version-3.18
version-3.16.5
在这里插入图片描述
version-4.5.9

在这里插入图片描述

  1. List item

在这里插入图片描述

批量转换json、生成的label一片黑、图像位深不是8bit以及多分类中相同类别label颜色不一致

在我使用的过程中,感觉高版本中网上的批量转换json文件的json_to_dataset.py,生成的label是黑的,位深是16位,当然应该是可调的,全黑因为mask填充的像素值很低基本看不出来。算了不讲废话了我的最终程序如下:
下面展示一些 内联代码片

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])
        filename = count[i][:-5]
        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_name_to_value = {'_background_': 0,
                                   'right': 1,	
                                   'left': 2}

            # 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()

labelme本身没有注意到生成标签一一对应的问题,一开始的时候我还在draw.py里面去该颜色的映射关系后来发现直接把label_name_to_vaule这一段注释掉改成自己想要对应的标签即可。
最后的小注意,如果遇到如下报错一般要查看labelme安装路径下utils中是否缺少draw.py,没有需要补上,然后在同样路径下的_init_.py文件下添加导入包的代码。

ModuleNotFoundError: No module named 'labelme.utils.draw'	#报错1

AttributeError: module 'labelme.utils' has no attribute 'draw_label'	#报错2

解决报错1

import io

import numpy as np
import PIL.Image
import PIL.ImageDraw


def label_colormap(N=256):
    def bitget(byteval, idx):
        return (byteval & (1 << idx)) != 0

    cmap = np.zeros((N, 3))
    for i in range(0, N):
        id = i
        r, g, b = 0, 0, 0
        for j in range(0, 8):
            # if i == 1:
            #     r = 255;
            #     g = 255;
            #     b = 255;
            # else:
            #     r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
            #     g = np.bitwise_or(g, (bitget(id, 0) << 7 - j))
            #     b = np.bitwise_or(b, (bitget(id, 0) << 7 - j))
            r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))      # bitwise_or()函数对数组中整数的二进制形式执行位或运算
            g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))      # bitget(a,b),获取二进制数a的第b位值
            b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
            id = (id >> 3)
        cmap[i, 0] = r
        cmap[i, 1] = g
        cmap[i, 2] = b
    #
    # cmap[1, :] = [255,0,0]
    # cmap[2, :] = [0,255,0]
    cmap = cmap.astype(np.float32) / 255
    return cmap


# similar function as skimage.color.label2rgb
def label2rgb(lbl, img=None, n_labels=None, alpha=0.5, thresh_suppress=0):
    if n_labels is None:
        n_labels = len(np.unique(lbl))

    cmap = label_colormap(n_labels)
    cmap = (cmap * 255).astype(np.uint8)

    lbl_viz = cmap[lbl]
    lbl_viz[lbl == -1] = (0, 0, 0)  # unlabeled

    if img is not None:
        img_gray = PIL.Image.fromarray(img).convert('LA')
        img_gray = np.asarray(img_gray.convert('RGB'))
        # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
        lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray
        lbl_viz = lbl_viz.astype(np.uint8)

    return lbl_viz


def draw_label(label, img=None, label_names=None, colormap=None):
    import matplotlib.pyplot as plt
    backend_org = plt.rcParams['backend']
    plt.switch_backend('agg')

    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 label_names is None:
        label_names = [str(l) for l in range(label.max() + 1)]

    if colormap is None:
        colormap = label_colormap(len(label_names))

    label_viz = label2rgb(label, img, n_labels=len(label_names))
    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('{value}: {name}'
                          .format(value=label_value, name=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()

    plt.switch_backend(backend_org)

    out_size = (label_viz.shape[1], label_viz.shape[0])
    out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
    out = np.asarray(out)
    return out

解决报错2

from .draw import label_colormap
from .draw import label2rgb
from .draw import draw_label
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值