def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG scheme = "free" if cfg.MODEL.SAMPLING_FREE_ON else "sampling" loss_evaluator = eval("FastRCNNLossComputation" + scheme.capitalize())(\ matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) class_weights = cfg.MODEL.ROI_BOX_HEAD.CLASS_WEIGHT class_compete = cfg.MODEL.ROI_BOX_HEAD.CLASS_COMPETE if len(class_weights) == 1: class_weights = class_weights * cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG use_focal_loss = cfg.MODEL.ROI_BOX_HEAD.USE_FOCAL_LOSS sigmoid_focal_loss = SigmoidFocalLoss(cfg.MODEL.RETINANET.LOSS_GAMMA, cfg.MODEL.RETINANET.LOSS_ALPHA) loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, class_weights, class_compete, cls_agnostic_bbox_reg, use_focal_loss, sigmoid_focal_loss) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( # Overlap threshold for an RoI to be considered foreground (if >= FG_IOU_THRESHOLD) cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, # 0.5 # Overlap threshold for an RoI to be considered background # (class = 0 if overlap in [0, BG_IOU_THRESHOLD)) cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, # 0.5 allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS # (10., 10., 5., 5.) box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, # 512 cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION # 0.25 ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG # False loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG cls_weight = None if cfg.MODEL.ROI_BOX_HEAD.CLS_LOSS_WEIGHT: cls_weight = torch.tensor( cfg.MODEL.ROI_BOX_HEAD.CLS_LOSS_WEIGHT).cuda() loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, cls_weight) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG func = registry.ROI_BOX_HEADS_LOSS[cfg.MODEL.ROI_BOX_HEAD.LOSS] loss_evaluator = func( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG sigmoid_focal_loss = SigmoidFocalLoss( cfg.MODEL.RETINANET.LOSS_GAMMA, cfg.MODEL.RETINANET.LOSS_ALPHA ) loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, sigmoid_focal_loss, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg, stage=None): if stage is None or stage == 1: matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS elif stage == 1: matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=True, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS elif stage == 2: matcher = Matcher( cfg.MODEL.ROI_HEADS.STAGE2.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.STAGE2.BG_IOU_THRESHOLD, allow_low_quality_matches=True, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.STAGE2.BBOX_REG_WEIGHTS elif stage == 3: matcher = Matcher( cfg.MODEL.ROI_HEADS.STAGE3.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.STAGE3.BG_IOU_THRESHOLD, allow_low_quality_matches=True, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.STAGE3.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) sampling = cfg.MODEL.SAMPLING if sampling == "random": fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) elif sampling == "balanced": fg_bg_sampler = IOUBalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG bbox_loss_type = cfg.MODEL.BBOX_LOSS_TYPE label_smoothing = cfg.MODEL.LABEL_SMOOTHING num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, bbox_loss_type, sampling, label_smoothing, num_classes ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS reg_angle_relative = cfg.MODEL.ROI_HEADS.BBOX_REG_ANGLE_RELATIVE box_coder = BoxCoder(weights=bbox_reg_weights, relative_angle=reg_angle_relative) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher_iou = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_LOW_IOU_THRESHOLD, allow_low_quality_matches=False, ) matcher_visible_iog = Matcher( cfg.MODEL.ROI_HEADS.FG_IOG_VISIBLE_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOG_VISIBLE_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG loss_evaluator = FastRCNNLossComputation(matcher_iou, matcher_visible_iog, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg) return loss_evaluator
def make_retinanet_loss_evaluator(cfg, box_coder): fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.RPN.BATCH_SIZE_PER_IMAGE, cfg.MODEL.RPN.POSITIVE_FRACTION ) matcher = Matcher( cfg.MODEL.RETINANET.FG_IOU_THRESHOLD, cfg.MODEL.RETINANET.BG_IOU_THRESHOLD, allow_low_quality_matches=True, ) sigmoid_focal_loss = SigmoidFocalLoss( cfg.MODEL.RETINANET.LOSS_GAMMA, cfg.MODEL.RETINANET.LOSS_ALPHA ) loss_evaluator = RetinaNetLossComputation( matcher, box_coder, generate_retinanet_labels, sigmoid_focal_loss, fg_bg_sampler, bbox_reg_beta = cfg.MODEL.RETINANET.BBOX_REG_BETA, embed_margin = cfg.MODEL.RETINANET.EMBED_MARGIN, embedding_loss = cfg.MODEL.RETINANET.EMBED_LOSS, regress_norm = cfg.MODEL.RETINANET.BBOX_REG_WEIGHT ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG # ######################## add by hui ######################################################## ohem_loss = None if cfg.MODEL.ROI_HEADS.OHEM == 1: ohem_loss = OHEMLoss(cfg.MODEL.ROI_HEADS.OHEM1_NEG_RATE) elif cfg.MODEL.ROI_HEADS.OHEM == 2: ohem_loss = OHEM2Loss(cfg.MODEL.ROI_HEADS.OHEM2_BATCH_SIZE_PER_IM * cfg.SOLVER.IMS_PER_BATCH // cfg.SOLVER.NUM_GPU, cfg.MODEL.ROI_HEADS.OHEM2_POSITIVE_FRACTION, hard_rate=cfg.MODEL.ROI_HEADS.OHEM2_HARD_RATE) loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, ohem_loss) # ################################################################################################################ return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG attribute_on = cfg.MODEL.ATTRIBUTE_ON loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, attribute_on ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = create_roi_head_matcher(cfg) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG classification_loss_type = cfg.MODEL.ROI_BOX_HEAD.CLASSIFICATION_LOSS num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES cfg = cfg loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, classification_loss_type, num_classes, boundingbox_loss_type=cfg.MODEL.ROI_BOX_HEAD.BOUNDINGBOX_LOSS_TYPE, cfg=cfg, ) return loss_evaluator
def make_roi_keypoint_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) resolution = cfg.MODEL.ROI_KEYPOINT_HEAD.RESOLUTION loss_evaluator = KeypointRCNNLossComputation(matcher, fg_bg_sampler, resolution) return loss_evaluator
def make_roi_pose_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler) return loss_evaluator
def make_retinanet_loss_evaluator(cfg, box_coder): classify_holistic_image = cfg.MODEL.CLASSIFY_HOLISTIC_IMAGE classify_objectness_image = cfg.MODEL.CLASSIFY_OBJECTNESS_IMAGE num_classes = cfg.MODEL.RETINANET.NUM_CLASSES num_holistic_classes = cfg.MODEL.RETINANET.NUM_HOLISTIC_CLASSES matcher = Matcher( cfg.MODEL.RETINANET.FG_IOU_THRESHOLD, cfg.MODEL.RETINANET.BG_IOU_THRESHOLD, allow_low_quality_matches=True, ) sigmoid_focal_loss = SigmoidFocalLoss(cfg.MODEL.RETINANET.LOSS_GAMMA, cfg.MODEL.RETINANET.LOSS_ALPHA) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.RPN.BATCH_SIZE_PER_IMAGE, cfg.MODEL.RPN.POSITIVE_FRACTION) loss_evaluator = RetinaNetLossComputation( matcher, box_coder, generate_retinanet_labels, sigmoid_focal_loss, bbox_reg_beta=cfg.MODEL.RETINANET.BBOX_REG_BETA, regress_norm=cfg.MODEL.RETINANET.BBOX_REG_WEIGHT, use_ignored_bbox=cfg.MODEL.RETINANET.LOSS_USE_IGNORE, classify_holistic_image=classify_holistic_image, num_classes=num_classes, num_holistic_classes=num_holistic_classes, classify_objectness_image=classify_objectness_image, objectness_norm=cfg.MODEL.RETINANET.OBJECTNESS_LOSS_WEIGHT, fg_bg_sampler=fg_bg_sampler, ) if classify_holistic_image: class_weights = cfg.MODEL.RETINANET.HOLISTIC_CLASS_WEIGHT holistic_classify_norm = cfg.MODEL.RETINANET.HOLISTIC_LOSS_WEIGHT use_focal_loss = cfg.MODEL.RETINANET.HOLISTIC_USE_FOCAL_LOSS, alpha = cfg.MODEL.RETINANET.HOLISTIC_ALPHA gamma = cfg.MODEL.RETINANET.HOLISTIC_GAMMA if len(class_weights) == 1: class_weights = class_weights * ( cfg.MODEL.RETINANET.NUM_HOLISTIC_CLASSES - 1) loss_holistic_evaluator = RetinaNetHolisticLossComputation( class_weights, holistic_classify_norm, use_focal_loss, alpha, gamma) else: loss_holistic_evaluator = None return loss_evaluator, loss_holistic_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder3D(is_corner_roi=cfg.MODEL.CORNER_ROI, weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) yaw_loss_mode = cfg.MODEL.LOSS.YAW_MODE add_gt_proposals = cfg.MODEL.RPN.ADD_GT_PROPOSALS ay = cfg.MODEL.ROI_HEADS.LABEL_AUG_THICKNESS_Y_TAR_ANC az = cfg.MODEL.ROI_HEADS.LABEL_AUG_THICKNESS_Z_TAR_ANC aug_thickness = { 'target_Y': ay[0], 'anchor_Y': ay[1], 'target_Z': az[0], 'anchor_Z': az[1] } in_classes = cfg.INPUT.CLASSES num_input_classes = len(in_classes) seperate_classes = cfg.MODEL.SEPARATE_CLASSES_ID class_specific = cfg.MODEL.CLASS_SPECIFIC seperate_classifier = SeperateClassifier(seperate_classes, num_input_classes, class_specific, 'ROI_LOSS') loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, yaw_loss_mode, add_gt_proposals, aug_thickness, seperate_classifier, class_specific=class_specific) return loss_evaluator, seperate_classifier
def make_roi_box_cascade_loss_evaluator(cfg, IOU, WEIGHTS): matcher = Matcher( IOU, 0.3, allow_low_quality_matches=True, ) bbox_reg_weights = WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) # 512,0.25 cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): #print(cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD) #print(cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD) matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.RBBOX_REG_WEIGHTS #print(bbox_reg_weights) box_coder = RBoxCoder(weights=bbox_reg_weights) #print(cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) edge_punished = cfg.MODEL.EDGE_PUNISHED loss_evaluator = FastRCNNLossComputation(matcher, fg_bg_sampler, box_coder, edge_punished) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG if cfg.MODEL.ROI_BOX_HEAD.USE_FOCAL: print("USING FOCAL LOSS FOR BOX HEAD") raise NotImplementedError() from maskrcnn_benchmark.modeling.rpn.retinanet.loss import SigmoidFocalLoss cls_loss = SigmoidFocalLoss( cfg.MODEL.ROI_BOX_HEAD.FOCAL.LOSS_GAMMA, cfg.MODEL.ROI_BOX_HEAD.FOCAL.LOSS_ALPHA ) else: cls_loss = F.cross_entropy loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_loss, cls_agnostic_bbox_reg ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG class_loss_func = cfg.MODEL.ROI_BOX_HEAD.CLASS_LOSS_FUNC class_loss_extra_argv = {} if class_loss_func == "CrossEntropyLoss": class_loss_extra_argv["smooth_eps"] = cfg.MODEL.ROI_BOX_HEAD.CLASS_LOSS_SMOOTH_EPS if class_loss_func == "SoftmaxFocalLoss": class_loss_extra_argv["alpha"] = cfg.MODEL.ROI_BOX_HEAD.CLASS_LOSS_ALPHA class_loss_extra_argv["gamma"] = cfg.MODEL.ROI_BOX_HEAD.CLASS_LOSS_GAMMA class_loss_extra_argv["smooth"] = cfg.MODEL.ROI_BOX_HEAD.CLASS_LOSS_SMOOTH class_loss_extra_argv["num_classes"] = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, class_loss_func=class_loss_func, class_loss_extra_argv=class_loss_extra_argv, ) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, discard_high_matches=cfg.MODEL.ROI_HEADS.HIGHEST_DISCARD, highest_threshold=cfg.MODEL.ROI_HEADS.HIGHEST_THRESHOLD, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.RBBOX_REG_WEIGHTS box_coder = RBoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) edge_punished = cfg.MODEL.EDGE_PUNISHED loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, edge_punished, discard_highest=cfg.MODEL.ROI_HEADS.HIGHEST_DISCARD) return loss_evaluator
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_agnostic_bbox_reg, pred_targets_as_true=cfg.MODEL.RPN.PRED_TARGETS_AS_TRUE) return loss_evaluator
data_loader = make_data_loader( cfg, is_train=True, is_distributed=False, start_iter=0, ) device = 'cpu' anchor_generator = make_rrpn_anchor_generator(cfg) num_anchors = anchor_generator.num_anchors_per_location() print(num_anchors) box_coder = BoxCoder(weights=None) #cfg.MODEL.RPN.BBOX_REG_WEIGHTS) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.RPN.BATCH_SIZE_PER_IMAGE, cfg.MODEL.RPN.POSITIVE_FRACTION) loss_evaluator = make_rpn_loss_evaluator(cfg, box_coder) start_iter = 0 for iteration, (images, targets, _) in enumerate(data_loader, start_iter): images = images.to(device) targets = [target.to(device) for target in targets] feature_maps = get_feature_maps(images.tensors, cfg.MODEL.RPN.ANCHOR_STRIDE) anchors = anchor_generator.forward(images, feature_maps) anchors = [ cat_boxlist(anchors_per_image) for anchors_per_image in anchors ] anchors_cnt = [len(a) for a in anchors]
def make_roi_box_loss_evaluator(cfg): matcher = Matcher( cfg.MODEL.ROI_HEADS.FG_IOU_THRESHOLD, cfg.MODEL.ROI_HEADS.BG_IOU_THRESHOLD, allow_low_quality_matches=False, ) bbox_reg_weights = cfg.MODEL.ROI_HEADS.BBOX_REG_WEIGHTS box_coder = BoxCoder(weights=bbox_reg_weights) fg_bg_sampler = BalancedPositiveNegativeSampler( cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE, cfg.MODEL.ROI_HEADS.POSITIVE_FRACTION ) cls_agnostic_bbox_reg = cfg.MODEL.CLS_AGNOSTIC_BBOX_REG cls_loss_fn_type = cfg.MODEL.ROI_HEADS.CLASSIFICATION_LOSS_FN cls_loss = {} if cls_loss_fn_type == "CE": cls_loss['fn'] = _cross_entropy_with_kwargs cls_loss['avg'] = False elif cls_loss_fn_type == "Focal": cls_loss['fn'] = SigmoidFocalLoss( cfg.MODEL.ROI_HEADS.FOCAL_LOSS_GAMMA, cfg.MODEL.ROI_HEADS.FOCAL_LOSS_ALPHA, ) cls_loss['avg'] = True elif cls_loss_fn_type == "ReducedFocal": cls_loss['fn'] = SigmoidReducedFocalLoss( cfg.MODEL.ROI_HEADS.FOCAL_LOSS_GAMMA, cfg.MODEL.ROI_HEADS.FOCAL_LOSS_ALPHA, cfg.MODEL.ROI_HEADS.REDUCED_FOCAL_LOSS_CUTOFF, cfg.MODEL.ROI_HEADS.CLASSIFICATION_LOSS_NORM, ) cls_loss['avg'] = True elif cls_loss_fn_type == "Class": # me being a lazy f**k # counts_dict = {6: 895135, 49: 1414550, 10: 54917, 54: 22780, 52: 5242, 11: 26608, 48: 5792, 7: 30454, 14: 17734, 3: 3190, 53: 7473, 51: 5739, 43: 3053, 5: 10108, 12: 16454, 17: 790, 56: 7281, 60: 464, 9: 15035, 13: 3403, 58: 5736, 57: 11459, 36: 836, 39: 1670, 20: 7744, 45: 1411, 19: 5213, 15: 4169, 8: 4671, 44: 4012, 25: 5254, 24: 3585, 29: 3201, 40: 5770, 34: 880, 26: 3107, 47: 2846, 59: 1609, 16: 582, 46: 248, 42: 464, 55: 594, 32: 1740, 27: 1089, 30: 1037, 28: 934, 50: 1285, 1: 322, 2: 1909, 4: 359, 38: 269, 37: 279, 35: 897, 23: 475, 22: 521, 31: 2504, 21: 442, 18: 70, 41: 1230, 33: 439} raise ValueError("deprecated class loss") elif cls_loss_fn_type == "AreaFocal": cls_loss['fn'] = SigmoidAreaReducedFocalLoss( cfg.MODEL.ROI_HEADS.FOCAL_LOSS_GAMMA, cfg.MODEL.ROI_HEADS.FOCAL_LOSS_ALPHA, cfg.MODEL.ROI_HEADS.AREA_LOSS_BETA, cfg.MODEL.ROI_HEADS.REDUCED_FOCAL_LOSS_CUTOFF, cfg.MODEL.ROI_HEADS.AREA_LOSS_THRESHOLD, cfg.MODEL.ROI_HEADS.CLASSIFICATION_LOSS_NORM, ) cls_loss['avg'] = True elif cls_loss_fn_type == "Area": cls_loss['fn'] = AreaLoss( cfg.MODEL.ROI_HEADS.AREA_LOSS_BETA, cfg.MODEL.ROI_HEADS.AREA_LOSS_THRESHOLD, ) cls_loss['avg'] = True else: raise ValueError("invalid classification loss type: {}".format(cls_loss_fn_type)) loss_evaluator = FastRCNNLossComputation( matcher, fg_bg_sampler, box_coder, cls_loss, cfg.MODEL.ROI_HEADS.CLS_LOSS_WT, cfg.MODEL.ROI_HEADS.BBOX_LOSS_WT, cls_agnostic_bbox_reg, ) return loss_evaluator