前言
在使用monai.data.CacheDataset
导入数据,以及monai.data.DataLoader
时,使用json
文件组织数据。
那么假设我们的数据图像文件放在images
文件夹里面,标签数据放在labels
文件夹里面,如何生成这种格式的json
文件呢,并且训练集和验证集的比例为8:2
2023/05/05更新
import json
import os
"""
数据组织结构:
data_base_dir:
images_dir:
train
val
labels_dir:
train
val
"""
if __name__ == '__main__':
file_dict = {}
images_base_dir = "/home/data/hablee_data_dir/aorta2023/images"
labels_base_dir = "/home/data/hablee_data_dir/aorta2023/mergeFourLabel"
train_images_dir = os.path.join(images_base_dir, "train")
train_labels_dir = os.path.join(labels_base_dir, "train")
val_images_dir = os.path.join(images_base_dir, "val")
val_labels_dir = os.path.join(labels_base_dir, "val")
train_filenames = os.listdir(train_labels_dir)
val_filenames = os.listdir(val_labels_dir)
# -> train
train_content = []
for filename in train_filenames:
image_path = os.path.join(train_images_dir, filename)
label_path = os.path.join(train_labels_dir, filename)
dict_content = {"image": image_path, "label":label_path}
train_content.append(dict_content)
# -> val
val_content = []
for filename in val_filenames:
image_path = os.path.join(val_images_dir, filename)
label_path = os.path.join(val_labels_dir, filename)
dict_content = {"image": image_path, "label":label_path}
val_content.append(dict_content)
labels_contents = {"0":"background", "1":"fdm", "2": "zdm", "3":"xxg", "4":"fei" }
file_dict["labels"] = labels_contents
file_dict["train"] = train_content
file_dict["val"] = val_content
# -> save
with open("four_cls.json", "w") as f:
json.dump(file_dict, f, indent=4)
代码
import json
import os
import random
if __name__ == '__main__':
demo = False
if demo==True:
price={}
price["a"] = 24
print(price)
with open("ts.json","w") as f:
json.dump(price,f,indent=4)
filedict = {}
data_dir = "G:/d_disk/airway_datasets"
filenames = os.listdir(os.path.join(data_dir,"train_data_nii"))
training_contents,validation_contents = [], []
val_length = 0
for filename in filenames:
if val_length<(len(filenames)*0.2): # 如果验证集比例小于0.2
if random.randint(1,2) == 1: # 并且随机数为1
# 则放到验证集
dic_content = {"image":f"train_data_nii/{filename}",
"label":f"train_label_nii/{filename}"}
validation_contents.append(dic_content)
else:
# 否则放到训练集
dic_content = {"image":f"train_data_nii/{filename}",
"label":f"train_label_nii/{filename}"}
training_contents.append(dic_content)
else:
# 否则放到训练集
dic_content = {"image":f"train_data_nii/{filename}",
"label":f"train_label_nii/{filename}"}
training_contents.append(dic_content)
labels_contents = {"0":"background","1":"airway"}
filedict["labels"] = labels_contents
filedict["training"] = training_contents
filedict["validation"] = validation_contents
with open("dataset.json","w") as f:
json.dump(filedict,f,indent=4)
完成!