MCPcopy Create free account
hub / github.com/apple/ml-sharp / MonodepthWithEncodingAdaptor

Class MonodepthWithEncodingAdaptor

src/sharp/models/monodepth.py:167–251  ·  view source on GitHub ↗

Monodepth model with feature maps.

Source from the content-addressed store, hash-verified

165
166
167class MonodepthWithEncodingAdaptor(nn.Module):
168 """Monodepth model with feature maps."""
169
170 def __init__(
171 self,
172 monodepth_predictor: MonodepthDensePredictionTransformer,
173 return_encoder_features: bool,
174 return_decoder_features: bool,
175 num_monodepth_layers: int,
176 sorting_monodepth: bool,
177 ):
178 """Initialize MonodepthWithEncodingAdaptor.
179
180 Args:
181 monodepth_predictor: The monodepth model.
182 return_encoder_features: Whether to return encoder features from monodepth model.
183 return_decoder_features: Whether to return decoder features from monodepth model.
184 num_monodepth_layers: How many layers the monodepth model predicts.
185 sorting_monodepth: Whether to sort the monodepth output (for two layer monodepth).
186 """
187 super().__init__()
188 self.monodepth_predictor = monodepth_predictor
189 self.return_encoder_features = return_encoder_features
190 self.return_decoder_features = return_decoder_features
191 self.num_monodepth_layers = num_monodepth_layers
192 self.sorting_monodepth = sorting_monodepth
193
194 def forward(self, image: torch.Tensor) -> MonodepthOutput:
195 """Process image and return disparity and feature maps."""
196 inputs = self.monodepth_predictor.normalizer(image)
197 encoder_output = self.monodepth_predictor.encoder(inputs)
198
199 num_encoder_features = len(self.monodepth_predictor.encoder.dims_encoder)
200
201 # NOTE: whether intermediate features are empty have already been decided
202 # in monodepth_predictor during create_monodepth_dpt.
203 encoder_features = encoder_output[:num_encoder_features]
204 intermediate_features = encoder_output[num_encoder_features:]
205 decoder_features = self.monodepth_predictor.decoder(encoder_features)
206 disparity = self.monodepth_predictor.head(decoder_features)
207
208 # We cannot use disparity.shape[1], otherwise the tracer will fail.
209 if self.num_monodepth_layers == 2 and self.sorting_monodepth:
210 first_layer_disparity = disparity.max(dim=1, keepdims=True).values
211 second_layer_disparity = disparity.min(dim=1, keepdims=True).values
212 disparity = torch.cat([first_layer_disparity, second_layer_disparity], dim=1)
213
214 output_features = []
215 if self.return_encoder_features:
216 output_features.extend(encoder_features)
217
218 if self.return_decoder_features:
219 output_features.append(decoder_features)
220
221 return MonodepthOutput(
222 disparity=disparity,
223 encoder_features=encoder_features,
224 decoder_features=decoder_features,

Callers 1

create_monodepth_adaptorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected