-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdensenet.py
132 lines (106 loc) · 4.58 KB
/
densenet.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
from keras.layers import (Conv3D, BatchNormalization, AveragePooling3D, concatenate, Lambda,
Activation, Input, GlobalAvgPool3D, Dense)
from keras.regularizers import l2 as l2_penalty
from keras.models import Model
from mylib.models.metrics import invasion_acc, invasion_precision, invasion_recall, invasion_fmeasure
PARAMS = {
'activation': lambda: Activation('relu'), # the activation functions
'bn_scale': True, # whether to use the scale function in BN
'weight_decay': 0., # l2 weight decay
'kernel_initializer': 'he_uniform', # initialization
'first_scale': lambda x: x / 128. - 1., # the first pre-processing function
'dhw': [32, 32, 32], # the input shape
'k': 16, # the `growth rate` in DenseNet
'bottleneck': 4, # the `bottleneck` in DenseNet
'compression': 2, # the `compression` in DenseNet
'first_layer': 32, # the channel of the first layer
'down_structure': [4, 4, 4], # the down-sample structure
'output_size': 3 # the output number of the classification head
}
def _conv_block(x, filters):
bn_scale = PARAMS['bn_scale']
activation = PARAMS['activation']
kernel_initializer = PARAMS['kernel_initializer']
weight_decay = PARAMS['weight_decay']
bottleneck = PARAMS['bottleneck']
x = BatchNormalization(scale=bn_scale, axis=-1)(x)
x = activation()(x)
x = Conv3D(filters * bottleneck, kernel_size=(1, 1, 1), padding='same', use_bias=False,
kernel_initializer=kernel_initializer,
kernel_regularizer=l2_penalty(weight_decay))(x)
x = BatchNormalization(scale=bn_scale, axis=-1)(x)
x = activation()(x)
x = Conv3D(filters, kernel_size=(3, 3, 3), padding='same', use_bias=True,
kernel_initializer=kernel_initializer,
kernel_regularizer=l2_penalty(weight_decay))(x)
return x
def _dense_block(x, n):
k = PARAMS['k']
for _ in range(n):
conv = _conv_block(x, k)
x = concatenate([conv, x], axis=-1)
return x
def _transmit_block(x, is_last):
bn_scale = PARAMS['bn_scale']
activation = PARAMS['activation']
kernel_initializer = PARAMS['kernel_initializer']
weight_decay = PARAMS['weight_decay']
compression = PARAMS['compression']
x = BatchNormalization(scale=bn_scale, axis=-1)(x)
x = activation()(x)
if is_last:
x = GlobalAvgPool3D()(x)
else:
*_, f = x.get_shape().as_list()
x = Conv3D(f // compression, kernel_size=(1, 1, 1), padding='same', use_bias=True,
kernel_initializer=kernel_initializer,
kernel_regularizer=l2_penalty(weight_decay))(x)
x = AveragePooling3D((2, 2, 2), padding='valid')(x)
return x
def get_model(weights=None, **kwargs):
for k, v in kwargs.items():
assert k in PARAMS
PARAMS[k] = v
print("Model hyper-parameters:", PARAMS)
dhw = PARAMS['dhw']
first_scale = PARAMS['first_scale']
first_layer = PARAMS['first_layer']
kernel_initializer = PARAMS['kernel_initializer']
weight_decay = PARAMS['weight_decay']
down_structure = PARAMS['down_structure']
output_size = PARAMS['output_size']
shape = dhw + [1]
inputs = Input(shape=shape)
if first_scale is not None:
scaled = Lambda(first_scale)(inputs)
else:
scaled = inputs
conv = Conv3D(first_layer, kernel_size=(3, 3, 3), padding='same', use_bias=True,
kernel_initializer=kernel_initializer,
kernel_regularizer=l2_penalty(weight_decay))(scaled)
downsample_times = len(down_structure)
for l, n in enumerate(down_structure):
db = _dense_block(conv, n)
conv = _transmit_block(db, l == downsample_times - 1)
if output_size == 1:
last_activation = 'sigmoid'
else:
last_activation = 'softmax'
outputs = Dense(output_size, activation=last_activation,
kernel_regularizer=l2_penalty(weight_decay),
kernel_initializer=kernel_initializer)(conv)
model = Model(inputs, outputs)
model.summary()
if weights is not None:
model.load_weights(weights, by_name=True)
return model
def get_compiled(loss='categorical_crossentropy', optimizer='adam',
metrics=["categorical_accuracy", invasion_acc,
invasion_precision, invasion_recall, invasion_fmeasure],
weights=None, **kwargs):
model = get_model(weights=weights, **kwargs)
model.compile(loss=loss, optimizer=optimizer,
metrics=[loss] + metrics)
return model
if __name__ == '__main__':
model = get_compiled()