Multi-scale attention segmentation model base class
| 39 | |
| 40 | |
| 41 | class MscaleBase(nn.Module): |
| 42 | """ |
| 43 | Multi-scale attention segmentation model base class |
| 44 | """ |
| 45 | def __init__(self): |
| 46 | super(MscaleBase, self).__init__() |
| 47 | self.criterion = None |
| 48 | self.fuse_aspp = False |
| 49 | |
| 50 | def _fwd(self, x, aspp_in=None): |
| 51 | pass |
| 52 | |
| 53 | def recurse_fuse_fwd(self, x, scales, aspp_lo=None, attn_lo=None): |
| 54 | """ |
| 55 | recursive eval for n-scales |
| 56 | |
| 57 | target resolution is fixed at 1.0 |
| 58 | |
| 59 | [0.5, 1.0]: |
| 60 | p_0.5, aspp_0.5, attn_0.5 = fwd(attn,aspp=None) |
| 61 | p_1.0 = recurse([1.0], aspp_0.5, attn_0.5) |
| 62 | p_1.0 = fwd(attn_0.5, aspp_0.5) |
| 63 | output = attn_0.5 * p_0.5 + (1 - attn_0.5) * p_1.0 |
| 64 | """ |
| 65 | this_scale = scales.pop() |
| 66 | if this_scale == 1.0: |
| 67 | x_resize = x |
| 68 | else: |
| 69 | x_resize = ResizeX(x, this_scale) |
| 70 | p, attn, aspp = self._fwd(x_resize, attn_lo=attn_lo, aspp_lo=aspp_lo) |
| 71 | |
| 72 | if this_scale == 1.0: |
| 73 | p_1x = p |
| 74 | attn_1x = attn |
| 75 | else: |
| 76 | p_1x = scale_as(p, x) |
| 77 | attn_1x = scale_as(attn, x) |
| 78 | |
| 79 | if len(scales) == 0: |
| 80 | output = p_1x |
| 81 | else: |
| 82 | output = attn_1x * p_1x |
| 83 | p_next, _ = self.recurse_fuse_fwd(x, scales, |
| 84 | attn_lo=attn, aspp_lo=aspp) |
| 85 | output += (1 - attn_1x) * p_next |
| 86 | return output, attn_1x |
| 87 | |
| 88 | def nscale_fused_forward(self, inputs, scales): |
| 89 | """ |
| 90 | multi-scale evaluation for model with fused_aspp feature |
| 91 | |
| 92 | Evaluation must happen in two directions: from low to high to feed |
| 93 | aspp features forward, then back down high to low to apply attention |
| 94 | such that the lower scale gets higher priority |
| 95 | """ |
| 96 | x_1x = inputs['images'] |
| 97 | assert 1.0 in scales, 'expected 1.0 to be the target scale' |
| 98 |
nothing calls this directly
no outgoing calls
no test coverage detected