Skip to content

unitorch.cli.models.dpt¤

DPTProcessor¤

Tip

core/process/dpt is the section for configuration of DPTProcessor.

Bases: DPTProcessor

DPT processor for depth estimation tasks.

Source code in src/unitorch/cli/models/dpt/processing.py
20
21
22
23
24
25
26
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/dpt/processing.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@classmethod
@config_defaults_init("core/process/dpt")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/process/dpt")
    pretrained_name = config.getoption("pretrained_name", "dpt-large")
    vision_config_path = config.getoption("vision_config_path", None)
    vision_config_path = pop_value(
        vision_config_path,
        nested_dict_value(pretrained_dpt_infos, pretrained_name, "vision_config"),
    )

    vision_config_path = cached_path(vision_config_path)

    return {
        "vision_config_path": vision_config_path,
    }

_inputs ¤

_inputs(image: Union[Image, str])
Source code in src/unitorch/cli/models/dpt/processing.py
45
46
47
48
49
50
51
52
53
@register_process("core/process/dpt/inputs")
def _inputs(
    self,
    image: Union[Image.Image, str],
):
    if isinstance(image, str):
        image = Image.open(image)
    outputs = super().classification(image=image)
    return TensorInputs(pixel_values=outputs.pixel_values)

DPTForDepthEstimation¤

Tip

core/model/dpt is the section for configuration of DPTForDepthEstimation.

Bases: DPTForDepthEstimation

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

from_config classmethod ¤

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

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

    return inst

forward ¤

forward()
Source code in src/unitorch/cli/models/dpt/modeling.py
58
59
60
61
def forward(
    self,
):
    raise NotImplementedError

segment ¤

segment(pixel_values: Tensor)
Source code in src/unitorch/cli/models/dpt/modeling.py
63
64
65
66
67
68
69
70
71
72
73
74
75
@config_defaults_method("core/model/dpt")
@torch.no_grad()
def segment(
    self,
    pixel_values: torch.Tensor,
):
    outputs = super().forward(
        pixel_values=pixel_values,
    )
    max_values = torch.amax(outputs.reshape(len(outputs), -1), dim=1)
    return SegmentationOutputs(
        masks=list([out / value for out, value in zip(outputs, max_values)]),
    )