|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -from keras.optimizers import SGD |
4 | | -from keras.layers import Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Flatten, Activation, add |
| 3 | +from keras import backend as K |
| 4 | +from keras.layers import Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Flatten, Activation, add, \ |
| 5 | + Dropout |
5 | 6 | from keras.layers.normalization import BatchNormalization |
6 | 7 | from keras.models import Model |
7 | | -from keras import backend as K |
8 | | - |
9 | 8 | from sklearn.metrics import log_loss |
10 | 9 |
|
11 | 10 |
|
@@ -80,7 +79,7 @@ def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)) |
80 | 79 | return x |
81 | 80 |
|
82 | 81 |
|
83 | | -def resnet50_model_old(img_rows, img_cols, color_type=1, num_classes=None): |
| 82 | +def resnet50_model(img_rows, img_cols, color_type=1, num_classes=None): |
84 | 83 | """ |
85 | 84 | Resnet 50 Model for Keras |
86 | 85 |
|
@@ -149,71 +148,15 @@ def resnet50_model_old(img_rows, img_cols, color_type=1, num_classes=None): |
149 | 148 |
|
150 | 149 | model.load_weights(weights_path) |
151 | 150 |
|
152 | | - return model |
153 | | - |
154 | | - |
155 | | -def resnet50_model_new(img_rows, img_cols, color_type, num_classes): |
156 | | - """ |
157 | | - Resnet 50 Model for Keras |
158 | | -
|
159 | | - Model Schema is based on |
160 | | - https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py |
161 | | -
|
162 | | - ImageNet Pretrained Weights |
163 | | - https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels.h5 |
164 | | -
|
165 | | - Parameters: |
166 | | - img_rows, img_cols - resolution of inputs |
167 | | - channel - 1 for grayscale, 3 for color |
168 | | - num_classes - number of class labels for our classification task |
169 | | - """ |
170 | | - |
171 | | - # Handle Dimension Ordering for different backends |
172 | | - global bn_axis |
173 | | - if K.image_dim_ordering() == 'tf': |
174 | | - bn_axis = 3 |
175 | | - img_input = Input(shape=(img_rows, img_cols, color_type)) |
176 | | - else: |
177 | | - bn_axis = 1 |
178 | | - img_input = Input(shape=(color_type, img_rows, img_cols)) |
179 | | - |
180 | | - x = ZeroPadding2D((3, 3))(img_input) |
181 | | - x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) |
182 | | - x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) |
183 | | - x = Activation('relu')(x) |
184 | | - x = MaxPooling2D((3, 3), strides=(2, 2))(x) |
185 | | - |
186 | | - x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) |
187 | | - x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') |
188 | | - x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') |
189 | | - |
190 | | - x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') |
191 | | - x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') |
192 | | - x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') |
193 | | - x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') |
194 | | - |
195 | | - x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') |
196 | | - x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') |
197 | | - x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') |
198 | | - x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') |
199 | | - x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') |
200 | | - x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') |
201 | | - |
202 | | - x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') |
203 | | - x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') |
204 | | - x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') |
205 | | - |
| 151 | + # Truncate and replace softmax layer for transfer learning |
| 152 | + # Cannot use model.layers.pop() since model is not of Sequential() type |
| 153 | + # The method below works since pre-trained weights are stored in layers but not in the model |
206 | 154 | x_newfc = AveragePooling2D((7, 7), name='avg_pool')(x) |
207 | 155 | x_newfc = Flatten()(x_newfc) |
208 | | - x_newfc = Dense(num_classes, activation='softmax', name='fc10')(x_newfc) |
| 156 | + x_newfc = Dense(num_classes, activation='softmax', name='fc8')(x_newfc) |
209 | 157 |
|
210 | | - # Create another model with our customized softmax |
211 | 158 | model = Model(img_input, x_newfc) |
212 | 159 |
|
213 | | - # Learning rate is changed to 0.001 |
214 | | - sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) |
215 | | - model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) |
216 | | - |
217 | 160 | return model |
218 | 161 |
|
219 | 162 |
|
|
0 commit comments