Last active
September 18, 2021 11:21
-
-
Save mjdietzx/5319e42637ed7ef095d430cb5c5e8c64 to your computer and use it in GitHub Desktop.
Clean and simple Keras implementation of the residual block (non-bottleneck) accompanying Deep Residual Learning: https://blog.waya.ai/deep-residual-learning-9610bb62c355.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import layers | |
def residual_block(y, nb_channels, _strides=(1, 1), _project_shortcut=False): | |
shortcut = y | |
# down-sampling is performed with a stride of 2 | |
y = layers.Conv2D(nb_channels, kernel_size=(3, 3), strides=_strides, padding='same')(y) | |
y = layers.BatchNormalization()(y) | |
y = layers.LeakyReLU()(y) | |
y = layers.Conv2D(nb_channels, kernel_size=(3, 3), strides=(1, 1), padding='same')(y) | |
y = layers.BatchNormalization()(y) | |
# identity shortcuts used directly when the input and output are of the same dimensions | |
if _project_shortcut or _strides != (1, 1): | |
# when the dimensions increase projection shortcut is used to match dimensions (done by 1×1 convolutions) | |
# when the shortcuts go across feature maps of two sizes, they are performed with a stride of 2 | |
shortcut = layers.Conv2D(nb_channels, kernel_size=(1, 1), strides=_strides, padding='same')(shortcut) | |
shortcut = layers.BatchNormalization()(shortcut) | |
y = layers.add([shortcut, y]) | |
y = layers.LeakyReLU()(y) | |
return y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MarviB16 I have a similar code and you can add it to your model this way
i = Input(shape=(x_train.shape[1],x_train.shape[2],x_train.shape[3]))
d = Conv2D(64,(7,7),activation='elu',kernel_regularizer=regularizers.l2(0.0001),padding='same')(i)#Con esto llegue a 0.716
d = residual_block(i,64,128,(1,1),False)# 64 ,64 para 0.716 #64 ,128 para 0.72 #ERA FALSE PARA 0.72
d = MaxPooling2D(pool_size=(2, 2))(d)
d = GaussianNoise(0.33)(d)#0.33 con 0.716
d = residual_block(d,128,256,sizematches=True)# 128,256 para 0.72 #ERA FALSE PARA 0.72
d = MaxPooling2D(pool_size=(2, 2))(d)
d = GaussianNoise(0.43)(d)#0.43 con 0.716
d = residual_block(d,256,512,sizematches=True) #ERA FALSE PARA 0.72
d = GaussianNoise(0.53)(d)#0.53 para 0.716
d = AveragePooling2D(pool_size=(4, 4))(d)#0.716 con pool size 4,4
d = Flatten()(d)
d= Dense(y_train.shape[1],activation='softmax')(d)