Args: input_tensor: tensor containing initial class logits. reference_tensor: the reference tensor used to guide the message passing. Returns: output (torch.Tensor): output tensor.
(self, input_tensor: torch.Tensor, reference_tensor: torch.Tensor)
| 69 | self.compatibility_matrix = compatibility_matrix |
| 70 | |
| 71 | def forward(self, input_tensor: torch.Tensor, reference_tensor: torch.Tensor): |
| 72 | """ |
| 73 | Args: |
| 74 | input_tensor: tensor containing initial class logits. |
| 75 | reference_tensor: the reference tensor used to guide the message passing. |
| 76 | |
| 77 | Returns: |
| 78 | output (torch.Tensor): output tensor. |
| 79 | """ |
| 80 | |
| 81 | # constructing spatial feature tensor |
| 82 | spatial_features = _create_coordinate_tensor(reference_tensor) |
| 83 | |
| 84 | # constructing final feature tensors for bilateral and gaussian kernel |
| 85 | bilateral_features = torch.cat( |
| 86 | [spatial_features / self.bilateral_spatial_sigma, reference_tensor / self.bilateral_color_sigma], dim=1 |
| 87 | ) |
| 88 | gaussian_features = spatial_features / self.gaussian_spatial_sigma |
| 89 | |
| 90 | # setting up output tensor |
| 91 | output_tensor = softmax(input_tensor, dim=1) |
| 92 | |
| 93 | # mean field loop |
| 94 | for _ in range(self.iterations): |
| 95 | # message passing step for both kernels |
| 96 | bilateral_output = PHLFilter.apply(output_tensor, bilateral_features) |
| 97 | gaussian_output = PHLFilter.apply(output_tensor, gaussian_features) |
| 98 | |
| 99 | # combining filter outputs |
| 100 | combined_output = self.bilateral_weight * bilateral_output + self.gaussian_weight * gaussian_output |
| 101 | |
| 102 | # optionally running a compatibility transform |
| 103 | if self.compatibility_matrix is not None: |
| 104 | flat = combined_output.flatten(start_dim=2).permute(0, 2, 1) |
| 105 | flat = torch.matmul(flat, self.compatibility_matrix) |
| 106 | combined_output = flat.permute(0, 2, 1).reshape(combined_output.shape) |
| 107 | |
| 108 | # update and normalize |
| 109 | output_tensor = softmax(input_tensor + self.update_factor * combined_output, dim=1) |
| 110 | |
| 111 | return output_tensor |
| 112 | |
| 113 | |
| 114 | # helper methods |
nothing calls this directly
no test coverage detected