Exemplo n.º 1
0
def DenseNet3D(blocks, input_shape=None, classes=1000):
    img_input = layers.Input(shape=input_shape)
    bn_axis = 4

    # Conv Layer 1
    x = layers.ZeroPadding3D(padding=((3, 3), (3, 3), (3, 3)))(img_input)
    x = layers.Conv3D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = layers.BatchNormalization(axis=bn_axis,
                                  epsilon=1.001e-5,
                                  name='conv1/bn')(x)
    x = layers.Activation('relu', name='conv1/relu')(x)
    x = layers.ZeroPadding3D(padding=((1, 1), (1, 1), (1, 1)))(x)
    x = layers.MaxPooling3D(3, strides=2, name='pool1')(x)

    # Dense Blocks
    for i, block in enumerate(blocks):
        x = dense_block3D(x, block, name='conv' + str(i + 2))
        if i < len(blocks) - 1:
            x = transition_block3D(x, 0.5, name='pool' + str(i + 2))

    # Final Layers
    x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
    x = layers.GlobalAveragePooling3D(name='avg_pool')(x)
    x = layers.Dense(classes, activation='softmax', name='fc')(x)

    # Create model
    model = models.Model(img_input, x, name='densenet3D')
    return model
Exemplo n.º 2
0
def resnet_graph(input_image, architecture, stage5=False):
    assert architecture in ["resnet50", "resnet101"]
    # Stage 1
    x = KL.ZeroPadding3D((3, 3, 3))(input_image)
    x = KL.Conv3D(24, (7, 7, 7),
                  strides=(2, 2, 2),
                  name='conv1',
                  use_bias=True)(x)
    C1 = x = KL.Activation('relu')(x)

    # Stage 2
    C2 = x = conv_block(x, 3, [24, 48], stage=2, block='a')
    # x = identity_block(x, 3, [16], stage=2, block='b')
    # C2 = x = identity_block(x, 3, [2, 2, 8], stage=2, block='c')
    # Stage 3
    C3 = x = conv_block(x, 3, [48, 96], stage=3, block='a')
    # x = identity_block(x, 3, [4, 4, 16], stage=3, block='b')
    # x = identity_block(x, 3, [4, 4, 16], stage=3, block='c')
    # C3 = x = identity_block(x, 3, [4, 4, 16], stage=3, block='d')
    # Stage 4
    x = conv_block(x, 3, [96, 192], stage=4, block='a')
    # block_count = {"resnet50": 5, "resnet101": 22}[architecture]
    # for i in range(block_count):
    #     x = identity_block(x, 3, [8, 8, 32], stage=4, block=chr(98+i))
    C4 = x
    # Stage 5
    if stage5:
        C5 = x = conv_block(x, 3, [192, 384], stage=5, block='a')
        #x = identity_block(x, 3, [16, 16, 64], stage=5, block='b')
        #C5 = x = identity_block(x, 3, [16, 16, 64], stage=5, block='c')
    else:
        C5 = None
    return [C1, C2, C3, C4, C5]
def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id):
    channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1

    in_channels = backend.int_shape(inputs)[channel_axis]
    pointwise_conv_filters = int(filters * alpha)
    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
    x = inputs
    prefix = 'block_{}_'.format(block_id)

    if block_id:
        # Expand
        x = layers.Conv3D(expansion * in_channels,
                          kernel_size=1,
                          padding='same',
                          use_bias=False,
                          activation=None,
                          name=prefix + 'expand')(x)
        x = layers.BatchNormalization(axis=channel_axis,
                                      epsilon=1e-3,
                                      momentum=0.999,
                                      name=prefix + 'expand_BN')(x)
        x = layers.ReLU(6., name=prefix + 'expand_relu')(x)
    else:
        prefix = 'expanded_conv_'

    # Depthwise
    if stride == 2:
        x = layers.ZeroPadding3D(padding=correct_pad(backend, x, 3),
                                 name=prefix + 'pad')(x)
    x = DepthwiseConv3D(kernel_size=3,
                        strides=stride,
                        activation=None,
                        use_bias=False,
                        padding='same' if stride == 1 else 'valid',
                        name=prefix + 'depthwise')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  epsilon=1e-3,
                                  momentum=0.999,
                                  name=prefix + 'depthwise_BN')(x)

    x = layers.ReLU(6., name=prefix + 'depthwise_relu')(x)

    # Project
    x = layers.Conv3D(pointwise_filters,
                      kernel_size=1,
                      padding='same',
                      use_bias=False,
                      activation=None,
                      name=prefix + 'project')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  epsilon=1e-3,
                                  momentum=0.999,
                                  name=prefix + 'project_BN')(x)

    if in_channels == pointwise_filters and stride == 1:
        return layers.Add(name=prefix + 'add')([inputs, x])
    return x
Exemplo n.º 4
0
def pad_to_multiple(x, block_shape):
    ori_shape = K.int_shape(x)

    pad_t = ori_shape[1] % block_shape[0]
    pad_f = ori_shape[2] % block_shape[1]
    padding = ((0, 0), (0, pad_t), (0, pad_f))

    new_x = L.ZeroPadding3D(padding=padding)(x)

    return new_x
Exemplo n.º 5
0
def get_resnet50_3D(show=False):
    if backend.image_data_format() == 'channels_last':
        bn_axis = 4
    else:
        bn_axis = 1
    inputs = layers.Input((None, None, None, 1))
    x = layers.ZeroPadding3D(padding=(3, 3, 3), name='conv1_pad')(inputs)
    x = layers.Conv3D(64, (7, 7, 7),
                      strides=(2, 2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling3D((3, 3, 3), strides=(2, 2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
    x = layers.GlobalAveragePooling3D()(x)
    x = layers.Dense(1024)(x)
    x = layers.Activation('relu')(x)
    x = layers.Dropout(0.2)(x)
    x = layers.Dense(1, name='fc')(x)
    x = layers.Activation(activation='sigmoid')(x)
    model = models.Model(inputs, x, name='resnet50')
    if show:
        model.summary()
        plot_model(model, 'resnet50_3D.pdf', True)
        model.save('resnet50_3D.h5')
    return model
Exemplo n.º 6
0
def res_unet_resnet18(image_depth = 16, image_rows = 256, image_cols = 256, input_channels = 3, train_encoder = True):

    # Block 1
    # get parameters for model layers
    no_scale_bn_params = get_bn_params(train_encoder, scale=False)
    bn_params = get_bn_params(train_encoder)
    conv_params = get_conv_params(train_encoder)
    init_filters = 64

    inputs = Input((image_depth, image_rows, image_cols, input_channels))
    # Block 1
    # get parameters for model layers
    x = layers.BatchNormalization(name='bn_data', **no_scale_bn_params)(inputs)
    x = layers.ZeroPadding3D(padding=(0, 3, 3))(x)
    x = layers.Conv3D(init_filters, (1, 7, 7), strides=(1, 1, 1), name='conv0', **conv_params)(x)
    x = layers.BatchNormalization(name='bn0', **bn_params)(x)
    x = layers.Activation('relu', name='relu0')(x)
    skip_connection_1 = x
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.MaxPooling3D((2, 3, 3), strides=(2, 2, 2), padding='valid', name='pooling0')(x)

    # Stage 1, Unit 1 - Settings
    stage = 0
    block = 0
    strides = (1, 1, 1)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1), name=sc_name, strides=strides, **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 1, Unit 2 - Settings
    stage = 0
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 1 - Settings
    stage = 1
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_2 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1), name=sc_name, strides=strides, **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3), strides=strides, name=conv_name + '1_convpool', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 2 - Settings
    stage = 1
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 1 - Settings
    stage = 2
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_3 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1), name=sc_name, strides=strides, **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3), strides=strides, name=conv_name + '1_convpool', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 2 - Settings
    stage = 2
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 1 - Settings
    stage = 3
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_4 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1), name=sc_name, strides=strides, **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3), strides=strides, name=conv_name + '1_convpool', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 2 - Settings
    stage = 3
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2 ** stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), strides=strides, name=conv_name + '1', **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2', **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Resnet OUTPUT
    x = layers.BatchNormalization(name='bn1', **bn_params)(x)
    x = layers.Activation('relu', name='relu1')(x)

    conv51 = Conv3D(512, (1, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv51', trainable=train_encoder)(x)
    conv52 = Conv3D(512, (1, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv52', trainable=train_encoder)(conv51)
    conc52 = concatenate([x, conv52], axis=4)

    up6 = concatenate([Conv3DTranspose(256, (2, 2, 2), strides=(2, 2, 2), padding='same', name='trans6')(conc52), skip_connection_4], axis=4)
    conv61 = Conv3D(256, (2, 3, 3), activation='relu', dilation_rate=(1, 2, 2), padding='same', name='conv61')(up6)
    conv62 = Conv3D(256, (2, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv62')(conv61)
    conc62 = concatenate([up6, conv62], axis=4)

    up7 = concatenate([Conv3DTranspose(128, (2, 2, 2), strides=(2, 2, 2), padding='same', name='trans7')(conc62), skip_connection_3], axis=4)
    conv71 = Conv3D(128, (3, 3, 3), activation='relu', dilation_rate=(1, 2, 2), padding='same', name='conv71')(up7)
    conv72 = Conv3D(128, (3, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv72')(conv71)
    conc72 = concatenate([up7, conv72], axis=4)

    up8 = concatenate([Conv3DTranspose(64, (2, 2, 2), strides=(2, 2, 2), padding='same', name='trans8')(conc72), skip_connection_2], axis=4)
    conv81 = Conv3D(64, (3, 3, 3), activation='relu', dilation_rate=(1, 2, 2), padding='same', name='conv81')(up8)
    conv82 = Conv3D(64, (3, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv82')(conv81)
    conc82 = concatenate([up8, conv82], axis=4)

    up9 = concatenate([Conv3DTranspose(32, (2, 2, 2), strides=(2, 2, 2), padding='same', name='trans9')(conc82), skip_connection_1], axis=4)
    conv91 = Conv3D(32, (3, 3, 3), activation='relu', dilation_rate=(1, 2, 2), padding='same', name='conv91')(up9)
    conv92 = Conv3D(32, (3, 3, 3), activation='relu', dilation_rate=(1, 1, 1), padding='same', name='conv92')(conv91)
    conc92 = concatenate([up9, conv92], axis=4)


    conv10 = Conv3D(1, (1, 1, 1), activation='sigmoid', name='conv10')(conc92)

    model = Model(inputs=[inputs], outputs=[conv10])

    return model
Exemplo n.º 7
0
def resnet_graph(input_image, architecture, stage5=False, train_bn=True):
    """Build a ResNet graph.
        architecture: Can be resnet50 or resnet101
        stage5: Boolean. If False, stage5 of the network is not created
        train_bn: Boolean. Train or freeze Batch Norm layers
    """
    assert architecture in ["resnet50", "resnet101"]
    # Stage 1
    x = KL.ZeroPadding3D((3, 3, 3))(input_image)
    x = KL.Conv3D(64, (7, 7, 7),
                  strides=(2, 2, 2),
                  name='conv1',
                  use_bias=True)(x)
    x = BatchNorm(name='bn_conv1')(x, training=train_bn)
    x = KL.Activation('relu')(x)
    C1 = x = KL.MaxPooling3D((3, 3, 3), strides=(1, 2, 2), padding="same")(x)
    # Stage 2
    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=2,
                   block='a',
                   strides=(1, 1, 1),
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=2,
                       block='b',
                       train_bn=train_bn)
    C2 = x = identity_block(x,
                            3, [64, 64, 256],
                            stage=2,
                            block='c',
                            train_bn=train_bn)
    # Stage 3
    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=3,
                   block='a',
                   train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='b',
                       train_bn=train_bn)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=3,
                       block='c',
                       train_bn=train_bn)
    C3 = x = identity_block(x,
                            3, [128, 128, 512],
                            stage=3,
                            block='d',
                            train_bn=train_bn)
    # Stage 4
    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=4,
                   block='a',
                   train_bn=train_bn)
    block_count = {"resnet50": 5, "resnet101": 22}[architecture]
    for i in range(block_count):
        x = identity_block(x,
                           3, [256, 256, 1024],
                           stage=4,
                           block=chr(98 + i),
                           train_bn=train_bn)
    C4 = x
    # Stage 5
    if stage5:
        x = conv_block(x,
                       3, [512, 512, 2048],
                       stage=5,
                       block='a',
                       train_bn=train_bn)
        x = identity_block(x,
                           3, [512, 512, 2048],
                           stage=5,
                           block='b',
                           train_bn=train_bn)
        C5 = x = identity_block(x,
                                3, [512, 512, 2048],
                                stage=5,
                                block='c',
                                train_bn=train_bn)
    else:
        C5 = None
    return [C1, C2, C3, C4, C5]
Exemplo n.º 8
0
def resnet_3d_graph(input_clip,
                    mode,
                    architecture,
                    stage5=False,
                    temporal=True):
    # input_clip: [bs, timesteps, height, width, channel]
    # TODO: add a param in config indicating which stages contain temporal layers
    assert mode in ['training', 'inference']
    training = True if mode == 'training' else False
    assert architecture in ["resnet50", "resnet101"]
    # Stage 1
    x = KL.ZeroPadding3D((0, 3, 3))(input_clip)
    x = KL.TimeDistributed(KL.Conv2D(64, (7, 7), strides=(2, 2),
                                     use_bias=True),
                           name='conv1')(x)
    x = TD(BatchNorm(axis=3), name='bn_conv1')(x)
    x = KL.Activation('relu')(x)
    C1 = x = KL.TimeDistributed(
        KL.MaxPooling2D((3, 3), strides=(2, 2), padding='same'))(x)
    # Stage 2
    x = conv_block_3d(x,
                      3,
                      3, [64, 64, 256],
                      stage=2,
                      block='a',
                      training=training,
                      strides=(1, 1))
    x = identity_block_3d(x,
                          3,
                          3, [64, 64, 256],
                          stage=2,
                          block='b',
                          training=training)
    C2 = x = identity_block_3d(x,
                               3,
                               3, [64, 64, 256],
                               stage=2,
                               block='c',
                               training=training)
    # Stage 3
    x = conv_block_3d(x,
                      3,
                      3, [128, 128, 512],
                      stage=3,
                      block='a',
                      training=training)
    x = identity_block_3d(x,
                          3,
                          3, [128, 128, 512],
                          stage=3,
                          block='b',
                          training=training)
    x = identity_block_3d(x,
                          3,
                          3, [128, 128, 512],
                          stage=3,
                          block='c',
                          training=training)
    C3 = x = identity_block_3d(x,
                               3,
                               3, [128, 128, 512],
                               stage=3,
                               block='d',
                               training=training)
    # Stage 4
    x = conv_block_3d(x,
                      3,
                      3, [256, 256, 1024],
                      stage=4,
                      block='a',
                      training=training)
    block_count = {"resnet50": 5, "resnet101": 22}[architecture]
    for i in range(block_count):
        x = identity_block_3d(x,
                              3,
                              3, [256, 256, 1024],
                              stage=4,
                              block=chr(98 + i),
                              training=training,
                              temporal=temporal)
    C4 = x
    # Stage 5
    if stage5:
        x = conv_block_3d(x,
                          3,
                          3, [512, 512, 2048],
                          stage=5,
                          block='a',
                          training=training,
                          temporal=temporal)
        x = identity_block_3d(x,
                              3,
                              3, [512, 512, 2048],
                              stage=5,
                              block='b',
                              training=training,
                              temporal=temporal)
        C5 = x = identity_block_3d(x,
                                   3,
                                   3, [512, 512, 2048],
                                   stage=5,
                                   block='c',
                                   training=training,
                                   temporal=temporal)
    else:
        C5 = None
    return [C1, C2, C3, C4, C5]
def _depthwise_conv_block(inputs,
                          pointwise_conv_filters,
                          alpha,
                          depth_multiplier=1,
                          strides=(1, 1, 1),
                          block_id=1):
    """Adds a depthwise convolution block.

    A depthwise convolution block consists of a depthwise conv,
    batch normalization, relu6, pointwise convolution,
    batch normalization and relu6 activation.

    # Arguments
        inputs: Input tensor of shape `(rows, cols, channels)`
            (with `channels_last` data format) or
            (channels, rows, cols) (with `channels_first` data format).
        pointwise_conv_filters: Integer, the dimensionality of the output space
            (i.e. the number of output filters in the pointwise convolution).
        alpha: controls the width of the network.
            - If `alpha` < 1.0, proportionally decreases the number
                of filters in each layer.
            - If `alpha` > 1.0, proportionally increases the number
                of filters in each layer.
            - If `alpha` = 1, default number of filters from the paper
                 are used at each layer.
        depth_multiplier: The number of depthwise convolution output channels
            for each input channel.
            The total number of depthwise convolution output
            channels will be equal to `filters_in * depth_multiplier`.
        strides: An integer or tuple/list of 2 integers,
            specifying the strides of the convolution
            along the width and height.
            Can be a single integer to specify the same value for
            all spatial dimensions.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.
        block_id: Integer, a unique identification designating
            the block number.

    # Input shape
        4D tensor with shape:
        `(batch, channels, rows, cols)` if data_format='channels_first'
        or 4D tensor with shape:
        `(batch, rows, cols, channels)` if data_format='channels_last'.

    # Output shape
        4D tensor with shape:
        `(batch, filters, new_rows, new_cols)`
        if data_format='channels_first'
        or 4D tensor with shape:
        `(batch, new_rows, new_cols, filters)`
        if data_format='channels_last'.
        `rows` and `cols` values might have changed due to stride.

    # Returns
        Output tensor of block.
    """
    channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1
    pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    if strides == (1, 1, 1):
        x = inputs
    else:
        x = layers.ZeroPadding3D(((0, 1), (0, 1), (0, 1)),
                                 name='conv_pad_%d' % block_id)(inputs)
    x = DepthwiseConv3D((3, 3, 3),
                        padding='same' if strides == (1, 1, 1) else 'valid',
                        depth_multiplier=depth_multiplier,
                        strides=strides,
                        use_bias=False,
                        name='conv_dw_%d' % block_id)(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='conv_dw_%d_bn' % block_id)(x)
    x = layers.ReLU(6., name='conv_dw_%d_relu' % block_id)(x)

    x = layers.Conv3D(pointwise_conv_filters, (1, 1, 1),
                      padding='same',
                      use_bias=False,
                      strides=(1, 1, 1),
                      name='conv_pw_%d' % block_id)(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  name='conv_pw_%d_bn' % block_id)(x)
    return layers.ReLU(6., name='conv_pw_%d_relu' % block_id)(x)
def _conv_block(inputs, filters, alpha, kernel=(3, 3, 3), strides=(1, 1, 1)):
    """Adds an initial convolution layer (with batch normalization and relu6).

    # Arguments
        inputs: Input tensor of shape `(rows, cols, 3)`
            (with `channels_last` data format) or
            (3, rows, cols) (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 32.
            E.g. `(224, 224, 3)` would be one valid value.
        filters: Integer, the dimensionality of the output space
            (i.e. the number of output filters in the convolution).
        alpha: controls the width of the network.
            - If `alpha` < 1.0, proportionally decreases the number
                of filters in each layer.
            - If `alpha` > 1.0, proportionally increases the number
                of filters in each layer.
            - If `alpha` = 1, default number of filters from the paper
                 are used at each layer.
        kernel: An integer or tuple/list of 2 integers, specifying the
            width and height of the 2D convolution window.
            Can be a single integer to specify the same value for
            all spatial dimensions.
        strides: An integer or tuple/list of 2 integers,
            specifying the strides of the convolution
            along the width and height.
            Can be a single integer to specify the same value for
            all spatial dimensions.
            Specifying any stride value != 1 is incompatible with specifying
            any `dilation_rate` value != 1.

    # Input shape
        4D tensor with shape:
        `(samples, channels, rows, cols)` if data_format='channels_first'
        or 4D tensor with shape:
        `(samples, rows, cols, channels)` if data_format='channels_last'.

    # Output shape
        4D tensor with shape:
        `(samples, filters, new_rows, new_cols)`
        if data_format='channels_first'
        or 4D tensor with shape:
        `(samples, new_rows, new_cols, filters)`
        if data_format='channels_last'.
        `rows` and `cols` values might have changed due to stride.

    # Returns
        Output tensor of block.
    """
    channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1
    filters = int(filters * alpha)
    x = layers.ZeroPadding3D(padding=((0, 1), (0, 1), (0, 1)),
                             name='conv1_pad')(inputs)
    x = layers.Conv3D(filters,
                      kernel,
                      padding='valid',
                      use_bias=False,
                      strides=strides,
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
    return layers.ReLU(6., name='conv1_relu')(x)
def MobileNetV2(input_shape=None,
                alpha=1.0,
                include_top=True,
                weights='imagenet',
                input_tensor=None,
                pooling=None,
                classes=1000,
                **kwargs):
    """Instantiates the MobileNetV2 architecture.

    # Arguments
        input_shape: optional shape tuple, to be specified if you would
            like to use a model with an input img resolution that is not
            (224, 224, 3).
            It should have exactly 3 inputs channels (224, 224, 3).
            You can also omit this option if you would like
            to infer input_shape from an input_tensor.
            If you choose to include both input_tensor and input_shape then
            input_shape will be used if they match, if the shapes
            do not match then we will throw an error.
            E.g. `(160, 160, 3)` would be one valid value.
        alpha: controls the width of the network. This is known as the
        width multiplier in the MobileNetV2 paper, but the name is kept for
        consistency with MobileNetV1 in Keras.
            - If `alpha` < 1.0, proportionally decreases the number
                of filters in each layer.
            - If `alpha` > 1.0, proportionally increases the number
                of filters in each layer.
            - If `alpha` = 1, default number of filters from the paper
                 are used at each layer.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of
            `layers.Input()`)
            to use as image input for the model.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional block.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional block, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape or invalid alpha, rows when
            weights='imagenet'
    """
    global backend, layers, models, keras_utils
    backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs)

    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError(
            'If using `weights` as `"imagenet"` with `include_top` '
            'as true, `classes` should be 1000')

    # Determine proper input shape and default size.
    # If both input_shape and input_tensor are used, they should match
    if input_shape is not None and input_tensor is not None:
        try:
            is_input_t_tensor = backend.is_keras_tensor(input_tensor)
        except ValueError:
            try:
                is_input_t_tensor = backend.is_keras_tensor(
                    keras_utils.get_source_inputs(input_tensor))
            except ValueError:
                raise ValueError('input_tensor: ', input_tensor,
                                 'is not type input_tensor')
        if is_input_t_tensor:
            if backend.image_data_format == 'channels_first':
                if backend.int_shape(input_tensor)[1] != input_shape[1]:
                    raise ValueError(
                        'input_shape: ', input_shape, 'and input_tensor: ',
                        input_tensor,
                        'do not meet the same shape requirements')
            else:
                if backend.int_shape(input_tensor)[2] != input_shape[1]:
                    raise ValueError(
                        'input_shape: ', input_shape, 'and input_tensor: ',
                        input_tensor,
                        'do not meet the same shape requirements')
        else:
            raise ValueError('input_tensor specified: ', input_tensor,
                             'is not a keras tensor')

    # If input_shape is None, infer shape from input_tensor
    if input_shape is None and input_tensor is not None:

        try:
            backend.is_keras_tensor(input_tensor)
        except ValueError:
            raise ValueError('input_tensor: ', input_tensor, 'is type: ',
                             type(input_tensor), 'which is not a valid type')

        if input_shape is None and not backend.is_keras_tensor(input_tensor):
            default_size = 224
        elif input_shape is None and backend.is_keras_tensor(input_tensor):
            if backend.image_data_format() == 'channels_first':
                rows = backend.int_shape(input_tensor)[2]
                cols = backend.int_shape(input_tensor)[3]
            else:
                rows = backend.int_shape(input_tensor)[1]
                cols = backend.int_shape(input_tensor)[2]

            if rows == cols and rows in [96, 128, 160, 192, 224]:
                default_size = rows
            else:
                default_size = 224

    # If input_shape is None and no input_tensor
    elif input_shape is None:
        default_size = 224

    # If input_shape is not None, assume default size
    else:
        if backend.image_data_format() == 'channels_first':
            rows = input_shape[1]
            cols = input_shape[2]
        else:
            rows = input_shape[0]
            cols = input_shape[1]

        if rows == cols and rows in [96, 128, 160, 192, 224]:
            default_size = rows
        else:
            default_size = 224

    if backend.image_data_format() == 'channels_last':
        row_axis, col_axis = (0, 1)
    else:
        row_axis, col_axis = (1, 2)
    rows = input_shape[row_axis]
    cols = input_shape[col_axis]

    if weights == 'imagenet':
        if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]:
            raise ValueError('If imagenet weights are being loaded, '
                             'alpha can be one of `0.35`, `0.50`, `0.75`, '
                             '`1.0`, `1.3` or `1.4` only.')

        if rows != cols or rows not in [96, 128, 160, 192, 224]:
            rows = 224
            warnings.warn('`input_shape` is undefined or non-square, '
                          'or `rows` is not in [96, 128, 160, 192, 224].'
                          ' Weights for input shape (224, 224) will be'
                          ' loaded as the default.')

    if input_tensor is None:
        img_input = layers.Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1

    first_block_filters = _make_divisible(32 * alpha, 8)
    x = layers.ZeroPadding3D(padding=correct_pad(backend, img_input, 3),
                             name='Conv1_pad')(img_input)
    x = layers.Conv3D(first_block_filters,
                      kernel_size=3,
                      strides=(2, 2, 2),
                      padding='valid',
                      use_bias=False,
                      name='Conv1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  epsilon=1e-3,
                                  momentum=0.999,
                                  name='bn_Conv1')(x)
    x = layers.ReLU(6., name='Conv1_relu')(x)

    x = _inverted_res_block(x,
                            filters=16,
                            alpha=alpha,
                            stride=1,
                            expansion=1,
                            block_id=0)

    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=1)
    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=2)

    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=3)
    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=4)
    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=5)

    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=6)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=7)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=8)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=9)

    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=10)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=11)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=12)

    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=13)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=14)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=15)

    x = _inverted_res_block(x,
                            filters=320,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=16)

    # no alpha applied to last conv as stated in the paper:
    # if the width multiplier is greater than 1 we
    # increase the number of output channels
    if alpha > 1.0:
        last_block_filters = _make_divisible(1280 * alpha, 8)
    else:
        last_block_filters = 1280

    x = layers.Conv3D(last_block_filters,
                      kernel_size=1,
                      use_bias=False,
                      name='Conv_1')(x)
    x = layers.BatchNormalization(axis=channel_axis,
                                  epsilon=1e-3,
                                  momentum=0.999,
                                  name='Conv_1_bn')(x)
    x = layers.ReLU(6., name='out_relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling3D()(x)
        x = layers.Dense(classes,
                         activation='softmax',
                         use_bias=True,
                         name='Logits')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling3D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling3D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = keras_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    model = models.Model(inputs,
                         x,
                         name='mobilenetv2_%0.2f_%s' % (alpha, rows))

    # Load weights.
    if weights == 'imagenet':
        if include_top:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '.h5')
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(model_name,
                                                weight_path,
                                                cache_subdir='models')
        else:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '_no_top' + '.h5')
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(model_name,
                                                weight_path,
                                                cache_subdir='models')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
Exemplo n.º 12
0
def resnet18_3d(image_depth=16,
                image_rows=256,
                image_cols=256,
                input_channels=3,
                train_encoder=True):

    # Block 1
    # get parameters for model layers
    no_scale_bn_params = get_bn_params(train_encoder, scale=False)
    bn_params = get_bn_params()
    conv_params = get_conv_params()
    init_filters = 64

    # INPUT
    inputs = layers.Input(
        (image_depth, image_rows, image_cols, input_channels))
    # resnet bottom
    x = layers.BatchNormalization(name='bn_data', **no_scale_bn_params)(inputs)
    x = layers.ZeroPadding3D(padding=(0, 3, 3))(x)
    x = layers.Conv3D(init_filters, (1, 7, 7),
                      strides=(1, 1, 1),
                      name='conv0',
                      **conv_params)(x)
    x = layers.BatchNormalization(name='bn0', **bn_params)(x)
    x = layers.Activation('relu', name='relu0')(x)
    skip_connection_1 = x
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.MaxPooling3D((2, 3, 3),
                            strides=(2, 2, 2),
                            padding='valid',
                            name='pooling0')(x)

    # Stage 1, Unit 1 - Settings
    stage = 0
    block = 0
    strides = (1, 1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 1, Unit 2 - Settings
    stage = 0
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 1 - Settings
    stage = 1
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_2 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 2, Unit 2 - Settings
    stage = 1
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 1 - Settings
    stage = 2
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_3 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 3, Unit 2 - Settings
    stage = 2
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 1 - Settings
    stage = 3
    block = 0
    strides = (2, 2, 2)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 1 - Layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    skip_connection_4 = x
    # defining shortcut connection
    shortcut = layers.Conv3D(filters, (1, 1, 1),
                             name=sc_name,
                             strides=strides,
                             **conv_params)(x)
    # continue with convolution layers
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (2, 3, 3),
                      strides=strides,
                      name=conv_name + '1_convpool',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Stage 4, Unit 2 - Settings
    stage = 3
    block = 1
    strides = (1, 1, 1)
    filters = init_filters * (2**stage)
    conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
    # Stage 1, Block 2 - Layers
    # defining shortcut connection
    shortcut = x
    # continue with convolution layers
    x = layers.BatchNormalization(name=bn_name + '1', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '1')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3),
                      strides=strides,
                      name=conv_name + '1',
                      **conv_params)(x)
    x = layers.BatchNormalization(name=bn_name + '2', **bn_params)(x)
    x = layers.Activation('relu', name=relu_name + '2')(x)
    x = layers.ZeroPadding3D(padding=(0, 1, 1))(x)
    x = layers.Conv3D(filters, (1, 3, 3), name=conv_name + '2',
                      **conv_params)(x)
    x = layers.Add()([x, shortcut])

    # Resnet OUTPUT
    x = layers.BatchNormalization(name='bn1', **bn_params)(x)
    x = layers.Activation('relu', name='relu1')(x)

    model = Model(inputs=[inputs], outputs=[x], name='resnet18')

    model.summary()

    plot_model(model, to_file='model.png')

    # weights_path = utils.get_file('resnet18_imagenet_1000_no_top.h5.h5', WEIGHTS_PATH_NO_TOP,
    #     cache_subdir='models')
    #
    # model.load_weights(weights_path)

    return model