def detr_resnet101_panoptic(pretrained=False, num_classes=91, threshold=0.85, return_postprocessor=False): """ DETR-DC5 R101 with 6 encoder and 6 decoder layers. Achieves 45.1 PQ on COCO val5k. threshold is the minimum confidence required for keeping segments in the prediction """ model = _make_detr("resnet101", dilation=False, num_classes=num_classes, mask=True) is_thing_map = {i: i <= 90 for i in range(250)} if pretrained: checkpoint = torch.hub.load_state_dict_from_url( url= "https://dl.fbaipublicfiles.com/detr/detr-r101-panoptic-40021d53.pth", map_location="cpu", check_hash=True, ) model.load_state_dict(checkpoint["model"]) if return_postprocessor: return model, PostProcessPanoptic(is_thing_map, threshold=threshold) return model
def detr_resnet50_dc5_panoptic(pretrained=False, num_classes=91, threshold=0.85, return_postprocessor=False): """ DETR-DC5 R50 with 6 encoder and 6 decoder layers. The last block of ResNet-50 has dilation to increase output resolution. Achieves 44.6 on COCO val5k. threshold is the minimum confidence required for keeping segments in the prediction """ model = _make_detr("resnet50", dilation=True, num_classes=num_classes, mask=True) is_thing_map = {i: i <= 90 for i in range(250)} if pretrained: checkpoint = torch.hub.load_state_dict_from_url( url= "https://dl.fbaipublicfiles.com/detr/detr-r50-dc5-panoptic-da08f1b1.pth", map_location="cpu", check_hash=True, ) model.load_state_dict(checkpoint["model"]) if return_postprocessor: return model, PostProcessPanoptic(is_thing_map, threshold=threshold) return model
def detr_resnet50_dc5_panoptic(pretrained=False, num_classes=250, threshold=0.85, return_postprocessor=False): """ DETR-DC5 R50 with 6 encoder and 6 decoder layers. The last block of ResNet-50 has dilation to increase output resolution. Achieves 44.6 on COCO val5k. threshold is the minimum confidence required for keeping segments in the prediction """ model = _make_detr('resnet50', dilation=True, num_classes=num_classes, mask=True) is_thing_map = {i: (i <= 90) for i in range(250)} if pretrained: checkpoint = paddle.load( 'https://dl.fbaipublicfiles.com/detr/detr-r50-dc5-panoptic-da08f1b1.pdiparams' ) model.load_state_dict(checkpoint['model']) if return_postprocessor: return model, PostProcessPanoptic(is_thing_map, threshold=threshold) return model
def build(args): num_classes = 20 if args.dataset_file != 'coco' else 91 if args.dataset_file == "coco_panoptic": num_classes = 250 device = torch.device(args.device) backbone = build_backbone(args) transformer = build_transformer(args) model = DETR( args, backbone, transformer, num_classes=num_classes, num_queries=args.num_queries, aux_loss=args.aux_loss, ) if args.masks: model = DETRsegm(model, freeze_detr=(args.frozen_weights is not None)) matcher = build_matcher(args) weight_dict = {'loss_ce': 1, 'loss_bbox': args.bbox_loss_coef} weight_dict['loss_giou'] = args.giou_loss_coef if args.masks: weight_dict["loss_mask"] = args.mask_loss_coef weight_dict["loss_dice"] = args.dice_loss_coef # TODO this is a hack if args.aux_loss: aux_weight_dict = {} for i in range(args.dec_layers - 1): aux_weight_dict.update( {k + f'_{i}': v for k, v in weight_dict.items()}) weight_dict.update(aux_weight_dict) losses = ['labels', 'boxes', 'cardinality'] if args.masks: losses += ["masks"] criterion = SetCriterion(num_classes, matcher=matcher, weight_dict=weight_dict, eos_coef=args.eos_coef, losses=losses) criterion.to(device) postprocessors = {'bbox': PostProcess()} if args.masks: postprocessors['segm'] = PostProcessSegm() if args.dataset_file == "coco_panoptic": is_thing_map = {i: i <= 90 for i in range(201)} postprocessors["panoptic"] = PostProcessPanoptic(is_thing_map, threshold=0.85) return model, criterion, postprocessors
def detr_resnet50_panoptic(pretrained=False, num_classes=250, threshold=\ 0.85, return_postprocessor=False): """ DETR R50 with 6 encoder and 6 decoder layers. Achieves 43.4 PQ on COCO val5k. threshold is the minimum confidence required for keeping segments in the prediction """ model = _make_detr('resnet50', dilation=False, num_classes=num_classes, mask=True) is_thing_map = {i: (i <= 90) for i in range(250)} if pretrained: checkpoint = paddle.load( 'https://dl.fbaipublicfiles.com/detr/detr-r50-panoptic-00ce5173.pdiparams' ) model.load_state_dict(checkpoint['model']) if return_postprocessor: return model, PostProcessPanoptic(is_thing_map, threshold=threshold) return model
def build(args): # the `num_classes` naming here is somewhat misleading. # it indeed corresponds to `max_obj_id + 1`, where max_obj_id # is the maximum id for a class in your dataset. For example, # COCO has a max_obj_id of 90, so we pass `num_classes` to be 91. # As another example, for a dataset that has a single class with id 1, # you should pass `num_classes` to be 2 (max_obj_id + 1). # For more details on this, check the following discussion # https://github.com/facebookresearch/detr/issues/108#issuecomment-650269223 num_classes = 20 if args.dataset_file != 'coco' else 91 if args.dataset_file == "coco_panoptic": # for panoptic, we just add a num_classes that is large enough to hold # max_obj_id + 1, but the exact value doesn't really matter num_classes = 250 device = torch.device(args.device) backbone = build_backbone(args) if int(os.environ.get("cross_transformer", 0)): transformer = build_cross_transformer(args) elif int(os.environ.get("sparse_transformer", 0)): transformer = build_sparse_transformer(args) elif int(os.environ.get("linear_transformer", 0)): transformer = build_linear_transformer(args) else: transformer = build_transformer(args) model = DETR( backbone, transformer, num_classes=num_classes, num_queries=args.num_queries, aux_loss=args.aux_loss, ) if args.masks: model = DETRsegm(model, freeze_detr=(args.frozen_weights is not None)) matcher = build_matcher(args) weight_dict = {'loss_ce': 1, 'loss_bbox': args.bbox_loss_coef} weight_dict['loss_giou'] = args.giou_loss_coef if args.masks: weight_dict["loss_mask"] = args.mask_loss_coef weight_dict["loss_dice"] = args.dice_loss_coef # TODO this is a hack if args.aux_loss: aux_weight_dict = {} for i in range(args.dec_layers - 1): aux_weight_dict.update( {k + f'_{i}': v for k, v in weight_dict.items()}) weight_dict.update(aux_weight_dict) losses = ['labels', 'boxes', 'cardinality'] if args.masks: losses += ["masks"] criterion = SetCriterion(num_classes, matcher=matcher, weight_dict=weight_dict, eos_coef=args.eos_coef, losses=losses) criterion.to(device) postprocessors = {'bbox': PostProcess()} if args.masks: postprocessors['segm'] = PostProcessSegm() if args.dataset_file == "coco_panoptic": is_thing_map = {i: i <= 90 for i in range(201)} postprocessors["panoptic"] = PostProcessPanoptic(is_thing_map, threshold=0.85) return model, criterion, postprocessors