This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathmodel_DenseNet.py
66 lines (48 loc) · 1.98 KB
/
model_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
from keras.models import Model
from keras.layers import Input, Conv3D, Dense, BatchNormalization, Concatenate, Dropout, AveragePooling3D, GlobalAveragePooling3D, Activation
from keras.optimizers import Adam
from config import *
def bn_relu_conv(x, filters, kernel_size=(3, 3, 3)):
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv3D(filters=filters, kernel_size=kernel_size, padding='same')(x)
return x
def dense_block(x):
print('dense block')
print(x.get_shape())
for _ in range(DENSE_NET_BLOCK_LAYERS):
y = x
if DENSE_NET_ENABLE_BOTTLENETCK:
y = bn_relu_conv(y, filters=DENSE_NET_GROWTH_RATE, kernel_size=(1, 1, 1))
y = bn_relu_conv(y, filters=DENSE_NET_GROWTH_RATE, kernel_size=(3, 3, 3))
x = Concatenate(axis=4)([x, y])
print(x.get_shape())
return x
def transition_block(x):
print('transition block')
print(x.get_shape())
filters = x.get_shape()[4].value
filters = int(filters * DENSE_NET_TRANSITION_COMPRESSION)
x = Conv3D(filters=filters, kernel_size=(1, 1, 1), padding='same')(x)
x = AveragePooling3D(pool_size=(2, 2, 2), padding='same')(x)
print(x.get_shape())
return x
def get_DenseNet_classifier():
inputs = Input((CLASSIFY_INPUT_WIDTH, CLASSIFY_INPUT_HEIGHT, CLASSIFY_INPUT_DEPTH, CLASSIFY_INPUT_CHANNEL))
x = Conv3D(DENSE_NET_INITIAL_CONV_DIM, (3, 3, 3), padding='same')(inputs)
print('input')
print(x.get_shape())
for i in range(DENSE_NET_BLOCKS):
x = dense_block(x)
if i != DENSE_NET_BLOCKS - 1:
x = transition_block(x)
print('top')
x = GlobalAveragePooling3D()(x)
print(x.get_shape())
if DENSE_NET_ENABLE_DROPOUT:
x = Dropout(DENSE_NET_DROPOUT)(x)
x = Dense(2, activation='softmax')(x)
print(x.get_shape())
model = Model(inputs=inputs, outputs=x)
model.compile(optimizer=Adam(lr=TRAIN_CLASSIFY_LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy'])
return model