faster_rcnn_resnet101 在 tensorflow 中定义在哪里(比如层在哪里)
Where is faster_rcnn_resnet101 (like where are the layers) defined in tensorflow
我是机器学习的新手,我目前正在使用 Tensorflow Object Detection API 进行对象检测,我使用的模型是 faster_rcnn_resnet101.
我正在寻找的是定义架构的 python 代码,例如层数(如我附加的代码,来自 (https://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/) 的 Tensorflow 教程。 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
Tensorflow 使用特征提取,它使用先前网络学习的表示从新样本中提取有意义的特征。
Faster_RCNN_ResNet_101 特征提取器在此 class 中定义: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
,因此 Faster R-CNN 检测模型的一般张量流实现可能在 https://github.com/tensorflow/models/blob/master/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py
中定义
对象检测 api 使用 tf-slim 构建模型。 Tf-slim 是一个包含大量预定义 CNN 的张量流 api,它提供了 CNN 的构建块。
在对象检测中 api,使用的 CNN 称为特征提取器,这些特征提取器有包装器 classes,它们为不同的模型架构提供了统一的接口。
例如模型faster_rcnn_resnet101
使用了resnet101作为特征提取器,所以[=16=下的文件faster_rcnn_resnet_v1_feature_extractor.py
中有对应的FasterRCNNResnetV1FeatureExtractor
包装器class ] 目录。
from nets import resnet_utils
from nets import resnet_v1
slim = tf.contrib.slim
在此 class 中,您会发现他们使用 slim
来构建特征提取器。 nets
是 slim
的一个模块,其中包含许多预定义的 CNN。所以关于你的模型定义代码(层),你应该能够在 nets 模块中找到它,这里是 resnet_v1
class.
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 个块。
所以我们完成了 resnet 部分,第一阶段特征提取器 (resnet101) 提取的那些特征将被馈送到 proposal generator 并且它将生成区域,这些区域连同特征,然后将被送入 box classifier 进行 class 预测和 bbox 回归。
faster_rcnn
部分,指定为meta_architectures
,meta_architectures
是将classification架构转换为检测架构的receipe,在本例中,来自resnet101
到 faster_rcnn
。这是 faster_rcnn_meta_architecture
(source) 的图表。
这里你看到方框classifier部分,还有池化操作(针对裁剪区域)和卷积操作(针对裁剪区域提取特征).又在classfaster_rcnn_meta_arch
,this line is the maxpool operation and the later convolution operation is performed in the feature extractor class又是第二阶段。你可以清楚地看到正在使用另一个块。
def _extract_box_classifier_features(self, proposal_feature_maps, scope):
"""Extracts second stage box classifier features.
Args:
proposal_feature_maps: A 4-D float tensor with shape
[batch_size * self.max_num_proposals, crop_height, crop_width, depth]
representing the feature map cropped to each proposal.
scope: A scope name (unused).
Returns:
proposal_classifier_features: A 4-D float tensor with shape
[batch_size * self.max_num_proposals, height, width, depth]
representing box classifier features for each proposal.
"""
with tf.variable_scope(self._architecture, reuse=self._reuse_weights):
with slim.arg_scope(
resnet_utils.resnet_arg_scope(
batch_norm_epsilon=1e-5,
batch_norm_scale=True,
weight_decay=self._weight_decay)):
with slim.arg_scope([slim.batch_norm],
is_training=self._train_batch_norm):
blocks = [
resnet_utils.Block('block4', resnet_v1.bottleneck, [{
'depth': 2048,
'depth_bottleneck': 512,
'stride': 1
}] * 3)
]
proposal_classifier_features = resnet_utils.stack_blocks_dense(
proposal_feature_maps, blocks)
return proposal_classifier_features
我是机器学习的新手,我目前正在使用 Tensorflow Object Detection API 进行对象检测,我使用的模型是 faster_rcnn_resnet101.
我正在寻找的是定义架构的 python 代码,例如层数(如我附加的代码,来自 (https://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/) 的 Tensorflow 教程。 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
Tensorflow 使用特征提取,它使用先前网络学习的表示从新样本中提取有意义的特征。
Faster_RCNN_ResNet_101 特征提取器在此 class 中定义: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
,因此 Faster R-CNN 检测模型的一般张量流实现可能在 https://github.com/tensorflow/models/blob/master/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py
对象检测 api 使用 tf-slim 构建模型。 Tf-slim 是一个包含大量预定义 CNN 的张量流 api,它提供了 CNN 的构建块。 在对象检测中 api,使用的 CNN 称为特征提取器,这些特征提取器有包装器 classes,它们为不同的模型架构提供了统一的接口。
例如模型faster_rcnn_resnet101
使用了resnet101作为特征提取器,所以[=16=下的文件faster_rcnn_resnet_v1_feature_extractor.py
中有对应的FasterRCNNResnetV1FeatureExtractor
包装器class ] 目录。
from nets import resnet_utils
from nets import resnet_v1
slim = tf.contrib.slim
在此 class 中,您会发现他们使用 slim
来构建特征提取器。 nets
是 slim
的一个模块,其中包含许多预定义的 CNN。所以关于你的模型定义代码(层),你应该能够在 nets 模块中找到它,这里是 resnet_v1
class.
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 个块。
所以我们完成了 resnet 部分,第一阶段特征提取器 (resnet101) 提取的那些特征将被馈送到 proposal generator 并且它将生成区域,这些区域连同特征,然后将被送入 box classifier 进行 class 预测和 bbox 回归。
faster_rcnn
部分,指定为meta_architectures
,meta_architectures
是将classification架构转换为检测架构的receipe,在本例中,来自resnet101
到 faster_rcnn
。这是 faster_rcnn_meta_architecture
(source) 的图表。
这里你看到方框classifier部分,还有池化操作(针对裁剪区域)和卷积操作(针对裁剪区域提取特征).又在classfaster_rcnn_meta_arch
,this line is the maxpool operation and the later convolution operation is performed in the feature extractor class又是第二阶段。你可以清楚地看到正在使用另一个块。
def _extract_box_classifier_features(self, proposal_feature_maps, scope):
"""Extracts second stage box classifier features.
Args:
proposal_feature_maps: A 4-D float tensor with shape
[batch_size * self.max_num_proposals, crop_height, crop_width, depth]
representing the feature map cropped to each proposal.
scope: A scope name (unused).
Returns:
proposal_classifier_features: A 4-D float tensor with shape
[batch_size * self.max_num_proposals, height, width, depth]
representing box classifier features for each proposal.
"""
with tf.variable_scope(self._architecture, reuse=self._reuse_weights):
with slim.arg_scope(
resnet_utils.resnet_arg_scope(
batch_norm_epsilon=1e-5,
batch_norm_scale=True,
weight_decay=self._weight_decay)):
with slim.arg_scope([slim.batch_norm],
is_training=self._train_batch_norm):
blocks = [
resnet_utils.Block('block4', resnet_v1.bottleneck, [{
'depth': 2048,
'depth_bottleneck': 512,
'stride': 1
}] * 3)
]
proposal_classifier_features = resnet_utils.stack_blocks_dense(
proposal_feature_maps, blocks)
return proposal_classifier_features