Пример #1
0
def resnet101(inputs,
              num_classes=None,
              is_training=True,
              global_pool=False,
              output_stride=None,
              spatial_squeeze=False,
              reuse=tf.AUTO_REUSE,
              scope='resnet_v2_101'):
    """ResNet-101 model of [1]. See resnet_v2() for arg and return description."""
    blocks = [
        resnet_v2.resnet_v2_block('block1',
                                  base_depth=64,
                                  num_units=3,
                                  stride=2),
        resnet_v2.resnet_v2_block('block2',
                                  base_depth=128,
                                  num_units=4,
                                  stride=2),
        resnet_v2.resnet_v2_block('block3',
                                  base_depth=256,
                                  num_units=23,
                                  stride=2),
    ]
    return resnet_v2.resnet_v2(inputs,
                               blocks,
                               num_classes,
                               is_training=is_training,
                               global_pool=global_pool,
                               output_stride=output_stride,
                               include_root_block=True,
                               reuse=reuse,
                               scope=scope)
Пример #2
0
def resnet_v2_50(inputs,
                 num_classes=None,
                 is_training=True,
                 global_pool=True,
                 output_stride=None,
                 reuse=None,
                 scope='resnet_v2_50'):
    """
        ResNet-50 model of [1]. See resnet_v2() for arg and return description.
    """
    blocks = [
        resnet_v2.resnet_v2_block('block1', base_depth=64, num_units=3, stride=1),
        resnet_v2.resnet_v2_block('block2', base_depth=128, num_units=4, stride=2),
        resnet_v2.resnet_v2_block('block3', base_depth=256, num_units=6, stride=2),
        resnet_v2.resnet_v2_block('block4', base_depth=512, num_units=3, stride=2),
    ]

    return resnet_v2.resnet_v2(
        inputs,
        blocks,
        num_classes,
        is_training=is_training,
        global_pool=global_pool,
        output_stride=output_stride,
        include_root_block=True,
        reuse=reuse,
        scope=scope)
Пример #3
0
def resnet_from_blocks(inputs,
                       blocks,
                       num_classes=None,
                       is_training=None,
                       global_pool=False,
                       output_stride=None,
                       reuse=None,
                       scope=None):
    return resnet_v2.resnet_v2(inputs,
                               blocks,
                               num_classes,
                               is_training,
                               global_pool,
                               output_stride,
                               include_root_block=True,
                               reuse=reuse,
                               scope=scope)
Пример #4
0
 def _resnet_small(self,
                   inputs,
                   num_classes=None,
                   global_pool=True,
                   output_stride=None,
                   include_root_block=True,
                   reuse=None,
                   scope='resnet_v2_small'):
   """A shallow and thin ResNet v2 for faster tests."""
   block = resnet_v2.resnet_v2_block
   blocks = [
       block('block1', base_depth=1, num_units=3, stride=2),
       block('block2', base_depth=2, num_units=3, stride=2),
       block('block3', base_depth=4, num_units=3, stride=2),
       block('block4', base_depth=8, num_units=2, stride=1),
   ]
   return resnet_v2.resnet_v2(inputs, blocks, num_classes, global_pool,
                              output_stride, include_root_block, reuse, scope)
Пример #5
0
 def _resnet_small(self,
                   inputs,
                   num_classes=None,
                   global_pool=True,
                   output_stride=None,
                   include_root_block=True,
                   reuse=None,
                   scope='resnet_v2_small'):
   """A shallow and thin ResNet v2 for faster tests."""
   bottleneck = resnet_v2.bottleneck
   blocks = [
       resnet_utils.Block('block1', bottleneck, [(4, 1, 1)] * 2 + [(4, 1, 2)]),
       resnet_utils.Block('block2', bottleneck, [(8, 2, 1)] * 2 + [(8, 2, 2)]),
       resnet_utils.Block('block3', bottleneck,
                          [(16, 4, 1)] * 2 + [(16, 4, 2)]),
       resnet_utils.Block('block4', bottleneck, [(32, 8, 1)] * 2)
   ]
   return resnet_v2.resnet_v2(inputs, blocks, num_classes, global_pool,
                              output_stride, include_root_block, reuse, scope)
 def _resnet_small(self,
                   inputs,
                   num_classes=None,
                   global_pool=True,
                   output_stride=None,
                   include_root_block=True,
                   reuse=None,
                   scope='resnet_v2_small'):
     """A shallow and thin ResNet v2 for faster tests."""
     block = resnet_v2.resnet_v2_block
     blocks = [
         block('block1', base_depth=1, num_units=3, stride=2),
         block('block2', base_depth=2, num_units=3, stride=2),
         block('block3', base_depth=4, num_units=3, stride=2),
         block('block4', base_depth=8, num_units=2, stride=1),
     ]
     return resnet_v2.resnet_v2(inputs, blocks, num_classes, global_pool,
                                output_stride, include_root_block, reuse,
                                scope)
Пример #7
0
def resnet_v2_50(inputs,
                 num_classes=None,
                 is_training=True,
                 global_pool=False,
                 output_stride=None,
                 reuse=None,
                 scope='resnet_v2_50'):
    """ResNet-50 model of [1]. See resnet_v2() for arg and return description."""
    resnet_v2_block = resnet_v2.resnet_v2_block
    blocks = [
        resnet_v2_block('block1', base_depth=64, num_units=3, stride=2),
        resnet_v2_block('block2', base_depth=128, num_units=4, stride=2),
        resnet_v2_block('block3', base_depth=256, num_units=6, stride=2),
        resnet_v2_block('block4', base_depth=512, num_units=3, stride=1),
    ]

    # custom root block, because original root block will use maxpooling with valid padding.
    with tf.variable_scope(scope) as sc:
        with slim.arg_scope([slim.conv2d],
                            activation_fn=None,
                            normalizer_fn=None):
            net = resnet_utils.conv2d_same(inputs,
                                           64,
                                           7,
                                           stride=2,
                                           scope='conv1')  # stride 2
        net = slim.max_pool2d(net, [3, 3],
                              stride=2,
                              scope='pool1',
                              padding='SAME')  # stride 2
    return resnet_v2.resnet_v2(net,
                               blocks,
                               num_classes,
                               is_training=is_training,
                               global_pool=global_pool,
                               output_stride=output_stride,
                               include_root_block=False,
                               reuse=reuse,
                               scope=scope)
Пример #8
0
 def _resnet_small(self,
                   inputs,
                   num_classes=None,
                   global_pool=True,
                   output_stride=None,
                   include_root_block=True,
                   reuse=None,
                   scope='resnet_v2_small'):
     """A shallow and thin ResNet v2 for faster tests."""
     bottleneck = resnet_v2.bottleneck
     blocks = [
         resnet_utils.Block('block1', bottleneck,
                            [(4, 1, 1)] * 2 + [(4, 1, 2)]),
         resnet_utils.Block('block2', bottleneck,
                            [(8, 2, 1)] * 2 + [(8, 2, 2)]),
         resnet_utils.Block('block3', bottleneck,
                            [(16, 4, 1)] * 2 + [(16, 4, 2)]),
         resnet_utils.Block('block4', bottleneck, [(32, 8, 1)] * 2)
     ]
     return resnet_v2.resnet_v2(inputs, blocks, num_classes, global_pool,
                                output_stride, include_root_block, reuse,
                                scope)
Пример #9
0
def feature_extractor_resnet(images,
                             dim=256,
                             weight_decay=0.0001,
                             batch_norm_decay=0.999,
                             batch_renorm_decay=0.99,
                             batch_renorm_rmax=3.,
                             batch_renorm_dmax=5.,
                             is_training=True,
                             use_conv3d=True):
    from tensorflow.contrib.slim.python.slim.nets import resnet_v2
    if use_conv3d:
        orig_shape = tf.shape(images)
        # [N,T,H,W,C] -> [N*T,H,W,C]
        images = tf.reshape(images, tf.concat([[-1], orig_shape[2:]], 0))

    resnet_arg_scope = resnet_v2.resnet_arg_scope(
        weight_decay=weight_decay, batch_norm_decay=batch_norm_decay)
    # batch size is small so we use batch renormalization
    batch_norm_key = filter(lambda x: 'batch_norm' in x,
                            resnet_arg_scope.keys())[0]
    resnet_arg_scope[batch_norm_key].update({
        'renorm': True,
        'renorm_decay': batch_renorm_decay,
        'renorm_clipping': {
            'rmin': 1. / batch_renorm_rmax,
            'rmax': batch_renorm_rmax,
            'dmax': batch_renorm_dmax
        }
    })

    with slim.arg_scope(resnet_arg_scope):
        blocks = [
            resnet_v2.resnet_v2_block('block1',
                                      base_depth=16,
                                      num_units=3,
                                      stride=2),
            resnet_v2.resnet_v2_block('block2',
                                      base_depth=32,
                                      num_units=4,
                                      stride=2),
            resnet_v2.resnet_v2_block('block3',
                                      base_depth=64,
                                      num_units=6,
                                      stride=2),  #256
            resnet_v2.resnet_v2_block('block4',
                                      base_depth=128,
                                      num_units=3,
                                      stride=1)  #512
        ]
        _, end_points = resnet_v2.resnet_v2(images,
                                            blocks,
                                            is_training=is_training,
                                            include_root_block=False)
    net = end_points['resnet_v2/block4']
    if use_conv3d:
        # [N*T,H',W',C'] -> [N,T,H',W',C']
        net = tf.reshape(net, tf.concat(
            [orig_shape[:2], tf.shape(net)[1:]], 0))

    arg_scope = convert_resnet_arg_scope_to_slim(resnet_arg_scope)
    arg_scope[slim.conv2d].update({'stride': 1, 'padding': 'SAME'})
    arg_scope[slim.conv3d].update({'stride': 1, 'padding': 'SAME'})
    arg_scope[slim.batch_norm]['is_training'] = is_training
    with slim.arg_scope(arg_scope):
        if use_conv3d:
            net = slim.conv3d(net, 512, [3, 3, 3])
            net = slim.conv3d(net, 256, [1, 1, 1])
            net = slim.conv3d(net, 512, [3, 3, 3])
            # the last layer without activation function
            feature_map = slim.conv3d(net,
                                      dim, [1, 1, 1],
                                      activation_fn=None,
                                      normalizer_fn=None)
        else:
            # the last layer without activation function
            feature_map = slim.conv2d(net,
                                      dim, [1, 1],
                                      activation_fn=None,
                                      normalizer_fn=None)
    return feature_map
Пример #10
0
  def _build_network(self, sess, is_training=True):
    # select initializers
    if cfg.TRAIN.TRUNCATED:
      initializer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)
      initializer_bbox = tf.truncated_normal_initializer(mean=0.0, stddev=0.001)
    else:
      initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)
      initializer_bbox = tf.random_normal_initializer(mean=0.0, stddev=0.001)

    # choose different blocks for different number of layers
    if self._num_layers == 50:
      blocks = [resnet_v2_block('block1', base_depth=64, num_units=3, stride=2),
                resnet_v2_block('block2', base_depth=128, num_units=4, stride=2),
                # use stride 1 for the last conv4 layer
                resnet_v2_block('block3', base_depth=256, num_units=6, stride=1),
                resnet_v2_block('block4', base_depth=512, num_units=3, stride=1)]

    elif self._num_layers == 101:
      blocks = [resnet_v2_block('block1', base_depth=64, num_units=3, stride=2),
                resnet_v2_block('block2', base_depth=128, num_units=4, stride=2),
                # use stride 1 for the last conv4 layer
                resnet_v2_block('block3', base_depth=256, num_units=23, stride=1),
                resnet_v2_block('block4', base_depth=512, num_units=3, stride=1)]

    elif self._num_layers == 152:
      blocks = [resnet_v2_block('block1', base_depth=64, num_units=3, stride=2),
                resnet_v2_block('block2', base_depth=128, num_units=8, stride=2),
                # use stride 1 for the last conv4 layer
                resnet_v2_block('block3', base_depth=256, num_units=36, stride=1),
                resnet_v2_block('block4', base_depth=512, num_units=3, stride=1)]

    else:
      # other numbers are not supported
      raise NotImplementedError

    assert (0 <= cfg.RESNET.FIXED_BLOCKS <= 3)
    # Now the base is always fixed during training
    with slim.arg_scope(resnet_arg_scope(is_training=False)):
      net_conv = self._build_base()
    if cfg.RESNET.FIXED_BLOCKS > 0:
      with slim.arg_scope(resnet_arg_scope(is_training=False)):
        net_conv, _ = resnet_v2.resnet_v2(net_conv,
                                     blocks[0:cfg.RESNET.FIXED_BLOCKS],
                                     global_pool=False,
                                     include_root_block=False,
                                     scope=self._resnet_scope)
    if cfg.RESNET.FIXED_BLOCKS < 3:
      with slim.arg_scope(resnet_arg_scope(is_training=is_training)):
        net_conv, _ = resnet_v2.resnet_v2(net_conv,
                                           blocks[cfg.RESNET.FIXED_BLOCKS:-1],
                                           global_pool=False,
                                           include_root_block=False,
                                           scope=self._resnet_scope)

    self._act_summaries.append(net_conv)
    self._layers['head'] = net_conv
    with tf.variable_scope(self._resnet_scope, self._resnet_scope):
      # build the anchors for the image
      self._anchor_component()
      # region proposal network
      rois = self._region_proposal(net_conv, is_training, initializer)
      # region of interest pooling
      if cfg.POOLING_MODE == 'crop':
        pool5 = self._crop_pool_layer(net_conv, rois, "pool5")
      else:
        raise NotImplementedError

    with slim.arg_scope(resnet_arg_scope(is_training=is_training)):
      fc7, _ = resnet_v2.resnet_v2(pool5,
                                   blocks[-1:],
                                   global_pool=False,
                                   include_root_block=False,
                                   scope=self._resnet_scope)

    with tf.variable_scope(self._resnet_scope, self._resnet_scope):
      # average pooling done by reduce_mean
      fc7 = tf.reduce_mean(fc7, axis=[1, 2])
      # region classification
      cls_prob, poly_pred = self._region_classification(fc7, is_training,
                                                        initializer, initializer_bbox)

    self._score_summaries.update(self._predictions)

    return rois, cls_prob, poly_pred