| 152 | return out |
| 153 | |
| 154 | class SparseInsDecoder(nn.Module): |
| 155 | def __init__(self, cfg, **kargs) -> None: |
| 156 | super().__init__() |
| 157 | in_channels = cfg.encoder.out_dims + 2 |
| 158 | self.output_iam = cfg.decoder.output_iam |
| 159 | self.scale_factor = cfg.decoder.scale_factor |
| 160 | self.sparse_decoder_weight = cfg.sparse_decoder_weight |
| 161 | self.inst_branch = InstanceBranch(cfg.decoder, in_channels) |
| 162 | # dim, num_convs, kernel_dim, in_channels |
| 163 | self.mask_branch = MaskBranch(cfg.decoder, in_channels) |
| 164 | self.sparse_inst_crit = SparseInstCriterion( |
| 165 | num_classes=cfg.decoder.num_classes, |
| 166 | matcher=SparseInstMatcher(), |
| 167 | cfg=cfg) |
| 168 | self._init_weights() |
| 169 | |
| 170 | def _init_weights(self): |
| 171 | self.inst_branch._init_weights() |
| 172 | self.mask_branch._init_weights() |
| 173 | |
| 174 | @torch.no_grad() |
| 175 | def compute_coordinates(self, x): |
| 176 | h, w = x.size(2), x.size(3) |
| 177 | y_loc = -1.0 + 2.0 * torch.arange(h, device=x.device) / (h - 1) |
| 178 | x_loc = -1.0 + 2.0 * torch.arange(w, device=x.device) / (w - 1) |
| 179 | y_loc, x_loc = torch.meshgrid(y_loc, x_loc) |
| 180 | y_loc = y_loc.expand([x.shape[0], 1, -1, -1]) |
| 181 | x_loc = x_loc.expand([x.shape[0], 1, -1, -1]) |
| 182 | locations = torch.cat([x_loc, y_loc], 1) |
| 183 | return locations.to(x) |
| 184 | |
| 185 | def forward(self, features, is_training=True, **kwargs): |
| 186 | output = {} |
| 187 | coord_features = self.compute_coordinates(features) |
| 188 | features = torch.cat([coord_features, features], dim=1) |
| 189 | inst_output = self.inst_branch( |
| 190 | features, is_training=is_training) |
| 191 | output.update(inst_output) |
| 192 | |
| 193 | if is_training: |
| 194 | mask_features = self.mask_branch(features) |
| 195 | pred_kernel = inst_output['pred_kernel'] |
| 196 | N = pred_kernel.shape[1] |
| 197 | B, C, H, W = mask_features.shape |
| 198 | |
| 199 | pred_masks = torch.bmm(pred_kernel, mask_features.view( |
| 200 | B, C, H * W)).view(B, N, H, W) |
| 201 | pred_masks = F.interpolate( |
| 202 | pred_masks, scale_factor=self.scale_factor, |
| 203 | mode='bilinear', align_corners=False) |
| 204 | output.update(dict( |
| 205 | pred_masks=pred_masks)) |
| 206 | |
| 207 | if self.training: |
| 208 | sparse_inst_losses, matched_indices = self.loss( |
| 209 | output, |
| 210 | lane_idx_map=kwargs.get('lane_idx_map'), |
| 211 | input_shape=kwargs.get('input_shape') |