Skip to content

unitorch.models.segformer¤

SegformerProcessor¤

Bases: HfImageClassificationProcessor

Initializes the SegformerProcessor.

Parameters:

Name Type Description Default
vision_config_path str

Path to the SegformerImageProcessor configuration file.

required
Source code in src/unitorch/models/segformer/processing.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def __init__(
    self,
    vision_config_path: str,
):
    """
    Initializes the SegformerProcessor.

    Args:
        vision_config_path (str): Path to the SegformerImageProcessor configuration file.
    """
    vision_processor = SegformerImageProcessor.from_json_file(vision_config_path)
    super().__init__(
        vision_processor=vision_processor,
    )

SegformerForSegmentation¤

Bases: GenericModel

Initializes the SegformerForSegmentation model.

Parameters:

Name Type Description Default
config_path str

Path to the Segformer configuration file.

required
Source code in src/unitorch/models/segformer/modeling.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(
    self,
    config_path: str,
):
    """
    Initializes the SegformerForSegmentation model.

    Args:
        config_path (str): Path to the Segformer configuration file.
    """
    super().__init__()
    config = SegformerConfig.from_json_file(config_path)
    self.segformer = SegformerModel(config)
    self.decode_head = SegformerDecodeHead(config)
    self.init_weights()

segformer instance-attribute ¤

segformer = SegformerModel(config)

decode_head instance-attribute ¤

decode_head = SegformerDecodeHead(config)

forward ¤

forward(pixel_values: Tensor)

Forward pass of the SegformerForSegmentation model.

Parameters:

Name Type Description Default
pixel_values Tensor

Input image pixel values.

required

Returns:

Name Type Description
GenericOutputs

Object containing upsampled segmentation logits.

Source code in src/unitorch/models/segformer/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
57
def forward(
    self,
    pixel_values: torch.Tensor,
):
    """
    Forward pass of the SegformerForSegmentation model.

    Args:
        pixel_values (torch.Tensor): Input image pixel values.

    Returns:
        GenericOutputs: Object containing upsampled segmentation logits.
    """
    outputs = self.segformer(
        pixel_values=pixel_values,
        output_hidden_states=True,
        return_dict=True,
    )
    logits = self.decode_head(outputs.hidden_states)
    upsampled_logits = nn.functional.interpolate(
        logits,
        scale_factor=4,
        mode="bilinear",
        align_corners=False,
    )
    return GenericOutputs(logits=upsampled_logits)