forked from coursat-ai/MultiCheXNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTL_model.py
140 lines (115 loc) · 5.34 KB
/
MTL_model.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from .utils.ModelBlock import ModelBlock
from .utils.Encoder import Encoder
from .utils.Detector import Detector
from .utils.Segmenter import Segmenter
from .utils.Classifier import Classifier
from .utils.COVID_Classifier import COVID_Classifier
from .utils.NIH_Classifier import Classifier as NIH_classifier
class MTL_model():
def __init__(self,dim=(256,256),add_class_head=True,add_detector_head=True,
add_segmenter_head=True,classifier_type="MTL"):
img_size = 256
n_classes = 1
self.encoder = Encoder(weights=None)
self.encoder_num_layers = len(self.encoder.model.layers)
self.add_class_head=add_class_head
self.add_detector_head=add_detector_head
self.add_segmenter_head=add_segmenter_head
heads = []
if self.add_class_head:
if classifier_type=="MTL":
self.classifier = Classifier(self.encoder)
if classifier_type == "COVID":
self.classifier = COVID_Classifier(self.encoder)
if classifier_type == "NIH":
self.classifier = NIH_classifier(self.encoder)
heads.append(self.classifier)
if self.add_detector_head:
self.detector = Detector(self.encoder, img_size, n_classes)
heads.append(self.detector)
if self.add_segmenter_head:
self.segmenter = Segmenter(self.encoder)
heads.append(self.segmenter)
if self.add_class_head and self.add_detector_head and self.add_segmenter_head:
self.classification_layers = [504,507,510,513,516]
self.detector_layers = [505,508,511,514,517]
self.segmenter_layers = sorted(list((set(range(427,519)) - set(self.classification_layers) - set(self.detector_layers))))
self.MTL_model = ModelBlock.add_heads(self.encoder, heads)
if int(self.add_class_head)+int(self.add_detector_head)+int(self.add_segmenter_head) ==1:
if self.add_class_head:
self.classification_layers =list(range(self.encoder_num_layers,len(self.MTL_model.layers)))
if self.add_detector_head:
self.detector_layers =list(range(self.encoder_num_layers,len(self.MTL_model.layers)))
if self.add_segmenter_head:
self.segmenter_layers =list(range(self.encoder_num_layers,len(self.MTL_model.layers)))
def get_MTL_loss(self,classification_loss=None,detector_loss=None,segmenter_loss=None):
combined_losses = []
if self.add_class_head:
if classification_loss!=None:
classification_loss = classification_loss
else:
classification_loss = self.classifier.loss
combined_losses.append(classification_loss)
if self.add_detector_head:
if detector_loss!=None:
detector_loss= detector_loss
else:
detector_loss = self.detector.loss
combined_losses.append(detector_loss)
if self.add_segmenter_head:
if segmenter_loss!=None:
segmenter_loss = segmenter_loss
else:
segmenter_loss = self.segmenter.loss
combined_losses.append(segmenter_loss)
if len(combined_losses)==1:
combined_losses = combined_losses[0]
return combined_losses
def load_weights(mtl_clss,weight_path, weight_part ,source, skip_end=0):
"""
mtl_clss:
MTL_class to load weights to
weight_part:
detector
segmenter
classifier
encoder
source:
detector
segmenter
classifier
MTL
numpy
"""
if source=='detector':
other_model=MTL_model(add_class_head=False,add_detector_head=True,add_segmenter_head=False)
other_model.MTL_model.load_weights(weight_path)
elif source=='segmenter':
other_model=MTL_model(add_class_head=False,add_detector_head=False,add_segmenter_head=True)
other_model.MTL_model.load_weights(weight_path)
elif source=='classifier':
other_model=MTL_model(add_class_head=True,add_detector_head=False,add_segmenter_head=False)
other_model.MTL_model.load_weights(weight_path)
elif source== 'MTL':
other_model=MTL_model()
other_model.MTL_model.load_weights(weight_path)
if source=='MTL':
if weight_part=='detector':
other_model_layers=other_model.detector_layers
this_model_layers = mtl_clss.detector_layers
if weight_part=='segmenter':
other_model_layers=other_model.segmenter_layers
this_model_layers=mtl_clss.segmenter_layers
if weight_part=='classifier':
other_model_layers=other_model.classification_layers
this_model_layers= mtl_clss.classification_layers
if weight_part=='encoder':
other_model_layers = list(range(0,other_model.encoder_num_layers))
this_model_layers = list(range(0,mtl_clss.encoder_num_layers))
if skip_end>0:
other_model_layers=other_model_layers[:-skip_end]
this_model_layers=this_model_layers[:-skip_end]
for index_to , index_from in zip(this_model_layers,other_model_layers):
print(index_to,index_from)
mtl_clss.MTL_model.layers[index_to].set_weights(other_model.MTL_model.layers[index_from].get_weights())
return mtl_clss