Skip to content

Commit 412a10f

Browse files
committed
complete resnet50 impl
1 parent f15c696 commit 412a10f

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

keras_image_classifier/library/resnet.py

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import keras.backend as K
1515
import tensorflow as tf
1616

17+
from keras_image_classifier.library.resnets_utils import convert_to_one_hot, load_dataset
18+
1719
K.set_image_data_format('channels_last')
1820
K.set_learning_phase(1)
1921

@@ -145,4 +147,106 @@ def convolutional_block_test():
145147
A = convolutional_block(A_prev, f=2, filters=[2, 4, 6], stage=1, block='a')
146148
test.run(tf.global_variables_initializer())
147149
out = test.run([A], feed_dict={A_prev: X, K.learning_phase(): 0})
148-
print("out = " + str(out[0][1][1][0]))
150+
print("out = " + str(out[0][1][1][0]))
151+
152+
153+
def ResNet50(input_shape=(64, 64, 3), classes=6):
154+
"""
155+
Implementation of the popular ResNet50 the following architecture:
156+
CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
157+
-> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER
158+
159+
Arguments:
160+
input_shape -- shape of the images of the dataset
161+
classes -- integer, number of classes
162+
163+
Returns:
164+
model -- a Model() instance in Keras
165+
"""
166+
167+
# Define the input as a tensor with shape input_shape
168+
X_input = Input(input_shape)
169+
170+
# Zero-Padding
171+
X = ZeroPadding2D((3, 3))(X_input)
172+
173+
# Stage 1
174+
X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=glorot_uniform(seed=0))(X)
175+
X = BatchNormalization(axis=3, name='bn_conv1')(X)
176+
X = Activation('relu')(X)
177+
X = MaxPooling2D((3, 3), strides=(2, 2))(X)
178+
179+
# Stage 2
180+
X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=1)
181+
X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
182+
X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')
183+
184+
### START CODE HERE ###
185+
186+
# Stage 3 (≈4 lines)
187+
X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2)
188+
X = identity_block(X, 3, [128, 128, 512], stage=3, block='b')
189+
X = identity_block(X, 3, [128, 128, 512], stage=3, block='c')
190+
X = identity_block(X, 3, [128, 128, 512], stage=3, block='d')
191+
192+
# Stage 4 (≈6 lines)
193+
X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2)
194+
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')
195+
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')
196+
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')
197+
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')
198+
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')
199+
200+
# Stage 5 (≈3 lines)
201+
X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2)
202+
X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
203+
X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')
204+
205+
# AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
206+
X = AveragePooling2D(pool_size=(2, 2), name='avg_pool')(X)
207+
208+
### END CODE HERE ###
209+
210+
# output layer
211+
X = Flatten()(X)
212+
X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer=glorot_uniform(seed=0))(X)
213+
214+
# Create model
215+
model = Model(inputs=X_input, outputs=X, name='ResNet50')
216+
217+
return model
218+
219+
220+
def ResNet50_test():
221+
model = ResNet50(input_shape=(64, 64, 3), classes=6)
222+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
223+
224+
dataset_dir_path = '../training/resnet_datasets'
225+
226+
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset(dataset_dir_path)
227+
228+
# Normalize image vectors
229+
X_train = X_train_orig / 255.
230+
X_test = X_test_orig / 255.
231+
232+
# Convert training and test labels to one hot matrices
233+
Y_train = convert_to_one_hot(Y_train_orig, 6).T
234+
Y_test = convert_to_one_hot(Y_test_orig, 6).T
235+
236+
print("number of training examples = " + str(X_train.shape[0]))
237+
print("number of test examples = " + str(X_test.shape[0]))
238+
print("X_train shape: " + str(X_train.shape))
239+
print("Y_train shape: " + str(Y_train.shape))
240+
print("X_test shape: " + str(X_test.shape))
241+
print("Y_test shape: " + str(Y_test.shape))
242+
243+
244+
def main():
245+
identity_block_test()
246+
convolutional_block_test()
247+
ResNet50_test()
248+
249+
250+
if __name__ == '__main__':
251+
main()
252+

keras_image_classifier/library/resnets_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import h5py
55
import math
66

7-
def load_dataset():
8-
train_dataset = h5py.File('datasets/train_signs.h5', "r")
7+
def load_dataset(dir_path):
8+
train_dataset = h5py.File(dir_path + '/train_signs.h5', "r")
99
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
1010
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
1111

12-
test_dataset = h5py.File('datasets/test_signs.h5', "r")
12+
test_dataset = h5py.File(dir_path + '/test_signs.h5', "r")
1313
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
1414
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
1515

1.41 MB
Binary file not shown.
12.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)