a b/Main.py
1
import os
2
from Segmenation import BreastSeg,BreastTumor
3
from ImageProcessing import read_image,Norm_Zscore,imgnorm,save_image,labeling_seg
4
import argparse
5
import torch
6
try:
7
    torch._utils._rebuild_tensor_v2
8
except AttributeError:
9
    def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks):
10
        tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride)
11
        tensor.requires_grad = requires_grad
12
        tensor._backward_hooks = backward_hooks
13
        return tensor
14
    torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2
15
    
16
    
17
parser = argparse.ArgumentParser(description='Tumor Segmentation from DCE-MRI')
18
parser.add_argument('--cuda', type=int, default='1', required=False, help='Run in GPU')
19
parser.add_argument('--pre', type=str, default='Data/Img1_pre.nii.gz', required=False, help='Image path for pre-constrast image')
20
parser.add_argument('--post', type=str, default='Data/Img1_post.nii.gz', required=False, help='Image path for post-constrast image')
21
parser.add_argument('--outfolder',type=str,default='Results',required=False,help='Folder for saving results')
22
opt = parser.parse_args()
23
print(opt)
24
25
if opt.cuda and not torch.cuda.is_available():
26
    raise Exception("No GPU found, please run without --cuda 0")
27
28
29
30
savepath = opt.outfolder
31
32
if not os.path.exists(savepath):
33
    os.makedirs(savepath)
34
35
# read images and output spacing information   
36
print('Reading images')      
37
img_pre, img_post, img_sub, scale_subject = read_image(opt)
38
39
# perform the intensity normalization 1) remover outliers 2) Z-score normalization 
40
# spatial normalization will be done before segmentation in the functions of BrestSeg and BreastTumor
41
print('Performing image normalization')
42
img_pre  = Norm_Zscore(imgnorm(img_pre))
43
img_post = Norm_Zscore(imgnorm(img_post))
44
img_sub  = Norm_Zscore(imgnorm(img_sub))
45
46
# read models
47
# the weights of the models will be loaded in the function of BrestSeg and BreastTumor
48
from Models_3D import ModelBreast,ModelTumor
49
50
51
if opt.cuda:
52
    model_breast = ModelBreast(1,1).cuda()
53
else:
54
    model_breast = ModelBreast(1,1)
55
if opt.cuda:
56
    model_tumor = ModelTumor(3,1).cuda()
57
else:
58
    model_tumor = ModelTumor(3,1)
59
    
60
    
61
# perform image segmentation    
62
print('Performing image segmentation')
63
breast_mask = BreastSeg(img_pre,scale_subject,model_breast,opt)
64
prob_1st, seg_2nd = BreastTumor(img_sub,img_post,breast_mask,scale_subject,model_tumor,model_tumor,opt)
65
66
# perform labeling
67
labeled_image = labeling_seg(seg_2nd)
68
69
# save images
70
print('Saving segmentations')
71
save_image(breast_mask,opt,'breast_mask.nii.gz')
72
save_image(prob_1st,opt,'prob_1st.nii.gz')
73
save_image(labeled_image,opt,'seg_2nd.nii.gz')
74
print('Please check the segmentation results in the result folder ---> "Results"')
75
76
77
78
79
80