|
| 1 | +""" |
| 2 | +BSD 3-Clause License |
| 3 | +
|
| 4 | +Copyright (c) Soumith Chintala 2016, |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without |
| 8 | +modification, are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | +* Redistributions of source code must retain the above copyright notice, this |
| 11 | + list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer in the documentation |
| 15 | + and/or other materials provided with the distribution. |
| 16 | +
|
| 17 | +* Neither the name of the copyright holder nor the names of its |
| 18 | + contributors may be used to endorse or promote products derived from |
| 19 | + this software without specific prior written permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 25 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +""" |
| 32 | + |
| 33 | +import torch |
| 34 | +from torch import nn |
| 35 | +from torch.nn import functional as F |
| 36 | + |
| 37 | +__all__ = ["DeepLabV3Decoder"] |
| 38 | + |
| 39 | + |
| 40 | +class DeepLabV3Decoder(nn.Sequential): |
| 41 | + def __init__(self, in_channels, out_channels=256, atrous_rates=(12, 24, 36)): |
| 42 | + super().__init__( |
| 43 | + ASPP(in_channels, out_channels, atrous_rates), |
| 44 | + nn.Conv2d(out_channels, out_channels, 3, padding=1, bias=False), |
| 45 | + nn.BatchNorm2d(out_channels), |
| 46 | + nn.ReLU(), |
| 47 | + ) |
| 48 | + self.out_channels = out_channels |
| 49 | + |
| 50 | + def forward(self, *features): |
| 51 | + return super().forward(features[-1]) |
| 52 | + |
| 53 | + |
| 54 | +class ASPPConv(nn.Sequential): |
| 55 | + def __init__(self, in_channels, out_channels, dilation): |
| 56 | + modules = [ |
| 57 | + nn.Conv2d(in_channels, out_channels, 3, padding=dilation, dilation=dilation, bias=False), |
| 58 | + nn.BatchNorm2d(out_channels), |
| 59 | + nn.ReLU() |
| 60 | + ] |
| 61 | + super(ASPPConv, self).__init__(*modules) |
| 62 | + |
| 63 | + |
| 64 | +class ASPPPooling(nn.Sequential): |
| 65 | + def __init__(self, in_channels, out_channels): |
| 66 | + super(ASPPPooling, self).__init__( |
| 67 | + nn.AdaptiveAvgPool2d(1), |
| 68 | + nn.Conv2d(in_channels, out_channels, 1, bias=False), |
| 69 | + nn.BatchNorm2d(out_channels), |
| 70 | + nn.ReLU()) |
| 71 | + |
| 72 | + def forward(self, x): |
| 73 | + size = x.shape[-2:] |
| 74 | + for mod in self: |
| 75 | + x = mod(x) |
| 76 | + return F.interpolate(x, size=size, mode='bilinear', align_corners=False) |
| 77 | + |
| 78 | + |
| 79 | +class ASPP(nn.Module): |
| 80 | + def __init__(self, in_channels, out_channels, atrous_rates): |
| 81 | + super(ASPP, self).__init__() |
| 82 | + modules = [] |
| 83 | + modules.append(nn.Sequential( |
| 84 | + nn.Conv2d(in_channels, out_channels, 1, bias=False), |
| 85 | + nn.BatchNorm2d(out_channels), |
| 86 | + nn.ReLU())) |
| 87 | + |
| 88 | + rate1, rate2, rate3 = tuple(atrous_rates) |
| 89 | + modules.append(ASPPConv(in_channels, out_channels, rate1)) |
| 90 | + modules.append(ASPPConv(in_channels, out_channels, rate2)) |
| 91 | + modules.append(ASPPConv(in_channels, out_channels, rate3)) |
| 92 | + modules.append(ASPPPooling(in_channels, out_channels)) |
| 93 | + |
| 94 | + self.convs = nn.ModuleList(modules) |
| 95 | + |
| 96 | + self.project = nn.Sequential( |
| 97 | + nn.Conv2d(5 * out_channels, out_channels, 1, bias=False), |
| 98 | + nn.BatchNorm2d(out_channels), |
| 99 | + nn.ReLU(), |
| 100 | + nn.Dropout(0.5)) |
| 101 | + |
| 102 | + def forward(self, x): |
| 103 | + res = [] |
| 104 | + for conv in self.convs: |
| 105 | + res.append(conv(x)) |
| 106 | + res = torch.cat(res, dim=1) |
| 107 | + return self.project(res) |
0 commit comments