Esempio n. 1
0
def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1, scope=None):
    with tf.variable_scope(scope, 'bottleneck_v1') as sc:
        depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
        if depth == depth_in:
            shortcut = subsample(inputs, stride, 'shortcut')
        else:
            shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
                                        b_init=None, name='shortcut_conv')
            shortcut = GroupNormLayer(layer=shortcut, act=tf.identity, name='shortcut_bn/BatchNorm')
        # bottleneck layer 1
        residual = tl.layers.Conv2d(inputs, depth_bottleneck, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
                                    name='conv1')
        residual = GroupNormLayer(layer=residual, act=tf.nn.relu, name='conv1_bn/BatchNorm')

        # bottleneck layer 2
        residual = conv2d_same(residual, depth_bottleneck, kernel_size=3, strides= stride, rate=rate, scope='conv2')

        # bottleneck layer 3
        residual = tl.layers.Conv2d(residual, depth, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
                                    name='conv3')
        residual = GroupNormLayer(layer=residual, act=tf.identity, name='conv3_bn/BatchNorm',
                                  scale_init=tf.constant_initializer(0.0))
        output = ElementwiseLayer(layer=[shortcut, residual],
                                  combine_fn=tf.add,
                                  name='combine_layer',
                                  act=tf.nn.relu)
        return output
Esempio n. 2
0
def bottleneck_IR(inputs,
                  depth,
                  depth_bottleneck,
                  stride,
                  rate=1,
                  w_init=None,
                  scope=None,
                  trainable=None):
    with tf.variable_scope(scope, 'bottleneck_v1') as sc:
        depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
        if depth == depth_in:
            shortcut = subsample(inputs, stride, 'shortcut')
        else:
            shortcut = tl.layers.Conv2d(inputs,
                                        depth,
                                        filter_size=(1, 1),
                                        strides=(stride, stride),
                                        act=None,
                                        W_init=w_init,
                                        b_init=None,
                                        name='shortcut_conv',
                                        use_cudnn_on_gpu=True)
            shortcut = GroupNormLayer(layer=shortcut,
                                      act=tf.identity,
                                      name='shortcut_bn/BatchNorm')
        # bottleneck layer 1
        residual = GroupNormLayer(layer=inputs,
                                  act=tf.identity,
                                  name='conv1_bn1')
        residual = tl.layers.Conv2d(residual,
                                    depth_bottleneck,
                                    filter_size=(3, 3),
                                    strides=(1, 1),
                                    act=None,
                                    b_init=None,
                                    W_init=w_init,
                                    name='conv1',
                                    use_cudnn_on_gpu=True)
        residual = GroupNormLayer(layer=residual,
                                  act=tf.identity,
                                  name='conv1_bn2')
        # bottleneck prelu
        residual = tl.layers.PReluLayer(residual)
        # bottleneck layer 2
        residual = conv2d_same(residual,
                               depth,
                               kernel_size=3,
                               strides=stride,
                               rate=rate,
                               w_init=w_init,
                               scope='conv2',
                               trainable=trainable)
        output = ElementwiseLayer(layer=[shortcut, residual],
                                  combine_fn=tf.add,
                                  name='combine_layer',
                                  act=None)
        return output
Esempio n. 3
0
def resnet(inputs,
           bottle_neck,
           blocks,
           w_init=None,
           trainable=None,
           scope=None):
    with tf.variable_scope(scope):
        net_inputs = tl.layers.InputLayer(inputs, name='input_layer')
        if bottle_neck:
            net = tl.layers.Conv2d(net_inputs,
                                   n_filter=64,
                                   filter_size=(3, 3),
                                   strides=(1, 1),
                                   act=None,
                                   W_init=w_init,
                                   b_init=None,
                                   name='conv1',
                                   use_cudnn_on_gpu=True)
            net = GroupNormLayer(layer=net,
                                 act=tf.identity,
                                 name='group_norm_0')
            net = tl.layers.PReluLayer(net, name='prelu0')
        else:
            raise ValueError(
                'The standard resnet must support the bottleneck layer')
        for block in blocks:
            with tf.variable_scope(block.scope):
                for i, var in enumerate(block.args):
                    with tf.variable_scope('unit_%d' % (i + 1)):
                        net = block.unit_fn(
                            net,
                            depth=var['depth'],
                            depth_bottleneck=var['depth_bottleneck'],
                            w_init=w_init,
                            stride=var['stride'],
                            rate=var['rate'],
                            scope=None,
                            trainable=trainable)
        net = GroupNormLayer(layer=net, act=tf.identity, name='E_GN_0')
        net = tl.layers.DropoutLayer(net, keep=0.4, name='E_Dropout')
        net_shape = net.outputs.get_shape()
        net = tl.layers.ReshapeLayer(
            net,
            shape=[-1, net_shape[1] * net_shape[2] * net_shape[3]],
            name='E_Reshapelayer')
        net = tl.layers.DenseLayer(net,
                                   n_units=512,
                                   W_init=w_init,
                                   name='E_DenseLayer')
        # net = GroupNormLayer(layer=net, act=tf.identity, name='E_GN_1')
        return net
Esempio n. 4
0
def conv2d_same(inputs, num_outputs, kernel_size, strides, rate=1, w_init=None, scope=None, trainable=None):
    '''
    Reference slim resnet
    :param inputs:
    :param num_outputs:
    :param kernel_size:
    :param strides:
    :param rate:
    :param scope:
    :return:
    '''
    if strides == 1:
        if rate == 1:
            nets = tl.layers.Conv2d(inputs, n_filter=num_outputs, filter_size=(kernel_size, kernel_size), b_init=None,
                                   strides=(strides, strides), W_init=w_init, act=None, padding='SAME', name=scope,
                                    use_cudnn_on_gpu=True)
            nets = GroupNormLayer(layer=nets, act=tf.identity, name=scope+'_bn/GroupNorm')
        else:
            nets = tl.layers.AtrousConv2dLayer(inputs, n_filter=num_outputs, filter_size=(kernel_size, kernel_size),
                                               rate=rate, act=None, W_init=w_init, padding='SAME', name=scope)
            nets = GroupNormLayer(layer=nets, act=tf.identity, name=scope+'_bn/GroupNorm')
        return nets
    else:
        kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1)
        pad_total = kernel_size_effective - 1
        pad_beg = pad_total // 2
        pad_end = pad_total - pad_beg
        inputs = tl.layers.PadLayer(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]], name='padding_%s' % scope)
        if rate == 1:
            nets = tl.layers.Conv2d(inputs, n_filter=num_outputs, filter_size=(kernel_size, kernel_size), b_init=None,
                                    strides=(strides, strides), W_init=w_init, act=None, padding='VALID', name=scope,
                                    use_cudnn_on_gpu=True)
            nets = GroupNormLayer(layer=nets, act=tf.identity, name=scope+'_bn/GroupNorm')
        else:
            nets = tl.layers.AtrousConv2dLayer(inputs, n_filter=num_outputs, filter_size=(kernel_size, kernel_size), b_init=None,
                                              rate=rate, act=None, W_init=w_init, padding='SAME', name=scope)
            nets = GroupNormLayer(layer=nets, act=tf.identity, name=scope+'_bn/GroupNorm')
        return nets
Esempio n. 5
0
def bottleneck_IR_SE(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
    with tf.variable_scope(scope, 'bottleneck_v1') as sc:
        depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
        if depth == depth_in:
            shortcut = subsample(inputs, stride, 'shortcut')
        else:
            shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
                                        W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
            shortcut = GroupNormLayer(layer=shortcut, act=tf.identity, name='shortcut_bn/BatchNorm')
        residual = GroupNormLayer(layer=inputs, act=tf.identity, name='conv1_bn1')
        residual = tl.layers.Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
                                    W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
        residual = GroupNormLayer(layer=residual, act=tf.identity, name='conv1_bn2')
        # bottleneck prelu
        residual = tl.layers.PReluLayer(residual)
        # bottleneck layer 2
        residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
        # squeeze
        squeeze = tl.layers.InputLayer(tf.reduce_mean(residual.outputs, axis=[1, 2]), name='squeeze_layer')
        # excitation
        excitation1 = tl.layers.DenseLayer(squeeze, n_units=int(depth/16.0), act=tf.nn.relu,
                                           W_init=w_init, name='excitation_1')
        # excitation1 = tl.layers.PReluLayer(excitation1, name='excitation_prelu')
        excitation2 = tl.layers.DenseLayer(excitation1, n_units=depth, act=tf.nn.sigmoid,
                                           W_init=w_init, name='excitation_2')
        # scale
        scale = tl.layers.ReshapeLayer(excitation2, shape=[tf.shape(excitation2.outputs)[0], 1, 1, depth], name='excitation_reshape')

        residual_se = ElementwiseLayer(layer=[residual, scale],
                                       combine_fn=tf.multiply,
                                       name='scale_layer',
                                       act=None)

        output = ElementwiseLayer(layer=[shortcut, residual_se],
                                  combine_fn=tf.add,
                                  name='combine_layer',
                                  act=tf.nn.relu)
        return output