Ejemplo n.º 1
0
def inception_resnet(input_tensor, n, t1=2, t2=3, n_pool=3, scale=0.1):

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

    tower_1 = bunit.conv3d_bn(input_tensor, n, (1, 1, 1), padding='same')
    tower_1 = bunit.conv3d_bn(tower_1, n, (t1, 1, 1), padding='same')
    tower_1 = bunit.conv3d_bn(tower_1, n, (1, t1, 1), padding='same')
    tower_1 = bunit.conv3d_bn(tower_1, n, (1, 1, t1), padding='same')

    tower_2 = bunit.conv3d_bn(input_tensor, n, (1, 1, 1), padding='same')
    tower_2 = bunit.conv3d_bn(tower_2, n, (t2, 1, 1), padding='same')
    tower_2 = bunit.conv3d_bn(tower_2, n, (1, t2, 1), padding='same')
    tower_2 = bunit.conv3d_bn(tower_2, n, (1, 1, t2), padding='same')

    tower_3 = keras.layers.MaxPooling3D((n_pool, n_pool, n_pool),
                                        strides=(1, 1, 1),
                                        padding='same')(input_tensor)
    tower_3 = bunit.conv3d_bn(tower_3, n, (1, 1, 1), padding='same')

    up = keras.layers.concatenate([tower_1, tower_2, tower_3],
                                  axis=channel_axis)

    x = keras.layers.Lambda(
        lambda inputs, scale: inputs[0] + inputs[1] * scale,
        output_shape=K.int_shape(input_tensor)[1:],
        arguments={'scale': scale},
    )([input_tensor, up])

    return x
Ejemplo n.º 2
0
def inception_block4(input_tensor, n, t0=2, t1=4, t2=5, n_pool=3, scale=0.1):

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

    tower_0 = bunit.conv3d_bn(input_tensor, n, (t0,1,1), padding='same')
    tower_0 = bunit.conv3d_bn(tower_0, n, (1,t0,1), padding='same')
    tower_0 = bunit.conv3d_bn(tower_0, n, (1,1,t0), padding='same')

    tower_1 = bunit.conv3d_bn(input_tensor, n, (t1,1,1), padding='same')
    tower_1 = bunit.conv3d_bn(tower_1, n, (1,t1,1), padding='same')
    tower_1 = bunit.conv3d_bn(tower_1, n, (1,1,t1), padding='same')

    tower_4 = bunit.conv3d_bn(input_tensor, n, (1,1,t2), padding='same')

    tower_3 = keras.layers.MaxPooling3D((n_pool, n_pool, n_pool),
                                        strides=(1,1,1), padding='same')(input_tensor)
    tower_3 = bunit.conv3d_bn(tower_3, n, (1,1,1), padding='same')

    up = keras.layers.concatenate(
        [tower_0, tower_1, tower_3, tower_4], axis = channel_axis)
    return up
Ejemplo n.º 3
0
def InceptionResNetV2_small(input_tensor=None, pooling=None, kernel=(2, 2, 3)):
    """Instantiates the Inception-ResNet v2 architecture.
    Optionally loads weights pre-trained on ImageNet.
    Note that when using TensorFlow, for best performance you should
    set `"image_data_format": "channels_last"` in your Keras config
    at `~/.keras/keras.json`.
    The model and the weights are compatible with both TensorFlow and Theano
    backends (but not CNTK). The data format convention used by the model is
    the one specified in your Keras config file.
    Note that the default input image size for this model is 299x299, instead
    of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
    function is different (i.e., do not use `imagenet_utils.preprocess_input()`
    with this model. Use `preprocess_input()` defined in this module instead).
    # Arguments
        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 layer.
            - `'avg'` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `'max'` means that global max pooling will be applied.
    # Returns
        Output tensor for the block
    """

    # Stem block: 35 x 35 x 192
    x = bunit.conv3d_bn(input_tensor, 32, kernel, padding='same')
    x = bunit.conv3d_bn(x, 32, kernel, padding='same')
    x = bunit.conv3d_bn(x, 64, kernel)
    x = MaxPooling3D((1, 1, 2), strides=1)(x)
    x = bunit.conv3d_bn(x, 80, 1, padding='same')
    x = bunit.conv3d_bn(x, 96, kernel, padding='same')
    x = MaxPooling3D((2, 2, 3), strides=(1, 1, 2))(x)

    # Mixed 5b (Inception-A block): 35 x 35 x 320
    branch_0 = bunit.conv3d_bn(x, 96, 1, padding='same')
    branch_1 = bunit.conv3d_bn(x, 48, 1)
    branch_1 = bunit.conv3d_bn(branch_1, 64, 5, padding='same')
    branch_2 = bunit.conv3d_bn(x, 64, 1)
    branch_2 = bunit.conv3d_bn(branch_2, 96, 3)
    branch_2 = bunit.conv3d_bn(branch_2, 96, 3, padding='same')
    branch_pool = AveragePooling3D(3, 1, padding='same')(x)
    branch_pool = bunit.conv3d_bn(branch_pool, 64, 1)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    print('imaga_data_format {}'.format(K.image_data_format()))
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    x = Concatenate(axis=channel_axis, name='mixed_5b')(branches)

    # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
    for block_idx in range(1, 11):
        x = bunit.inception_resnet_block(x,
                                         scale=0.17,
                                         block_type='block35',
                                         block_idx=block_idx)

    # Mixed 6a (Reduction-A block): 17 x 17 x 1088
    branch_0 = bunit.conv3d_bn(x,
                               192, (2, 2, 3),
                               strides=(2, 2, 2),
                               padding='valid')
    branch_1 = bunit.conv3d_bn(x, 128, 1)
    branch_1 = bunit.conv3d_bn(branch_1, 128, 2)
    branch_1 = bunit.conv3d_bn(branch_1,
                               192, (2, 2, 3),
                               strides=(2, 2, 2),
                               padding='valid')
    branch_pool = MaxPooling3D((2, 2, 3), strides=(2, 2, 2),
                               padding='valid')(x)
    branches = [branch_0, branch_1, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)

    # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
    for block_idx in range(1, 11):
        x = bunit.inception_resnet_block(x,
                                         scale=0.1,
                                         block_type='block17',
                                         block_idx=block_idx)

    x = bunit.inception_resnet_block(x,
                                     scale=1.,
                                     activation=None,
                                     block_type='block8',
                                     block_idx=10)

    # Final convolution block: 8 x 8 x 1536
    x = bunit.conv3d_bn(x, 3200, 1, name='conv_7b')

    if pooling == 'avg':
        x = GlobalAveragePooling3D()(x)
    elif pooling == 'max':
        x = GlobalMaxPooling3D()(x)
    return x