customize.py 6.26 KB
Newer Older
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
1
2
3
4
5
6
7
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang
## ECE Department, Rutgers University
## Email: zhang.hang@rutgers.edu
## Copyright (c) 2017
##
## This source code is licensed under the MIT-style license found in the
Hang Zhang's avatar
sync BN  
Hang Zhang committed
8
## LICENSE file in the root directory of this source tree
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
9
10
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Hang Zhang's avatar
sync BN  
Hang Zhang committed
11
"""Encoding Custermized NN Module"""
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
12
import torch
Zhang's avatar
Zhang committed
13
14
from torch.nn import Module, Sequential, Conv2d, ReLU, AdaptiveAvgPool2d, \
    NLLLoss, BCELoss, CrossEntropyLoss
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
15
16
17
18
from torch.nn import functional as F

from .syncbn import BatchNorm2d

Zhang's avatar
Zhang committed
19
20
__all__ = ['GramMatrix', 'SegmentationLosses', 'View', 'Sum', 'Mean',
           'Normalize', 'PyramidPooling']
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
21
22


Hang Zhang's avatar
path  
Hang Zhang committed
23
24
class GramMatrix(Module):
    r""" Gram Matrix for a 4D convolutional featuremaps as a mini-batch
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
25
26

    .. math::
Hang Zhang's avatar
path  
Hang Zhang committed
27
        \mathcal{G} = \sum_{h=1}^{H_i}\sum_{w=1}^{W_i} \mathcal{F}_{h,w}\mathcal{F}_{h,w}^T
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
28
    """
Hang Zhang's avatar
path  
Hang Zhang committed
29
30
31
32
33
34
    def forward(self, y):
        (b, ch, h, w) = y.size()
        features = y.view(b, ch, w * h)
        features_t = features.transpose(1, 2)
        gram = features.bmm(features_t) / (ch * h * w)
        return gram
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
35

Zhang's avatar
Zhang committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def softmax_crossentropy(input, target, weight, size_average, ignore_index, reduce=True):
    return F.nll_loss(F.log_softmax(input, 1), target, weight,
                      size_average, ignore_index, reduce)

class SegmentationLosses(CrossEntropyLoss):
    """2D Cross Entropy Loss with Auxilary Loss"""
    def __init__(self, aux, aux_weight=0.2, weight=None, size_average=True, ignore_index=-1):
        super(SegmentationLosses, self).__init__(weight, size_average, ignore_index)
        self.aux = aux
        self.aux_weight = aux_weight

    def forward(self, *inputs):
        if not self.aux:
            return super(SegmentationLosses, self).forward(*inputs)
        pred1, pred2, target = tuple(inputs)
        loss1 = super(SegmentationLosses, self).forward(pred1, target)
        loss2 = super(SegmentationLosses, self).forward(pred2, target)
        return loss1 + self.aux_weight * loss2

"""
class SegmentationLosses(Module):
    def __init__(self, aux, aux_weight=0.2, weight=None, size_average=True, ignore_index=-1):
        super(SegmentationLosses, self).__init__()
        self.aux = aux
        self.aux_weight = aux_weight
        # Somehow the size averge is not handled correctly on multi-gpu, so we average by ourself.
        self.nll_loss = NLLLoss(weight, ignore_index=ignore_index, reduce=True)

    def _forward_each(self, inputs, targets):
        return self.nll_loss(F.log_softmax(inputs, dim=1), targets)

    def forward(self, *inputs):
        if not self.aux:
            return self._forward_each(*inputs)
        pred1, pred2, target = tuple(inputs)
        loss1 = self._forward_each(pred1, target)
        loss2 = self._forward_each(pred2, target)
        return loss1 + self.aux_weight * loss2
"""

Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
76
77
78
79
80
81
82
83
84
85
86
87
88

class View(Module):
    """Reshape the input into different size, an inplace operator, support
    SelfParallel mode.
    """
    def __init__(self, *args):
        super(View, self).__init__()
        if len(args) == 1 and isinstance(args[0], torch.Size):
            self.size = args[0]
        else:
            self.size = torch.Size(args)

    def forward(self, input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
89
        return input.view(self.size)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
90
91


Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
92
93
94
95
96
97
98
class Sum(Module):
    def __init__(self, dim, keep_dim=False):
        super(Sum, self).__init__()
        self.dim = dim
        self.keep_dim = keep_dim

    def forward(self, input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
99
        return input.sum(self.dim, self.keep_dim)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
100
101
102
103
104
105
106
107
108


class Mean(Module):
    def __init__(self, dim, keep_dim=False):
        super(Mean, self).__init__()
        self.dim = dim
        self.keep_dim = keep_dim

    def forward(self, input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
109
        return input.mean(self.dim, self.keep_dim)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
110
111


Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Normalize(Module):
    r"""Performs :math:`L_p` normalization of inputs over specified dimension.

    Does:

    .. math::
        v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}

    for each subtensor v over dimension dim of input. Each subtensor is
    flattened into a vector, i.e. :math:`\lVert v \rVert_p` is not a matrix
    norm.

    With default arguments normalizes over the second dimension with Euclidean
    norm.

    Args:
        p (float): the exponent value in the norm formulation. Default: 2
        dim (int): the dimension to reduce. Default: 1
    """
    def __init__(self, p=2, dim=1):
        super(Normalize, self).__init__()
        self.p = p
Hang Zhang's avatar
sync BN  
Hang Zhang committed
134
        self.dim = dim
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
135
136

    def forward(self, x):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
137
        return F.normalize(x, self.p, self.dim, eps=1e-10)
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
138
139


Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
140
141
class PyramidPooling(Module):
    """
Hang Zhang's avatar
sync BN  
Hang Zhang committed
142
    Reference:
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        Zhao, Hengshuang, et al. *"Pyramid scene parsing network."*
    """
    def __init__(self, in_channels):
        super(PyramidPooling, self).__init__()
        self.pool1 = AdaptiveAvgPool2d(1)
        self.pool2 = AdaptiveAvgPool2d(2)
        self.pool3 = AdaptiveAvgPool2d(3)
        self.pool4 = AdaptiveAvgPool2d(6)

        out_channels = int(in_channels/4)
        self.conv1 = Sequential(Conv2d(in_channels, out_channels, 1),
                                BatchNorm2d(out_channels),
                                ReLU(True))
        self.conv2 = Sequential(Conv2d(in_channels, out_channels, 1),
                                BatchNorm2d(out_channels),
                                ReLU(True))
        self.conv3 = Sequential(Conv2d(in_channels, out_channels, 1),
                                BatchNorm2d(out_channels),
                                ReLU(True))
        self.conv4 = Sequential(Conv2d(in_channels, out_channels, 1),
                                BatchNorm2d(out_channels),
                                ReLU(True))

    def _cat_each(self, x, feat1, feat2, feat3, feat4):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
167
        assert(len(x) == len(feat1))
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
168
169
        z = []
        for i in range(len(x)):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
170
            z.append(torch.cat((x[i], feat1[i], feat2[i], feat3[i], feat4[i]), 1))
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
171
172
173
        return z

    def forward(self, x):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
174
175
176
177
178
179
        _, _, h, w = x.size()
        feat1 = F.upsample(self.conv1(self.pool1(x)), (h, w), mode='bilinear')
        feat2 = F.upsample(self.conv2(self.pool2(x)), (h, w), mode='bilinear')
        feat3 = F.upsample(self.conv3(self.pool3(x)), (h, w), mode='bilinear')
        feat4 = F.upsample(self.conv4(self.pool4(x)), (h, w), mode='bilinear')
        return torch.cat((x, feat1, feat2, feat3, feat4), 1)