-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_json.py
63 lines (41 loc) · 1.37 KB
/
create_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# --------------------------------------------------------
# Copyright (C) 2021 NVIDIA Corporation. All rights reserved.
# Nvidia Source Code License-NC
# Transformer Pretraining Code: Yucheng, Vishwesh, Ali
# --------------------------------------------------------
import os
import numpy as np
from numpy.random import randint
from PIL import Image
import nibabel as nb
import json
# Generate JSON file
import os
traindir = './train/images'
valdir = './validation/images'
json_file = './renalseg.json'
sublist = [s for s in os.listdir(traindir)]
allnum = len(sublist)
datadict = {}
datadict['training'] = []
datadict['validation'] = []
for f in sublist:
ifile = "train/images/" + f
t_dict = {"image": '', 'label':''}
t_dict['image'] = ifile
f_segname = f.split('.nii.gz')[0] + '_seg.nii.gz'
ilabel = "train/labels/" + f_segname
t_dict['label'] = ilabel
datadict['training'].append(t_dict)
sublist = [s for s in os.listdir(valdir)]
allnum = len(sublist)
for f in sublist:
ifile = "validation/images/" + f
t_dict = {"image": '', 'label':''}
t_dict['image'] = ifile
f_segname = f.split('.nii.gz')[0] + '_seg.nii.gz'
ilabel = "validation/labels/" + f_segname
t_dict['label'] = ilabel
datadict['validation'].append(t_dict)
with open(json_file, 'w') as f:
json.dump(datadict, f, indent=4, sort_keys=True)