如果您想使用Pascal Context数据集,请安装Detail,然后运行以下命令将注释转换为正确的格式。
1.安装Detail
进入项目终端
#即 这是在我自己的项目下直接进行克隆操作:
git clone https://github.com/zhanghang1989/detail-api.git $PASCAL_CTX
# 获得detail_api
若是出现下面的问题可以手动下载detail-api的压缩包文件到项目中,再进行解压.
我的就是git时候出了问题,然后手动下载的,服务器有时候也不稳定。
5、进行detail_api文件夹的PythonAPI中
cd 你的路径/PythonAPI
然后python setup.py install
可能没有Cython
直接用pip install Cython
再跑python setup.py install
2.格式转换
Pascal Context的训练和验证集可以从这里下
要从原始数据集中分离训练和验证集,您可以从此处下载trainval_merged. json。下载链接https://codalabuser.blob.core.windows.net/public/trainval_merged.json
python tools/convert_datasets/pascal_context.py data/VOCdevkit data/VOCdevkit/VOC2010/trainval_merged.json
其中pascal_context.py如下
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import os.path as osp
from functools import partialimport mmcv
import numpy as np
from detail import Detail
from PIL import Image_mapping = np.sort(np.array([0, 2, 259, 260, 415, 324, 9, 258, 144, 18, 19, 22, 23, 397, 25, 284,158, 159, 416, 33, 162, 420, 454, 295, 296, 427, 44, 45, 46, 308, 59,440, 445, 31, 232, 65, 354, 424, 68, 326, 72, 458, 34, 207, 80, 355,85, 347, 220, 349, 360, 98, 187, 104, 105, 366, 189, 368, 113, 115]))
_key = np.array(range(len(_mapping))).astype('uint8')def generate_labels(img_id, detail, out_dir):def _class_to_index(mask, _mapping, _key):# assert the valuesvalues = np.unique(mask)for i in range(len(values)):assert (values[i] in _mapping)index = np.digitize(mask.ravel(), _mapping, right=True)return _key[index].reshape(mask.shape)mask = Image.fromarray(_class_to_index(detail.getMask(img_id), _mapping=_mapping, _key=_key))filename = img_id['file_name']mask.save(osp.join(out_dir, filename.replace('jpg', 'png')))return osp.splitext(osp.basename(filename))[0]def parse_args():parser = argparse.ArgumentParser(description='Convert PASCAL VOC annotations to mmsegmentation format')parser.add_argument('devkit_path', help='pascal voc devkit path')parser.add_argument('json_path', help='annoation json filepath')parser.add_argument('-o', '--out_dir', help='output path')args = parser.parse_args()return argsdef main():args = parse_args()devkit_path = args.devkit_pathif args.out_dir is None:out_dir = osp.join(devkit_path, 'VOC2010', 'SegmentationClassContext')else:out_dir = args.out_dirjson_path = args.json_pathmmcv.mkdir_or_exist(out_dir)img_dir = osp.join(devkit_path, 'VOC2010', 'JPEGImages')train_detail = Detail(json_path, img_dir, 'train')train_ids = train_detail.getImgs()val_detail = Detail(json_path, img_dir, 'val')val_ids = val_detail.getImgs()mmcv.mkdir_or_exist(osp.join(devkit_path, 'VOC2010/ImageSets/SegmentationContext'))train_list = mmcv.track_progress(partial(generate_labels, detail=train_detail, out_dir=out_dir),train_ids)with open(osp.join(devkit_path, 'VOC2010/ImageSets/SegmentationContext','train.txt'), 'w') as f:f.writelines(line + '\n' for line in sorted(train_list))val_list = mmcv.track_progress(partial(generate_labels, detail=val_detail, out_dir=out_dir), val_ids)with open(osp.join(devkit_path, 'VOC2010/ImageSets/SegmentationContext','val.txt'), 'w') as f:f.writelines(line + '\n' for line in sorted(val_list))print('Done!')if __name__ == '__main__':main()
已经在转换啦,慢慢等待就好,可以干点其他的,或者浅休息一下。
two years later...