下面是分离数据集的代码:
"""
@ Date: 2020/6/29
@ Author: Xiao Zhuo
@ Brief: Split CHAOS DataSet into my directory
@ Filename: split\_dataset\_1.py
"""
# -\*- coding: utf-8 -\*-
import os
import shutil
import random
dst_TrainData = "./data/train/Data"
dst_TrainGround = "./data/train/Ground"
dst_TestData = "./data/val/Data"
dst_TestGround = "./data/val/Ground"
def collect\_T1\_name(patient_dir):
ground_paths = list()
inphase_paths = list()
t1_datadir = os.path.join(patient_dir, "T1DUAL")
ground_dir = os.path.join(t1_datadir, "Ground")
ground_names = os.listdir(ground_dir)
nums_ground = len(ground_names)
# 拼接Ground文件夹的文件,存入到ground\_paths列表中
for i in range(nums_ground):
ground_paths.append(os.path.join(ground_dir, ground_names[i]))
inphase_dir = os.path.join(t1_datadir, "DICOM\_anon", "InPhase")
inphase_names = os.listdir(inphase_dir)
nums_inphase = len(inphase_names)
# 拼接inphase文件夹的文件,存入到inphase\_paths列表中
for i in range(nums_inphase):
inphase_paths.append(os.path.join(inphase_dir, inphase_names[i]))
return ground_paths, inphase_paths
if __name__ == '\_\_main\_\_':
dataset_dir = os.path.join("CHAOS\_Train\_Sets", "Train\_Sets", "MR")
train_pct = 0.8
test_pct = 0.2
for root, dirs, files in os.walk(dataset_dir):
random.shuffle(dirs)
dir_count = len(dirs)
train_point = int(dir_count \* train_pct)
i = 0
for sub_dir in dirs: # sub\_dir代表病人编号
if i < train_point:
patient_dir = os.path.join(root, sub_dir)
ground_paths, inphase_paths = collect_T1_name(patient_dir)
for num in range(len(ground_paths)):
dst_groundpath = os.path.join(dst_TrainGround, "T1\_Patient%s\_No%d.png" % (sub_dir, num))
shutil.copy(ground_paths[num], dst_groundpath)
## 下面待修改
for num in range(len(inphase_paths)):
dst_inphasepath = os.path.join(dst_TrainData, "T1\_Patient%s\_No%d.dcm" % (sub_dir, num))
shutil.copy(inphase_paths[num], dst_inphasepath)
i += 1
else:
patient_dir = os.path.join(root, sub_dir)
ground_paths, inphase_paths = collect_T1_name(patient_dir)
for num in range(len(ground_paths)):
dst_groundpath = os.path.join(dst_TestGround, "T1\_Patient%s\_No%d.png" % (sub_dir, num))
shutil.copy(ground_paths[num], dst_groundpath)
for num in range(len(inphase_paths)):
dst_inphasepath = os.path.join(dst_TestData, "T1\_Patient%s\_No%d.dcm" % (sub_dir, num))
shutil.