File size: 2,691 Bytes
aef7d7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5107c4c
aef7d7f
 
 
 
f0542d9
aef7d7f
 
 
 
 
659a4c2
aef7d7f
 
ab90404
 
 
 
 
 
 
 
 
 
641c00f
 
 
ab90404
 
 
 
 
 
f795b75
ab90404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f65fde
ab90404
 
 
 
aef7d7f
 
5107c4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_set_up():
    import torch
    TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
    CUDA_VERSION = torch.__version__.split("+")[-1]
    print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION)
    print(f'GPU available: {torch.cuda.is_available()}')
    print(torch.cuda.get_device_capability())

    # print("detectron2:", detectron2.__version__)

def load_model():
    # def predictor(img):
    #     return {}
    # return predictor
    # import some common detectron2 utilities
    import torch
    from detectron2 import model_zoo
    from detectron2.engine import DefaultPredictor
    from detectron2.config import get_cfg
    from detectron2.data.datasets import register_coco_instances

    import os
    import numpy as np

    ## define relevant parameters
    cfg = get_cfg()
    cfg.merge_from_file("./configs/test_model_config.yaml")
    if not torch.cuda.is_available():
        cfg.MODEL.DEVICE = "cpu"
    else:
        cfg.MODEL.DEVICE = 'cuda'
    predictor = DefaultPredictor(cfg)

    return predictor

def mask_nms(masks, scores, nms_threshold=0.5):
    import supervision as sv
    
    order = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)
    keep = []
    while order:
        i = order.pop(0)
        keep.append(i)
        for j in order:

            intersection = masks[i] * masks[j]
            union = masks[i] + masks[j]
            iou = intersection.sum() / union.sum()

            # Remove masks with IoU greater than the threshold
            if iou > nms_threshold:
                order.remove(j)
    return keep

def apply_nms(prediction, mask=False, cls_agnostic_nms=0.8):
    from torchvision.ops import nms
    from detectron2.structures import Instances
    
    if mask:
        # print("Applying mask NMS")
        nms_indices = mask_nms(prediction["instances"].pred_masks.numpy(),
                               prediction["instances"]._fields["scores"], cls_agnostic_nms)
    else:
        # print("Applying box NMS")
        nms_indices = nms(prediction["instances"].pred_boxes.tensor,
                          prediction["instances"].scores, cls_agnostic_nms)

    pred = {"instances": Instances(image_size=prediction["instances"].image_size,
                                   pred_boxes=prediction["instances"].pred_boxes[nms_indices],
                                   scores=prediction["instances"].scores[nms_indices],
                                   pred_classes=prediction["instances"].pred_classes[nms_indices],
                                   pred_masks=prediction["instances"].pred_masks[nms_indices])}

    return pred

if __name__ == '__main__':
    # get_set_up()
    load_model()