Ejemplo n.º 1
0
 def call(self, new_node, training):
   if not self.conv_bn_act_pattern:
     new_node = utils.activation_fn(new_node, self.act_type)
   new_node = self.conv_op(new_node)
   new_node = self.bn(new_node, training=training)
   if self.conv_bn_act_pattern:
     new_node = utils.activation_fn(new_node, self.act_type)
   return new_node
Ejemplo n.º 2
0
    def test_swish(self):
        features = tf.constant([.5, 10])

        result = utils.activation_fn(features, 'swish')
        expected = features * tf.sigmoid(features)
        self.assertAllClose(result, expected)

        result = utils.activation_fn(features, 'swish_native')
        self.assertAllClose(result, expected)
Ejemplo n.º 3
0
 def call(self, new_node):
     if not self.conv_bn_act_pattern:
         new_node = utils.activation_fn(new_node, self.act_type)
     new_node = self.conv_op(new_node)
     new_node = self.bn(new_node, training=self.is_training)
     act_type = None if not self.conv_bn_act_pattern else self.act_type
     if act_type:
         new_node = utils.activation_fn(new_node, act_type)
     return new_node
Ejemplo n.º 4
0
    def test_activation_compatibility(self):

        for act_type in ['swish', 'swish_native', 'relu', 'relu6']:
            act = utils_keras.ActivationFn(act_type)
            for i in range(-2, 2):
                i = float(i)
                self.assertEqual(
                    utils.activation_fn(i, act_type).numpy(),
                    act.call(i).numpy())
Ejemplo n.º 5
0
 def _call(image):
     original_image = image
     image = conv_op(image)
     image = bn(image, training=training)
     if self.act_type:
         image = utils.activation_fn(image, act_type)
     if i > 0 and self.survival_prob:
         image = utils.drop_connect(image, training, self.survival_prob)
         image = image + original_image
     return image
Ejemplo n.º 6
0
  def call(self, feats, training):
    x = feats[-1]
    skips = list(reversed(feats[:-1]))

    for con2d_t, con2d_t_bn, skip in zip(self.con2d_ts, self.con2d_t_bns,
                                         skips):
      x = con2d_t(x)
      x = con2d_t_bn(x, training)
      x = utils.activation_fn(x, self.act_type)
      x = tf.concat([x, skip], axis=-1)

    # This is the last layer of the model
    return self.head_transpose(x)  # 64x64 -> 128x128
Ejemplo n.º 7
0
  def call(self, inputs, training):
    """Call boxnet."""
    box_outputs = []
    for level_id in range(0, self.max_level - self.min_level + 1):
      image = inputs[level_id]
      for i in range(self.repeats):
        original_image = image
        image = self.conv_ops[i](image)
        image = self.bns[i][level_id](image, training=training)
        if self.act_type:
          image = utils.activation_fn(image, self.act_type)
        if i > 0 and self.survival_prob:
          image = utils.drop_connect(image, training, self.survival_prob)
          image = image + original_image

      box_outputs.append(self.boxes(image))

    return box_outputs
Ejemplo n.º 8
0
    def call(self, inputs, **kwargs):
        """Call boxnet."""
        box_outputs = {}
        for level in range(self.min_level, self.max_level + 1):
            image = inputs[level]
            for i in range(self.repeats):
                original_image = image
                image = self.conv_ops[i](image)
                image = self.bns[i][level](image, training=self.is_training)
                if self.act_type:
                    image = utils.activation_fn(image, self.act_type)
                if i > 0 and self.survival_prob:
                    image = utils.drop_connect(image, self.is_training,
                                               self.survival_prob)
                    image = image + original_image

            box_outputs[level] = self.boxes(image)

        return box_outputs
Ejemplo n.º 9
0
    def call(self, inputs, **kwargs):
        """Call ClassNet."""

        class_outputs = []
        for level in range(self.min_level, self.max_level + 1):
            image = inputs[level - self.min_level]
            for i in range(self.repeats):
                original_image = image
                image = self.conv_ops[i](image)
                image = self.bns[i][level](image, training=self.is_training)
                if self.act_type:
                    image = utils.activation_fn(image, self.act_type)
                if i > 0 and self.survival_prob:
                    image = utils.drop_connect(image, self.is_training,
                                               self.survival_prob)
                    image = image + original_image

            class_outputs.append(self.classes(image))

        return class_outputs
Ejemplo n.º 10
0
def build_bifpn_layer(feats, feat_sizes, config):
    """Builds a feature pyramid given previous feature pyramid and config."""
    p = config  # use p to denote the network config.
    if p.fpn_config:
        fpn_config = p.fpn_config
    else:
        fpn_config = fpn_configs.get_fpn_config(p.fpn_name, p.min_level,
                                                p.max_level,
                                                p.fpn_weight_method)

    num_output_connections = [0 for _ in feats]
    for i, fnode in enumerate(fpn_config.nodes):
        with tf.variable_scope('fnode{}'.format(i)):
            logging.info('fnode %d : %s', i, fnode)
            new_node_height = feat_sizes[fnode['feat_level']]['height']
            new_node_width = feat_sizes[fnode['feat_level']]['width']
            nodes = []
            for idx, input_offset in enumerate(fnode['inputs_offsets']):
                input_node = feats[input_offset]
                num_output_connections[input_offset] += 1
                input_node = resample_feature_map(
                    input_node,
                    '{}_{}_{}'.format(idx, input_offset, len(feats)),
                    new_node_height,
                    new_node_width,
                    p.fpn_num_filters,
                    p.apply_bn_for_resampling,
                    p.is_training_bn,
                    p.conv_after_downsample,
                    strategy=p.strategy,
                    data_format=config.data_format)
                nodes.append(input_node)

            new_node = fuse_features(nodes, fpn_config.weight_method)

            with tf.variable_scope('op_after_combine{}'.format(len(feats))):
                if not p.conv_bn_act_pattern:
                    new_node = utils.activation_fn(new_node, p.act_type)

                if p.separable_conv:
                    conv_op = functools.partial(tf.layers.separable_conv2d,
                                                depth_multiplier=1)
                else:
                    conv_op = tf.layers.conv2d

                new_node = conv_op(new_node,
                                   filters=p.fpn_num_filters,
                                   kernel_size=(3, 3),
                                   padding='same',
                                   use_bias=not p.conv_bn_act_pattern,
                                   data_format=config.data_format,
                                   name='conv')

                new_node = utils.batch_norm_act(
                    new_node,
                    is_training_bn=p.is_training_bn,
                    act_type=None if not p.conv_bn_act_pattern else p.act_type,
                    data_format=config.data_format,
                    strategy=p.strategy,
                    name='bn')

            feats.append(new_node)
            num_output_connections.append(0)

    output_feats = {}
    for l in range(p.min_level, p.max_level + 1):
        for i, fnode in enumerate(reversed(fpn_config.nodes)):
            if fnode['feat_level'] == l:
                output_feats[l] = feats[-1 - i]
                break
    return output_feats
Ejemplo n.º 11
0
 def _call(image):
     image = conv_op(image)
     image = bn(image, training=training)
     if self.act_type:
         image = utils.activation_fn(image, act_type)
     return image
Ejemplo n.º 12
0
 def test_mish(self):
     features = tf.constant([.5, 10])
     result = utils.activation_fn(features, 'mish')
     self.assertAllClose(result, [0.37524524, 10.0])
Ejemplo n.º 13
0
 def test_relu6(self):
     features = tf.constant([.5, 10])
     result = utils.activation_fn(features, 'relu6')
     self.assertAllClose(result, [0.5, 6])
Ejemplo n.º 14
0
 def test_hswish(self):
     features = tf.constant([.5, 10])
     result = utils.activation_fn(features, 'hswish')
     self.assertAllClose(result, [0.29166667, 10.0])
Ejemplo n.º 15
0
def build_bifpn_layer(feats, feat_sizes, config):
    """Builds a feature pyramid given previous feature pyramid and config.

    Args:
    feats: [P3, P4, P5, P6, P7]
    config: a dict-like config, including all parameters.

    Returns:
        {3:P3", 4:P4", 5:P5", 6:P6", 7:P7"}
    """
    if config.fpn_config:
        fpn_config = config.fpn_config
    else:
        fpn_config = bifpn_dynamic_config(config.min_level, config.max_level,
                                          config.fpn_weight_method)

    num_output_connections = [0 for _ in feats]
    for i, fnode in enumerate(fpn_config.nodes):
        logging.info('fnode %d : %s', i, fnode)
        new_node_height = feat_sizes[fnode['feat_level']]['height']
        new_node_width = feat_sizes[fnode['feat_level']]['width']
        nodes = []
        for idx, input_offset in enumerate(fnode['inputs_offsets']):
            input_node = feats[input_offset]
            num_output_connections[input_offset] += 1
            input_node = resample_feature_map(
                input_node, '{}_{}_{}'.format(idx, input_offset,
                                              len(feats)), new_node_height,
                new_node_width, config.fpn_num_filters, config.is_training_bn)
            nodes.append(input_node)

        # Combine all nodes.
        dtype = nodes[0].dtype
        edge_weights = [
            tf.nn.relu(tf.cast(tf.Variable(1.0, name='WSM'), dtype=dtype))
            for _ in range(len(fnode['inputs_offsets']))
        ]
        weights_sum = tf.add_n(edge_weights)
        nodes = [
            nodes[i] * edge_weights[i] / (weights_sum + 0.0001)
            for i in range(len(nodes))
        ]
        new_node = tf.add_n(nodes)

        new_node = utils.activation_fn(new_node, config.act_type)

        new_node = tf.keras.layers.SeparableConv2D(
            new_node,
            filters=config.fpn_num_filters,
            kernel_size=(3, 3),
            padding='same',
            use_bias=True,
            depth_multiplier=1,
            name='conv')

        new_node = tf.keras.layers.BatchNormalization(
            inputs=new_node, training=config.is_training_bn, name='bn')

        feats.append(new_node)
        num_output_connections.append(0)

    output_feats = {}
    for level in range(config.min_level, config.max_level + 1):
        for i, fnode in enumerate(reversed(fpn_config.nodes)):
            if fnode['feat_level'] == level:
                output_feats[level] = feats[-1 - i]
                break
    return output_feats
Ejemplo n.º 16
0
def build_bifpn_layer(feats, feat_sizes, config):
  """Builds a feature pyramid given previous feature pyramid and config."""
  p = config  # use p to denote the network config.
  if p.fpn_config:
    fpn_config = p.fpn_config
  else:
    fpn_config = get_fpn_config(p.fpn_name, p.min_level, p.max_level,
                                p.fpn_weight_method)

  num_output_connections = [0 for _ in feats]
  for i, fnode in enumerate(fpn_config.nodes):
    with tf.variable_scope('fnode{}'.format(i)):
      logging.info('fnode %d : %s', i, fnode)
      new_node_height = feat_sizes[fnode['feat_level']]['height']
      new_node_width = feat_sizes[fnode['feat_level']]['width']
      nodes = []
      for idx, input_offset in enumerate(fnode['inputs_offsets']):
        input_node = feats[input_offset]
        num_output_connections[input_offset] += 1
        input_node = resample_feature_map(
            input_node, '{}_{}_{}'.format(idx, input_offset, len(feats)),
            new_node_height, new_node_width, p.fpn_num_filters,
            p.apply_bn_for_resampling, p.is_training_bn,
            p.conv_after_downsample,
            p.use_native_resize_op,
            p.pooling_type,
            data_format=config.data_format)
        nodes.append(input_node)

      # Combine all nodes.
      dtype = nodes[0].dtype
      if fpn_config.weight_method == 'attn':
        edge_weights = [tf.cast(tf.Variable(1.0, name='WSM'), dtype=dtype)
                        for _ in range(len(fnode['inputs_offsets']))]
        normalized_weights = tf.nn.softmax(tf.stack(edge_weights))
        nodes = tf.stack(nodes, axis=-1)
        new_node = tf.reduce_sum(tf.multiply(nodes, normalized_weights), -1)
      elif fpn_config.weight_method == 'fastattn':
        edge_weights = [
            tf.nn.relu(tf.cast(tf.Variable(1.0, name='WSM'), dtype=dtype))
            for _ in range(len(fnode['inputs_offsets']))
        ]
        weights_sum = tf.add_n(edge_weights)
        nodes = [nodes[i] * edge_weights[i] / (weights_sum + 0.0001)
                 for i in range(len(nodes))]
        new_node = tf.add_n(nodes)
      elif fpn_config.weight_method == 'sum':
        new_node = tf.add_n(nodes)
      else:
        raise ValueError(
            'unknown weight_method {}'.format(fpn_config.weight_method))

      with tf.variable_scope('op_after_combine{}'.format(len(feats))):
        if not p.conv_bn_act_pattern:
          new_node = utils.activation_fn(new_node, p.act_type)

        if p.separable_conv:
          conv_op = functools.partial(
              tf.layers.separable_conv2d, depth_multiplier=1)
        else:
          conv_op = tf.layers.conv2d

        new_node = conv_op(
            new_node,
            filters=p.fpn_num_filters,
            kernel_size=(3, 3),
            padding='same',
            use_bias=True if not p.conv_bn_act_pattern else False,
            data_format=config.data_format,
            name='conv')

        new_node = utils.batch_norm_act(
            new_node,
            is_training_bn=p.is_training_bn,
            act_type=None if not p.conv_bn_act_pattern else p.act_type,
            data_format=config.data_format,
            use_tpu=p.use_tpu,
            name='bn')

      feats.append(new_node)
      num_output_connections.append(0)

  output_feats = {}
  for l in range(p.min_level, p.max_level + 1):
    for i, fnode in enumerate(reversed(fpn_config.nodes)):
      if fnode['feat_level'] == l:
        output_feats[l] = feats[-1 - i]
        break
  return output_feats