Deep Learning Using Python + Keras (Chapter 3) - ResNet - CodeProject
Deep Learning Using Python + Keras (Chapter 3) - ResNet - CodeProject
Third article of a serie of articles introducing deep learning coding in Python and Keras framework.
Introduction
This article doesn't give you an introduction to deep learning. You are supposed to know the basis of deep learning and a little of
Python coding. The main objective of this article is to introduce you to the basis of Keras framework and use with another known
library to make a quick experiment and take the first conclusions.
Background
This article shows the ResNet architecture. Introduced by Microsoft, won the ILSVRC (ImageNet Large Scale Visual Recognition
Challenge) in 2015. You can see the paper at https://arxiv.org/abs/1512.03385.
The key concept is to increase the layer number introducing a residual connection (with an identity layer). This layer go to the next
layer directly, improving the learning proccess.
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 1/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
We will do the same experiment than previous chapters. I will not show the sections of loading the CIFAR-100 dataset, setting up
the experiment and download python libraries. All are the same than previous chapter.
if include_top:
x = Flatten()(x)
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 2/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
x = Dense(classes, activation='softmax', name='fc1000')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
return model
def create_custom_resnet50():
model = CustomResNet50(include_top=True, input_tensor=None, input_shape=(32,32,3),
pooling=None, classes=100)
return model
custom_resnet50_model = create_custom_resnet50()
custom_resnet50_model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['acc',
'mse'])
custom_resnet50_model.summary()
_______________________________________________________________________________________________
___
Layer (type) Output Shape Param # Connected to
===============================================================================================
===
input_1 (InputLayer) (None, 32, 32, 3) 0
_______________________________________________________________________________________________
___
conv1_pad (ZeroPadding2D) (None, 36, 36, 3) 0 input_1[0][0]
_______________________________________________________________________________________________
___
res2a_branch2a (Conv2D) (None, 18, 18, 32) 128 conv1_pad[0][0]
_______________________________________________________________________________________________
___
bn2a_branch2a (BatchNormalizati (None, 18, 18, 32) 128 res2a_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_1 (Activation) (None, 18, 18, 32) 0 bn2a_branch2a[0][0]
_______________________________________________________________________________________________
___
res2a_branch2b (Conv2D) (None, 18, 18, 32) 9248 activation_1[0][0]
_______________________________________________________________________________________________
___
bn2a_branch2b (BatchNormalizati (None, 18, 18, 32) 128 res2a_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_2 (Activation) (None, 18, 18, 32) 0 bn2a_branch2b[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 3/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
res2a_branch2c (Conv2D) (None, 18, 18, 64) 2112 activation_2[0][0]
_______________________________________________________________________________________________
___
res2a_branch1 (Conv2D) (None, 18, 18, 64) 256 conv1_pad[0][0]
_______________________________________________________________________________________________
___
bn2a_branch2c (BatchNormalizati (None, 18, 18, 64) 256 res2a_branch2c[0][0]
_______________________________________________________________________________________________
___
bn2a_branch1 (BatchNormalizatio (None, 18, 18, 64) 256 res2a_branch1[0][0]
_______________________________________________________________________________________________
___
add_1 (Add) (None, 18, 18, 64) 0 bn2a_branch2c[0][0]
bn2a_branch1[0][0]
_______________________________________________________________________________________________
___
activation_3 (Activation) (None, 18, 18, 64) 0 add_1[0][0]
_______________________________________________________________________________________________
___
res2b_branch2a (Conv2D) (None, 18, 18, 32) 2080 activation_3[0][0]
_______________________________________________________________________________________________
___
bn2b_branch2a (BatchNormalizati (None, 18, 18, 32) 128 res2b_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_4 (Activation) (None, 18, 18, 32) 0 bn2b_branch2a[0][0]
_______________________________________________________________________________________________
___
res2b_branch2b (Conv2D) (None, 18, 18, 32) 9248 activation_4[0][0]
_______________________________________________________________________________________________
___
bn2b_branch2b (BatchNormalizati (None, 18, 18, 32) 128 res2b_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_5 (Activation) (None, 18, 18, 32) 0 bn2b_branch2b[0][0]
_______________________________________________________________________________________________
___
res2b_branch2c (Conv2D) (None, 18, 18, 64) 2112 activation_5[0][0]
_______________________________________________________________________________________________
___
bn2b_branch2c (BatchNormalizati (None, 18, 18, 64) 256 res2b_branch2c[0][0]
_______________________________________________________________________________________________
___
add_2 (Add) (None, 18, 18, 64) 0 bn2b_branch2c[0][0]
activation_3[0][0]
_______________________________________________________________________________________________
___
activation_6 (Activation) (None, 18, 18, 64) 0 add_2[0][0]
_______________________________________________________________________________________________
___
res2c_branch2a (Conv2D) (None, 18, 18, 32) 2080 activation_6[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 4/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
bn2c_branch2a (BatchNormalizati (None, 18, 18, 32) 128 res2c_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_7 (Activation) (None, 18, 18, 32) 0 bn2c_branch2a[0][0]
_______________________________________________________________________________________________
___
res2c_branch2b (Conv2D) (None, 18, 18, 32) 9248 activation_7[0][0]
_______________________________________________________________________________________________
___
bn2c_branch2b (BatchNormalizati (None, 18, 18, 32) 128 res2c_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_8 (Activation) (None, 18, 18, 32) 0 bn2c_branch2b[0][0]
_______________________________________________________________________________________________
___
res2c_branch2c (Conv2D) (None, 18, 18, 64) 2112 activation_8[0][0]
_______________________________________________________________________________________________
___
bn2c_branch2c (BatchNormalizati (None, 18, 18, 64) 256 res2c_branch2c[0][0]
_______________________________________________________________________________________________
___
add_3 (Add) (None, 18, 18, 64) 0 bn2c_branch2c[0][0]
activation_6[0][0]
_______________________________________________________________________________________________
___
activation_9 (Activation) (None, 18, 18, 64) 0 add_3[0][0]
_______________________________________________________________________________________________
___
res3a_branch2a (Conv2D) (None, 18, 18, 64) 4160 activation_9[0][0]
_______________________________________________________________________________________________
___
bn3a_branch2a (BatchNormalizati (None, 18, 18, 64) 256 res3a_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_10 (Activation) (None, 18, 18, 64) 0 bn3a_branch2a[0][0]
_______________________________________________________________________________________________
___
res3a_branch2b (Conv2D) (None, 18, 18, 64) 36928 activation_10[0][0]
_______________________________________________________________________________________________
___
bn3a_branch2b (BatchNormalizati (None, 18, 18, 64) 256 res3a_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_11 (Activation) (None, 18, 18, 64) 0 bn3a_branch2b[0][0]
_______________________________________________________________________________________________
___
res3a_branch2c (Conv2D) (None, 18, 18, 256) 16640 activation_11[0][0]
_______________________________________________________________________________________________
___
res3a_branch1 (Conv2D) (None, 18, 18, 256) 16640 activation_9[0][0]
_______________________________________________________________________________________________
___
bn3a_branch2c (BatchNormalizati (None, 18, 18, 256) 1024 res3a_branch2c[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 5/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
bn3a_branch1 (BatchNormalizatio (None, 18, 18, 256) 1024 res3a_branch1[0][0]
_______________________________________________________________________________________________
___
add_4 (Add) (None, 18, 18, 256) 0 bn3a_branch2c[0][0]
bn3a_branch1[0][0]
_______________________________________________________________________________________________
___
activation_12 (Activation) (None, 18, 18, 256) 0 add_4[0][0]
_______________________________________________________________________________________________
___
res3b_branch2a (Conv2D) (None, 18, 18, 64) 16448 activation_12[0][0]
_______________________________________________________________________________________________
___
bn3b_branch2a (BatchNormalizati (None, 18, 18, 64) 256 res3b_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_13 (Activation) (None, 18, 18, 64) 0 bn3b_branch2a[0][0]
_______________________________________________________________________________________________
___
res3b_branch2b (Conv2D) (None, 18, 18, 64) 36928 activation_13[0][0]
_______________________________________________________________________________________________
___
bn3b_branch2b (BatchNormalizati (None, 18, 18, 64) 256 res3b_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_14 (Activation) (None, 18, 18, 64) 0 bn3b_branch2b[0][0]
_______________________________________________________________________________________________
___
res3b_branch2c (Conv2D) (None, 18, 18, 256) 16640 activation_14[0][0]
_______________________________________________________________________________________________
___
bn3b_branch2c (BatchNormalizati (None, 18, 18, 256) 1024 res3b_branch2c[0][0]
_______________________________________________________________________________________________
___
add_5 (Add) (None, 18, 18, 256) 0 bn3b_branch2c[0][0]
activation_12[0][0]
_______________________________________________________________________________________________
___
activation_15 (Activation) (None, 18, 18, 256) 0 add_5[0][0]
_______________________________________________________________________________________________
___
res3c_branch2a (Conv2D) (None, 18, 18, 64) 16448 activation_15[0][0]
_______________________________________________________________________________________________
___
bn3c_branch2a (BatchNormalizati (None, 18, 18, 64) 256 res3c_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_16 (Activation) (None, 18, 18, 64) 0 bn3c_branch2a[0][0]
_______________________________________________________________________________________________
___
res3c_branch2b (Conv2D) (None, 18, 18, 64) 36928 activation_16[0][0]
_______________________________________________________________________________________________
___
bn3c_branch2b (BatchNormalizati (None, 18, 18, 64) 256 res3c_branch2b[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 6/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
activation_17 (Activation) (None, 18, 18, 64) 0 bn3c_branch2b[0][0]
_______________________________________________________________________________________________
___
res3c_branch2c (Conv2D) (None, 18, 18, 256) 16640 activation_17[0][0]
_______________________________________________________________________________________________
___
bn3c_branch2c (BatchNormalizati (None, 18, 18, 256) 1024 res3c_branch2c[0][0]
_______________________________________________________________________________________________
___
add_6 (Add) (None, 18, 18, 256) 0 bn3c_branch2c[0][0]
activation_15[0][0]
_______________________________________________________________________________________________
___
activation_18 (Activation) (None, 18, 18, 256) 0 add_6[0][0]
_______________________________________________________________________________________________
___
res4a_branch2a (Conv2D) (None, 9, 9, 128) 32896 activation_18[0][0]
_______________________________________________________________________________________________
___
bn4a_branch2a (BatchNormalizati (None, 9, 9, 128) 512 res4a_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_19 (Activation) (None, 9, 9, 128) 0 bn4a_branch2a[0][0]
_______________________________________________________________________________________________
___
res4a_branch2b (Conv2D) (None, 9, 9, 128) 147584 activation_19[0][0]
_______________________________________________________________________________________________
___
bn4a_branch2b (BatchNormalizati (None, 9, 9, 128) 512 res4a_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_20 (Activation) (None, 9, 9, 128) 0 bn4a_branch2b[0][0]
_______________________________________________________________________________________________
___
res4a_branch2c (Conv2D) (None, 9, 9, 512) 66048 activation_20[0][0]
_______________________________________________________________________________________________
___
res4a_branch1 (Conv2D) (None, 9, 9, 512) 131584 activation_18[0][0]
_______________________________________________________________________________________________
___
bn4a_branch2c (BatchNormalizati (None, 9, 9, 512) 2048 res4a_branch2c[0][0]
_______________________________________________________________________________________________
___
bn4a_branch1 (BatchNormalizatio (None, 9, 9, 512) 2048 res4a_branch1[0][0]
_______________________________________________________________________________________________
___
add_7 (Add) (None, 9, 9, 512) 0 bn4a_branch2c[0][0]
bn4a_branch1[0][0]
_______________________________________________________________________________________________
___
activation_21 (Activation) (None, 9, 9, 512) 0 add_7[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 7/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
res4b_branch2a (Conv2D) (None, 9, 9, 128) 65664 activation_21[0][0]
_______________________________________________________________________________________________
___
bn4b_branch2a (BatchNormalizati (None, 9, 9, 128) 512 res4b_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_22 (Activation) (None, 9, 9, 128) 0 bn4b_branch2a[0][0]
_______________________________________________________________________________________________
___
res4b_branch2b (Conv2D) (None, 9, 9, 128) 147584 activation_22[0][0]
_______________________________________________________________________________________________
___
bn4b_branch2b (BatchNormalizati (None, 9, 9, 128) 512 res4b_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_23 (Activation) (None, 9, 9, 128) 0 bn4b_branch2b[0][0]
_______________________________________________________________________________________________
___
res4b_branch2c (Conv2D) (None, 9, 9, 512) 66048 activation_23[0][0]
_______________________________________________________________________________________________
___
bn4b_branch2c (BatchNormalizati (None, 9, 9, 512) 2048 res4b_branch2c[0][0]
_______________________________________________________________________________________________
___
add_8 (Add) (None, 9, 9, 512) 0 bn4b_branch2c[0][0]
activation_21[0][0]
_______________________________________________________________________________________________
___
activation_24 (Activation) (None, 9, 9, 512) 0 add_8[0][0]
_______________________________________________________________________________________________
___
res4c_branch2a (Conv2D) (None, 9, 9, 128) 65664 activation_24[0][0]
_______________________________________________________________________________________________
___
bn4c_branch2a (BatchNormalizati (None, 9, 9, 128) 512 res4c_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_25 (Activation) (None, 9, 9, 128) 0 bn4c_branch2a[0][0]
_______________________________________________________________________________________________
___
res4c_branch2b (Conv2D) (None, 9, 9, 128) 147584 activation_25[0][0]
_______________________________________________________________________________________________
___
bn4c_branch2b (BatchNormalizati (None, 9, 9, 128) 512 res4c_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_26 (Activation) (None, 9, 9, 128) 0 bn4c_branch2b[0][0]
_______________________________________________________________________________________________
___
res4c_branch2c (Conv2D) (None, 9, 9, 512) 66048 activation_26[0][0]
_______________________________________________________________________________________________
___
bn4c_branch2c (BatchNormalizati (None, 9, 9, 512) 2048 res4c_branch2c[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 8/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
add_9 (Add) (None, 9, 9, 512) 0 bn4c_branch2c[0][0]
activation_24[0][0]
_______________________________________________________________________________________________
___
activation_27 (Activation) (None, 9, 9, 512) 0 add_9[0][0]
_______________________________________________________________________________________________
___
res4d_branch2a (Conv2D) (None, 9, 9, 128) 65664 activation_27[0][0]
_______________________________________________________________________________________________
___
bn4d_branch2a (BatchNormalizati (None, 9, 9, 128) 512 res4d_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_28 (Activation) (None, 9, 9, 128) 0 bn4d_branch2a[0][0]
_______________________________________________________________________________________________
___
res4d_branch2b (Conv2D) (None, 9, 9, 128) 147584 activation_28[0][0]
_______________________________________________________________________________________________
___
bn4d_branch2b (BatchNormalizati (None, 9, 9, 128) 512 res4d_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_29 (Activation) (None, 9, 9, 128) 0 bn4d_branch2b[0][0]
_______________________________________________________________________________________________
___
res4d_branch2c (Conv2D) (None, 9, 9, 512) 66048 activation_29[0][0]
_______________________________________________________________________________________________
___
bn4d_branch2c (BatchNormalizati (None, 9, 9, 512) 2048 res4d_branch2c[0][0]
_______________________________________________________________________________________________
___
add_10 (Add) (None, 9, 9, 512) 0 bn4d_branch2c[0][0]
activation_27[0][0]
_______________________________________________________________________________________________
___
activation_30 (Activation) (None, 9, 9, 512) 0 add_10[0][0]
_______________________________________________________________________________________________
___
res5a_branch2a (Conv2D) (None, 5, 5, 256) 131328 activation_30[0][0]
_______________________________________________________________________________________________
___
bn5a_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5a_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_31 (Activation) (None, 5, 5, 256) 0 bn5a_branch2a[0][0]
_______________________________________________________________________________________________
___
res5a_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_31[0][0]
_______________________________________________________________________________________________
___
bn5a_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5a_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_32 (Activation) (None, 5, 5, 256) 0 bn5a_branch2b[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 9/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
res5a_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_32[0][0]
_______________________________________________________________________________________________
___
res5a_branch1 (Conv2D) (None, 5, 5, 1024) 525312 activation_30[0][0]
_______________________________________________________________________________________________
___
bn5a_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5a_branch2c[0][0]
_______________________________________________________________________________________________
___
bn5a_branch1 (BatchNormalizatio (None, 5, 5, 1024) 4096 res5a_branch1[0][0]
_______________________________________________________________________________________________
___
add_11 (Add) (None, 5, 5, 1024) 0 bn5a_branch2c[0][0]
bn5a_branch1[0][0]
_______________________________________________________________________________________________
___
activation_33 (Activation) (None, 5, 5, 1024) 0 add_11[0][0]
_______________________________________________________________________________________________
___
res5b_branch2a (Conv2D) (None, 5, 5, 256) 262400 activation_33[0][0]
_______________________________________________________________________________________________
___
bn5b_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5b_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_34 (Activation) (None, 5, 5, 256) 0 bn5b_branch2a[0][0]
_______________________________________________________________________________________________
___
res5b_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_34[0][0]
_______________________________________________________________________________________________
___
bn5b_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5b_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_35 (Activation) (None, 5, 5, 256) 0 bn5b_branch2b[0][0]
_______________________________________________________________________________________________
___
res5b_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_35[0][0]
_______________________________________________________________________________________________
___
bn5b_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5b_branch2c[0][0]
_______________________________________________________________________________________________
___
add_12 (Add) (None, 5, 5, 1024) 0 bn5b_branch2c[0][0]
activation_33[0][0]
_______________________________________________________________________________________________
___
activation_36 (Activation) (None, 5, 5, 1024) 0 add_12[0][0]
_______________________________________________________________________________________________
___
res5c_branch2a (Conv2D) (None, 5, 5, 256) 262400 activation_36[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 10/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
bn5c_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5c_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_37 (Activation) (None, 5, 5, 256) 0 bn5c_branch2a[0][0]
_______________________________________________________________________________________________
___
res5c_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_37[0][0]
_______________________________________________________________________________________________
___
bn5c_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5c_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_38 (Activation) (None, 5, 5, 256) 0 bn5c_branch2b[0][0]
_______________________________________________________________________________________________
___
res5c_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_38[0][0]
_______________________________________________________________________________________________
___
bn5c_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5c_branch2c[0][0]
_______________________________________________________________________________________________
___
add_13 (Add) (None, 5, 5, 1024) 0 bn5c_branch2c[0][0]
activation_36[0][0]
_______________________________________________________________________________________________
___
activation_39 (Activation) (None, 5, 5, 1024) 0 add_13[0][0]
_______________________________________________________________________________________________
___
res5d_branch2a (Conv2D) (None, 5, 5, 256) 262400 activation_39[0][0]
_______________________________________________________________________________________________
___
bn5d_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5d_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_40 (Activation) (None, 5, 5, 256) 0 bn5d_branch2a[0][0]
_______________________________________________________________________________________________
___
res5d_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_40[0][0]
_______________________________________________________________________________________________
___
bn5d_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5d_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_41 (Activation) (None, 5, 5, 256) 0 bn5d_branch2b[0][0]
_______________________________________________________________________________________________
___
res5d_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_41[0][0]
_______________________________________________________________________________________________
___
bn5d_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5d_branch2c[0][0]
_______________________________________________________________________________________________
___
add_14 (Add) (None, 5, 5, 1024) 0 bn5d_branch2c[0][0]
activation_39[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 11/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
activation_42 (Activation) (None, 5, 5, 1024) 0 add_14[0][0]
_______________________________________________________________________________________________
___
res5e_branch2a (Conv2D) (None, 5, 5, 256) 262400 activation_42[0][0]
_______________________________________________________________________________________________
___
bn5e_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5e_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_43 (Activation) (None, 5, 5, 256) 0 bn5e_branch2a[0][0]
_______________________________________________________________________________________________
___
res5e_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_43[0][0]
_______________________________________________________________________________________________
___
bn5e_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5e_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_44 (Activation) (None, 5, 5, 256) 0 bn5e_branch2b[0][0]
_______________________________________________________________________________________________
___
res5e_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_44[0][0]
_______________________________________________________________________________________________
___
bn5e_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5e_branch2c[0][0]
_______________________________________________________________________________________________
___
add_15 (Add) (None, 5, 5, 1024) 0 bn5e_branch2c[0][0]
activation_42[0][0]
_______________________________________________________________________________________________
___
activation_45 (Activation) (None, 5, 5, 1024) 0 add_15[0][0]
_______________________________________________________________________________________________
___
res5f_branch2a (Conv2D) (None, 5, 5, 256) 262400 activation_45[0][0]
_______________________________________________________________________________________________
___
bn5f_branch2a (BatchNormalizati (None, 5, 5, 256) 1024 res5f_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_46 (Activation) (None, 5, 5, 256) 0 bn5f_branch2a[0][0]
_______________________________________________________________________________________________
___
res5f_branch2b (Conv2D) (None, 5, 5, 256) 590080 activation_46[0][0]
_______________________________________________________________________________________________
___
bn5f_branch2b (BatchNormalizati (None, 5, 5, 256) 1024 res5f_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_47 (Activation) (None, 5, 5, 256) 0 bn5f_branch2b[0][0]
_______________________________________________________________________________________________
___
res5f_branch2c (Conv2D) (None, 5, 5, 1024) 263168 activation_47[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 12/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
bn5f_branch2c (BatchNormalizati (None, 5, 5, 1024) 4096 res5f_branch2c[0][0]
_______________________________________________________________________________________________
___
add_16 (Add) (None, 5, 5, 1024) 0 bn5f_branch2c[0][0]
activation_45[0][0]
_______________________________________________________________________________________________
___
activation_48 (Activation) (None, 5, 5, 1024) 0 add_16[0][0]
_______________________________________________________________________________________________
___
res6a_branch2a (Conv2D) (None, 3, 3, 512) 524800 activation_48[0][0]
_______________________________________________________________________________________________
___
bn6a_branch2a (BatchNormalizati (None, 3, 3, 512) 2048 res6a_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_49 (Activation) (None, 3, 3, 512) 0 bn6a_branch2a[0][0]
_______________________________________________________________________________________________
___
res6a_branch2b (Conv2D) (None, 3, 3, 512) 2359808 activation_49[0][0]
_______________________________________________________________________________________________
___
bn6a_branch2b (BatchNormalizati (None, 3, 3, 512) 2048 res6a_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_50 (Activation) (None, 3, 3, 512) 0 bn6a_branch2b[0][0]
_______________________________________________________________________________________________
___
res6a_branch2c (Conv2D) (None, 3, 3, 2048) 1050624 activation_50[0][0]
_______________________________________________________________________________________________
___
res6a_branch1 (Conv2D) (None, 3, 3, 2048) 2099200 activation_48[0][0]
_______________________________________________________________________________________________
___
bn6a_branch2c (BatchNormalizati (None, 3, 3, 2048) 8192 res6a_branch2c[0][0]
_______________________________________________________________________________________________
___
bn6a_branch1 (BatchNormalizatio (None, 3, 3, 2048) 8192 res6a_branch1[0][0]
_______________________________________________________________________________________________
___
add_17 (Add) (None, 3, 3, 2048) 0 bn6a_branch2c[0][0]
bn6a_branch1[0][0]
_______________________________________________________________________________________________
___
activation_51 (Activation) (None, 3, 3, 2048) 0 add_17[0][0]
_______________________________________________________________________________________________
___
res6b_branch2a (Conv2D) (None, 3, 3, 512) 1049088 activation_51[0][0]
_______________________________________________________________________________________________
___
bn6b_branch2a (BatchNormalizati (None, 3, 3, 512) 2048 res6b_branch2a[0][0]
_______________________________________________________________________________________________
___
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 13/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
activation_52 (Activation) (None, 3, 3, 512) 0 bn6b_branch2a[0][0]
_______________________________________________________________________________________________
___
res6b_branch2b (Conv2D) (None, 3, 3, 512) 2359808 activation_52[0][0]
_______________________________________________________________________________________________
___
bn6b_branch2b (BatchNormalizati (None, 3, 3, 512) 2048 res6b_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_53 (Activation) (None, 3, 3, 512) 0 bn6b_branch2b[0][0]
_______________________________________________________________________________________________
___
res6b_branch2c (Conv2D) (None, 3, 3, 2048) 1050624 activation_53[0][0]
_______________________________________________________________________________________________
___
bn6b_branch2c (BatchNormalizati (None, 3, 3, 2048) 8192 res6b_branch2c[0][0]
_______________________________________________________________________________________________
___
add_18 (Add) (None, 3, 3, 2048) 0 bn6b_branch2c[0][0]
activation_51[0][0]
_______________________________________________________________________________________________
___
activation_54 (Activation) (None, 3, 3, 2048) 0 add_18[0][0]
_______________________________________________________________________________________________
___
res6c_branch2a (Conv2D) (None, 3, 3, 512) 1049088 activation_54[0][0]
_______________________________________________________________________________________________
___
bn6c_branch2a (BatchNormalizati (None, 3, 3, 512) 2048 res6c_branch2a[0][0]
_______________________________________________________________________________________________
___
activation_55 (Activation) (None, 3, 3, 512) 0 bn6c_branch2a[0][0]
_______________________________________________________________________________________________
___
res6c_branch2b (Conv2D) (None, 3, 3, 512) 2359808 activation_55[0][0]
_______________________________________________________________________________________________
___
bn6c_branch2b (BatchNormalizati (None, 3, 3, 512) 2048 res6c_branch2b[0][0]
_______________________________________________________________________________________________
___
activation_56 (Activation) (None, 3, 3, 512) 0 bn6c_branch2b[0][0]
_______________________________________________________________________________________________
___
res6c_branch2c (Conv2D) (None, 3, 3, 2048) 1050624 activation_56[0][0]
_______________________________________________________________________________________________
___
bn6c_branch2c (BatchNormalizati (None, 3, 3, 2048) 8192 res6c_branch2c[0][0]
_______________________________________________________________________________________________
___
add_19 (Add) (None, 3, 3, 2048) 0 bn6c_branch2c[0][0]
activation_54[0][0]
_______________________________________________________________________________________________
___
activation_57 (Activation) (None, 3, 3, 2048) 0 add_19[0][0]
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 14/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
_______________________________________________________________________________________________
___
avg_pool (AveragePooling2D) (None, 3, 3, 2048) 0 activation_57[0][0]
_______________________________________________________________________________________________
___
flatten_1 (Flatten) (None, 18432) 0 avg_pool[0][0]
_______________________________________________________________________________________________
___
fc1000 (Dense) (None, 100) 1843300 flatten_1[0][0]
===============================================================================================
===
Total params: 25,461,700
Trainable params: 25,407,812
Non-trainable params: 53,888
_______________________________________________________________________________________________
___
Let's see the metrics for the train and test results graphically (using matplotlib library, of course).
plt.figure(0)
plt.plot(crn50.history['acc'],'r')
plt.plot(crn50.history['val_acc'],'g')
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 15/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Accuracy")
plt.title("Training Accuracy vs Validation Accuracy")
plt.legend(['train','validation'])
plt.figure(1)
plt.plot(crn50.history['loss'],'r')
plt.plot(crn50.history['val_loss'],'g')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Loss")
plt.title("Training Loss vs Validation Loss")
plt.legend(['train','validation'])
plt.show()
The training has given acceptable results and has generalized well (0.0119).
Confussion matrix
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 16/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
Once we have trained our model, we want to see another metrics before taking any conclusion of the usability of the model we
have been created. For this, we will create the confusion matrix and, from that, we will see the precision, recall y F1-score metrics
(see wikipedia).
To create the confusion matrix, we need to make the predictions over the test set and then, we can create the confusion matrix and
show that metrics.
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 17/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
8 0.21 0.20 0.20 100
9 0.49 0.21 0.29 100
10 0.11 0.03 0.05 100
11 0.08 0.05 0.06 100
12 0.38 0.13 0.19 100
13 0.23 0.10 0.14 100
14 0.18 0.05 0.08 100
15 0.14 0.06 0.08 100
16 0.19 0.24 0.21 100
17 0.40 0.19 0.26 100
18 0.19 0.24 0.21 100
19 0.20 0.22 0.21 100
20 0.42 0.31 0.36 100
21 0.31 0.23 0.26 100
22 0.35 0.09 0.14 100
23 0.36 0.37 0.37 100
24 0.31 0.49 0.38 100
25 0.17 0.03 0.05 100
26 0.43 0.06 0.11 100
27 0.11 0.03 0.05 100
28 0.31 0.35 0.33 100
29 0.12 0.10 0.11 100
30 0.27 0.33 0.30 100
31 0.11 0.09 0.10 100
32 0.22 0.20 0.21 100
33 0.23 0.30 0.26 100
34 0.17 0.05 0.08 100
35 0.09 0.02 0.03 100
36 0.10 0.23 0.14 100
37 0.15 0.16 0.16 100
38 0.08 0.24 0.12 100
39 0.23 0.18 0.20 100
40 0.26 0.20 0.22 100
41 0.45 0.49 0.47 100
42 0.12 0.17 0.14 100
43 0.11 0.02 0.03 100
44 0.14 0.09 0.11 100
45 0.08 0.01 0.02 100
46 0.07 0.29 0.12 100
47 0.55 0.18 0.27 100
48 0.23 0.31 0.26 100
49 0.27 0.23 0.25 100
50 0.12 0.05 0.07 100
51 0.28 0.09 0.14 100
52 0.47 0.62 0.54 100
53 0.25 0.13 0.17 100
54 0.18 0.25 0.21 100
55 0.00 0.00 0.00 100
56 0.27 0.27 0.27 100
57 0.27 0.11 0.16 100
58 0.15 0.41 0.22 100
59 0.18 0.10 0.13 100
60 0.41 0.63 0.50 100
61 0.33 0.32 0.32 100
62 0.15 0.07 0.09 100
63 0.31 0.26 0.28 100
64 0.11 0.11 0.11 100
65 0.15 0.11 0.13 100
66 0.10 0.06 0.08 100
67 0.15 0.15 0.15 100
68 0.37 0.66 0.47 100
69 0.38 0.25 0.30 100
70 0.21 0.04 0.07 100
71 0.27 0.54 0.36 100
72 0.20 0.01 0.02 100
73 0.30 0.21 0.25 100
74 0.14 0.15 0.14 100
75 0.30 0.29 0.29 100
76 0.40 0.40 0.40 100
77 0.13 0.14 0.13 100
78 0.15 0.08 0.10 100
79 0.14 0.05 0.07 100
80 0.08 0.05 0.06 100
81 0.14 0.11 0.12 100
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 18/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
82 0.37 0.24 0.29 100
83 0.08 0.02 0.03 100
84 0.10 0.11 0.10 100
85 0.23 0.39 0.29 100
86 0.36 0.21 0.26 100
87 0.21 0.19 0.20 100
88 0.05 0.06 0.05 100
89 0.24 0.18 0.20 100
90 0.21 0.24 0.22 100
91 0.33 0.31 0.32 100
92 0.11 0.11 0.11 100
93 0.16 0.10 0.12 100
94 0.38 0.26 0.31 100
95 0.21 0.50 0.30 100
96 0.22 0.23 0.22 100
97 0.10 0.18 0.13 100
98 0.12 0.02 0.03 100
99 0.24 0.08 0.12 100
ROC Curve
The ROC curve is used by binary clasifiers because is a good tool to see the true positives rate versus false positives. Following
lines show the code for the multiclass classification ROC curve. This code is from DloLogy, but you can go to the Scikit
Learn documentation page.
n_classes = 100
# Plot linewidth.
lw = 2
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 19/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 20/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
Then, we will save the train history results to future comparisons and the model.
#Model
custom_resnet50_model.save(path_base + '/crn50.h5')
#Historical results
with open(path_base + '/crn50_history.txt', 'wb') as file_pi:
pickle.dump(crn50.history, file_pi)
Models comparisons
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 21/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
The next step is compare the metrics of the previous experiment with this results. We will compare accuracy, loss and mean
squared errors for the models ConvNet and regular net that we saw in previous chapters and some VGG models trained with the
same parameters.
plt.figure(0)
plt.plot(snn.history['val_acc'],'r')
plt.plot(scnn.history['val_acc'],'g')
plt.plot(vgg16.history['val_acc'],'b')
plt.plot(vgg19.history['val_acc'],'y')
plt.plot(vgg16Bis.history['val_acc'],'m')
plt.plot(crn50.history['val_acc'],'gold')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Accuracy")
plt.title("Simple NN Accuracy vs simple CNN Accuracy")
plt.legend(['simple NN','CNN','VGG 16','VGG 19','Custom VGG','Custom ResNet'])
plt.figure(0)
plt.plot(snn.history['val_loss'],'r')
plt.plot(scnn.history['val_loss'],'g')
plt.plot(vgg16.history['val_loss'],'b')
plt.plot(vgg19.history['val_loss'],'y')
plt.plot(vgg16Bis.history['val_loss'],'m')
plt.plot(crn50.history['val_loss'],'gold')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Loss")
plt.title("Simple NN Loss vs simple CNN Loss")
plt.legend(['simple NN','CNN','VGG 16','VGG 19','Custom VGG','Custom ResNet'])
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 22/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
plt.figure(0)
plt.plot(snn.history['val_mean_squared_error'],'r')
plt.plot(scnn.history['val_mean_squared_error'],'g')
plt.plot(vgg16.history['val_mean_squared_error'],'b')
plt.plot(vgg19.history['val_mean_squared_error'],'y')
plt.plot(vgg16Bis.history['val_mean_squared_error'],'m')
plt.plot(crn50.history['val_mean_squared_error'],'gold')
plt.xticks(np.arange(0, 11, 2.0))
plt.rcParams['figure.figsize'] = (8, 6)
plt.xlabel("Num of Epochs")
plt.ylabel("Mean Squared Error")
plt.title("Simple NN MSE vs simple CNN MSE")
plt.legend(['simple NN','CNN','VGG 16','VGG 19','Custom VGG','Custom ResNet'])
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 23/24
19/06/2018 Deep Learning using Python + Keras (Chapter 3): ResNet - CodeProject
Conclussion
As you can see, the architecture marks a turning point. Not only because it is of the best results than the previous architectures, but
also in the training times, since it allows to increase the layers with an acceptable time; and also in the number of parameters,
which has been reduced considerably with respect to the VGG architecture.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Jesús Utrera
Web04-2016 | 2.8.180618.1 | Last Updated 19 Jun 2018 Everything else Copyright © CodeProject, 1999-2018
https://www.codeproject.com/Articles/1248963/Deep-Learning-using-Python-plus-Keras-Chapter-Re?display=Print 24/24