-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdensenet.py
156 lines (122 loc) · 6.49 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
from __future__ import print_function
from keras.layers import Input, Dense, Dropout, Activation, Concatenate, BatchNormalization
from keras.models import Model
from keras.layers import Conv2D, GlobalAveragePooling2D, AveragePooling2D
from keras.regularizers import l2
def DenseNet(input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None, dropout_rate=None,
bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40):
"""
Creating a DenseNet
Arguments:
input_shape : shape of the input images. E.g. (28,28,1) for MNIST
dense_blocks : amount of dense blocks that will be created (default: 3)
dense_layers : number of layers in each dense block. You can also use a list for numbers of layers [2,4,3]
or define only 2 to add 2 layers at all dense blocks. -1 means that dense_layers will be calculated
by the given depth (default: -1)
growth_rate : number of filters to add per dense block (default: 12)
nb_classes : number of classes
dropout_rate : defines the dropout rate that is accomplished after each conv layer (except the first one).
In the paper the authors recommend a dropout of 0.2 (default: None)
bottleneck : (True / False) if true it will be added in convolution block (default: False)
compression : reduce the number of feature-maps at transition layer. In the paper the authors recomment a compression
of 0.5 (default: 1.0 - will have no compression effect)
weight_decay : weight decay of L2 regularization on weights (default: 1e-4)
depth : number or layers (default: 40)
Returns:
Model : A Keras model instance
"""
if nb_classes==None:
raise Exception('Please define number of classes (e.g. num_classes=10). This is required for final softmax.')
if compression <=0.0 or compression > 1.0:
raise Exception('Compression have to be a value between 0.0 and 1.0. If you set compression to 1.0 it will be turn off.')
if type(dense_layers) is list:
if len(dense_layers) != dense_blocks:
raise AssertionError('Number of dense blocks have to be same length to specified layers')
elif dense_layers == -1:
if bottleneck:
dense_layers = (depth - (dense_blocks + 1))/dense_blocks // 2
else:
dense_layers = (depth - (dense_blocks + 1))//dense_blocks
dense_layers = [int(dense_layers) for _ in range(dense_blocks)]
else:
dense_layers = [int(dense_layers) for _ in range(dense_blocks)]
img_input = Input(shape=input_shape)
nb_channels = growth_rate * 2
print('Creating DenseNet')
print('#############################################')
print('Dense blocks: %s' % dense_blocks)
print('Layers per dense block: %s' % dense_layers)
print('#############################################')
# Initial convolution layer
x = Conv2D(nb_channels, (3,3), padding='same',strides=(1,1),
use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)
# Building dense blocks
for block in range(dense_blocks):
# Add dense block
x, nb_channels = dense_block(x, dense_layers[block], nb_channels, growth_rate, dropout_rate, bottleneck, weight_decay)
if block < dense_blocks - 1: # if it's not the last dense block
# Add transition_block
x = transition_layer(x, nb_channels, dropout_rate, compression, weight_decay)
nb_channels = int(nb_channels * compression)
x = BatchNormalization(gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
x = Activation('relu')(x)
x = GlobalAveragePooling2D()(x)
x = Dense(nb_classes, activation='softmax', kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay))(x)
model_name = None
if growth_rate >= 36:
model_name = 'widedense'
else:
model_name = 'dense'
if bottleneck:
model_name = model_name + 'b'
if compression < 1.0:
model_name = model_name + 'c'
return Model(img_input, x, name=model_name), model_name
def dense_block(x, nb_layers, nb_channels, growth_rate, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
"""
Creates a dense block and concatenates inputs
"""
x_list = [x]
for i in range(nb_layers):
cb = convolution_block(x, growth_rate, dropout_rate, bottleneck, weight_decay)
x_list.append(cb)
x = Concatenate(axis=-1)(x_list)
nb_channels += growth_rate
return x, nb_channels
def convolution_block(x, nb_channels, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
"""
Creates a convolution block consisting of BN-ReLU-Conv.
Optional: bottleneck, dropout
"""
# Bottleneck
if bottleneck:
bottleneckWidth = 4
x = BatchNormalization(gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
x = Activation('relu')(x)
x = Conv2D(nb_channels * bottleneckWidth, (1, 1), use_bias=False, kernel_regularizer=l2(weight_decay))(x)
# Dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
# Standard (BN-ReLU-Conv)
x = BatchNormalization(gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
x = Activation('relu')(x)
x = Conv2D(nb_channels, (3, 3), padding='same', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
# Dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
return x
def transition_layer(x, nb_channels, dropout_rate=None, compression=1.0, weight_decay=1e-4):
"""
Creates a transition layer between dense blocks as transition, which do convolution and pooling.
Works as downsampling.
"""
x = BatchNormalization(gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
x = Activation('relu')(x)
x = Conv2D(int(nb_channels*compression), (1, 1), padding='same',
use_bias=False, kernel_regularizer=l2(weight_decay))(x)
# Adding dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
return x