-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodel.py
113 lines (100 loc) · 3.19 KB
/
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
from keras.initializers import Constant
from keras.layers import Input, Conv2D, Flatten, Activation, MaxPool2D, Dropout
from keras.models import Model
from focal_loss import focal_loss
img_width, img_height = 160, 160
loss_dict = {
"out_cancer": focal_loss(),
"out_compos": focal_loss(),
"out_echo": focal_loss(),
"out_shape": focal_loss(),
"out_calcs": focal_loss(),
"out_margin": focal_loss(),
}
loss_weights_dict = {
"out_cancer": 1.0,
"out_compos": 1.0,
"out_echo": 1.0,
"out_shape": 1.0,
"out_calcs": 1.0,
"out_margin": 1.0,
}
def multitask_cnn():
# 160x160x1
input_tensor = Input(shape=(img_height, img_width, 1), name="thyroid_input")
# 160x160x8
x = Conv2D(8, (3, 3), padding="same", activation="relu")(input_tensor)
# 80x80x8
x = MaxPool2D((2, 2), strides=(2, 2))(x)
# 80x80x12
x = Conv2D(12, (3, 3), padding="same", activation="relu")(x)
# 40x40x12
x = MaxPool2D((2, 2), strides=(2, 2))(x)
# 40x40x16
x = Conv2D(16, (3, 3), padding="same", activation="relu")(x)
# 20x20x16
x = MaxPool2D((2, 2), strides=(2, 2))(x)
# 20x20x24
x = Conv2D(24, (3, 3), padding="same", activation="relu")(x)
# 10x10x24
x = MaxPool2D((2, 2), strides=(2, 2))(x)
# 10x10x32
x = Conv2D(32, (3, 3), padding="same", activation="relu")(x)
# 5x5x32
x = MaxPool2D((2, 2), strides=(2, 2))(x)
# 5x5x48
x = Conv2D(48, (3, 3), padding="same", activation="relu")(x)
# 5x5x48
x = Dropout(0.5)(x)
y_cancer = Conv2D(
filters=1,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_cancer = Flatten()(y_cancer)
y_cancer = Activation("sigmoid", name="out_cancer")(y_cancer)
y_compos = Conv2D(
filters=5,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_compos = Flatten()(y_compos)
y_compos = Activation("softmax", name="out_compos")(y_compos)
y_echo = Conv2D(
filters=5,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_echo = Flatten()(y_echo)
y_echo = Activation("softmax", name="out_echo")(y_echo)
y_shape = Conv2D(
filters=1,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_shape = Flatten()(y_shape)
y_shape = Activation("sigmoid", name="out_shape")(y_shape)
y_calcs = Conv2D(
filters=5,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_calcs = Flatten()(y_calcs)
y_calcs = Activation("softmax", name="out_calcs")(y_calcs)
y_margin = Conv2D(
filters=4,
kernel_size=(5, 5),
kernel_initializer="glorot_normal",
bias_initializer=Constant(value=-0.9),
)(x)
y_margin = Flatten()(y_margin)
y_margin = Activation("softmax", name="out_margin")(y_margin)
return Model(
inputs=[input_tensor],
outputs=[y_cancer, y_compos, y_echo, y_shape, y_calcs, y_margin],
)