我想实现一个语义分割网络,并用PASCAL VOC 12训练它。基本事实被编码成颜色而不是标签,我正在寻找将其转换为标签的方法。此外,我还阅读了以下链接:
Tensorflow: How to create a Pascal VOC style image
有没有更多的用户朋友代码或算法来解决我的问题?
发布于 2019-03-06 07:58:16
我刚刚在PASCAL VOC数据集上遇到了同样的问题。然后,我检查了Deeplab中TensorFlow代码中的函数deeplab/datasets/remove_gt_colormap.py,用于加载和转换分割标签。
from PIL import Image
import tensorflow as tf
import cv2
def _remove_colormap_deeplab(filename):
"""Removes the color map from the annotation.
Args:
filename: Ground truth annotation filename.
Returns:
Annotation without the color map.
"""
return np.array(Image.open(filename))
def _save_annotation_deeplab(annotation, filename):
"""Saves the annotation as png file.
Args:
annotation: Segmentation annotation.
filename: Output filename.
"""
pil_image = Image.fromarray(annotation.astype(dtype=np.uint8))
with tf.gfile.Open(filename, mode='w') as f:
pil_image.save(f, 'PNG')
据我所知(,但不是100%确定的),np.array(Image.open(filename))
会根据加载的Pillow Image对象的mode将Pillow Image对象转换为numpy数组。这里我检查了加载的Pascal VOC标签/类图像,例如datasets/pascal_voc_seg/VOCdevkit/VOC2012/SegmentationClass/2011_003078.png
,是否具有模式P: palette (意思是8位像素,使用调色板映射到任何其他模式)。因此,由于'P' mode
,将此PIL Image对象转换为numpy数组,会将每个像素值映射到索引(即,对应于PASCAL VOC中定义的class or label
)到256个RGB值的调色板数组中。
您也可以查看我的jupyter note以了解详细信息。
发布于 2017-09-19 13:59:44
在PASCAL VOC 12中有21个类别- 20个对象和1个背景。类被编码为像素值。例如,属于背景的像素的值为0
。其余的类按照字母顺序从1
到20
进行编码。例如,类aeroplane
的像素值等于1
。在每个图像中,您可以有多个类。因此,如果您想要获取类标签,只需使用OpenCV或PIL读取相应的groundtruth图像,并找到图像中存在的不同像素值。像素值将为您提供图像中存在的对象类。在任何图像中,都不会有超过3到4个不同的类。但是,同一个类可以有多个实例。
发布于 2020-02-06 01:34:52
它是@Harsh和@ccj5351的组合。Tensorflow's approach标签被嵌入到图像中,这意味着你所要做的就是读取它们并识别像素值。它可以像使用np.unique(img)
一样简单,它将返回数组中的唯一值。如果它返回4个唯一值,那么就有4个类(包括背景)。你还必须首先删除色彩映射表。看看ccj5351,它可能会有所帮助。
https://stackoverflow.com/questions/46289423
复制相似问题