在tensorflow中定义的faster_rcnn_resnet101在哪里(就像图层在哪里)

时间:2019-04-14 22:02:52

标签: tensorflow conv-neural-network faster-rcnn

我真的是机器学习的新手,我目前正在使用Tensorflow对象检测API进行对象检测,而我使用的模型是fast_rcnn_resnet101。

我要寻找的是定义体系结构的python代码,例如层数(如我所附的代码,该代码来自Tensorflow教程(https://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/)。Tensorflow不像YOLO,我可以在哪里轻松找到架构的定义...

非常感谢您的帮助!我想知道,在哪里可以找到定义架构的文件 faster_Rcnn_resnet101

def create_convolutional_layer(input,                num_input_channels,                conv_filter_size,
               num_filters):

## We shall define the weights that will be trained using create_weights function.
weights = create_weights(shape=[conv_filter_size, conv_filter_size, num_input_channels, num_filters])
## We create biases using the create_biases function. These are also trained.
biases = create_biases(num_filters)

## Creating the convolutional layer
layer = tf.nn.conv2d(input=input,
                 filter=weights,
                 strides=[1, 1, 1, 1],
                 padding='SAME')

layer += biases

## We shall be using max-pooling.  
layer = tf.nn.max_pool(value=layer,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME')
## Output of pooling is fed to Relu which is the activation function for us.
layer = tf.nn.relu(layer)

return layer

2 个答案:

答案 0 :(得分:2)

Tensorflow使用特征提取(Feature Extraction),它使用以前的网络所学知识的表示从新样本中提取有意义的特征。

Faster_RCNN_ResNet_101特征提取器在此类中定义:https://github.com/tensorflow/models/blob/master/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor.py

class FasterRCNNResnet101FeatureExtractor(FasterRCNNResnetV1FeatureExtractor):
  """Faster R-CNN Resnet 101 feature extractor implementation."""

  def __init__(self,
               is_training,
               first_stage_features_stride,
               batch_norm_trainable=False,
               reuse_weights=None,
               weight_decay=0.0):
    """Constructor.
    Args:
      is_training: See base class.
      first_stage_features_stride: See base class.
      batch_norm_trainable: See base class.
      reuse_weights: See base class.
      weight_decay: See base class.
    Raises:
      ValueError: If `first_stage_features_stride` is not 8 or 16,
        or if `architecture` is not supported.
    """
    super(FasterRCNNResnet101FeatureExtractor, self).__init__(
        'resnet_v1_101', resnet_v1.resnet_v1_101, is_training,
        first_stage_features_stride, batch_norm_trainable,
        reuse_weights, weight_decay)

您可以在完整代码的顶部看到from object_detection.meta_architectures import faster_rcnn_meta_arch,因此,大概在https://github.com/tensorflow/models/blob/master/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py中定义了Faster R-CNN检测模型的常规tensorflow实现

答案 1 :(得分:0)

对象检测api使用tf-slim来构建模型。 Tf-slim是一个Tensorflow API,其中包含许多预定义的CNN,并提供CNN的构建块。 在对象检测api中,使用的CNN称为特征提取器,这些特征提取器有包装器类,它们为不同的模型体系结构提供了统一的接口。

例如,模型faster_rcnn_resnet101使用resnet101作为特征提取器,因此在FasterRCNNResnetV1FeatureExtractor目录下的文件faster_rcnn_resnet_v1_feature_extractor.py中有一个相应的models包装类。

from nets import resnet_utils
from nets import resnet_v1    
slim = tf.contrib.slim

在此类中,您会发现他们使用slim来构建特征提取器。 netsslim中的一个模块,其中包含许多预定义的CNN。因此,关于模型定义代码(层),应该可以在nets模块(这里是resnet_v1类)中找到它。

def resnet_v1_block(scope, base_depth, num_units, stride):
  """Helper function for creating a resnet_v1 bottleneck block.
  Args:
    scope: The scope of the block.
    base_depth: The depth of the bottleneck layer for each unit.
    num_units: The number of units in the block.
    stride: The stride of the block, implemented as a stride in the last unit.
      All other units have stride=1.
  Returns:
    A resnet_v1 bottleneck block.
  """
  return resnet_utils.Block(scope, bottleneck, [{
      'depth': base_depth * 4,
      'depth_bottleneck': base_depth,
      'stride': 1
  }] * (num_units - 1) + [{
      'depth': base_depth * 4,
      'depth_bottleneck': base_depth,
      'stride': stride
  }])


def resnet_v1_50(inputs,
                 num_classes=None,
                 is_training=True,
                 global_pool=True,
                 output_stride=None,
                 spatial_squeeze=True,
                 store_non_strided_activations=False,
                 min_base_depth=8,
                 depth_multiplier=1,
                 reuse=None,
                 scope='resnet_v1_50'):
  """ResNet-50 model of [1]. See resnet_v1() for arg and return description."""
  depth_func = lambda d: max(int(d * depth_multiplier), min_base_depth)
  blocks = [
      resnet_v1_block('block1', base_depth=depth_func(64), num_units=3,
                      stride=2),
      resnet_v1_block('block2', base_depth=depth_func(128), num_units=4,
                      stride=2),
      resnet_v1_block('block3', base_depth=depth_func(256), num_units=6,
                      stride=2),
      resnet_v1_block('block4', base_depth=depth_func(512), num_units=3,
                      stride=1),
  ]
  return resnet_v1(inputs, blocks, num_classes, is_training,
                   global_pool=global_pool, output_stride=output_stride,
                   include_root_block=True, spatial_squeeze=spatial_squeeze,
                   store_non_strided_activations=store_non_strided_activations,
                   reuse=reuse, scope=scope)

上面的示例代码说明了如何构建resnet50模型(选择resnet50,因为与resnet101相同的概念,但层数较少)。值得注意的是,resnet50有4个块,每个块包含[3,4,6,3]个单元。这是resnet50的图,您可以看到4个块。

enter image description here

因此,我们完成了resnet部分,由第一阶段特征提取器(resnet101)提取的那些特征将被馈送到建议生成器,它将生成区域,这些区域以及特征,然后将其输入到 box分类器中,以进行类预测和bbox回归。

faster_rcnn部分被指定为meta_architecturesmeta_architectures是将分类架构转换为检测架构(在这种情况下,从resnet101到{{1 }}。这是faster_rcnnsource)的图。

enter image description here

在框分类器部分中,您还会看到池操作(用于裁剪区域)和卷积操作(用于从裁剪区域提取特征)。在类faster_rcnn_meta_architecture中,this行是maxpool操作,稍后的卷积操作在feature extractor class中再次执行,但仅用于第二阶段。您可以清楚地看到正在使用另一个块。

faster_rcnn_meta_arch