ship-vs-iceberg/icenet/residual.py
2021-10-16 08:49:48 +02:00

191 lines
5.6 KiB
Python

"""Residual blocks: Conv and identity"""
from keras.initializers import glorot_uniform
from keras.layers import Add, Activation, Conv2D, BatchNormalization
def basic_block(X, f, filters, s, label):
"""
Residual net identity block.
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
label -- label to use for naming
s -- strides
Returns:
X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
"""
F1, F2 = filters
X_shortcut = X
# First component of main path
X = Conv2D(
F1,
kernel_size=(f, f),
strides=(s, s),
padding='same',
name='conv_%s_2a' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2a' % label)(X)
X = Activation('relu')(X)
# Second component of main path
X = Conv2D(
F2,
kernel_size=(f, f),
strides=(1, 1),
padding='same',
name='conv_%s_2b' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2b' % label)(X)
# Shortcut path
X_shortcut = Conv2D(
F2,
kernel_size=(1, 1),
strides=(s, s),
padding='valid',
name='conv_%s_1' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X_shortcut)
X_shortcut = BatchNormalization(axis=3, name='bn_%s_1' % label)(X_shortcut)
# Add shortcut value to main path, and pass it through a RELU activation
X = Add()([X_shortcut, X])
X = Activation('relu')(X)
return X
def identity_block(X, f, filters, label):
"""
Residual net identity block.
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
label -- label to use for naming
Returns:
X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
"""
F1, F2, F3 = filters
X_shortcut = X
# First component of main path
X = Conv2D(
F1,
kernel_size=(1, 1),
strides=(1,1),
padding='valid',
name='conv_%s_2a' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2a' % label)(X)
X = Activation('relu')(X)
# Second component of main path
X = Conv2D(
F2,
kernel_size=(f, f),
strides=(1, 1),
padding='same',
name='conv_%s_2b' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2b' % label)(X)
X = Activation('relu')(X)
# Third component of main path
X = Conv2D(
F3,
kernel_size=(1, 1),
strides=(1, 1),
padding='valid',
name='conv_%s_2c' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2c' % label)(X)
# Add shortcut value to main path, and pass it through a RELU activation
X = Add()([X_shortcut, X])
X = Activation('relu')(X)
return X
def convolutional_block(X, f, filters, label, s=2):
"""
Residual net convolutional block.
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
label -- label to use for naming
s -- Integer, specifying the stride to be used
Returns:
X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
"""
F1, F2, F3 = filters
X_shortcut = X
# First component of main path
X = Conv2D(
F1,
kernel_size=(1, 1),
strides=(s,s),
padding='valid',
name='conv_%s_2a' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2a' % label)(X)
X = Activation('relu')(X)
# Second component of main path
X = Conv2D(
F2,
kernel_size=(f, f),
strides=(1, 1),
padding='same',
name='conv_%s_2b' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2b' % label)(X)
X = Activation('relu')(X)
# Third component of main path
X = Conv2D(
F3,
kernel_size=(1, 1),
strides=(1, 1),
padding='valid',
name='conv_%s_2c' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X)
X = BatchNormalization(axis=3, name='bn_%s_2c' % label)(X)
# Shortcut path
X_shortcut = Conv2D(
F3,
kernel_size=(1, 1),
strides=(s, s),
padding='valid',
name='conv_%s_1' % label,
kernel_initializer=glorot_uniform(seed=0)
)(X_shortcut)
X_shortcut = BatchNormalization(axis=3, name='bn_%s_1' % label)(X_shortcut)
# Add shortcut value to main path, and pass it through a RELU activation
X = Add()([X_shortcut, X])
X = Activation('relu')(X)
return X