Beispiel #1
0
    def __init__(self, cfg, in_channels):
        super(FCOSHead, self).__init__()

        num_classes = cfg.MODEL.FCOS.NUM_CLASSES - 1

        cls_tower = []
        bbox_tower = []

        for _ in range(cfg.MODEL.FCOS.NUM_CONVS):
            conv = Conv2D(
                in_channels,
                kernel_size=3,
                strides=1,
                kernel_initializer=tf.random_normal_initializer(stddev=0.01),
                bias_initializer=tf.constant_initializer())
            cls_tower.append(conv)
            cls_tower.append(group_norm(conv))
            cls_tower.append(ReLU())

            bbox_tower.append(conv)
            bbox_tower.append(group_norm(conv))
            bbox_tower.append(ReLU())

        self.cls_tower = Sequential(layers=cls_tower, name='cls_tower')
        self.bbx_tower = Sequential(layers=bbox_tower, name='bbox_tower')

        prior_prob = cfg.MODEL.FCOS.PRIOR_PROB
        bias_value = -tf.log((1 - prior_prob) / prior_prob)

        self.cls_logit = Conv2D(
            num_classes,
            kernel_size=3,
            strides=1,
            kernel_initializer=tf.random_normal_initializer(stddev=0.01),
            bias_initializer=tf.constant_initializer(bias_value))
        self.bbox_pred = Conv2D(
            4,
            kernel_size=3,
            strides=1,
            kernel_initializer=tf.random_normal_initializer(stddev=0.01),
            bias_initializer=tf.constant_initializer())
        self.centerness = Conv2D(
            1,
            kernel_size=3,
            strides=1,
            kernel_initializer=tf.random_normal_initializer(stddev=0.01),
            bias_initializer=tf.constant_initializer())
Beispiel #2
0
 def __call__(self, last, reuse=None):
     format = self.data_format
     # function objects
     activation = self.activation
     if self.normalization == 'Batch':
         normalizer = lambda x: slim.batch_norm(x, 0.999, center=True, scale=True,
             is_training=self.training, data_format=format, renorm=False)
     elif self.normalization == 'Instance':
         normalizer = lambda x: slim.instance_norm(x, center=True, scale=True, data_format=format)
     elif self.normalization == 'Group':
         normalizer = lambda x: (slim.group_norm(x, x.shape[-3].value // 16, -3, (-2, -1))
             if format == 'NCHW' else slim.group_norm(x, x.shape[-1].value // 16, -1, (-3, -2)))
     else:
         normalizer = None
     regularizer = slim.l2_regularizer(self.weight_decay) if self.weight_decay else None
     # main model
     with tf.variable_scope(self.name, reuse=reuse):
         self.training = tf.Variable(False, trainable=False, name='training',
             collections=[tf.GraphKeys.GLOBAL_VARIABLES, tf.GraphKeys.MODEL_VARIABLES])
         last = self.def_model(last, activation, normalizer, regularizer)
     # trainable/model/save/restore variables
     self.tvars = tf.trainable_variables(self.name)
     self.mvars = tf.model_variables(self.name)
     self.mvars = [var for var in self.mvars if var not in self.tvars]
     self.svars = list(set(self.tvars + self.mvars))
     self.rvars = self.svars.copy()
     # variables that should be affected by weight decay
     import re
     self.wdvars = [var for var in self.tvars if re.findall(r'weight|kernel', var.name.split('/')[-1])]
     # restore moving average of trainable variables
     if self.var_ema > 0:
         with tf.variable_scope('EMA'):
             self.rvars = {**{self.ema.average_name(var): var for var in self.tvars},
                 **{var.op.name: var for var in self.mvars}}
     # return
     return last
Beispiel #3
0
def feature_normalize(x, normalization, name_space, reuse, bn_training, bn_decay):
    if normalization == 'W/O' or normalization is None:
        y = x
    elif normalization == 'batch':
        if bn_training is None or bn_decay is None:
            raise ValueError('The bn_training and bn_decay must have value'
                             ' when enable Batch Normalization !!!')
        y = tf_layers.batch_norm(x,
                                 reuse=reuse,
                                 is_training=bn_training,
                                 decay=bn_decay,
                                 scope=name_space + '/batch_norm')
    elif normalization == 'instance':
        y = tf_layers.instance_norm(x,
                                    reuse=reuse,
                                    scope=name_space + '/instance_norm')
    elif normalization == 'group':
        y = tf_layers.group_norm(x,
                                 reuse=reuse,
                                 scope=name_space + '/group_norm')
    else:
        raise ValueError('Unknown feature normalization method !!!')
    return y
Beispiel #4
0
def group_norm_tf(x, training=False, reuse=False, scope=None):
    return group_norm(x, reuse=reuse)
Beispiel #5
0
def group_norm(x, scope='group_normalization'):
    return layers.group_norm(x, scope=scope)
Beispiel #6
0
 def __call__(self, last, reuse=None):
     format = self.data_format
     kernel1 = [1, 8]
     stride1 = [1, 2]
     kernel2 = [1, 3]
     stride2 = [1, 2]
     # function objects
     activation = self.activation
     if self.normalization == 'Batch':
         normalizer = lambda x: slim.batch_norm(x,
                                                0.999,
                                                center=True,
                                                scale=True,
                                                is_training=self.training,
                                                data_format=format,
                                                renorm=False)
     elif self.normalization == 'Instance':
         normalizer = lambda x: slim.instance_norm(
             x, center=True, scale=True, data_format=format)
     elif self.normalization == 'Group':
         normalizer = lambda x: (slim.group_norm(
             x,
             x.shape.as_list()[-3] // 16, -3,
             (-2, -1)) if format == 'NCHW' else slim.group_norm(
                 x,
                 x.shape.as_list()[-1] // 16, -1, (-3, -2)))
     else:
         normalizer = None
     regularizer = slim.l2_regularizer(
         self.weight_decay) if self.weight_decay else None
     skip_connection = lambda x, y: x + y
     # skip_connection = lambda x, y: tf.concat([x, y], -3 if format == 'NCHW' else -1)
     # model scope
     with tf.variable_scope(self.name, reuse=reuse):
         # states
         self.training = tf.Variable(False,
                                     trainable=False,
                                     name='training',
                                     collections=[
                                         tf.GraphKeys.GLOBAL_VARIABLES,
                                         tf.GraphKeys.MODEL_VARIABLES
                                     ])
         skips = []
         # encoder
         with tf.variable_scope('InBlock'):
             last = self.EBlock(last, 16, 0, [1, 8], [1, 1], format, None,
                                None, regularizer)
         with tf.variable_scope('EBlock_0'):
             skips.append(last)
             last = self.EBlock(last, 32, 0, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_1'):
             skips.append(last)
             last = self.EBlock(last, 48, 0, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_2'):
             skips.append(last)
             last = self.EBlock(last, 64, 1, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_3'):
             skips.append(last)
             last = self.EBlock(last, 96, 1, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_4'):
             skips.append(last)
             last = self.EBlock(last, 128, 2, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_5'):
             skips.append(last)
             last = self.EBlock(last, 160, 2, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_6'):
             skips.append(last)
             last = self.EBlock(last, 192, 2, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_7'):
             skips.append(last)
             last = self.EBlock(last, 224, 3, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_8'):
             skips.append(last)
             last = self.EBlock(last, 256, 3, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         # decoder
         with tf.variable_scope('DBlock_8'):
             last = self.DBlock(last, 224, 3, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_7'):
             last = self.DBlock(last, 192, 2, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_6'):
             last = self.DBlock(last, 160, 2, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_5'):
             last = self.DBlock(last, 128, 2, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_4'):
             last = self.DBlock(last, 96, 1, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_3'):
             last = self.DBlock(last, 64, 1, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_2'):
             last = self.DBlock(last, 48, 0, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_1'):
             last = self.DBlock(last, 32, 0, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('DBlock_0'):
             last = self.DBlock(last, 16, 0, kernel2, stride2, format,
                                activation, normalizer, regularizer)
             last = skip_connection(last, skips.pop())
         with tf.variable_scope('OutBlock'):
             last = self.EBlock(last, self.in_channels, 0, [1, 8], [1, 1],
                                format, activation, normalizer, regularizer)
     # trainable/model/save/restore variables
     self.tvars = tf.trainable_variables(self.name)
     self.mvars = tf.model_variables(self.name)
     self.mvars = [i for i in self.mvars if i not in self.tvars]
     self.svars = list(set(self.tvars + self.mvars))
     self.rvars = self.svars.copy()
     # restore moving average of trainable variables
     if self.var_ema > 0:
         with tf.variable_scope('EMA'):
             self.rvars = {
                 **{self.ema.average_name(var): var
                    for var in self.tvars},
                 **{var.op.name: var
                    for var in self.mvars}
             }
     return last
Beispiel #7
0
 def __call__(self, last, reuse=None):
     # parameters
     format = self.data_format
     kernel1 = [3, 3]
     stride1 = [2, 2]
     # function objects
     activation = self.activation
     if self.normalization == 'Batch':
         normalizer = lambda x: slim.batch_norm(x,
                                                0.999,
                                                center=True,
                                                scale=True,
                                                is_training=self.training,
                                                data_format=format,
                                                renorm=False)
     elif self.normalization == 'Instance':
         normalizer = lambda x: slim.instance_norm(
             x, center=True, scale=True, data_format=format)
     elif self.normalization == 'Group':
         normalizer = lambda x: (slim.group_norm(
             x,
             x.shape.as_list()[-3] // 16, -3,
             (-2, -1)) if format == 'NCHW' else slim.group_norm(
                 x,
                 x.shape.as_list()[-1] // 16, -1, (-3, -2)))
     else:
         normalizer = None
     regularizer = slim.l2_regularizer(
         self.weight_decay) if self.weight_decay else None
     # model scope
     with tf.variable_scope(self.name, reuse=reuse):
         # states
         self.training = tf.Variable(False,
                                     trainable=False,
                                     name='training',
                                     collections=[
                                         tf.GraphKeys.GLOBAL_VARIABLES,
                                         tf.GraphKeys.MODEL_VARIABLES
                                     ])
         # encoder
         with tf.variable_scope('InBlock'):
             last = self.InBlock(last, 32, [7, 7], [1, 1], format,
                                 activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_1'):
             last = self.EBlock(last, 1, 48, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_2'):
             last = self.EBlock(last, 1, 64, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_3'):
             last = self.EBlock(last, 1, 96, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_4'):
             last = self.EBlock(last, 1, 128, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_5'):
             last = self.EBlock(last, 1, 192, kernel1, stride1, format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('EBlock_6'):
             last = self.EBlock(last, 1, 256, kernel1, [1, 1], format,
                                activation, normalizer, regularizer)
         with tf.variable_scope('PatchCritic'):
             last_channels = last.shape.as_list()[-3]
             critic_logit = self.ResBlock(last,
                                          last_channels, [3, 3], [1, 1],
                                          format=format,
                                          activation=activation,
                                          normalizer=normalizer,
                                          regularizer=regularizer)
             critic_logit = slim.conv2d(critic_logit,
                                        1, [7, 7], [1, 1],
                                        'SAME',
                                        format,
                                        1,
                                        None,
                                        None,
                                        weights_regularizer=regularizer)
         with tf.variable_scope('GlobalAveragePooling'):
             last = tf.reduce_mean(
                 last, [-2, -1] if format == 'NCHW' else [-3, -2])
         with tf.variable_scope('Domain'):
             last_channels = last.shape.as_list()[-1]
             domain_logit = slim.fully_connected(
                 last,
                 last_channels,
                 activation,
                 None,
                 weights_regularizer=regularizer)
             domain_logit = slim.fully_connected(
                 domain_logit,
                 self.num_domains,
                 None,
                 None,
                 weights_regularizer=regularizer)
     # trainable/model/save/restore variables
     self.tvars = tf.trainable_variables(self.name)
     self.mvars = tf.model_variables(self.name)
     self.mvars = [i for i in self.mvars if i not in self.tvars]
     self.svars = list(set(self.tvars + self.mvars))
     self.rvars = self.svars.copy()
     return critic_logit, domain_logit