| 135 | |
| 136 | |
| 137 | class MiDaSInference(nn.Module): |
| 138 | MODEL_TYPES_TORCH_HUB = [ |
| 139 | "DPT_Large", |
| 140 | "DPT_Hybrid", |
| 141 | "MiDaS_small" |
| 142 | ] |
| 143 | MODEL_TYPES_ISL = [ |
| 144 | "dpt_large", |
| 145 | "dpt_hybrid", |
| 146 | "midas_v21", |
| 147 | "midas_v21_small", |
| 148 | ] |
| 149 | |
| 150 | def __init__(self, model_type): |
| 151 | super().__init__() |
| 152 | assert (model_type in self.MODEL_TYPES_ISL) |
| 153 | model, _ = load_model(model_type) |
| 154 | self.model = model |
| 155 | self.model.train = disabled_train |
| 156 | |
| 157 | def forward(self, x): |
| 158 | # x in 0..1 as produced by calling self.transform on a 0..1 float64 numpy array |
| 159 | # NOTE: we expect that the correct transform has been called during dataloading. |
| 160 | with torch.no_grad(): |
| 161 | prediction = self.model(x) |
| 162 | prediction = torch.nn.functional.interpolate( |
| 163 | prediction.unsqueeze(1), |
| 164 | size=x.shape[2:], |
| 165 | mode="bicubic", |
| 166 | align_corners=False, |
| 167 | ) |
| 168 | assert prediction.shape == (x.shape[0], 1, x.shape[2], x.shape[3]) |
| 169 | return prediction |
| 170 |
nothing calls this directly
no outgoing calls
no test coverage detected