Beispiel #1
0
def postprocess(outputs, nested_test_image, args):
    image_shape = nested_test_image.tensors.shape[2:]
    postprocessor = PostProcess()
    results = postprocessor(outputs, dg.to_variable([image_shape]))

    image = plot_results(nested_test_image.tensors[0], results[0])

    output_path = args.output_dir + '/' + args.demo_image.split('/')[-1]
    cv2.imwrite(output_path, image)
Beispiel #2
0
def invoice(pretrained=False, num_classes=91, return_postprocessor=False):
    """
    DETR R50 with 6 encoder and 6 decoder layers.

    """
    model = _make_detr("resnet50", dilation=False, num_classes=num_classes)
    if pretrained:
        checkpoint = torch.hub.load_state_dict_from_url(
            url="invoice_large_transfer/checkpoint.pth",
            map_location="cpu",
            check_hash=True)
        model.load_state_dict(checkpoint["model"])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #3
0
def detr_resnet101(pretrained=False, num_classes=91, return_postprocessor=False):
    """
    DETR-DC5 R101 with 6 encoder and 6 decoder layers.

    Achieves 43.5/63.8 AP/AP50 on COCO val5k.
    """
    model = _make_detr("resnet101", dilation=False, num_classes=num_classes)
    if pretrained:
        checkpoint = torch.hub.load_state_dict_from_url(
            url="https://dl.fbaipublicfiles.com/detr/detr-r101-2c7b67e5.pth", map_location="cpu", check_hash=True
        )
        model.load_state_dict(checkpoint["model"])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #4
0
def detr_resnet101(pretrained=False,
                   num_classes=91,
                   return_postprocessor=False):
    """
    DETR-DC5 R101 with 6 encoder and 6 decoder layers.

    Achieves 43.5/63.8 AP/AP50 on COCO val5k.
    """
    model = _make_detr('resnet101', dilation=False, num_classes=num_classes)
    if pretrained:
        checkpoint = paddle.load(
            'https://dl.fbaipublicfiles.com/detr/detr-r101-2c7b67e5.pdiparams')
        model.load_state_dict(checkpoint['model'])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #5
0
def detr_resnet50(pretrained=False,
                  num_classes=91,
                  return_postprocessor=False):
    """
    DETR R50 with 6 encoder and 6 decoder layers.

    Achieves 42/62.4 AP/AP50 on COCO val5k.
    """
    model = _make_detr('resnet50', dilation=False, num_classes=num_classes)
    if pretrained:
        checkpoint = paddle.load(
            'https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pdiparams')
        model.load_state_dict(checkpoint['model'])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #6
0
def detr_resnet101_dc5(pretrained=False, num_classes=91, return_postprocessor=False):
    """
    DETR-DC5 R101 with 6 encoder and 6 decoder layers.

    The last block of ResNet-101 has dilation to increase
    output resolution.
    Achieves 44.9/64.7 AP/AP50 on COCO val5k.
    """
    model = _make_detr("resnet101", dilation=True, num_classes=num_classes)
    if pretrained:
        checkpoint = torch.hub.load_state_dict_from_url(
            url="https://dl.fbaipublicfiles.com/detr/detr-r101-dc5-a2e86def.pth", map_location="cpu", check_hash=True
        )
        model.load_state_dict(checkpoint["model"])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #7
0
def detr_resnet50(pretrained=False,
                  num_classes=91,
                  return_postprocessor=False):
    """
    DETR R50 with 6 encoder and 6 decoder layers.

    Achieves 42/62.4 AP/AP50 on COCO val5k.
    """
    model = _make_detr("resnet50", dilation=False, num_classes=num_classes)
    if pretrained:
        checkpoint = torch.hub.load_state_dict_from_url(
            url="https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth",
            map_location="cpu",
            check_hash=False)
        model.load_state_dict(checkpoint["model"])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #8
0
def detr_resnet101_dc5(pretrained=False,
                       num_classes=91,
                       return_postprocessor=False):
    """
    DETR-DC5 R101 with 6 encoder and 6 decoder layers.

    The last block of ResNet-101 has dilation to increase
    output resolution.
    Achieves 44.9/64.7 AP/AP50 on COCO val5k.
    """
    model = _make_detr('resnet101', dilation=True, num_classes=num_classes)
    if pretrained:
        checkpoint = paddle.load(
            'https://dl.fbaipublicfiles.com/detr/detr-r101-dc5-a2e86def.pdiparams'
        )
        model.load_state_dict(checkpoint['model'])
    if return_postprocessor:
        return model, PostProcess()
    return model
Beispiel #9
0
    pre_norm = False
    num_classes = 3
    num_queries = 100
    aux_loss = True

    backbone = build_backbone(lr_backbone, masks, backbone, dilation, hidden_dim, position_embedding)
    transformer = build_transformer(hidden_dim, dropout, nheads, dim_feedforward, enc_layers, dec_layers, pre_norm)
    model = DETR(
        backbone,
        transformer,
        num_classes=num_classes,
        num_queries=num_queries,
        aux_loss=aux_loss,
    )
    transform = make_coco_transforms()
    postprocessors = PostProcess()

    checkpoint = torch.load('/home/palm/PycharmProjects/detr/snapshots/1/checkpoint00295.pth')
    model.load_state_dict(checkpoint['model'])

    train_ints, valid_ints, labels, max_box_per_image = create_csv_training_instances(
        '/home/palm/PycharmProjects/algea/dataset/train_annotations',
        '/home/palm/PycharmProjects/algea/dataset/test_annotations',
        '/home/palm/PycharmProjects/algea/dataset/classes',
    )
    # os.listdir()
    all_detections = []
    all_annotations = []
    model.cuda()
    for instance in valid_ints:
        t = time.time()
Beispiel #10
0
    aux_loss=args.aux_loss,
)
matcher = HungarianMatcher(cost_class=args.set_cost_class, cost_bbox=args.set_cost_bbox, cost_giou=args.set_cost_giou)

weight_dict = {'loss_ce': 1, 'loss_bbox': args.bbox_loss_coef}
weight_dict['loss_giou'] = args.giou_loss_coef
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']
criterion = SetCriterion(args.num_classes, matcher=matcher, weight_dict=weight_dict,
                         eos_coef=args.eos_coef, losses=losses)
postprocessors = {'bbox': PostProcess()}

criterion.to(device)
model.to(device)

# %% set distributed model
model_without_ddp = model
if args.distributed:
    model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
    model_without_ddp = model.module
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('number of params:', n_parameters)

# %% set optimizer
param_dicts = [
    {"params": [p for n, p in model_without_ddp.named_parameters() if "backbone" not in n and p.requires_grad]},
Beispiel #11
0
def build_model(args):
    num_classes = 20 if args.dataset_file != 'coco' else 91
    if args.dataset_file == "coco_panoptic":
        num_classes = 250
    if args.dataset_file == "ImageNet":
        num_classes = 2  # feel free to this num_classes, positive integer larger than 1 is OK.
    device = torch.device(args.device)

    backbone = build_backbone(args)

    transformer = build_transformer(args)

    if args.dataset_file == "ImageNet":
        model = UPDETR(backbone,
                       transformer,
                       num_classes=num_classes,
                       num_queries=args.num_queries,
                       aux_loss=args.aux_loss,
                       num_patches=args.num_patches,
                       feature_recon=args.feature_recon,
                       query_shuffle=args.query_shuffle)
    else:
        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,
        'loss_giou': args.giou_loss_coef
    }
    if args.dataset_file == 'ImageNet' and args.feature_recon:
        weight_dict['loss_feature'] = 1
    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.dataset_file == 'ImageNet' and args.feature_recon:
        losses += ['feature']
    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