Пример #1
0
def add_residual_dense_block(in_layer, filter_dims, num_layers, act_func=tf.nn.relu, bn_phaze=False, scope='residual_dense_block'):
    with tf.variable_scope(scope):
        l = in_layer
        input_dims = in_layer.get_shape().as_list()
        num_channel_in = input_dims[-1]
        num_channel_out = input_dims[-1]

        for i in range(num_layers):
            l = layers.add_dense_layer(l, filter_dims=filter_dims, act_func=act_func, bn_phaze=bn_phaze,
                                       scope='layer' + str(i))
        l = layers.add_dense_transition_layer(l, filter_dims=[1, 1, num_channel_out], act_func=act_func,
                                     scope='dense_transition_1', bn_phaze=bn_phaze, use_pool=False)

        l = tf.add(l, in_layer)

    return l
Пример #2
0
def cnn_network(x, activation='swish', scope='cnn_network', reuse=False, bn_phaze=False, keep_prob=0.5):
    with tf.variable_scope(scope):
        if reuse:
            tf.get_variable_scope().reuse_variables()

        if activation == 'swish':
            act_func = util.swish
        elif activation == 'relu':
            act_func = tf.nn.relu
        else:
            act_func = tf.nn.sigmoid

        l = layers.conv(x, scope='conv1', filter_dims=[5, 5, 256], stride_dims=[1, 1], non_linear_fn=None, bias=False)

        with tf.variable_scope('dense_block_1'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func, bn_phaze=bn_phaze,
                                    scope='layer' + str(i))
            l = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth], act_func=act_func,
                                     scope='dense_transition_1', bn_phaze=bn_phaze)

        l = add_residual_dense_block(l, filter_dims=[3, 3, g_dense_block_depth], num_layers=3,
                                     act_func=act_func, bn_phaze=bn_phaze, scope='block_1')

        l = tf.nn.max_pool(l, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

        l = add_residual_dense_block(l, filter_dims=[3, 3, g_dense_block_depth], num_layers=3,
                                     act_func=act_func, bn_phaze=bn_phaze, scope='block_2')

        l = tf.nn.max_pool(l, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

        l = add_residual_dense_block(l, filter_dims=[3, 3, g_dense_block_depth], num_layers=3,
                                     act_func=act_func, bn_phaze=bn_phaze, scope='block_3')

        l = tf.nn.max_pool(l, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

        l = add_residual_dense_block(l, filter_dims=[3, 3, g_dense_block_depth], num_layers=3,
                                     act_func=act_func, bn_phaze=bn_phaze, scope='block_4')

        l = add_residual_dense_block(l, filter_dims=[3, 3, g_dense_block_depth], num_layers=3,
                                     act_func=act_func, bn_phaze=bn_phaze, scope='block_5')

        with tf.variable_scope('dense_block_last'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                           bn_phaze=bn_phaze, scope='layer' + str(i))
            scale_layer = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth],
                                                            act_func=act_func,
                                                            scope='dense_transition_1', bn_phaze=bn_phaze,
                                                            use_pool=False)
            last_dense_layer = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth],
                                                                 act_func=act_func,
                                                                 scope='dense_transition_2', bn_phaze=bn_phaze,
                                                                 use_pool=False)
            scale_layer = act_func(scale_layer)
            last_dense_layer = act_func(last_dense_layer)

        '''
        with tf.variable_scope('residual_block_1'):
            r = layers.add_residual_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer1')
            r = layers.add_residual_layer(r, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer2')
            l = tf.add(l, r)

        with tf.variable_scope('dense_block_2'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func, bn_phaze=bn_phaze,
                                    scope='layer' + str(i))
            l = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth], act_func=act_func,
                                     scope='dense_transition_1', bn_phaze=bn_phaze)

        with tf.variable_scope('residual_block_2'):
            r = layers.add_residual_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer1')
            r = layers.add_residual_layer(r, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer2')
            l = tf.add(l, r)

        with tf.variable_scope('dense_block_3'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func, bn_phaze=bn_phaze,
                                    scope='layer' + str(i))
            l = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth], act_func=act_func,
                                    scope='dense_transition_1', bn_phaze=bn_phaze)

        with tf.variable_scope('residual_block_3'):
            r = layers.add_residual_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer1')
            r = layers.add_residual_layer(r, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer2')
            l = tf.add(l, r)

        with tf.variable_scope('dense_block_4'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func, bn_phaze=bn_phaze,
                                           scope='layer' + str(i))
            l = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth], act_func=act_func,
                                                  scope='dense_transition_1', bn_phaze=bn_phaze)

        with tf.variable_scope('residual_block_4'):
            r = layers.add_residual_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer1')
            r = layers.add_residual_layer(r, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                          bn_phaze=bn_phaze, scope='layer2')
            l = tf.add(l, r)
        
        with tf.variable_scope('dense_block_last'):
            for i in range(g_dense_block_layers):
                l = layers.add_dense_layer(l, filter_dims=[3, 3, g_dense_block_depth], act_func=act_func,
                                    bn_phaze=bn_phaze, scope='layer' + str(i))
            scale_layer = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth], act_func=act_func,
                                                  scope='dense_transition_1', bn_phaze=bn_phaze, use_pool=False)
            last_dense_layer = layers.add_dense_transition_layer(l, filter_dims=[1, 1, g_dense_block_depth],
                                                            act_func=act_func,
                                                            scope='dense_transition_2', bn_phaze=bn_phaze, use_pool=False)
            scale_layer = act_func(scale_layer)
            last_dense_layer = act_func(last_dense_layer)
        '''

        return last_dense_layer, scale_layer