如何在张量流中为 ssd 网络加载和保存 Widerface 数据集?

How to load and save Widerface dataset for ssd network in tensorflow?

我想在 tensorflow 中为 ssd(单发多框检测器)网络加载和保存 Widerface 标签,但是 wider_face_train_bbx_gt 太复杂了。

如何在tensorflow中保存ssd网络的标签?

要加载数据集,我会向您解释如何在 TensorFlow 中使用 TensorPack(仅针对数据)执行此操作。

首先,我们需要包含边界框的 zip 文件和 mat 文件。以下部分基本上直接从zip文件和mat文件中读取

class RawWiderFaceReader(RNGDataFlow):
    """Read images directly from tar file without unpacking
    boxes: left, top, width, height
    """
    def __init__(self, matfile, zipfile):
        super(RawWiderFaceReader, self).__init__()
        self.matfile = matfile
        self.zipfile = zipfile
        self.subset = matfile.split('_')[-1].replace('.mat', '')
        f = sio.loadmat(matfile)
        events = [f['event_list'][i][0][0] for i in range(len(f['event_list']))]
        raw_files = [f['file_list'][i][0] for i in range(len(f['file_list']))]
        raw_bbx = [f['face_bbx_list'][i][0] for i in range(len(f['face_bbx_list']))]

        col_files = []
        for file, bbx in zip(raw_files, raw_bbx):
            for filee, bbxe in zip(file, bbx):
                col_files.append((filee[0][0], bbxe[0]))

        self.col_files2 = []
        for file, bbx in col_files:
            for ev in events:
                if file.startswith(ev.replace('--', '_')):
                    self.col_files2.append((str('WIDER_%s/images/' % self.subset + ev +
                                           '/' + file + '.jpg').encode('ascii', 'ignore'), bbx))
                    break

    def get_data(self):
        with ZipFile(self.zipfile, 'r') as zip_hnd:
            for fn, bbx in self.col_files2:
                buf = zip_hnd.read('%s' % fn)
                yield [buf, bbx]

它为您提供了一个生成器 get_data(),其中 returns jpeg 编码图像和边界框。它的存储方式似乎很复杂,因为它是一个包含 Matlab 生成的边界框的文件。 要绘制边界框,您可以使用:

def draw_rect(img, top, left, bottom, right, rgb, margin=1):
    m = margin
    r, g, b = rgb
    img[top:bottom, left - m:left + m, 0] = r
    img[top:bottom, left - m:left + m, 1] = g
    img[top:bottom, left - m:left + m, 2] = b

    img[top:bottom, right - m:right + m, 0] = r
    img[top:bottom, right - m:right + m, 1] = g
    img[top:bottom, right - m:right + m, 2] = b

    img[top - m:top + m, left:right, 0] = r
    img[top - m:top + m, left:right, 1] = g
    img[top - m:top + m, left:right, 2] = b

    img[bottom - m:bottom + m, left:right, 0] = r
    img[bottom - m:bottom + m, left:right, 1] = g
    img[bottom - m:bottom + m, left:right, 2] = b

    return img

整个脚本在这里: https://gist.github.com/PatWie/a743d2349f388b27ed3ef783919c3882

pip install -U git+https://github.com/ppwwyyxx/tensorpack.git 之后,您可以通过

启动它
python data_sampler.py --zip /scratch/patwie/data/wider_face/WIDER_val.zip \
                       --mat wider_face_split/wider_face_val.mat \
                       --debug

要将其转换为 lmdb 文件,您可以使用其他参数。此处无需解压数据。

要使用数据,就像在脚本中:

from tensorpack import *
ds = LMDBDataPoint('/scratch/wieschol/data/wider_face/WIDER_train.lmdb', shuffle=True)
ds = RawWiderFaceReader(matfile=args.mat, zipfile=args.zip)
ds.reset_state()
for jpeg, bbx in ds.get_data():
    rgb = cv2.imdecode(np.asarray(jpeg), cv2.IMREAD_COLOR)