A wrapper around monodepth network to extract features.
| 79 | |
| 80 | |
| 81 | class MonodepthFeatureEncoder(BaseEncoder): |
| 82 | """A wrapper around monodepth network to extract features.""" |
| 83 | |
| 84 | def __init__( |
| 85 | self, |
| 86 | monodepth_encoder: SlidingPyramidNetwork, |
| 87 | output_dims: list[int] | None = None, |
| 88 | freeze_projection: bool = False, |
| 89 | ) -> None: |
| 90 | """Initialize MonodepthFeatureExtractor.""" |
| 91 | super().__init__() |
| 92 | |
| 93 | self.encoder = monodepth_encoder |
| 94 | |
| 95 | # The monodepth network returns two feature maps for the first entry in |
| 96 | # backbone.encoder.dims_encoder. |
| 97 | monodepth_dims = self.encoder.dims_encoder |
| 98 | monodepth_dims = monodepth_dims |
| 99 | |
| 100 | if output_dims is not None: |
| 101 | if not len(output_dims) == len(monodepth_dims): |
| 102 | raise ValueError( |
| 103 | "When set, number of output dimensions must be equal to output " |
| 104 | f"dimensions of monodepth model {len(monodepth_dims)}." |
| 105 | ) |
| 106 | |
| 107 | self.projection = ProjectionModule(monodepth_dims, output_dims) |
| 108 | self.output_dims = output_dims |
| 109 | else: |
| 110 | self.projection = nn.Identity() |
| 111 | self.output_dims = monodepth_dims |
| 112 | |
| 113 | if freeze_projection: |
| 114 | self.projection.requires_grad_(False) |
| 115 | |
| 116 | def forward(self, input_features: torch.Tensor) -> list[torch.Tensor]: |
| 117 | """Extract multi-resolution features.""" |
| 118 | encodings = self.encoder(input_features[:, :3].contiguous()) |
| 119 | return self.projection(encodings) |
| 120 | |
| 121 | def internal_resolution(self) -> int: |
| 122 | """Internal resolution of the encoder.""" |
| 123 | return self.encoder.internal_resolution() |
nothing calls this directly
no outgoing calls
no test coverage detected