Skip to content

Commit b5d5969

Browse files
committed
updatae
1 parent 21a54a6 commit b5d5969

4 files changed

Lines changed: 16 additions & 81 deletions

File tree

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
img_width, img_height = 640, 640
1+
img_width, img_height = 224, 224
22
num_channels = 3
33
train_data = 'data/train'
44
valid_data = 'data/valid'

demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from utils import draw_str
1212

1313
if __name__ == '__main__':
14-
img_width, img_height = 640, 640
14+
img_width, img_height = 224, 224
1515
num_channels = 3
1616
num_classes = 80
1717

resnet_50.py

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# -*- coding: utf-8 -*-
22

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
56
from keras.layers.normalization import BatchNormalization
67
from keras.models import Model
7-
from keras import backend as K
8-
98
from sklearn.metrics import log_loss
109

1110

@@ -80,7 +79,7 @@ def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2))
8079
return x
8180

8281

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):
8483
"""
8584
Resnet 50 Model for Keras
8685
@@ -149,71 +148,15 @@ def resnet50_model_old(img_rows, img_cols, color_type=1, num_classes=None):
149148

150149
model.load_weights(weights_path)
151150

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
206154
x_newfc = AveragePooling2D((7, 7), name='avg_pool')(x)
207155
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)
209157

210-
# Create another model with our customized softmax
211158
model = Model(img_input, x_newfc)
212159

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-
217160
return model
218161

219162

train.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
23
import keras
34
import tensorflow as tf
45
from keras.callbacks import ModelCheckpoint, EarlyStopping
@@ -9,8 +10,7 @@
910

1011
from config import img_height, img_width, batch_size, patience, num_channels, num_classes, train_data, valid_data, \
1112
num_train_samples, num_valid_samples, num_epochs, verbose
12-
from migrate import migrate_model
13-
from resnet_50 import resnet50_model_new
13+
from resnet_50 import resnet50_model
1414
from utils import get_available_gpus, get_available_cpus
1515

1616
if __name__ == '__main__':
@@ -58,27 +58,19 @@ def on_epoch_end(self, epoch, logs=None):
5858
num_gpu = len(get_available_gpus())
5959
if num_gpu >= 2:
6060
with tf.device("/cpu:0"):
61+
model = resnet50_model(img_rows=img_height, img_cols=img_width, color_type=num_channels,
62+
num_classes=num_classes)
6163
if pretrained_path is not None:
62-
model = resnet50_model_new(img_rows=img_height, img_cols=img_width, color_type=num_channels,
63-
num_classes=num_classes)
6464
model.load_weights(pretrained_path)
65-
else:
66-
model = resnet50_model_new(img_rows=img_height, img_cols=img_width, color_type=num_channels,
67-
num_classes=num_classes)
68-
migrate_model(model)
6965

7066
new_model = multi_gpu_model(model, gpus=num_gpu)
7167
# rewrite the callback: saving through the original model and not the multi-gpu model.
7268
model_checkpoint = MyCbk(model)
7369
else:
70+
new_model = resnet50_model(img_rows=img_height, img_cols=img_width, color_type=num_channels,
71+
num_classes=num_classes)
7472
if pretrained_path is not None:
75-
new_model = resnet50_model_new(img_rows=img_height, img_cols=img_width, color_type=num_channels,
76-
num_classes=num_classes)
7773
new_model.load_weights(pretrained_path)
78-
else:
79-
new_model = resnet50_model_new(img_rows=img_height, img_cols=img_width, color_type=num_channels,
80-
num_classes=num_classes)
81-
migrate_model(new_model)
8274

8375
sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
8476
new_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

0 commit comments

Comments
 (0)