研究是以新的方式做事。这给如何在代码中创建抽象带来了压力,这对于任何规模巨大的研究工程项目来说都是一个挑战:
在detectron2中,有两种类型的接口一起解决这两种问题:
例如,可以通过以下方式构建掩码R-CNN模型:在这里插入代码片
# load proper yaml config file, then
model = build_model(cfg
model = GeneralizedRCNN(
cfg,
roi_heads=StandardROIHeads(cfg, batch_size_per_image=666),
pixel_std=[57.0, 57.0, 57.0])
model = GeneralizedRCNN(
backbone=FPN(
ResNet(
BasicStem(3, 64, norm="FrozenBN"),
ResNet.make_default_stages(50, stride_in_1x1=True, norm="FrozenBN"),
out_features=["res2", "res3", "res4", "res5"],
).freeze(2),
["res2", "res3", "res4", "res5"],
256,
top_block=LastLevelMaxPool(),
),
proposal_generator=RPN(
in_features=["p2", "p3", "p4", "p5", "p6"],
head=StandardRPNHead(in_channels=256, num_anchors=3),
anchor_generator=DefaultAnchorGenerator(
sizes=[[32], [64], [128], [256], [512]],
aspect_ratios=[0.5, 1.0, 2.0],
strides=[4, 8, 16, 32, 64],
offset=0.0,
),
anchor_matcher=Matcher([0.3, 0.7], [0, -1, 1], allow_low_quality_matches=True),
box2box_transform=Box2BoxTransform([1.0, 1.0, 1.0, 1.0]),
batch_size_per_image=256,
positive_fraction=0.5,
pre_nms_topk=(2000, 1000),
post_nms_topk=(1000, 1000),
nms_thresh=0.7,
),
roi_heads=StandardROIHeads(
num_classes=80,
batch_size_per_image=512,
positive_fraction=0.25,
proposal_matcher=Matcher([0.5], [0, 1], allow_low_quality_matches=False),
box_in_features=["p2", "p3", "p4", "p5"],
box_pooler=ROIPooler(7, (1.0 / 4, 1.0 / 8, 1.0 / 16, 1.0 / 32), 0, "ROIAlignV2"),
box_head=FastRCNNConvFCHead(
ShapeSpec(channels=256, height=7, width=7), conv_dims=[], fc_dims=[1024, 1024]
),
box_predictor=FastRCNNOutputLayers(
ShapeSpec(channels=1024),
test_score_thresh=0.05,
box2box_transform=Box2BoxTransform((10, 10, 5, 5)),
num_classes=80,
),
mask_in_features=["p2", "p3", "p4", "p5"],
mask_pooler=ROIPooler(14, (1.0 / 4, 1.0 / 8, 1.0 / 16, 1.0 / 32), 0, "ROIAlignV2"),
mask_head=MaskRCNNConvUpsampleHead(
ShapeSpec(channels=256, width=14, height=14),
num_classes=80,
conv_dims=[256, 256, 256, 256, 256],
),
),
pixel_mean=[103.530, 116.280, 123.675],
pixel_std=[1.0, 1.0, 1.0],
input_format="BGR",
)