-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
54 lines (46 loc) · 1.54 KB
/
utils.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
import os
import numpy as np
def calcu_dice(Y_pred, Y_gt, K=255):
"""
calculate two input dice value
:param Y_pred:
:param Y_gt:
:param K:
:return:
"""
intersection = 2 * np.sum(Y_pred[Y_gt == K])
denominator = np.sum(Y_pred) + np.sum(Y_gt)
loss = (intersection / denominator)
return loss
def file_name_path(file_dir, dir=True, file=False):
"""
get root path,sub_dirs,all_sub_files
:param file_dir:
:return:
"""
for root, dirs, files in os.walk(file_dir):
if len(dirs) and dir:
print("sub_dirs:", dirs)
return dirs
if len(files) and file:
print("files:", files)
return files
def save_file2csv(file_dir, file_name):
"""
save file path to csv
:param file_dir:preprocess data path
:param file_name:output csv name
:return:
"""
out = open(file_name, 'w')
image = "Image"
mask = "Mask"
file_image_dir = file_dir + "/" + image
file_mask_dir = file_dir + "/" + mask
file_paths = file_name_path(file_image_dir, dir=False, file=True)
out.writelines("Image,Mask" + "\n")
for index in range(len(file_paths)):
out_file_image_path = file_image_dir + "/" + file_paths[index]
out_file_mask_path = file_mask_dir + "/" + file_paths[index]
out.writelines(out_file_image_path + "," + out_file_mask_path + "\n")
save_file2csv("/storage/research/Intern19_v2/KidneyTumorSegmentationChallenge/scratch/train", "trainnpy.csv")