-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDenseNet_3D.py
65 lines (47 loc) · 1.81 KB
/
DenseNet_3D.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
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv3D, Dense, BatchNormalization, Concatenate, Dropout, AveragePooling3D, GlobalAveragePooling3D, Activation
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().as_list()[-1]
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 DenseNet3D(inputs,num_classes):
inputs = inputs
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(units=num_classes,activation='sigmoid')(x)
print(x.get_shape())
model = Model(inputs=inputs, outputs=x)
return model