环境:tensorflow 1.12
, win10
.
目的:resnet 50
网络结构在某些情况下过深,增加tensorflow object detection api
特征提取器resnet 18
。
安装obeject detection api
安装步骤参考TensorFlow Models。
protoc
选择3.4.0版本。
构建resnet 18
针对slim中的net,修改resnet 50得到resnet18
。
def resnet_v1_18(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_18'):
"""ResNet-18 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=2,
stride=2),
resnet_v1_block('block2', base_depth=depth_func(128), num_units=2,
stride=2),
resnet_v1_block('block3', base_depth=depth_func(256), num_units=2,
stride=2),
resnet_v1_block('block4', base_depth=depth_func(512), num_units=2,
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)
resnet_v1_18.default_image_size = resnet_v1.default_image_size
定义新的Feature Extractor
参考官方给出的文档 define your own model。
- 在faster_rcnn_resnet_v1_feature_extractor中添加resnet18:
class FasterRCNNResnet18FeatureExtractor(FasterRCNNResnetV1FeatureExtractor):
"""Faster R-CNN Resnet 18 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(FasterRCNNResnet18FeatureExtractor, self).__init__(
'resnet_v1_18', resnet_v1.resnet_v1_18, is_training,
first_stage_features_stride, batch_norm_trainable,
reuse_weights, weight_decay)
- 在model_builder中注册:
FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP = {
'faster_rcnn_nas':
frcnn_nas.FasterRCNNNASFeatureExtractor,
'faster_rcnn_pnas':
frcnn_pnas.FasterRCNNPNASFeatureExtractor,
'faster_rcnn_inception_resnet_v2':
frcnn_inc_res.FasterRCNNInceptionResnetV2FeatureExtractor,
'faster_rcnn_inception_v2':
frcnn_inc_v2.FasterRCNNInceptionV2FeatureExtractor,
'faster_rcnn_resnet18':
frcnn_resnet_v1.FasterRCNNResnet18FeatureExtractor,
'faster_rcnn_resnet50':
frcnn_resnet_v1.FasterRCNNResnet50FeatureExtractor,
'faster_rcnn_resnet101':
frcnn_resnet_v1.FasterRCNNResnet101FeatureExtractor,
'faster_rcnn_resnet152':
frcnn_resnet_v1.FasterRCNNResnet152FeatureExtractor,
}
- 修改faster_rcnn_resnet50_coco.config的
type
:
feature_extractor {
type: 'faster_rcnn_resnet18'
first_stage_features_stride: 16 # RPN steps
}
因为没有提供预训练的模型,注释config
中的以下两个设置:
# fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/model.ckpt"
# from_detection_checkpoint: true