"src/array/cuda/spmat_op_impl_csr.hip" did not exist on "16e771c09c7488f08da59b0328d698eed53ea45e"
customize.py 4.23 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 Customized Functions"""
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
12
13
14
import math
import torch
from torch.autograd import Function, Variable
Hang Zhang's avatar
sync BN  
Hang Zhang committed
15
from torch.nn.modules.utils import _pair
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
16
17
18
19
20
21

from .._ext import encoding_lib

__all__ = ['dilatedavgpool2d']

class _dilatedavgpool2d(Function):
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
22
23
    @staticmethod
    def forward(ctx, input, kernel_size, stride, padding,
Hang Zhang's avatar
sync BN  
Hang Zhang committed
24
                dilation=1):
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
25
        ctx.kH, ctx.kW = _pair(kernel_size)
Hang Zhang's avatar
sync BN  
Hang Zhang committed
26
        ctx.dH, ctx.dW = _pair(stride if stride is not None else kernel_size)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
27
28
        ctx.padH, ctx.padW = _pair(padding)
        ctx.dilationH, ctx.dilationW = _pair(dilation)
Hang Zhang's avatar
sync BN  
Hang Zhang committed
29
30
        b, c, h, w = input.size()
        if ctx.dH == 1 and ctx.dW == 1:
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
31
32
33
            # keep the size for dilated avgpool
            ow, oh = w, h
        else:
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
34
35
36
            ow = math.floor(float(w-ctx.kW+2*ctx.padW)/float(ctx.dW)) +1
            oh = math.floor(float(h-ctx.kH+2*ctx.padH)/float(ctx.dH)) +1
        with torch.cuda.device_of(input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
37
            output = input.new(b, c, oh, ow)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
38
39
40
        ctx.save_for_backward(input)
        if isinstance(input, torch.cuda.FloatTensor):
            with torch.cuda.device_of(input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
41
42
43
                encoding_lib.Encoding_Float_DilatedAvgPool2d_Forward(
                    input, output, ctx.kH, ctx.kW, ctx.dH, ctx.dW, ctx.padH,
                    ctx.padW, ctx.dilationH, ctx.dilationW)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
44
45
        elif isinstance(input, torch.cuda.DoubleTensor):
            with torch.cuda.device_of(input):
Hang Zhang's avatar
sync BN  
Hang Zhang committed
46
47
48
                encoding_lib.Encoding_Double_DilatedAvgPool2d_Forward(
                    input, output, ctx.kH, ctx.kW, ctx.dH, ctx.dW, ctx.padH,
                    ctx.padW, ctx.dilationH, ctx.dilationW)
Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
49
50
        else:
            raise RuntimeError('Unimplemented data type!')
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
51
52
        return output

Hang Zhang's avatar
v0.1.0  
Hang Zhang committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    @staticmethod
    def backward(ctx, gradOutput):
        input, = ctx.saved_variables
        with torch.cuda.device_of(input):
            gradInput = Variable(input.data.new().resize_as_(input.data))
        if isinstance(input.data, torch.cuda.FloatTensor):
            with torch.cuda.device_of(input.data):
                encoding_lib.Encoding_Float_DilatedAvgPool2d_Backward(
                    gradInput.data, gradOutput.data,
                    ctx.kH, ctx.kW, ctx.dH, ctx.dW, ctx.padH, ctx.padW,
                    ctx.dilationH, ctx.dilationW)
        elif isinstance(input.data, torch.cuda.DoubleTensor):
            with torch.cuda.device_of(input.data):
                encoding_lib.Encoding_Double_DilatedAvgPool2d_Backward(
                    gradInput.data, gradOutput.data,
                    ctx.kH, ctx.kW, ctx.dH, ctx.dW, ctx.padH, ctx.padW,
                    ctx.dilationH, ctx.dilationW)
        else:
            raise RuntimeError('Unimplemented data type!')
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
72
73
74
        return gradInput, None, None, None, None


Hang Zhang's avatar
sync BN  
Hang Zhang committed
75
76
77
78
def dilatedavgpool2d(input, kernel_size, stride=None, padding=0,
                     dilation=1):
    """Dilated Average Pool 2d, for dilation of DenseNet.

Zhang's avatar
v0.2.0  
Zhang committed
79
80
    Reference:

Hang Zhang's avatar
sync BN  
Hang Zhang committed
81
82
        Hang Zhang, Kristin Dana, Jianping Shi, Zhongyue Zhang, Xiaogang Wang,
        Ambrish Tyagi, Amit Agrawal. “Context Encoding for Semantic Segmentation. CVPR 2018
Hang Zhang's avatar
v1.0.1  
Hang Zhang committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    Applies 2D average-pooling operation in kh x kw regions by step size
    dh x dw steps. The number of output features is equal to the number of
    input planes.

    See :class:`~encoding.nn.DilatedAvgPool2d` for details and output shape.

    Args:
        input: input tensor (minibatch x in_channels x iH x iW)
        kernel_size: size of the pooling region, a single number or a
          tuple (kh x kw)
        stride: stride of the pooling operation, a single number or a
          tuple (sh x sw). Default is equal to kernel size
        padding: implicit zero padding on the input, a single number or
          a tuple (padh x padw), Default: 0
        dilation: the dilation parameter similar to Conv2d
    """
Hang Zhang's avatar
sync BN  
Hang Zhang committed
100
    return _dilatedavgpool2d.apply(input, kernel_size, stride, padding, dilation)