Skip to content

unitorch.cli.models.detr¤

DetrProcessor¤

Tip

core/process/detr is the section for configuration of DetrProcessor.

Bases: DetrProcessor

Source code in src/unitorch/cli/models/detr/processing.py
24
25
26
27
28
29
30
def __init__(
    self,
    vision_config_path: str,
):
    super().__init__(
        vision_config_path=vision_config_path,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/detr/processing.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@classmethod
@config_defaults_init("core/process/detr")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/process/detr")
    pretrained_name = config.getoption("pretrained_name", "detr-resnet-50")
    vision_config_path = config.getoption("vision_config_path", None)
    vision_config_path = pop_value(
        vision_config_path,
        nested_dict_value(pretrained_detr_infos, pretrained_name, "vision_config"),
    )

    vision_config_path = cached_path(vision_config_path)

    return {
        "vision_config_path": vision_config_path,
    }

_image ¤

_image(image: Union[Image, str])
Source code in src/unitorch/cli/models/detr/processing.py
49
50
51
52
53
54
55
56
57
58
59
@register_process("core/process/detr/image")
def _image(
    self,
    image: Union[Image.Image, str],
):
    outputs = super().image(
        image=image,
    )
    return TensorSeqInputs(
        images=outputs.image,
    )

_detection ¤

_detection(
    image: Union[Image, str],
    bboxes: List[List[float]],
    classes: List[int],
    do_eval: Optional[bool] = False,
)
Source code in src/unitorch/cli/models/detr/processing.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@register_process("core/process/detr/detection")
def _detection(
    self,
    image: Union[Image.Image, str],
    bboxes: List[List[float]],
    classes: List[int],
    do_eval: Optional[bool] = False,
):
    outputs = super().detection(
        image=image,
        bboxes=bboxes,
        classes=classes,
    )
    if do_eval:
        new_h, new_w = outputs.image.size()[1:]
        bboxes = outputs.bboxes
        bboxes[:, 0] = bboxes[:, 0] * new_w
        bboxes[:, 1] = bboxes[:, 1] * new_h
        bboxes[:, 2] = bboxes[:, 2] * new_w
        bboxes[:, 3] = bboxes[:, 3] * new_h
        return TensorSeqInputs(images=outputs.image), DetectionTargets(
            bboxes=bboxes,
            classes=outputs.classes,
        )

    return TensorSeqInputs(
        images=outputs.image,
        bboxes=outputs.bboxes,
        classes=outputs.classes,
    )

_post_dectection ¤

_post_dectection(outputs: DetectionOutputs)
Source code in src/unitorch/cli/models/detr/processing.py
92
93
94
95
96
97
98
@register_process("core/postprocess/detr/detection")
def _post_dectection(self, outputs: DetectionOutputs):
    results = outputs.to_pandas()
    results["bboxes"] = [bboxes.tolist() for bboxes in outputs.bboxes]
    results["scores"] = [scores.tolist() for scores in outputs.scores]
    results["classes"] = [classes.tolist() for classes in outputs.classes]
    return WriterOutputs(results)

DetrForDetection¤

Tip

core/model/detection/detr is the section for configuration of DetrForDetection.

Bases: DetrForDetection

Source code in src/unitorch/cli/models/detr/modeling.py
23
24
25
26
27
28
29
30
31
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = None,
):
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/detr/modeling.py
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
@classmethod
@config_defaults_init("core/model/detection/detr")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/detection/detr")
    pretrained_name = config.getoption("pretrained_name", "detr-resnet-50")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_detr_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)
    num_classes = config.getoption("num_classes", None)

    inst = cls(
        config_path=config_path,
        num_classes=num_classes,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_detr_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(images, bboxes, classes)
Source code in src/unitorch/cli/models/detr/modeling.py
62
63
64
65
66
67
68
def forward(self, images, bboxes, classes):
    outputs = super().forward(
        images=images,
        bboxes=bboxes,
        classes=classes,
    )
    return LossOutputs(loss=outputs)

detect ¤

detect(images, norm_bboxes: bool = False)
Source code in src/unitorch/cli/models/detr/modeling.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@config_defaults_method("core/model/detection/detr")
def detect(
    self,
    images,
    norm_bboxes: bool = False,
):
    outputs = super().detect(
        images=images,
        norm_bboxes=norm_bboxes,
    )
    return DetectionOutputs(
        bboxes=outputs.bboxes,
        scores=outputs.scores,
        classes=outputs.classes,
    )