-
Notifications
You must be signed in to change notification settings - Fork 8
/
nn.py
67 lines (55 loc) · 2.42 KB
/
nn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
"""
Copyright 2017 the shiftnet_cuda_v2 authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import shiftnet_cuda
import torch
from torch.autograd import Function, Variable
class ShiftFn(Function):
@staticmethod
def forward(ctx, src):
dst = torch.cuda.FloatTensor(src.size())
ret = shiftnet_cuda.moduloshift3x3_nchw(src, dst)
assert ret == 1
return dst
@staticmethod
def backward(ctx, grad_dst):
grad_src = torch.cuda.FloatTensor(grad_dst.data.size())
ret = shiftnet_cuda.moduloshift3x3bwd_nchw(grad_dst.data, grad_src)
assert ret == 1
return Variable(grad_src, requires_grad=grad_dst.requires_grad)
class GenericShiftFn(Function):
@staticmethod
def forward(ctx, src, kernel_size, dilate_factor):
ctx.kernel_size = kernel_size
ctx.dilate_factor = dilate_factor
dst = torch.cuda.FloatTensor(src.size())
ret = shiftnet_cuda.moduloshiftgeneric_nchw(src, dst, kernel_size, dilate_factor, 1)
assert ret == 1, "GenericShiftFn: forward: invalid args, your kernel or dilation are probably too large"
return dst
@staticmethod
def backward(ctx, grad_dst):
grad_src = torch.cuda.FloatTensor(grad_dst.data.size())
ret = shiftnet_cuda.moduloshiftgeneric_nchw(grad_dst.data, grad_src, ctx.kernel_size, ctx.dilate_factor, -1)
assert ret == 1, "GenericShiftFn: backward: invalid args, your kernel or dilation are probably too large"
return Variable(grad_src, requires_grad=grad_dst.requires_grad), None, None
class Shift3x3_cuda(torch.nn.Module):
def __init__(self):
super(Shift3x3_cuda, self).__init__()
def forward(self, x):
return ShiftFn.apply(x)
class GenericShift_cuda(torch.nn.Module):
def __init__(self, kernel_size, dilate_factor=1):
super(GenericShift_cuda, self).__init__()
self._kernel_size = kernel_size
self._dilate_factor = dilate_factor
def forward(self, x):
return GenericShiftFn.apply(x, self._kernel_size, self._dilate_factor)