|
@@ -52,6 +52,7 @@ def _default_anchorgen():
|
|
|
class LineNet(BaseDetectionNet):
|
|
|
def __init__(self, cfg, **kwargs):
|
|
|
cfg = read_yaml(cfg)
|
|
|
+ self.cfg=cfg
|
|
|
backbone = cfg['backbone']
|
|
|
num_classes = cfg['num_classes']
|
|
|
|
|
@@ -61,213 +62,215 @@ class LineNet(BaseDetectionNet):
|
|
|
norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
|
|
|
backbone = resnet50(weights=None, progress=True, norm_layer=norm_layer)
|
|
|
backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers)
|
|
|
- out_channels = backbone.out_channels
|
|
|
-
|
|
|
- min_size = 512,
|
|
|
- max_size = 1333,
|
|
|
- rpn_pre_nms_top_n_train = 2000,
|
|
|
- rpn_pre_nms_top_n_test = 1000,
|
|
|
- rpn_post_nms_top_n_train = 2000,
|
|
|
- rpn_post_nms_top_n_test = 1000,
|
|
|
- rpn_nms_thresh = 0.7,
|
|
|
- rpn_fg_iou_thresh = 0.7,
|
|
|
- rpn_bg_iou_thresh = 0.3,
|
|
|
- rpn_batch_size_per_image = 256,
|
|
|
- rpn_positive_fraction = 0.5,
|
|
|
- rpn_score_thresh = 0.0,
|
|
|
- box_score_thresh = 0.05,
|
|
|
- box_nms_thresh = 0.5,
|
|
|
- box_detections_per_img = 100,
|
|
|
- box_fg_iou_thresh = 0.5,
|
|
|
- box_bg_iou_thresh = 0.5,
|
|
|
- box_batch_size_per_image = 512,
|
|
|
- box_positive_fraction = 0.25,
|
|
|
- bbox_reg_weights = None,
|
|
|
-
|
|
|
- line_head = LineRCNNHeads(out_channels, 5)
|
|
|
- line_predictor = LineRCNNPredictor(cfg)
|
|
|
+ print(f'out_chanenels:{backbone.out_channels}')
|
|
|
+ self.construct(backbone=backbone,num_classes=num_classes,**kwargs)
|
|
|
+ # out_channels = backbone.out_channels
|
|
|
+ #
|
|
|
+ # min_size = 512,
|
|
|
+ # max_size = 1333,
|
|
|
+ # rpn_pre_nms_top_n_train = 2000,
|
|
|
+ # rpn_pre_nms_top_n_test = 1000,
|
|
|
+ # rpn_post_nms_top_n_train = 2000,
|
|
|
+ # rpn_post_nms_top_n_test = 1000,
|
|
|
+ # rpn_nms_thresh = 0.7,
|
|
|
+ # rpn_fg_iou_thresh = 0.7,
|
|
|
+ # rpn_bg_iou_thresh = 0.3,
|
|
|
+ # rpn_batch_size_per_image = 256,
|
|
|
+ # rpn_positive_fraction = 0.5,
|
|
|
+ # rpn_score_thresh = 0.0,
|
|
|
+ # box_score_thresh = 0.05,
|
|
|
+ # box_nms_thresh = 0.5,
|
|
|
+ # box_detections_per_img = 100,
|
|
|
+ # box_fg_iou_thresh = 0.5,
|
|
|
+ # box_bg_iou_thresh = 0.5,
|
|
|
+ # box_batch_size_per_image = 512,
|
|
|
+ # box_positive_fraction = 0.25,
|
|
|
+ # bbox_reg_weights = None,
|
|
|
+ #
|
|
|
+ # line_head = LineRCNNHeads(out_channels, 5)
|
|
|
+ # line_predictor = LineRCNNPredictor(cfg)
|
|
|
+ # rpn_anchor_generator = _default_anchorgen()
|
|
|
+ # rpn_head = RPNHead(out_channels, rpn_anchor_generator.num_anchors_per_location()[0])
|
|
|
+ # rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
|
|
|
+ # rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)
|
|
|
+ #
|
|
|
+ # rpn = RegionProposalNetwork(
|
|
|
+ # rpn_anchor_generator,
|
|
|
+ # rpn_head,
|
|
|
+ # rpn_fg_iou_thresh,
|
|
|
+ # rpn_bg_iou_thresh,
|
|
|
+ # rpn_batch_size_per_image,
|
|
|
+ # rpn_positive_fraction,
|
|
|
+ # rpn_pre_nms_top_n,
|
|
|
+ # rpn_post_nms_top_n,
|
|
|
+ # rpn_nms_thresh,
|
|
|
+ # score_thresh=rpn_score_thresh,
|
|
|
+ # )
|
|
|
+ #
|
|
|
+ # box_roi_pool = MultiScaleRoIAlign(featmap_names=["0", "1", "2", "3"], output_size=7, sampling_ratio=2)
|
|
|
+ #
|
|
|
+ # resolution = box_roi_pool.output_size[0]
|
|
|
+ # representation_size = 1024
|
|
|
+ # box_head = TwoMLPHead(out_channels * resolution ** 2, representation_size)
|
|
|
+ # representation_size = 1024
|
|
|
+ # box_predictor = BoxPredictor(representation_size, num_classes)
|
|
|
+ #
|
|
|
+ # roi_heads = RoIHeads(
|
|
|
+ # # Box
|
|
|
+ # box_roi_pool,
|
|
|
+ # box_head,
|
|
|
+ # box_predictor,
|
|
|
+ # line_head,
|
|
|
+ # line_predictor,
|
|
|
+ # box_fg_iou_thresh,
|
|
|
+ # box_bg_iou_thresh,
|
|
|
+ # box_batch_size_per_image,
|
|
|
+ # box_positive_fraction,
|
|
|
+ # bbox_reg_weights,
|
|
|
+ # box_score_thresh,
|
|
|
+ # box_nms_thresh,
|
|
|
+ # box_detections_per_img,
|
|
|
+ # )
|
|
|
+ # image_mean = [0.485, 0.456, 0.406]
|
|
|
+ # image_std = [0.229, 0.224, 0.225]
|
|
|
+ # transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std, **kwargs)
|
|
|
+ # super().__init__(backbone, rpn, roi_heads, transform)
|
|
|
+ # self.roi_heads = roi_heads
|
|
|
+
|
|
|
+ def construct(
|
|
|
+ self,
|
|
|
+ backbone,
|
|
|
+ num_classes=None,
|
|
|
+ # transform parameters
|
|
|
+ min_size=512,
|
|
|
+ max_size=1333,
|
|
|
+ image_mean=None,
|
|
|
+ image_std=None,
|
|
|
+ # RPN parameters
|
|
|
+ rpn_anchor_generator=None,
|
|
|
+ rpn_head=None,
|
|
|
+ rpn_pre_nms_top_n_train=2000,
|
|
|
+ rpn_pre_nms_top_n_test=1000,
|
|
|
+ rpn_post_nms_top_n_train=2000,
|
|
|
+ rpn_post_nms_top_n_test=1000,
|
|
|
+ rpn_nms_thresh=0.7,
|
|
|
+ rpn_fg_iou_thresh=0.7,
|
|
|
+ rpn_bg_iou_thresh=0.3,
|
|
|
+ rpn_batch_size_per_image=256,
|
|
|
+ rpn_positive_fraction=0.5,
|
|
|
+ rpn_score_thresh=0.0,
|
|
|
+ # Box parameters
|
|
|
+ box_roi_pool=None,
|
|
|
+ box_head=None,
|
|
|
+ box_predictor=None,
|
|
|
+ box_score_thresh=0.05,
|
|
|
+ box_nms_thresh=0.5,
|
|
|
+ box_detections_per_img=100,
|
|
|
+ box_fg_iou_thresh=0.5,
|
|
|
+ box_bg_iou_thresh=0.5,
|
|
|
+ box_batch_size_per_image=512,
|
|
|
+ box_positive_fraction=0.25,
|
|
|
+ bbox_reg_weights=None,
|
|
|
+ # line parameters
|
|
|
+ line_head=None,
|
|
|
+ line_predictor=None,
|
|
|
+ **kwargs,
|
|
|
+ ):
|
|
|
+
|
|
|
+ if not hasattr(backbone, "out_channels"):
|
|
|
+ raise ValueError(
|
|
|
+ "backbone should contain an attribute out_channels "
|
|
|
+ "specifying the number of output channels (assumed to be the "
|
|
|
+ "same for all the levels)"
|
|
|
+ )
|
|
|
+
|
|
|
+ if not isinstance(rpn_anchor_generator, (AnchorGenerator, type(None))):
|
|
|
+ raise TypeError(
|
|
|
+ f"rpn_anchor_generator should be of type AnchorGenerator or None instead of {type(rpn_anchor_generator)}"
|
|
|
+ )
|
|
|
+ if not isinstance(box_roi_pool, (MultiScaleRoIAlign, type(None))):
|
|
|
+ raise TypeError(
|
|
|
+ f"box_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(box_roi_pool)}"
|
|
|
+ )
|
|
|
+
|
|
|
+ if num_classes is not None:
|
|
|
+ if box_predictor is not None:
|
|
|
+ raise ValueError("num_classes should be None when box_predictor is specified")
|
|
|
+ else:
|
|
|
+ if box_predictor is None:
|
|
|
+ raise ValueError("num_classes should not be None when box_predictor is not specified")
|
|
|
+
|
|
|
+ out_channels = backbone.out_channels
|
|
|
+
|
|
|
+ if line_head is None:
|
|
|
+ num_class = 5
|
|
|
+ line_head = LineRCNNHeads(out_channels, num_class)
|
|
|
+
|
|
|
+ if line_predictor is None:
|
|
|
+ line_predictor = LineRCNNPredictor(self.cfg)
|
|
|
+
|
|
|
+ if rpn_anchor_generator is None:
|
|
|
rpn_anchor_generator = _default_anchorgen()
|
|
|
+ if rpn_head is None:
|
|
|
rpn_head = RPNHead(out_channels, rpn_anchor_generator.num_anchors_per_location()[0])
|
|
|
- rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
|
|
|
- rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)
|
|
|
-
|
|
|
- rpn = RegionProposalNetwork(
|
|
|
- rpn_anchor_generator,
|
|
|
- rpn_head,
|
|
|
- rpn_fg_iou_thresh,
|
|
|
- rpn_bg_iou_thresh,
|
|
|
- rpn_batch_size_per_image,
|
|
|
- rpn_positive_fraction,
|
|
|
- rpn_pre_nms_top_n,
|
|
|
- rpn_post_nms_top_n,
|
|
|
- rpn_nms_thresh,
|
|
|
- score_thresh=rpn_score_thresh,
|
|
|
- )
|
|
|
|
|
|
+ rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
|
|
|
+ rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)
|
|
|
+
|
|
|
+ rpn = RegionProposalNetwork(
|
|
|
+ rpn_anchor_generator,
|
|
|
+ rpn_head,
|
|
|
+ rpn_fg_iou_thresh,
|
|
|
+ rpn_bg_iou_thresh,
|
|
|
+ rpn_batch_size_per_image,
|
|
|
+ rpn_positive_fraction,
|
|
|
+ rpn_pre_nms_top_n,
|
|
|
+ rpn_post_nms_top_n,
|
|
|
+ rpn_nms_thresh,
|
|
|
+ score_thresh=rpn_score_thresh,
|
|
|
+ )
|
|
|
+
|
|
|
+ if box_roi_pool is None:
|
|
|
box_roi_pool = MultiScaleRoIAlign(featmap_names=["0", "1", "2", "3"], output_size=7, sampling_ratio=2)
|
|
|
|
|
|
+ if box_head is None:
|
|
|
resolution = box_roi_pool.output_size[0]
|
|
|
representation_size = 1024
|
|
|
box_head = TwoMLPHead(out_channels * resolution ** 2, representation_size)
|
|
|
+
|
|
|
+ if box_predictor is None:
|
|
|
representation_size = 1024
|
|
|
box_predictor = BoxPredictor(representation_size, num_classes)
|
|
|
|
|
|
- roi_heads = RoIHeads(
|
|
|
- # Box
|
|
|
- box_roi_pool,
|
|
|
- box_head,
|
|
|
- box_predictor,
|
|
|
- line_head,
|
|
|
- line_predictor,
|
|
|
- box_fg_iou_thresh,
|
|
|
- box_bg_iou_thresh,
|
|
|
- box_batch_size_per_image,
|
|
|
- box_positive_fraction,
|
|
|
- bbox_reg_weights,
|
|
|
- box_score_thresh,
|
|
|
- box_nms_thresh,
|
|
|
- box_detections_per_img,
|
|
|
- )
|
|
|
+ roi_heads = RoIHeads(
|
|
|
+ # Box
|
|
|
+ box_roi_pool,
|
|
|
+ box_head,
|
|
|
+ box_predictor,
|
|
|
+ line_head,
|
|
|
+ line_predictor,
|
|
|
+ box_fg_iou_thresh,
|
|
|
+ box_bg_iou_thresh,
|
|
|
+ box_batch_size_per_image,
|
|
|
+ box_positive_fraction,
|
|
|
+ bbox_reg_weights,
|
|
|
+ box_score_thresh,
|
|
|
+ box_nms_thresh,
|
|
|
+ box_detections_per_img,
|
|
|
+ )
|
|
|
+
|
|
|
+ if image_mean is None:
|
|
|
image_mean = [0.485, 0.456, 0.406]
|
|
|
+ if image_std is None:
|
|
|
image_std = [0.229, 0.224, 0.225]
|
|
|
- transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std, **kwargs)
|
|
|
- super().__init__(backbone, rpn, roi_heads, transform)
|
|
|
- self.roi_heads = roi_heads
|
|
|
-
|
|
|
- # def __init__(
|
|
|
- # self,
|
|
|
- # backbone,
|
|
|
- # num_classes=None,
|
|
|
- # # transform parameters
|
|
|
- # min_size=512,
|
|
|
- # max_size=1333,
|
|
|
- # image_mean=None,
|
|
|
- # image_std=None,
|
|
|
- # # RPN parameters
|
|
|
- # rpn_anchor_generator=None,
|
|
|
- # rpn_head=None,
|
|
|
- # rpn_pre_nms_top_n_train=2000,
|
|
|
- # rpn_pre_nms_top_n_test=1000,
|
|
|
- # rpn_post_nms_top_n_train=2000,
|
|
|
- # rpn_post_nms_top_n_test=1000,
|
|
|
- # rpn_nms_thresh=0.7,
|
|
|
- # rpn_fg_iou_thresh=0.7,
|
|
|
- # rpn_bg_iou_thresh=0.3,
|
|
|
- # rpn_batch_size_per_image=256,
|
|
|
- # rpn_positive_fraction=0.5,
|
|
|
- # rpn_score_thresh=0.0,
|
|
|
- # # Box parameters
|
|
|
- # box_roi_pool=None,
|
|
|
- # box_head=None,
|
|
|
- # box_predictor=None,
|
|
|
- # box_score_thresh=0.05,
|
|
|
- # box_nms_thresh=0.5,
|
|
|
- # box_detections_per_img=100,
|
|
|
- # box_fg_iou_thresh=0.5,
|
|
|
- # box_bg_iou_thresh=0.5,
|
|
|
- # box_batch_size_per_image=512,
|
|
|
- # box_positive_fraction=0.25,
|
|
|
- # bbox_reg_weights=None,
|
|
|
- # # line parameters
|
|
|
- # line_head=None,
|
|
|
- # line_predictor=None,
|
|
|
- # **kwargs,
|
|
|
- # ):
|
|
|
- #
|
|
|
- # if not hasattr(backbone, "out_channels"):
|
|
|
- # raise ValueError(
|
|
|
- # "backbone should contain an attribute out_channels "
|
|
|
- # "specifying the number of output channels (assumed to be the "
|
|
|
- # "same for all the levels)"
|
|
|
- # )
|
|
|
- #
|
|
|
- # if not isinstance(rpn_anchor_generator, (AnchorGenerator, type(None))):
|
|
|
- # raise TypeError(
|
|
|
- # f"rpn_anchor_generator should be of type AnchorGenerator or None instead of {type(rpn_anchor_generator)}"
|
|
|
- # )
|
|
|
- # if not isinstance(box_roi_pool, (MultiScaleRoIAlign, type(None))):
|
|
|
- # raise TypeError(
|
|
|
- # f"box_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(box_roi_pool)}"
|
|
|
- # )
|
|
|
- #
|
|
|
- # if num_classes is not None:
|
|
|
- # if box_predictor is not None:
|
|
|
- # raise ValueError("num_classes should be None when box_predictor is specified")
|
|
|
- # else:
|
|
|
- # if box_predictor is None:
|
|
|
- # raise ValueError("num_classes should not be None when box_predictor is not specified")
|
|
|
- #
|
|
|
- # out_channels = backbone.out_channels
|
|
|
- #
|
|
|
- # if line_head is None:
|
|
|
- # num_class = 5
|
|
|
- # line_head = LineRCNNHeads(out_channels, num_class)
|
|
|
- #
|
|
|
- # if line_predictor is None:
|
|
|
- # line_predictor = LineRCNNPredictor()
|
|
|
- #
|
|
|
- # if rpn_anchor_generator is None:
|
|
|
- # rpn_anchor_generator = _default_anchorgen()
|
|
|
- # if rpn_head is None:
|
|
|
- # rpn_head = RPNHead(out_channels, rpn_anchor_generator.num_anchors_per_location()[0])
|
|
|
- #
|
|
|
- # rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
|
|
|
- # rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)
|
|
|
- #
|
|
|
- # rpn = RegionProposalNetwork(
|
|
|
- # rpn_anchor_generator,
|
|
|
- # rpn_head,
|
|
|
- # rpn_fg_iou_thresh,
|
|
|
- # rpn_bg_iou_thresh,
|
|
|
- # rpn_batch_size_per_image,
|
|
|
- # rpn_positive_fraction,
|
|
|
- # rpn_pre_nms_top_n,
|
|
|
- # rpn_post_nms_top_n,
|
|
|
- # rpn_nms_thresh,
|
|
|
- # score_thresh=rpn_score_thresh,
|
|
|
- # )
|
|
|
- #
|
|
|
- # if box_roi_pool is None:
|
|
|
- # box_roi_pool = MultiScaleRoIAlign(featmap_names=["0", "1", "2", "3"], output_size=7, sampling_ratio=2)
|
|
|
- #
|
|
|
- # if box_head is None:
|
|
|
- # resolution = box_roi_pool.output_size[0]
|
|
|
- # representation_size = 1024
|
|
|
- # box_head = TwoMLPHead(out_channels * resolution ** 2, representation_size)
|
|
|
- #
|
|
|
- # if box_predictor is None:
|
|
|
- # representation_size = 1024
|
|
|
- # box_predictor = BoxPredictor(representation_size, num_classes)
|
|
|
- #
|
|
|
- # roi_heads = RoIHeads(
|
|
|
- # # Box
|
|
|
- # box_roi_pool,
|
|
|
- # box_head,
|
|
|
- # box_predictor,
|
|
|
- # line_head,
|
|
|
- # line_predictor,
|
|
|
- # box_fg_iou_thresh,
|
|
|
- # box_bg_iou_thresh,
|
|
|
- # box_batch_size_per_image,
|
|
|
- # box_positive_fraction,
|
|
|
- # bbox_reg_weights,
|
|
|
- # box_score_thresh,
|
|
|
- # box_nms_thresh,
|
|
|
- # box_detections_per_img,
|
|
|
- # )
|
|
|
- #
|
|
|
- # if image_mean is None:
|
|
|
- # image_mean = [0.485, 0.456, 0.406]
|
|
|
- # if image_std is None:
|
|
|
- # image_std = [0.229, 0.224, 0.225]
|
|
|
- # transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std, **kwargs)
|
|
|
- #
|
|
|
- # super().__init__(backbone, rpn, roi_heads, transform)
|
|
|
- #
|
|
|
- # self.roi_heads = roi_heads
|
|
|
-
|
|
|
- # self.roi_heads.line_head = line_head
|
|
|
- # self.roi_heads.line_predictor = line_predictor
|
|
|
+ transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std, **kwargs)
|
|
|
+
|
|
|
+ super().__init__(backbone, rpn, roi_heads, transform)
|
|
|
+
|
|
|
+ self.roi_heads = roi_heads
|
|
|
+
|
|
|
+ self.roi_heads.line_head = line_head
|
|
|
+ self.roi_heads.line_predictor = line_predictor
|
|
|
|
|
|
def train_by_cfg(self, cfg):
|
|
|
# cfg = read_yaml(cfg)
|