nnUnet如何生成自己数据集的json文件


前言

最近在学习nnUnet这个框架,想用在自己的数据集上,所以就github了一番,这里做个记录。

一、nnUnet安装

nnUNet官方地址如下:
https://github.com/MIC-DKFZ/nnUNet

开始之前,请务必参考如下博客:
(四:2020.07.28)nnUNet最舒服的训练教程(让我的奶奶也会用nnUNet(上))(9.07更新)

二、具体步骤

请准备好你的train数据,和对应的label文件,要求都为.nii.gz格式。请创建一个空的如:Task66_liver的文件夹,然后执行下面代码,推荐使用jupyter notebook

1、导入相应的库

import glob
import os
import re
import json
from collections import OrderedDict

2、请创建一个空的Task66_liver文件夹,

#将YOUR DIR替换成你自己的目录
path_originalData = "/YOUR_DIR/datasets/Task66_liver/"

os.mkdir(path_originalData+"imagesTr/")
os.mkdir(path_originalData+"labelsTr/")
os.mkdir(path_originalData+"imagesTs/")
os.mkdir(path_originalData+"labelsTs/")

3、下面是一个list的函数


def list_sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """   
    def tryint(s):
        try:
            return int(s)
        except:
            return s
    def alphanum_key(s):
        """ Turn a string into a list of string and number chunks.
            "z23a" -> ["z", 23, "a"]
        """
        return [ tryint(c) for c in re.split('([0-9]+)', s) ]
    l.sort(key=alphanum_key)
    return l

4、读取目录并得到一个list


train_image = list_sort_nicely(glob.glob(path_originalData+"imagesTr/*"))
train_label = list_sort_nicely(glob.glob(path_originalData+"labelsTr/*"))
test_image = list_sort_nicely(glob.glob(path_originalData+"imagesTs/*"))
test_label = list_sort_nicely(glob.glob(path_originalData+"labelsTs/*"))

train_image = ["{}".format(patient_no.split('/')[-1]) for patient_no in train_image]
train_label = ["{}".format(patient_no.split('/')[-1]) for patient_no in train_label]
test_image = ["{}".format(patient_no.split('/')[-1]) for patient_no in test_image]
#输出一下目录的情况,看是否成功
print(len(train_image),len(train_label),len(test_image),len(test_label), train_image[0])

5、创建json文件,根据需要修改下面的某些值


#####下面是创建json文件的内容
#可以根据你的数据集,修改里面的描述
json_dict = OrderedDict()
json_dict['name'] = "liver"
json_dict['description'] = " Segmentation"
json_dict['tensorImageSize'] = "3D"
json_dict['reference'] = "see challenge website"
json_dict['licence'] = "see challenge website"
json_dict['release'] = "0.0"
#这里填入模态信息,0表示只有一个模态,还可以加入“1”:“MRI”之类的描述,详情请参考官方源码给出的示例
json_dict['modality'] = {
    "0": "CT"
}

#这里为label文件中的多个标签,比如这里有血管、胆管、结石、肿块四个标签,名字可以按需要命名
json_dict['labels'] = {
    "0": "Background",
    "1": "vessel ",#静脉血管
    "2": "bileduck",#胆管
    "3": "stone",#结石
    "4": "lump" #肿块
}

#下面部分不需要修改>>>>>>
json_dict['numTraining'] = len(train_image)
json_dict['numTest'] = len(test_image)

json_dict['training'] = []
for idx in range(len(train_image)):
    json_dict['training'].append({'image': "./imagesTr/%s" % train_image[idx], "label": "./labelsTr/%s" % train_label[idx]})

json_dict['test'] = ["./imagesTs/%s" % i for i in test_image]

with open(os.path.join(path_originalData, "dataset.json"), 'w') as f:
    json.dump(json_dict, f, indent=4, sort_keys=True)
#<<<<<<<

参考

本文主要参考了:

https://github.com/kevinkwshin/easyNNUNET/blob/master/0_NNUNET_Preprocess.ipynb

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值