Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
SOLOv2-pytorch
Commits
ef86c404
Commit
ef86c404
authored
Jan 15, 2019
by
yhcao6
Browse files
add cpp dcn extension
parent
a8ec6fb3
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
2510 additions
and
0 deletions
+2510
-0
mmdet/ops/dcn/functions/__init__.py
mmdet/ops/dcn/functions/__init__.py
+0
-0
mmdet/ops/dcn/functions/deform_conv.py
mmdet/ops/dcn/functions/deform_conv.py
+124
-0
mmdet/ops/dcn/functions/modulated_dcn_func.py
mmdet/ops/dcn/functions/modulated_dcn_func.py
+133
-0
mmdet/ops/dcn/modules/__init__.py
mmdet/ops/dcn/modules/__init__.py
+0
-0
mmdet/ops/dcn/modules/deform_conv.py
mmdet/ops/dcn/modules/deform_conv.py
+45
-0
mmdet/ops/dcn/modules/modulated_dcn.py
mmdet/ops/dcn/modules/modulated_dcn.py
+186
-0
mmdet/ops/dcn/setup.py
mmdet/ops/dcn/setup.py
+12
-0
mmdet/ops/dcn/setup_modulated.py
mmdet/ops/dcn/setup_modulated.py
+13
-0
mmdet/ops/dcn/src/deform_conv_cuda.cpp
mmdet/ops/dcn/src/deform_conv_cuda.cpp
+429
-0
mmdet/ops/dcn/src/deform_conv_cuda_kernel.cu
mmdet/ops/dcn/src/deform_conv_cuda_kernel.cu
+461
-0
mmdet/ops/dcn/src/deform_psroi_pooling_cuda.cu
mmdet/ops/dcn/src/deform_psroi_pooling_cuda.cu
+363
-0
mmdet/ops/dcn/src/modulated_dcn_cuda.cpp
mmdet/ops/dcn/src/modulated_dcn_cuda.cpp
+324
-0
mmdet/ops/dcn/src/modulated_deform_im2col_cuda.cu
mmdet/ops/dcn/src/modulated_deform_im2col_cuda.cu
+420
-0
No files found.
mmdet/ops/dcn/functions/__init__.py
0 → 100644
View file @
ef86c404
mmdet/ops/dcn/functions/deform_conv.py
0 → 100644
View file @
ef86c404
import
torch
from
torch.autograd
import
Function
from
torch.nn.modules.utils
import
_pair
from
..
import
deform_conv
def
deform_conv_function
(
input
,
offset
,
weight
,
stride
=
1
,
padding
=
0
,
dilation
=
1
,
deform_groups
=
1
,
im2col_step
=
64
):
if
input
is
not
None
and
input
.
dim
()
!=
4
:
raise
ValueError
(
"Expected 4D tensor as input, got {}D tensor instead."
.
format
(
input
.
dim
()))
f
=
DeformConvFunction
(
_pair
(
stride
),
_pair
(
padding
),
_pair
(
dilation
),
deform_groups
,
im2col_step
)
return
f
(
input
,
offset
,
weight
)
class
DeformConvFunction
(
Function
):
def
__init__
(
self
,
stride
,
padding
,
dilation
,
deformable_groups
=
1
,
im2col_step
=
64
):
super
(
DeformConvFunction
,
self
).
__init__
()
self
.
stride
=
stride
self
.
padding
=
padding
self
.
dilation
=
dilation
self
.
deformable_groups
=
deformable_groups
self
.
im2col_step
=
im2col_step
def
forward
(
self
,
input
,
offset
,
weight
):
self
.
save_for_backward
(
input
,
offset
,
weight
)
output
=
input
.
new
(
*
self
.
_output_size
(
input
,
weight
))
self
.
bufs_
=
[
input
.
new
(),
input
.
new
()]
# columns, ones
if
not
input
.
is_cuda
:
raise
NotImplementedError
else
:
if
isinstance
(
input
,
torch
.
autograd
.
Variable
):
if
not
isinstance
(
input
.
data
,
torch
.
cuda
.
FloatTensor
):
raise
NotImplementedError
else
:
if
not
isinstance
(
input
,
torch
.
cuda
.
FloatTensor
):
raise
NotImplementedError
cur_im2col_step
=
min
(
self
.
im2col_step
,
input
.
shape
[
0
])
assert
(
input
.
shape
[
0
]
%
cur_im2col_step
)
==
0
,
'im2col step must divide batchsize'
deform_conv
.
deform_conv_forward_cuda
(
input
,
weight
,
offset
,
output
,
self
.
bufs_
[
0
],
self
.
bufs_
[
1
],
weight
.
size
(
3
),
weight
.
size
(
2
),
self
.
stride
[
1
],
self
.
stride
[
0
],
self
.
padding
[
1
],
self
.
padding
[
0
],
self
.
dilation
[
1
],
self
.
dilation
[
0
],
self
.
deformable_groups
,
cur_im2col_step
)
return
output
def
backward
(
self
,
grad_output
):
input
,
offset
,
weight
=
self
.
saved_tensors
grad_input
=
grad_offset
=
grad_weight
=
None
if
not
grad_output
.
is_cuda
:
raise
NotImplementedError
else
:
if
isinstance
(
grad_output
,
torch
.
autograd
.
Variable
):
if
not
isinstance
(
grad_output
.
data
,
torch
.
cuda
.
FloatTensor
):
raise
NotImplementedError
else
:
if
not
isinstance
(
grad_output
,
torch
.
cuda
.
FloatTensor
):
raise
NotImplementedError
cur_im2col_step
=
min
(
self
.
im2col_step
,
input
.
shape
[
0
])
assert
(
input
.
shape
[
0
]
%
cur_im2col_step
)
==
0
,
'im2col step must divide batchsize'
if
self
.
needs_input_grad
[
0
]
or
self
.
needs_input_grad
[
1
]:
grad_input
=
input
.
new
(
*
input
.
size
()).
zero_
()
grad_offset
=
offset
.
new
(
*
offset
.
size
()).
zero_
()
deform_conv
.
deform_conv_backward_input_cuda
(
input
,
offset
,
grad_output
,
grad_input
,
grad_offset
,
weight
,
self
.
bufs_
[
0
],
weight
.
size
(
3
),
weight
.
size
(
2
),
self
.
stride
[
1
],
self
.
stride
[
0
],
self
.
padding
[
1
],
self
.
padding
[
0
],
self
.
dilation
[
1
],
self
.
dilation
[
0
],
self
.
deformable_groups
,
cur_im2col_step
)
if
self
.
needs_input_grad
[
2
]:
grad_weight
=
weight
.
new
(
*
weight
.
size
()).
zero_
()
deform_conv
.
deform_conv_backward_parameters_cuda
(
input
,
offset
,
grad_output
,
grad_weight
,
self
.
bufs_
[
0
],
self
.
bufs_
[
1
],
weight
.
size
(
3
),
weight
.
size
(
2
),
self
.
stride
[
1
],
self
.
stride
[
0
],
self
.
padding
[
1
],
self
.
padding
[
0
],
self
.
dilation
[
1
],
self
.
dilation
[
0
],
self
.
deformable_groups
,
1
,
cur_im2col_step
)
return
grad_input
,
grad_offset
,
grad_weight
def
_output_size
(
self
,
input
,
weight
):
channels
=
weight
.
size
(
0
)
output_size
=
(
input
.
size
(
0
),
channels
)
for
d
in
range
(
input
.
dim
()
-
2
):
in_size
=
input
.
size
(
d
+
2
)
pad
=
self
.
padding
[
d
]
kernel
=
self
.
dilation
[
d
]
*
(
weight
.
size
(
d
+
2
)
-
1
)
+
1
stride
=
self
.
stride
[
d
]
output_size
+=
((
in_size
+
(
2
*
pad
)
-
kernel
)
//
stride
+
1
,
)
if
not
all
(
map
(
lambda
s
:
s
>
0
,
output_size
)):
raise
ValueError
(
"convolution input is too small (output would be {})"
.
format
(
'x'
.
join
(
map
(
str
,
output_size
))))
return
output_size
mmdet/ops/dcn/functions/modulated_dcn_func.py
0 → 100644
View file @
ef86c404
#!/usr/bin/env python
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
torch
from
torch.autograd
import
Function
from
..
import
modulated_dcn
as
_backend
class
ModulatedDeformConvFunction
(
Function
):
def
__init__
(
self
,
stride
,
padding
,
dilation
=
1
,
deformable_groups
=
1
):
super
(
ModulatedDeformConvFunction
,
self
).
__init__
()
self
.
stride
=
stride
self
.
padding
=
padding
self
.
dilation
=
dilation
self
.
deformable_groups
=
deformable_groups
def
forward
(
self
,
input
,
offset
,
mask
,
weight
,
bias
):
if
not
input
.
is_cuda
:
raise
NotImplementedError
if
weight
.
requires_grad
or
mask
.
requires_grad
or
offset
.
requires_grad
\
or
input
.
requires_grad
:
self
.
save_for_backward
(
input
,
offset
,
mask
,
weight
,
bias
)
output
=
input
.
new
(
*
self
.
_infer_shape
(
input
,
weight
))
self
.
_bufs
=
[
input
.
new
(),
input
.
new
()]
_backend
.
modulated_deform_conv_cuda_forward
(
input
,
weight
,
bias
,
self
.
_bufs
[
0
],
offset
,
mask
,
output
,
self
.
_bufs
[
1
],
weight
.
shape
[
2
],
weight
.
shape
[
3
],
self
.
stride
,
self
.
stride
,
self
.
padding
,
self
.
padding
,
self
.
dilation
,
self
.
dilation
,
self
.
deformable_groups
)
return
output
def
backward
(
self
,
grad_output
):
if
not
grad_output
.
is_cuda
:
raise
NotImplementedError
input
,
offset
,
mask
,
weight
,
bias
=
self
.
saved_tensors
grad_input
=
input
.
new
(
*
input
.
size
()).
zero_
()
grad_offset
=
offset
.
new
(
*
offset
.
size
()).
zero_
()
grad_mask
=
mask
.
new
(
*
mask
.
size
()).
zero_
()
grad_weight
=
weight
.
new
(
*
weight
.
size
()).
zero_
()
grad_bias
=
bias
.
new
(
*
bias
.
size
()).
zero_
()
_backend
.
modulated_deform_conv_cuda_backward
(
input
,
weight
,
bias
,
self
.
_bufs
[
0
],
offset
,
mask
,
self
.
_bufs
[
1
],
grad_input
,
grad_weight
,
grad_bias
,
grad_offset
,
grad_mask
,
grad_output
,
weight
.
shape
[
2
],
weight
.
shape
[
3
],
self
.
stride
,
self
.
stride
,
self
.
padding
,
self
.
padding
,
self
.
dilation
,
self
.
dilation
,
self
.
deformable_groups
)
return
grad_input
,
grad_offset
,
grad_mask
,
grad_weight
,
grad_bias
def
_infer_shape
(
self
,
input
,
weight
):
n
=
input
.
size
(
0
)
channels_out
=
weight
.
size
(
0
)
height
,
width
=
input
.
shape
[
2
:
4
]
kernel_h
,
kernel_w
=
weight
.
shape
[
2
:
4
]
height_out
=
(
height
+
2
*
self
.
padding
-
(
self
.
dilation
*
(
kernel_h
-
1
)
+
1
))
//
self
.
stride
+
1
width_out
=
(
width
+
2
*
self
.
padding
-
(
self
.
dilation
*
(
kernel_w
-
1
)
+
1
))
//
self
.
stride
+
1
return
(
n
,
channels_out
,
height_out
,
width_out
)
class
DeformRoIPoolingFunction
(
Function
):
def
__init__
(
self
,
spatial_scale
,
pooled_size
,
output_dim
,
no_trans
,
group_size
=
1
,
part_size
=
None
,
sample_per_part
=
4
,
trans_std
=
.
0
):
super
(
DeformRoIPoolingFunction
,
self
).
__init__
()
self
.
spatial_scale
=
spatial_scale
self
.
pooled_size
=
pooled_size
self
.
output_dim
=
output_dim
self
.
no_trans
=
no_trans
self
.
group_size
=
group_size
self
.
part_size
=
pooled_size
if
part_size
is
None
else
part_size
self
.
sample_per_part
=
sample_per_part
self
.
trans_std
=
trans_std
assert
self
.
trans_std
>=
0.0
and
self
.
trans_std
<=
1.0
def
forward
(
self
,
data
,
rois
,
offset
):
if
not
data
.
is_cuda
:
raise
NotImplementedError
output
=
data
.
new
(
*
self
.
_infer_shape
(
data
,
rois
))
output_count
=
data
.
new
(
*
self
.
_infer_shape
(
data
,
rois
))
_backend
.
deform_psroi_pooling_cuda_forward
(
data
,
rois
,
offset
,
output
,
output_count
,
self
.
no_trans
,
self
.
spatial_scale
,
self
.
output_dim
,
self
.
group_size
,
self
.
pooled_size
,
self
.
part_size
,
self
.
sample_per_part
,
self
.
trans_std
)
# if data.requires_grad or rois.requires_grad or offset.requires_grad:
# self.save_for_backward(data, rois, offset, output_count)
self
.
data
=
data
self
.
rois
=
rois
self
.
offset
=
offset
self
.
output_count
=
output_count
return
output
def
backward
(
self
,
grad_output
):
if
not
grad_output
.
is_cuda
:
raise
NotImplementedError
# data, rois, offset, output_count = self.saved_tensors
data
=
self
.
data
rois
=
self
.
rois
offset
=
self
.
offset
output_count
=
self
.
output_count
grad_input
=
data
.
new
(
*
data
.
size
()).
zero_
()
grad_offset
=
offset
.
new
(
*
offset
.
size
()).
zero_
()
_backend
.
deform_psroi_pooling_cuda_backward
(
grad_output
,
data
,
rois
,
offset
,
output_count
,
grad_input
,
grad_offset
,
self
.
no_trans
,
self
.
spatial_scale
,
self
.
output_dim
,
self
.
group_size
,
self
.
pooled_size
,
self
.
part_size
,
self
.
sample_per_part
,
self
.
trans_std
)
return
grad_input
,
torch
.
zeros
(
rois
.
shape
).
cuda
(),
grad_offset
def
_infer_shape
(
self
,
data
,
rois
):
# _, c, h, w = data.shape[:4]
# c = data.shape[1]
n
=
rois
.
shape
[
0
]
return
n
,
self
.
output_dim
,
self
.
pooled_size
,
self
.
pooled_size
mmdet/ops/dcn/modules/__init__.py
0 → 100644
View file @
ef86c404
mmdet/ops/dcn/modules/deform_conv.py
0 → 100644
View file @
ef86c404
import
math
import
torch
import
torch.nn
as
nn
from
torch.nn.modules.module
import
Module
from
torch.nn.modules.utils
import
_pair
from
..functions.deform_conv
import
deform_conv_function
class
DeformConv
(
Module
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
=
1
,
padding
=
0
,
dilation
=
1
,
num_deformable_groups
=
1
):
super
(
DeformConv
,
self
).
__init__
()
self
.
in_channels
=
in_channels
self
.
out_channels
=
out_channels
self
.
kernel_size
=
_pair
(
kernel_size
)
self
.
stride
=
_pair
(
stride
)
self
.
padding
=
_pair
(
padding
)
self
.
dilation
=
_pair
(
dilation
)
self
.
num_deformable_groups
=
num_deformable_groups
self
.
weight
=
nn
.
Parameter
(
torch
.
Tensor
(
out_channels
,
in_channels
,
*
self
.
kernel_size
))
self
.
reset_parameters
()
def
reset_parameters
(
self
):
n
=
self
.
in_channels
for
k
in
self
.
kernel_size
:
n
*=
k
stdv
=
1.
/
math
.
sqrt
(
n
)
self
.
weight
.
data
.
uniform_
(
-
stdv
,
stdv
)
def
forward
(
self
,
input
,
offset
):
return
deform_conv_function
(
input
,
offset
,
self
.
weight
,
self
.
stride
,
self
.
padding
,
self
.
dilation
,
self
.
num_deformable_groups
)
mmdet/ops/dcn/modules/modulated_dcn.py
0 → 100644
View file @
ef86c404
#!/usr/bin/env python
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
math
import
torch
from
torch
import
nn
from
torch.nn.modules.utils
import
_pair
from
..functions.modulated_dcn_func
import
DeformRoIPoolingFunction
from
..functions.modulated_dcn_func
import
ModulatedDeformConvFunction
class
ModulatedDeformConv
(
nn
.
Module
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
,
padding
,
dilation
=
1
,
deformable_groups
=
1
,
no_bias
=
True
):
super
(
ModulatedDeformConv
,
self
).
__init__
()
self
.
in_channels
=
in_channels
self
.
out_channels
=
out_channels
self
.
kernel_size
=
_pair
(
kernel_size
)
self
.
stride
=
stride
self
.
padding
=
padding
self
.
dilation
=
dilation
self
.
deformable_groups
=
deformable_groups
self
.
no_bias
=
no_bias
self
.
weight
=
nn
.
Parameter
(
torch
.
Tensor
(
out_channels
,
in_channels
,
*
self
.
kernel_size
))
self
.
bias
=
nn
.
Parameter
(
torch
.
zeros
(
out_channels
))
self
.
reset_parameters
()
if
self
.
no_bias
:
self
.
bias
.
requires_grad
=
False
def
reset_parameters
(
self
):
n
=
self
.
in_channels
for
k
in
self
.
kernel_size
:
n
*=
k
stdv
=
1.
/
math
.
sqrt
(
n
)
self
.
weight
.
data
.
uniform_
(
-
stdv
,
stdv
)
self
.
bias
.
data
.
zero_
()
def
forward
(
self
,
input
,
offset
,
mask
):
func
=
ModulatedDeformConvFunction
(
self
.
stride
,
self
.
padding
,
self
.
dilation
,
self
.
deformable_groups
)
return
func
(
input
,
offset
,
mask
,
self
.
weight
,
self
.
bias
)
class
ModulatedDeformConvPack
(
ModulatedDeformConv
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
,
padding
,
dilation
=
1
,
deformable_groups
=
1
,
no_bias
=
False
):
super
(
ModulatedDeformConvPack
,
self
).
__init__
(
in_channels
,
out_channels
,
kernel_size
,
stride
,
padding
,
dilation
,
deformable_groups
,
no_bias
)
self
.
conv_offset_mask
=
nn
.
Conv2d
(
self
.
in_channels
,
self
.
deformable_groups
*
3
*
self
.
kernel_size
[
0
]
*
self
.
kernel_size
[
1
],
kernel_size
=
self
.
kernel_size
,
stride
=
(
self
.
stride
,
self
.
stride
),
padding
=
(
self
.
padding
,
self
.
padding
),
bias
=
True
)
self
.
init_offset
()
def
init_offset
(
self
):
self
.
conv_offset_mask
.
weight
.
data
.
zero_
()
self
.
conv_offset_mask
.
bias
.
data
.
zero_
()
def
forward
(
self
,
input
):
out
=
self
.
conv_offset_mask
(
input
)
o1
,
o2
,
mask
=
torch
.
chunk
(
out
,
3
,
dim
=
1
)
offset
=
torch
.
cat
((
o1
,
o2
),
dim
=
1
)
mask
=
torch
.
sigmoid
(
mask
)
func
=
ModulatedDeformConvFunction
(
self
.
stride
,
self
.
padding
,
self
.
dilation
,
self
.
deformable_groups
)
return
func
(
input
,
offset
,
mask
,
self
.
weight
,
self
.
bias
)
class
DeformRoIPooling
(
nn
.
Module
):
def
__init__
(
self
,
spatial_scale
,
pooled_size
,
output_dim
,
no_trans
,
group_size
=
1
,
part_size
=
None
,
sample_per_part
=
4
,
trans_std
=
.
0
):
super
(
DeformRoIPooling
,
self
).
__init__
()
self
.
spatial_scale
=
spatial_scale
self
.
pooled_size
=
pooled_size
self
.
out_size
=
pooled_size
self
.
output_dim
=
output_dim
self
.
no_trans
=
no_trans
self
.
group_size
=
group_size
self
.
part_size
=
pooled_size
if
part_size
is
None
else
part_size
self
.
sample_per_part
=
sample_per_part
self
.
trans_std
=
trans_std
self
.
func
=
DeformRoIPoolingFunction
(
self
.
spatial_scale
,
self
.
pooled_size
,
self
.
output_dim
,
self
.
no_trans
,
self
.
group_size
,
self
.
part_size
,
self
.
sample_per_part
,
self
.
trans_std
)
def
forward
(
self
,
data
,
rois
,
offset
):
if
self
.
no_trans
:
offset
=
data
.
new
()
return
self
.
func
(
data
,
rois
,
offset
)
class
ModulatedDeformRoIPoolingPack
(
DeformRoIPooling
):
def
__init__
(
self
,
spatial_scale
,
pooled_size
,
output_dim
,
no_trans
,
group_size
=
1
,
part_size
=
None
,
sample_per_part
=
4
,
trans_std
=
.
0
,
deform_fc_dim
=
1024
):
super
(
ModulatedDeformRoIPoolingPack
,
self
).
__init__
(
spatial_scale
,
pooled_size
,
output_dim
,
no_trans
,
group_size
,
part_size
,
sample_per_part
,
trans_std
)
self
.
deform_fc_dim
=
deform_fc_dim
if
not
no_trans
:
self
.
func_offset
=
DeformRoIPoolingFunction
(
self
.
spatial_scale
,
self
.
pooled_size
,
self
.
output_dim
,
True
,
self
.
group_size
,
self
.
part_size
,
self
.
sample_per_part
,
self
.
trans_std
)
self
.
offset_fc
=
nn
.
Sequential
(
nn
.
Linear
(
self
.
pooled_size
*
self
.
pooled_size
*
self
.
output_dim
,
self
.
deform_fc_dim
),
nn
.
ReLU
(
inplace
=
True
),
nn
.
Linear
(
self
.
deform_fc_dim
,
self
.
deform_fc_dim
),
nn
.
ReLU
(
inplace
=
True
),
nn
.
Linear
(
self
.
deform_fc_dim
,
self
.
pooled_size
*
self
.
pooled_size
*
2
))
self
.
offset_fc
[
4
].
weight
.
data
.
zero_
()
self
.
offset_fc
[
4
].
bias
.
data
.
zero_
()
self
.
mask_fc
=
nn
.
Sequential
(
nn
.
Linear
(
self
.
pooled_size
*
self
.
pooled_size
*
self
.
output_dim
,
self
.
deform_fc_dim
),
nn
.
ReLU
(
inplace
=
True
),
nn
.
Linear
(
self
.
deform_fc_dim
,
self
.
pooled_size
*
self
.
pooled_size
*
1
),
nn
.
Sigmoid
())
self
.
mask_fc
[
2
].
weight
.
data
.
zero_
()
self
.
mask_fc
[
2
].
bias
.
data
.
zero_
()
def
forward
(
self
,
data
,
rois
):
if
self
.
no_trans
:
offset
=
data
.
new
()
else
:
n
=
rois
.
shape
[
0
]
offset
=
data
.
new
()
x
=
self
.
func_offset
(
data
,
rois
,
offset
)
offset
=
self
.
offset_fc
(
x
.
view
(
n
,
-
1
))
offset
=
offset
.
view
(
n
,
2
,
self
.
pooled_size
,
self
.
pooled_size
)
mask
=
self
.
mask_fc
(
x
.
view
(
n
,
-
1
))
mask
=
mask
.
view
(
n
,
1
,
self
.
pooled_size
,
self
.
pooled_size
)
feat
=
self
.
func
(
data
,
rois
,
offset
)
*
mask
return
feat
return
self
.
func
(
data
,
rois
,
offset
)
mmdet/ops/dcn/setup.py
0 → 100644
View file @
ef86c404
from
setuptools
import
setup
from
torch.utils.cpp_extension
import
BuildExtension
,
CUDAExtension
setup
(
name
=
'deform_conv'
,
ext_modules
=
[
CUDAExtension
(
'deform_conv'
,
[
'src/deform_conv_cuda.cpp'
,
'src/deform_conv_cuda_kernel.cu'
,
]),
],
cmdclass
=
{
'build_ext'
:
BuildExtension
})
mmdet/ops/dcn/setup_modulated.py
0 → 100644
View file @
ef86c404
from
setuptools
import
setup
from
torch.utils.cpp_extension
import
BuildExtension
,
CUDAExtension
setup
(
name
=
'modulated_deform_conv'
,
ext_modules
=
[
CUDAExtension
(
'modulated_dcn'
,
[
'src/modulated_dcn_cuda.cpp'
,
'src/modulated_deform_im2col_cuda.cu'
,
'src/deform_psroi_pooling_cuda.cu'
]),
],
cmdclass
=
{
'build_ext'
:
BuildExtension
})
mmdet/ops/dcn/src/deform_conv_cuda.cpp
0 → 100644
View file @
ef86c404
#include <torch/torch.h>
#include <cmath>
#include <vector>
void
deformable_im2col
(
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
data_col
);
void
deformable_col2im
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
grad_im
);
void
deformable_col2im_coord
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
grad_offset
);
void
shape_check
(
at
::
Tensor
input
,
at
::
Tensor
offset
,
at
::
Tensor
*
gradOutput
,
at
::
Tensor
weight
,
int
kH
,
int
kW
,
int
dH
,
int
dW
,
int
padH
,
int
padW
,
int
dilationH
,
int
dilationW
,
int
deformable_group
)
{
// AT_CHECK(weight->nDimension == 4, 5,
// "4D weight tensor (nOutputPlane,nInputPlane,kH,kW) expected, "
// "but got: %s",
// weight->nDimension);
AT_CHECK
(
weight
.
ndimension
()
==
4
,
"4D weight tensor (nOutputPlane,nInputPlane,kH,kW) expected, "
"but got: %s"
,
weight
.
ndimension
());
AT_CHECK
(
weight
.
is_contiguous
(),
"weight tensor has to be contiguous"
);
AT_CHECK
(
kW
>
0
&&
kH
>
0
,
"kernel size should be greater than zero, but got kH: %d kW: %d"
,
kH
,
kW
);
// AT_CHECK((weight->size[2] == kH && weight->size[3] == kW), 9,
// "kernel size should be consistent with weight, ",
// "but got kH: %d kW: %d weight.size(2): %d, weight.size(3): %d", kH,
// kW, weight->size[2], weight->size[3]);
AT_CHECK
((
weight
.
size
(
2
)
==
kH
&&
weight
.
size
(
3
)
==
kW
),
"kernel size should be consistent with weight, "
,
"but got kH: %d kW: %d weight.size(2): %d, weight.size(3): %d"
,
kH
,
kW
,
weight
.
size
(
2
),
weight
.
size
(
3
));
AT_CHECK
(
dW
>
0
&&
dH
>
0
,
"stride should be greater than zero, but got dH: %d dW: %d"
,
dH
,
dW
);
AT_CHECK
(
dilationW
>
0
&&
dilationH
>
0
,
"dilation should be greater than 0, but got dilationH: %d dilationW: %d"
,
dilationH
,
dilationW
);
// int ndim = input->nDimension;
int
ndim
=
input
.
ndimension
();
int
dimf
=
0
;
int
dimh
=
1
;
int
dimw
=
2
;
if
(
ndim
==
4
)
{
dimf
++
;
dimh
++
;
dimw
++
;
}
AT_CHECK
(
ndim
==
3
||
ndim
==
4
,
"3D or 4D input tensor expected but got: %s"
,
ndim
);
// long nInputPlane = weight->size[1];
// long inputHeight = input->size[dimh];
// long inputWidth = input->size[dimw];
// long nOutputPlane = weight->size[0];
long
nInputPlane
=
weight
.
size
(
1
);
long
inputHeight
=
input
.
size
(
dimh
);
long
inputWidth
=
input
.
size
(
dimw
);
long
nOutputPlane
=
weight
.
size
(
0
);
long
outputHeight
=
(
inputHeight
+
2
*
padH
-
(
dilationH
*
(
kH
-
1
)
+
1
))
/
dH
+
1
;
long
outputWidth
=
(
inputWidth
+
2
*
padW
-
(
dilationW
*
(
kW
-
1
)
+
1
))
/
dW
+
1
;
AT_CHECK
(
nInputPlane
%
deformable_group
==
0
,
"input channels must divide deformable group size"
);
if
(
outputWidth
<
1
||
outputHeight
<
1
)
AT_ERROR
(
"Given input size: (%ld x %ld x %ld). "
"Calculated output size: (%ld x %ld x %ld). Output size is too small"
,
nInputPlane
,
inputHeight
,
inputWidth
,
nOutputPlane
,
outputHeight
,
outputWidth
);
AT_CHECK
(
input
.
size
(
1
)
==
nInputPlane
,
"invalid number of input planes, expected: %d, but got: %d"
,
nInputPlane
,
input
.
size
(
1
));
AT_CHECK
((
inputHeight
>=
kH
&&
inputWidth
>=
kW
),
"input image is smaller than kernel"
);
// AT_CHECK(
// (offset->size[2] == outputHeight && offset->size[3] == outputWidth), 3,
// "invalid spatial size of offset, expected height: %d width: %d, but got height: %d width: %d", outputHeight, outputWidth,
// offset->size[2], offset->size[3]);
AT_CHECK
(
(
offset
.
size
(
2
)
==
outputHeight
&&
offset
.
size
(
3
)
==
outputWidth
),
"invalid spatial size of offset, expected height: %d width: %d, but got height: %d width: %d"
,
outputHeight
,
outputWidth
,
offset
.
size
(
2
),
offset
.
size
(
3
));
AT_CHECK
((
offset
.
size
(
1
)
==
deformable_group
*
2
*
kH
*
kW
),
"invalid number of channels of offset"
);
if
(
gradOutput
!=
NULL
)
{
AT_CHECK
(
gradOutput
->
size
(
dimf
)
==
nOutputPlane
,
"invalid number of gradOutput planes, expected: %d, but got: %d"
,
nOutputPlane
,
gradOutput
->
size
(
dimf
));
AT_CHECK
((
gradOutput
->
size
(
dimh
)
==
outputHeight
&&
gradOutput
->
size
(
dimw
)
==
outputWidth
),
"invalid size of gradOutput, expected height: %d width: %d , but got height: %d width: %d"
,
outputHeight
,
outputWidth
,
gradOutput
->
size
(
dimh
),
gradOutput
->
size
(
dimw
));
}
}
int
deform_conv_forward_cuda
(
at
::
Tensor
input
,
at
::
Tensor
weight
,
at
::
Tensor
offset
,
at
::
Tensor
output
,
at
::
Tensor
columns
,
at
::
Tensor
ones
,
int
kW
,
int
kH
,
int
dW
,
int
dH
,
int
padW
,
int
padH
,
int
dilationW
,
int
dilationH
,
int
deformable_group
,
int
im2col_step
)
{
// todo: resize columns to include im2col: done
// todo: add im2col_step as input
// todo: add new output buffer and transpose it to output (or directly transpose output)
// todo: possibly change data indexing because of parallel_imgs
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 6, input, weight, offset,
// output, columns, ones));
shape_check
(
input
,
offset
,
NULL
,
weight
,
kH
,
kW
,
dH
,
dW
,
padH
,
padW
,
dilationH
,
dilationW
,
deformable_group
);
input
=
input
.
contiguous
();
offset
=
offset
.
contiguous
();
weight
=
weight
.
contiguous
();
int
batch
=
1
;
if
(
input
.
ndimension
()
==
3
)
{
// Force batch
batch
=
0
;
input
.
unsqueeze_
(
0
);
offset
.
unsqueeze_
(
0
);
}
// todo: assert batchsize dividable by im2col_step
long
batchSize
=
input
.
size
(
0
);
long
nInputPlane
=
input
.
size
(
1
);
long
inputHeight
=
input
.
size
(
2
);
long
inputWidth
=
input
.
size
(
3
);
long
nOutputPlane
=
weight
.
size
(
0
);
long
outputWidth
=
(
inputWidth
+
2
*
padW
-
(
dilationW
*
(
kW
-
1
)
+
1
))
/
dW
+
1
;
long
outputHeight
=
(
inputHeight
+
2
*
padH
-
(
dilationH
*
(
kH
-
1
)
+
1
))
/
dH
+
1
;
AT_CHECK
((
offset
.
size
(
0
)
==
batchSize
),
"invalid batch size of offset"
);
// bias = bias ? THCudaTensor_newContiguous(state, bias) : bias;
output
=
output
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
nOutputPlane
,
outputHeight
,
outputWidth
});
columns
=
at
::
zeros
({
nInputPlane
*
kW
*
kH
,
im2col_step
*
outputHeight
*
outputWidth
},
input
.
type
());
if
(
ones
.
ndimension
()
!=
2
||
ones
.
size
(
0
)
*
ones
.
size
(
1
)
<
outputHeight
*
outputWidth
)
{
ones
=
at
::
ones
({
outputHeight
,
outputWidth
},
input
.
type
());
}
input
=
input
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
at
::
Tensor
output_buffer
=
at
::
zeros
({
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
*
outputHeight
,
outputWidth
},
output
.
type
());
for
(
int
elt
=
0
;
elt
<
batchSize
/
im2col_step
;
elt
++
)
{
deformable_im2col
(
input
[
elt
],
offset
[
elt
],
nInputPlane
,
inputHeight
,
inputWidth
,
kH
,
kW
,
padH
,
padW
,
dH
,
dW
,
dilationH
,
dilationW
,
im2col_step
,
deformable_group
,
columns
);
output_buffer
[
elt
]
=
output_buffer
[
elt
].
flatten
(
1
).
addmm_
(
weight
.
flatten
(
1
),
columns
).
view_as
(
output_buffer
[
elt
]);
}
// the reason I use seemingly redundant output_buffer is that THCudaTensor API handles successive transpose and resize poorly
output_buffer
=
output_buffer
.
view
(
{
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
,
outputHeight
,
outputWidth
});
output_buffer
.
transpose_
(
1
,
2
);
output
.
copy_
(
output_buffer
);
output
=
output
.
view
({
batchSize
,
nOutputPlane
,
outputHeight
,
outputWidth
});
input
=
input
.
view
({
batchSize
,
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
batchSize
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
if
(
batch
==
0
)
{
output
=
output
.
view
({
nOutputPlane
,
outputHeight
,
outputWidth
});
input
=
input
.
view
({
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
offset
.
size
(
1
),
offset
.
size
(
2
),
offset
.
size
(
3
)});
}
return
1
;
}
int
deform_conv_backward_input_cuda
(
at
::
Tensor
input
,
at
::
Tensor
offset
,
at
::
Tensor
gradOutput
,
at
::
Tensor
gradInput
,
at
::
Tensor
gradOffset
,
at
::
Tensor
weight
,
at
::
Tensor
columns
,
int
kW
,
int
kH
,
int
dW
,
int
dH
,
int
padW
,
int
padH
,
int
dilationW
,
int
dilationH
,
int
deformable_group
,
int
im2col_step
)
{
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 6, input, gradOutput, weight,
// offset, columns, gradInput));
shape_check
(
input
,
offset
,
&
gradOutput
,
weight
,
kH
,
kW
,
dH
,
dW
,
padH
,
padW
,
dilationH
,
dilationW
,
deformable_group
);
input
=
input
.
contiguous
();
offset
=
offset
.
contiguous
();
gradOutput
=
gradOutput
.
contiguous
();
weight
=
weight
.
contiguous
();
int
batch
=
1
;
if
(
input
.
ndimension
()
==
3
)
{
// Force batch
batch
=
0
;
input
=
input
.
view
({
1
,
input
.
size
(
0
),
input
.
size
(
1
),
input
.
size
(
2
)});
offset
=
offset
.
view
({
1
,
offset
.
size
(
0
),
offset
.
size
(
1
),
offset
.
size
(
2
)});
gradOutput
=
gradOutput
.
view
({
1
,
gradOutput
.
size
(
0
),
gradOutput
.
size
(
1
),
gradOutput
.
size
(
2
)});
}
long
batchSize
=
input
.
size
(
0
);
long
nInputPlane
=
input
.
size
(
1
);
long
inputHeight
=
input
.
size
(
2
);
long
inputWidth
=
input
.
size
(
3
);
long
nOutputPlane
=
weight
.
size
(
0
);
long
outputWidth
=
(
inputWidth
+
2
*
padW
-
(
dilationW
*
(
kW
-
1
)
+
1
))
/
dW
+
1
;
long
outputHeight
=
(
inputHeight
+
2
*
padH
-
(
dilationH
*
(
kH
-
1
)
+
1
))
/
dH
+
1
;
AT_CHECK
((
offset
.
size
(
0
)
==
batchSize
),
3
,
"invalid batch size of offset"
);
gradInput
=
gradInput
.
view
({
batchSize
,
nInputPlane
,
inputHeight
,
inputWidth
});
columns
=
at
::
zeros
({
nInputPlane
*
kW
*
kH
,
im2col_step
*
outputHeight
*
outputWidth
},
input
.
type
());
// change order of grad output
gradOutput
=
gradOutput
.
view
(
{
batchSize
/
im2col_step
,
im2col_step
,
nOutputPlane
,
outputHeight
,
outputWidth
});
gradOutput
.
transpose_
(
1
,
2
);
at
::
Tensor
gradOutputBuffer
=
at
::
zeros_like
(
gradOutput
);
gradOutputBuffer
=
gradOutputBuffer
.
view
(
{
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
,
outputHeight
,
outputWidth
});
gradOutputBuffer
.
copy_
(
gradOutput
);
gradOutputBuffer
=
gradOutputBuffer
.
view
(
{
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
*
outputHeight
,
outputWidth
});
gradOutput
.
transpose_
(
1
,
2
);
gradOutput
=
gradOutput
.
view
({
batchSize
,
nOutputPlane
,
outputHeight
,
outputWidth
});
gradInput
=
gradInput
.
view
(
{
batchSize
/
im2col_step
,
im2col_step
,
nInputPlane
,
inputHeight
,
inputWidth
});
input
=
input
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
nInputPlane
,
inputHeight
,
inputWidth
});
gradOffset
=
gradOffset
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
offset
=
offset
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
for
(
int
elt
=
0
;
elt
<
batchSize
/
im2col_step
;
elt
++
)
{
columns
=
columns
.
addmm_
(
weight
.
flatten
(
1
).
transpose
(
0
,
1
),
gradOutputBuffer
[
elt
].
flatten
(
1
));
deformable_col2im_coord
(
columns
,
input
[
elt
],
offset
[
elt
],
nInputPlane
,
inputHeight
,
inputWidth
,
kH
,
kW
,
padH
,
padW
,
dH
,
dW
,
dilationH
,
dilationW
,
im2col_step
,
deformable_group
,
gradOffset
[
elt
]);
deformable_col2im
(
columns
,
offset
[
elt
],
nInputPlane
,
inputHeight
,
inputWidth
,
kH
,
kW
,
padH
,
padW
,
dH
,
dW
,
dilationH
,
dilationW
,
im2col_step
,
deformable_group
,
gradInput
[
elt
]);
}
gradInput
=
gradInput
.
view
({
batchSize
,
nInputPlane
,
inputHeight
,
inputWidth
});
input
=
input
.
view
({
batchSize
,
nInputPlane
,
inputHeight
,
inputWidth
});
gradOffset
=
gradOffset
.
view
({
batchSize
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
offset
=
offset
.
view
({
batchSize
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
if
(
batch
==
0
)
{
gradOutput
=
gradOutput
.
view
({
nOutputPlane
,
outputHeight
,
outputWidth
});
input
=
input
.
view
({
nInputPlane
,
inputHeight
,
inputWidth
});
gradInput
=
gradInput
.
view
({
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
offset
.
size
(
1
),
offset
.
size
(
2
),
offset
.
size
(
3
)});
gradOffset
=
gradOffset
.
view
({
offset
.
size
(
1
),
offset
.
size
(
2
),
offset
.
size
(
3
)});
}
return
1
;
}
int
deform_conv_backward_parameters_cuda
(
at
::
Tensor
input
,
at
::
Tensor
offset
,
at
::
Tensor
gradOutput
,
at
::
Tensor
gradWeight
,
// at::Tensor gradBias,
at
::
Tensor
columns
,
at
::
Tensor
ones
,
int
kW
,
int
kH
,
int
dW
,
int
dH
,
int
padW
,
int
padH
,
int
dilationW
,
int
dilationH
,
int
deformable_group
,
float
scale
,
int
im2col_step
)
{
// todo: transpose and reshape outGrad
// todo: reshape columns
// todo: add im2col_step as input
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 5, input, offset, gradOutput,
// gradWeight, columns));
shape_check
(
input
,
offset
,
&
gradOutput
,
gradWeight
,
kH
,
kW
,
dH
,
dW
,
padH
,
padW
,
dilationH
,
dilationW
,
deformable_group
);
input
=
input
.
contiguous
();
offset
=
offset
.
contiguous
();
gradOutput
=
gradOutput
.
contiguous
();
int
batch
=
1
;
if
(
input
.
ndimension
()
==
3
)
{
// Force batch
batch
=
0
;
input
=
input
.
view
(
at
::
IntList
({
1
,
input
.
size
(
0
),
input
.
size
(
1
),
input
.
size
(
2
)}));
gradOutput
=
gradOutput
.
view
({
1
,
gradOutput
.
size
(
0
),
gradOutput
.
size
(
1
),
gradOutput
.
size
(
2
)});
}
long
batchSize
=
input
.
size
(
0
);
long
nInputPlane
=
input
.
size
(
1
);
long
inputHeight
=
input
.
size
(
2
);
long
inputWidth
=
input
.
size
(
3
);
long
nOutputPlane
=
gradWeight
.
size
(
0
);
long
outputWidth
=
(
inputWidth
+
2
*
padW
-
(
dilationW
*
(
kW
-
1
)
+
1
))
/
dW
+
1
;
long
outputHeight
=
(
inputHeight
+
2
*
padH
-
(
dilationH
*
(
kH
-
1
)
+
1
))
/
dH
+
1
;
AT_CHECK
((
offset
.
size
(
0
)
==
batchSize
),
"invalid batch size of offset"
);
columns
=
at
::
zeros
({
nInputPlane
*
kW
*
kH
,
im2col_step
*
outputHeight
*
outputWidth
},
input
.
type
());
gradOutput
=
gradOutput
.
view
(
{
batchSize
/
im2col_step
,
im2col_step
,
nOutputPlane
,
outputHeight
,
outputWidth
});
gradOutput
.
transpose_
(
1
,
2
);
at
::
Tensor
gradOutputBuffer
=
at
::
zeros_like
(
gradOutput
);
gradOutputBuffer
=
gradOutputBuffer
.
view
(
{
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
,
outputHeight
,
outputWidth
});
gradOutputBuffer
.
copy_
(
gradOutput
);
gradOutputBuffer
=
gradOutputBuffer
.
view
(
{
batchSize
/
im2col_step
,
nOutputPlane
,
im2col_step
*
outputHeight
,
outputWidth
});
gradOutput
.
transpose_
(
1
,
2
);
gradOutput
=
gradOutput
.
view
({
batchSize
,
nOutputPlane
,
outputHeight
,
outputWidth
});
input
=
input
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
batchSize
/
im2col_step
,
im2col_step
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
for
(
int
elt
=
0
;
elt
<
batchSize
/
im2col_step
;
elt
++
)
{
deformable_im2col
(
input
[
elt
],
offset
[
elt
],
nInputPlane
,
inputHeight
,
inputWidth
,
kH
,
kW
,
padH
,
padW
,
dH
,
dW
,
dilationH
,
dilationW
,
im2col_step
,
deformable_group
,
columns
);
gradWeight
.
copy_
(
gradWeight
.
flatten
(
1
).
addmm_
(
gradOutputBuffer
[
elt
].
flatten
(
1
),
columns
.
transpose
(
1
,
0
),
1.0
,
scale
).
view_as
(
gradWeight
));
}
input
=
input
.
view
({
batchSize
,
nInputPlane
,
inputHeight
,
inputWidth
});
offset
=
offset
.
view
({
batchSize
,
deformable_group
*
2
*
kH
*
kW
,
outputHeight
,
outputWidth
});
if
(
batch
==
0
)
{
gradOutput
=
gradOutput
.
view
({
nOutputPlane
,
outputHeight
,
outputWidth
});
input
=
input
.
view
({
nInputPlane
,
inputHeight
,
inputWidth
});
}
return
1
;
}
PYBIND11_MODULE
(
TORCH_EXTENSION_NAME
,
m
)
{
m
.
def
(
"deform_conv_forward_cuda"
,
&
deform_conv_forward_cuda
,
"deform forward (CUDA)"
);
m
.
def
(
"deform_conv_backward_input_cuda"
,
&
deform_conv_backward_input_cuda
,
"deform_conv_backward_input (CUDA)"
);
m
.
def
(
"deform_conv_backward_parameters_cuda"
,
&
deform_conv_backward_parameters_cuda
,
"deform_conv_backward_parameters (CUDA)"
);
}
mmdet/ops/dcn/src/deform_conv_cuda_kernel.cu
0 → 100644
View file @
ef86c404
/*!
******************* BEGIN Caffe Copyright Notice and Disclaimer ****************
*
* COPYRIGHT
*
* All contributions by the University of California:
* Copyright (c) 2014-2017 The Regents of the University of California (Regents)
* All rights reserved.
*
* All other contributions:
* Copyright (c) 2014-2017, the respective contributors
* All rights reserved.
*
* Caffe uses a shared copyright model: each contributor holds copyright over
* their contributions to Caffe. The project versioning records all such
* contribution and copyright details. If a contributor wants to further mark
* their specific copyright on a particular contribution, they should indicate
* their copyright solely in the commit message of the change when it is
* committed.
*
* LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* CONTRIBUTION AGREEMENT
*
* By contributing to the BVLC/caffe repository through pull-request, comment,
* or otherwise, the contributor releases their content to the
* license and copyright terms herein.
*
***************** END Caffe Copyright Notice and Disclaimer ********************
*
* Copyright (c) 2018 Microsoft
* Licensed under The MIT License [see LICENSE for details]
* \file modulated_deformable_im2col.cuh
* \brief Function definitions of converting an image to
* column matrix based on kernel, padding, dilation, and offset.
* These functions are mainly used in deformable convolution operators.
* \ref: https://arxiv.org/abs/1703.06211
* \author Yuwen Xiong, Haozhi Qi, Jifeng Dai, Xizhou Zhu, Han Hu, Dazhi Cheng
*/
#include <ATen/ATen.h>
#include <THC/THCAtomics.cuh>
#include <stdio.h>
#include <math.h>
#include <float.h>
using
namespace
at
;
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \
i += blockDim.x * gridDim.x)
const
int
CUDA_NUM_THREADS
=
1024
;
inline
int
GET_BLOCKS
(
const
int
N
)
{
return
(
N
+
CUDA_NUM_THREADS
-
1
)
/
CUDA_NUM_THREADS
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
deformable_im2col_bilinear
(
const
scalar_t
*
bottom_data
,
const
int
data_width
,
const
int
height
,
const
int
width
,
scalar_t
h
,
scalar_t
w
)
{
int
h_low
=
floor
(
h
);
int
w_low
=
floor
(
w
);
int
h_high
=
h_low
+
1
;
int
w_high
=
w_low
+
1
;
scalar_t
lh
=
h
-
h_low
;
scalar_t
lw
=
w
-
w_low
;
scalar_t
hh
=
1
-
lh
,
hw
=
1
-
lw
;
scalar_t
v1
=
0
;
if
(
h_low
>=
0
&&
w_low
>=
0
)
v1
=
bottom_data
[
h_low
*
data_width
+
w_low
];
scalar_t
v2
=
0
;
if
(
h_low
>=
0
&&
w_high
<=
width
-
1
)
v2
=
bottom_data
[
h_low
*
data_width
+
w_high
];
scalar_t
v3
=
0
;
if
(
h_high
<=
height
-
1
&&
w_low
>=
0
)
v3
=
bottom_data
[
h_high
*
data_width
+
w_low
];
scalar_t
v4
=
0
;
if
(
h_high
<=
height
-
1
&&
w_high
<=
width
-
1
)
v4
=
bottom_data
[
h_high
*
data_width
+
w_high
];
scalar_t
w1
=
hh
*
hw
,
w2
=
hh
*
lw
,
w3
=
lh
*
hw
,
w4
=
lh
*
lw
;
scalar_t
val
=
(
w1
*
v1
+
w2
*
v2
+
w3
*
v3
+
w4
*
v4
);
return
val
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
get_gradient_weight
(
scalar_t
argmax_h
,
scalar_t
argmax_w
,
const
int
h
,
const
int
w
,
const
int
height
,
const
int
width
)
{
if
(
argmax_h
<=
-
1
||
argmax_h
>=
height
||
argmax_w
<=
-
1
||
argmax_w
>=
width
)
{
//empty
return
0
;
}
int
argmax_h_low
=
floor
(
argmax_h
);
int
argmax_w_low
=
floor
(
argmax_w
);
int
argmax_h_high
=
argmax_h_low
+
1
;
int
argmax_w_high
=
argmax_w_low
+
1
;
scalar_t
weight
=
0
;
if
(
h
==
argmax_h_low
&&
w
==
argmax_w_low
)
weight
=
(
h
+
1
-
argmax_h
)
*
(
w
+
1
-
argmax_w
);
if
(
h
==
argmax_h_low
&&
w
==
argmax_w_high
)
weight
=
(
h
+
1
-
argmax_h
)
*
(
argmax_w
+
1
-
w
);
if
(
h
==
argmax_h_high
&&
w
==
argmax_w_low
)
weight
=
(
argmax_h
+
1
-
h
)
*
(
w
+
1
-
argmax_w
);
if
(
h
==
argmax_h_high
&&
w
==
argmax_w_high
)
weight
=
(
argmax_h
+
1
-
h
)
*
(
argmax_w
+
1
-
w
);
return
weight
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
get_coordinate_weight
(
scalar_t
argmax_h
,
scalar_t
argmax_w
,
const
int
height
,
const
int
width
,
const
scalar_t
*
im_data
,
const
int
data_width
,
const
int
bp_dir
)
{
if
(
argmax_h
<=
-
1
||
argmax_h
>=
height
||
argmax_w
<=
-
1
||
argmax_w
>=
width
)
{
//empty
return
0
;
}
int
argmax_h_low
=
floor
(
argmax_h
);
int
argmax_w_low
=
floor
(
argmax_w
);
int
argmax_h_high
=
argmax_h_low
+
1
;
int
argmax_w_high
=
argmax_w_low
+
1
;
scalar_t
weight
=
0
;
if
(
bp_dir
==
0
)
{
if
(
argmax_h_low
>=
0
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_w_low
+
1
-
argmax_w
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_low
];
if
(
argmax_h_low
>=
0
&&
argmax_w_high
<=
width
-
1
)
weight
+=
-
1
*
(
argmax_w
-
argmax_w_low
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_high
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_low
>=
0
)
weight
+=
(
argmax_w_low
+
1
-
argmax_w
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_low
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_w
-
argmax_w_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_high
];
}
else
if
(
bp_dir
==
1
)
{
if
(
argmax_h_low
>=
0
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_h_low
+
1
-
argmax_h
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_low
];
if
(
argmax_h_low
>=
0
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_h_low
+
1
-
argmax_h
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_high
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_h
-
argmax_h_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_low
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_h
-
argmax_h_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_high
];
}
return
weight
;
}
template
<
typename
scalar_t
>
__global__
void
deformable_im2col_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_im
,
const
scalar_t
*
data_offset
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
num_channels
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
data_col
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
// index index of output matrix
const
int
w_col
=
index
%
width_col
;
const
int
h_col
=
(
index
/
width_col
)
%
height_col
;
const
int
b_col
=
(
index
/
width_col
/
height_col
)
%
batch_size
;
const
int
c_im
=
(
index
/
width_col
/
height_col
)
/
batch_size
;
const
int
c_col
=
c_im
*
kernel_h
*
kernel_w
;
// compute deformable group index
const
int
deformable_group_index
=
c_im
/
channel_per_deformable_group
;
const
int
h_in
=
h_col
*
stride_h
-
pad_h
;
const
int
w_in
=
w_col
*
stride_w
-
pad_w
;
scalar_t
*
data_col_ptr
=
data_col
+
((
c_col
*
batch_size
+
b_col
)
*
height_col
+
h_col
)
*
width_col
+
w_col
;
//const scalar_t* data_im_ptr = data_im + ((b_col * num_channels + c_im) * height + h_in) * width + w_in;
const
scalar_t
*
data_im_ptr
=
data_im
+
(
b_col
*
num_channels
+
c_im
)
*
height
*
width
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b_col
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
for
(
int
i
=
0
;
i
<
kernel_h
;
++
i
)
{
for
(
int
j
=
0
;
j
<
kernel_w
;
++
j
)
{
const
int
data_offset_h_ptr
=
((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_col
)
*
width_col
+
w_col
;
const
int
data_offset_w_ptr
=
((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_col
)
*
width_col
+
w_col
;
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
scalar_t
val
=
static_cast
<
scalar_t
>
(
0
);
const
scalar_t
h_im
=
h_in
+
i
*
dilation_h
+
offset_h
;
const
scalar_t
w_im
=
w_in
+
j
*
dilation_w
+
offset_w
;
if
(
h_im
>
-
1
&&
w_im
>
-
1
&&
h_im
<
height
&&
w_im
<
width
)
{
//const scalar_t map_h = i * dilation_h + offset_h;
//const scalar_t map_w = j * dilation_w + offset_w;
//const int cur_height = height - h_in;
//const int cur_width = width - w_in;
//val = deformable_im2col_bilinear(data_im_ptr, width, cur_height, cur_width, map_h, map_w);
val
=
deformable_im2col_bilinear
(
data_im_ptr
,
width
,
height
,
width
,
h_im
,
w_im
);
}
*
data_col_ptr
=
val
;
data_col_ptr
+=
batch_size
*
height_col
*
width_col
;
}
}
}
}
void
deformable_im2col
(
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
data_col
)
{
// num_axes should be smaller than block size
// todo: check parallel_imgs is correctly passed in
int
height_col
=
(
height
+
2
*
pad_h
-
(
dilation_h
*
(
ksize_h
-
1
)
+
1
))
/
stride_h
+
1
;
int
width_col
=
(
width
+
2
*
pad_w
-
(
dilation_w
*
(
ksize_w
-
1
)
+
1
))
/
stride_w
+
1
;
int
num_kernels
=
channels
*
height_col
*
width_col
*
parallel_imgs
;
int
channel_per_deformable_group
=
channels
/
deformable_group
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_im
.
type
(),
"deformable_im2col_gpu"
,
([
&
]
{
const
scalar_t
*
data_im_
=
data_im
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
deformable_im2col_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_im_
,
data_offset_
,
height
,
width
,
ksize_h
,
ksize_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
parallel_imgs
,
channels
,
deformable_group
,
height_col
,
width_col
,
data_col_
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in deformable_im2col: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
template
<
typename
scalar_t
>
__global__
void
deformable_col2im_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_col
,
const
scalar_t
*
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
grad_im
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
const
int
j
=
(
index
/
width_col
/
height_col
/
batch_size
)
%
kernel_w
;
const
int
i
=
(
index
/
width_col
/
height_col
/
batch_size
/
kernel_w
)
%
kernel_h
;
const
int
c
=
index
/
width_col
/
height_col
/
batch_size
/
kernel_w
/
kernel_h
;
// compute the start and end of the output
const
int
deformable_group_index
=
c
/
channel_per_deformable_group
;
int
w_out
=
index
%
width_col
;
int
h_out
=
(
index
/
width_col
)
%
height_col
;
int
b
=
(
index
/
width_col
/
height_col
)
%
batch_size
;
int
w_in
=
w_out
*
stride_w
-
pad_w
;
int
h_in
=
h_out
*
stride_h
-
pad_h
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
int
data_offset_h_ptr
=
((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_out
)
*
width_col
+
w_out
;
const
int
data_offset_w_ptr
=
((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_out
)
*
width_col
+
w_out
;
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
const
scalar_t
cur_inv_h_data
=
h_in
+
i
*
dilation_h
+
offset_h
;
const
scalar_t
cur_inv_w_data
=
w_in
+
j
*
dilation_w
+
offset_w
;
const
scalar_t
cur_top_grad
=
data_col
[
index
];
const
int
cur_h
=
(
int
)
cur_inv_h_data
;
const
int
cur_w
=
(
int
)
cur_inv_w_data
;
for
(
int
dy
=
-
2
;
dy
<=
2
;
dy
++
)
{
for
(
int
dx
=
-
2
;
dx
<=
2
;
dx
++
)
{
if
(
cur_h
+
dy
>=
0
&&
cur_h
+
dy
<
height
&&
cur_w
+
dx
>=
0
&&
cur_w
+
dx
<
width
&&
abs
(
cur_inv_h_data
-
(
cur_h
+
dy
))
<
1
&&
abs
(
cur_inv_w_data
-
(
cur_w
+
dx
))
<
1
)
{
int
cur_bottom_grad_pos
=
((
b
*
channels
+
c
)
*
height
+
cur_h
+
dy
)
*
width
+
cur_w
+
dx
;
scalar_t
weight
=
get_gradient_weight
(
cur_inv_h_data
,
cur_inv_w_data
,
cur_h
+
dy
,
cur_w
+
dx
,
height
,
width
);
atomicAdd
(
grad_im
+
cur_bottom_grad_pos
,
weight
*
cur_top_grad
);
}
}
}
}
}
void
deformable_col2im
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
grad_im
)
{
// todo: make sure parallel_imgs is passed in correctly
int
height_col
=
(
height
+
2
*
pad_h
-
(
dilation_h
*
(
ksize_h
-
1
)
+
1
))
/
stride_h
+
1
;
int
width_col
=
(
width
+
2
*
pad_w
-
(
dilation_w
*
(
ksize_w
-
1
)
+
1
))
/
stride_w
+
1
;
int
num_kernels
=
channels
*
ksize_h
*
ksize_w
*
height_col
*
width_col
*
parallel_imgs
;
int
channel_per_deformable_group
=
channels
/
deformable_group
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_col
.
type
(),
"deformable_col2im_gpu"
,
([
&
]
{
const
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
scalar_t
*
grad_im_
=
grad_im
.
data
<
scalar_t
>
();
deformable_col2im_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_col_
,
data_offset_
,
channels
,
height
,
width
,
ksize_h
,
ksize_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
parallel_imgs
,
deformable_group
,
height_col
,
width_col
,
grad_im_
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in deformable_col2im: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
template
<
typename
scalar_t
>
__global__
void
deformable_col2im_coord_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_col
,
const
scalar_t
*
data_im
,
const
scalar_t
*
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
offset_channels
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
grad_offset
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
scalar_t
val
=
0
;
int
w
=
index
%
width_col
;
int
h
=
(
index
/
width_col
)
%
height_col
;
int
c
=
(
index
/
width_col
/
height_col
)
%
offset_channels
;
int
b
=
(
index
/
width_col
/
height_col
)
/
offset_channels
;
// compute the start and end of the output
const
int
deformable_group_index
=
c
/
(
2
*
kernel_h
*
kernel_w
);
const
int
col_step
=
kernel_h
*
kernel_w
;
int
cnt
=
0
;
const
scalar_t
*
data_col_ptr
=
data_col
+
deformable_group_index
*
channel_per_deformable_group
*
batch_size
*
width_col
*
height_col
;
const
scalar_t
*
data_im_ptr
=
data_im
+
(
b
*
deformable_group
+
deformable_group_index
)
*
channel_per_deformable_group
/
kernel_h
/
kernel_w
*
height
*
width
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
int
offset_c
=
c
-
deformable_group_index
*
2
*
kernel_h
*
kernel_w
;
for
(
int
col_c
=
(
offset_c
/
2
);
col_c
<
channel_per_deformable_group
;
col_c
+=
col_step
)
{
const
int
col_pos
=
(((
col_c
*
batch_size
+
b
)
*
height_col
)
+
h
)
*
width_col
+
w
;
const
int
bp_dir
=
offset_c
%
2
;
int
j
=
(
col_pos
/
width_col
/
height_col
/
batch_size
)
%
kernel_w
;
int
i
=
(
col_pos
/
width_col
/
height_col
/
batch_size
/
kernel_w
)
%
kernel_h
;
int
w_out
=
col_pos
%
width_col
;
int
h_out
=
(
col_pos
/
width_col
)
%
height_col
;
int
w_in
=
w_out
*
stride_w
-
pad_w
;
int
h_in
=
h_out
*
stride_h
-
pad_h
;
const
int
data_offset_h_ptr
=
(((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_out
)
*
width_col
+
w_out
);
const
int
data_offset_w_ptr
=
(((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_out
)
*
width_col
+
w_out
);
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
scalar_t
inv_h
=
h_in
+
i
*
dilation_h
+
offset_h
;
scalar_t
inv_w
=
w_in
+
j
*
dilation_w
+
offset_w
;
if
(
inv_h
<=
-
1
||
inv_w
<=
-
1
||
inv_h
>=
height
||
inv_w
>=
width
)
{
inv_h
=
inv_w
=
-
2
;
}
const
scalar_t
weight
=
get_coordinate_weight
(
inv_h
,
inv_w
,
height
,
width
,
data_im_ptr
+
cnt
*
height
*
width
,
width
,
bp_dir
);
val
+=
weight
*
data_col_ptr
[
col_pos
];
cnt
+=
1
;
}
grad_offset
[
index
]
=
val
;
}
}
void
deformable_col2im_coord
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
ksize_h
,
const
int
ksize_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
parallel_imgs
,
const
int
deformable_group
,
at
::
Tensor
grad_offset
)
{
int
height_col
=
(
height
+
2
*
pad_h
-
(
dilation_h
*
(
ksize_h
-
1
)
+
1
))
/
stride_h
+
1
;
int
width_col
=
(
width
+
2
*
pad_w
-
(
dilation_w
*
(
ksize_w
-
1
)
+
1
))
/
stride_w
+
1
;
int
num_kernels
=
height_col
*
width_col
*
2
*
ksize_h
*
ksize_w
*
deformable_group
*
parallel_imgs
;
int
channel_per_deformable_group
=
channels
*
ksize_h
*
ksize_w
/
deformable_group
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_col
.
type
(),
"deformable_col2im_coord_gpu"
,
([
&
]
{
const
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
const
scalar_t
*
data_im_
=
data_im
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
scalar_t
*
grad_offset_
=
grad_offset
.
data
<
scalar_t
>
();
deformable_col2im_coord_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_col_
,
data_im_
,
data_offset_
,
channels
,
height
,
width
,
ksize_h
,
ksize_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
parallel_imgs
,
2
*
ksize_h
*
ksize_w
*
deformable_group
,
deformable_group
,
height_col
,
width_col
,
grad_offset_
);
}));
}
mmdet/ops/dcn/src/deform_psroi_pooling_cuda.cu
0 → 100644
View file @
ef86c404
/*!
* Copyright (c) 2017 Microsoft
* Licensed under The MIT License [see LICENSE for details]
* \file deformable_psroi_pooling.cu
* \brief
* \author Yi Li, Guodong Zhang, Jifeng Dai
*/
/***************** Adapted by Charles Shang *********************/
#include <ATen/ATen.h>
#include <THC/THCAtomics.cuh>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using
namespace
at
;
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
const
int
CUDA_NUM_THREADS
=
1024
;
inline
int
GET_BLOCKS
(
const
int
N
)
{
return
(
N
+
CUDA_NUM_THREADS
-
1
)
/
CUDA_NUM_THREADS
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
bilinear_interp
(
const
scalar_t
*
data
,
const
scalar_t
x
,
const
scalar_t
y
,
const
int
width
,
const
int
height
)
{
int
x1
=
floor
(
x
);
int
x2
=
ceil
(
x
);
int
y1
=
floor
(
y
);
int
y2
=
ceil
(
y
);
scalar_t
dist_x
=
(
scalar_t
)(
x
-
x1
);
scalar_t
dist_y
=
(
scalar_t
)(
y
-
y1
);
scalar_t
value11
=
data
[
y1
*
width
+
x1
];
scalar_t
value12
=
data
[
y2
*
width
+
x1
];
scalar_t
value21
=
data
[
y1
*
width
+
x2
];
scalar_t
value22
=
data
[
y2
*
width
+
x2
];
scalar_t
value
=
(
1
-
dist_x
)
*
(
1
-
dist_y
)
*
value11
+
(
1
-
dist_x
)
*
dist_y
*
value12
+
dist_x
*
(
1
-
dist_y
)
*
value21
+
dist_x
*
dist_y
*
value22
;
return
value
;
}
template
<
typename
scalar_t
>
__global__
void
DeformablePSROIPoolForwardKernel
(
const
int
count
,
const
scalar_t
*
bottom_data
,
const
scalar_t
spatial_scale
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
pooled_height
,
const
int
pooled_width
,
const
scalar_t
*
bottom_rois
,
const
scalar_t
*
bottom_trans
,
const
int
no_trans
,
const
scalar_t
trans_std
,
const
int
sample_per_part
,
const
int
output_dim
,
const
int
group_size
,
const
int
part_size
,
const
int
num_classes
,
const
int
channels_each_class
,
scalar_t
*
top_data
,
scalar_t
*
top_count
)
{
CUDA_KERNEL_LOOP
(
index
,
count
)
{
// The output is in order (n, ctop, ph, pw)
int
pw
=
index
%
pooled_width
;
int
ph
=
(
index
/
pooled_width
)
%
pooled_height
;
int
ctop
=
(
index
/
pooled_width
/
pooled_height
)
%
output_dim
;
int
n
=
index
/
pooled_width
/
pooled_height
/
output_dim
;
// [start, end) interval for spatial sampling
const
scalar_t
*
offset_bottom_rois
=
bottom_rois
+
n
*
5
;
int
roi_batch_ind
=
offset_bottom_rois
[
0
];
scalar_t
roi_start_w
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
1
]))
*
spatial_scale
-
0.5
;
scalar_t
roi_start_h
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
2
]))
*
spatial_scale
-
0.5
;
scalar_t
roi_end_w
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
3
])
+
1.
)
*
spatial_scale
-
0.5
;
scalar_t
roi_end_h
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
4
])
+
1.
)
*
spatial_scale
-
0.5
;
// Force too small ROIs to be 1x1
scalar_t
roi_width
=
max
(
roi_end_w
-
roi_start_w
,
0.1
);
//avoid 0
scalar_t
roi_height
=
max
(
roi_end_h
-
roi_start_h
,
0.1
);
// Compute w and h at bottom
scalar_t
bin_size_h
=
roi_height
/
(
scalar_t
)(
pooled_height
);
scalar_t
bin_size_w
=
roi_width
/
(
scalar_t
)(
pooled_width
);
scalar_t
sub_bin_size_h
=
bin_size_h
/
(
scalar_t
)(
sample_per_part
);
scalar_t
sub_bin_size_w
=
bin_size_w
/
(
scalar_t
)(
sample_per_part
);
int
part_h
=
floor
((
scalar_t
)(
ph
)
/
pooled_height
*
part_size
);
int
part_w
=
floor
((
scalar_t
)(
pw
)
/
pooled_width
*
part_size
);
int
class_id
=
ctop
/
channels_each_class
;
scalar_t
trans_x
=
no_trans
?
(
scalar_t
)(
0
)
:
bottom_trans
[(((
n
*
num_classes
+
class_id
)
*
2
)
*
part_size
+
part_h
)
*
part_size
+
part_w
]
*
(
scalar_t
)
trans_std
;
scalar_t
trans_y
=
no_trans
?
(
scalar_t
)(
0
)
:
bottom_trans
[(((
n
*
num_classes
+
class_id
)
*
2
+
1
)
*
part_size
+
part_h
)
*
part_size
+
part_w
]
*
(
scalar_t
)
trans_std
;
scalar_t
wstart
=
(
scalar_t
)(
pw
)
*
bin_size_w
+
roi_start_w
;
wstart
+=
trans_x
*
roi_width
;
scalar_t
hstart
=
(
scalar_t
)(
ph
)
*
bin_size_h
+
roi_start_h
;
hstart
+=
trans_y
*
roi_height
;
scalar_t
sum
=
0
;
int
count
=
0
;
int
gw
=
floor
((
scalar_t
)(
pw
)
*
group_size
/
pooled_width
);
int
gh
=
floor
((
scalar_t
)(
ph
)
*
group_size
/
pooled_height
);
gw
=
min
(
max
(
gw
,
0
),
group_size
-
1
);
gh
=
min
(
max
(
gh
,
0
),
group_size
-
1
);
const
scalar_t
*
offset_bottom_data
=
bottom_data
+
(
roi_batch_ind
*
channels
)
*
height
*
width
;
for
(
int
ih
=
0
;
ih
<
sample_per_part
;
ih
++
)
{
for
(
int
iw
=
0
;
iw
<
sample_per_part
;
iw
++
)
{
scalar_t
w
=
wstart
+
iw
*
sub_bin_size_w
;
scalar_t
h
=
hstart
+
ih
*
sub_bin_size_h
;
// bilinear interpolation
if
(
w
<
-
0.5
||
w
>
width
-
0.5
||
h
<
-
0.5
||
h
>
height
-
0.5
)
{
continue
;
}
w
=
min
(
max
(
w
,
0.
),
width
-
1.
);
h
=
min
(
max
(
h
,
0.
),
height
-
1.
);
int
c
=
(
ctop
*
group_size
+
gh
)
*
group_size
+
gw
;
scalar_t
val
=
bilinear_interp
(
offset_bottom_data
+
c
*
height
*
width
,
w
,
h
,
width
,
height
);
sum
+=
val
;
count
++
;
}
}
top_data
[
index
]
=
count
==
0
?
(
scalar_t
)(
0
)
:
sum
/
count
;
top_count
[
index
]
=
count
;
}
}
template
<
typename
scalar_t
>
__global__
void
DeformablePSROIPoolBackwardAccKernel
(
const
int
count
,
const
scalar_t
*
top_diff
,
const
scalar_t
*
top_count
,
const
int
num_rois
,
const
scalar_t
spatial_scale
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
pooled_height
,
const
int
pooled_width
,
const
int
output_dim
,
scalar_t
*
bottom_data_diff
,
scalar_t
*
bottom_trans_diff
,
const
scalar_t
*
bottom_data
,
const
scalar_t
*
bottom_rois
,
const
scalar_t
*
bottom_trans
,
const
int
no_trans
,
const
scalar_t
trans_std
,
const
int
sample_per_part
,
const
int
group_size
,
const
int
part_size
,
const
int
num_classes
,
const
int
channels_each_class
)
{
CUDA_KERNEL_LOOP
(
index
,
count
)
{
// The output is in order (n, ctop, ph, pw)
int
pw
=
index
%
pooled_width
;
int
ph
=
(
index
/
pooled_width
)
%
pooled_height
;
int
ctop
=
(
index
/
pooled_width
/
pooled_height
)
%
output_dim
;
int
n
=
index
/
pooled_width
/
pooled_height
/
output_dim
;
// [start, end) interval for spatial sampling
const
scalar_t
*
offset_bottom_rois
=
bottom_rois
+
n
*
5
;
int
roi_batch_ind
=
offset_bottom_rois
[
0
];
scalar_t
roi_start_w
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
1
]))
*
spatial_scale
-
0.5
;
scalar_t
roi_start_h
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
2
]))
*
spatial_scale
-
0.5
;
scalar_t
roi_end_w
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
3
])
+
1.
)
*
spatial_scale
-
0.5
;
scalar_t
roi_end_h
=
(
scalar_t
)(
round
(
offset_bottom_rois
[
4
])
+
1.
)
*
spatial_scale
-
0.5
;
// Force too small ROIs to be 1x1
scalar_t
roi_width
=
max
(
roi_end_w
-
roi_start_w
,
0.1
);
//avoid 0
scalar_t
roi_height
=
max
(
roi_end_h
-
roi_start_h
,
0.1
);
// Compute w and h at bottom
scalar_t
bin_size_h
=
roi_height
/
(
scalar_t
)(
pooled_height
);
scalar_t
bin_size_w
=
roi_width
/
(
scalar_t
)(
pooled_width
);
scalar_t
sub_bin_size_h
=
bin_size_h
/
(
scalar_t
)(
sample_per_part
);
scalar_t
sub_bin_size_w
=
bin_size_w
/
(
scalar_t
)(
sample_per_part
);
int
part_h
=
floor
((
scalar_t
)(
ph
)
/
pooled_height
*
part_size
);
int
part_w
=
floor
((
scalar_t
)(
pw
)
/
pooled_width
*
part_size
);
int
class_id
=
ctop
/
channels_each_class
;
scalar_t
trans_x
=
no_trans
?
(
scalar_t
)(
0
)
:
bottom_trans
[(((
n
*
num_classes
+
class_id
)
*
2
)
*
part_size
+
part_h
)
*
part_size
+
part_w
]
*
(
scalar_t
)
trans_std
;
scalar_t
trans_y
=
no_trans
?
(
scalar_t
)(
0
)
:
bottom_trans
[(((
n
*
num_classes
+
class_id
)
*
2
+
1
)
*
part_size
+
part_h
)
*
part_size
+
part_w
]
*
(
scalar_t
)
trans_std
;
scalar_t
wstart
=
(
scalar_t
)(
pw
)
*
bin_size_w
+
roi_start_w
;
wstart
+=
trans_x
*
roi_width
;
scalar_t
hstart
=
(
scalar_t
)(
ph
)
*
bin_size_h
+
roi_start_h
;
hstart
+=
trans_y
*
roi_height
;
if
(
top_count
[
index
]
<=
0
)
{
continue
;
}
scalar_t
diff_val
=
top_diff
[
index
]
/
top_count
[
index
];
const
scalar_t
*
offset_bottom_data
=
bottom_data
+
roi_batch_ind
*
channels
*
height
*
width
;
scalar_t
*
offset_bottom_data_diff
=
bottom_data_diff
+
roi_batch_ind
*
channels
*
height
*
width
;
int
gw
=
floor
((
scalar_t
)(
pw
)
*
group_size
/
pooled_width
);
int
gh
=
floor
((
scalar_t
)(
ph
)
*
group_size
/
pooled_height
);
gw
=
min
(
max
(
gw
,
0
),
group_size
-
1
);
gh
=
min
(
max
(
gh
,
0
),
group_size
-
1
);
for
(
int
ih
=
0
;
ih
<
sample_per_part
;
ih
++
)
{
for
(
int
iw
=
0
;
iw
<
sample_per_part
;
iw
++
)
{
scalar_t
w
=
wstart
+
iw
*
sub_bin_size_w
;
scalar_t
h
=
hstart
+
ih
*
sub_bin_size_h
;
// bilinear interpolation
if
(
w
<
-
0.5
||
w
>
width
-
0.5
||
h
<
-
0.5
||
h
>
height
-
0.5
)
{
continue
;
}
w
=
min
(
max
(
w
,
0.
),
width
-
1.
);
h
=
min
(
max
(
h
,
0.
),
height
-
1.
);
int
c
=
(
ctop
*
group_size
+
gh
)
*
group_size
+
gw
;
// backward on feature
int
x0
=
floor
(
w
);
int
x1
=
ceil
(
w
);
int
y0
=
floor
(
h
);
int
y1
=
ceil
(
h
);
scalar_t
dist_x
=
w
-
x0
,
dist_y
=
h
-
y0
;
scalar_t
q00
=
(
1
-
dist_x
)
*
(
1
-
dist_y
);
scalar_t
q01
=
(
1
-
dist_x
)
*
dist_y
;
scalar_t
q10
=
dist_x
*
(
1
-
dist_y
);
scalar_t
q11
=
dist_x
*
dist_y
;
int
bottom_index_base
=
c
*
height
*
width
;
atomicAdd
(
offset_bottom_data_diff
+
bottom_index_base
+
y0
*
width
+
x0
,
q00
*
diff_val
);
atomicAdd
(
offset_bottom_data_diff
+
bottom_index_base
+
y1
*
width
+
x0
,
q01
*
diff_val
);
atomicAdd
(
offset_bottom_data_diff
+
bottom_index_base
+
y0
*
width
+
x1
,
q10
*
diff_val
);
atomicAdd
(
offset_bottom_data_diff
+
bottom_index_base
+
y1
*
width
+
x1
,
q11
*
diff_val
);
if
(
no_trans
)
{
continue
;
}
scalar_t
U00
=
offset_bottom_data
[
bottom_index_base
+
y0
*
width
+
x0
];
scalar_t
U01
=
offset_bottom_data
[
bottom_index_base
+
y1
*
width
+
x0
];
scalar_t
U10
=
offset_bottom_data
[
bottom_index_base
+
y0
*
width
+
x1
];
scalar_t
U11
=
offset_bottom_data
[
bottom_index_base
+
y1
*
width
+
x1
];
scalar_t
diff_x
=
(
U11
*
dist_y
+
U10
*
(
1
-
dist_y
)
-
U01
*
dist_y
-
U00
*
(
1
-
dist_y
))
*
trans_std
*
diff_val
;
diff_x
*=
roi_width
;
scalar_t
diff_y
=
(
U11
*
dist_x
+
U01
*
(
1
-
dist_x
)
-
U10
*
dist_x
-
U00
*
(
1
-
dist_x
))
*
trans_std
*
diff_val
;
diff_y
*=
roi_height
;
atomicAdd
(
bottom_trans_diff
+
(((
n
*
num_classes
+
class_id
)
*
2
)
*
part_size
+
part_h
)
*
part_size
+
part_w
,
diff_x
);
atomicAdd
(
bottom_trans_diff
+
(((
n
*
num_classes
+
class_id
)
*
2
+
1
)
*
part_size
+
part_h
)
*
part_size
+
part_w
,
diff_y
);
}
}
}
}
void
DeformablePSROIPoolForward
(
const
at
::
Tensor
data
,
const
at
::
Tensor
bbox
,
const
at
::
Tensor
trans
,
at
::
Tensor
out
,
at
::
Tensor
top_count
,
const
int
batch
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
num_bbox
,
const
int
channels_trans
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
)
{
const
int
pooled_height
=
pooled_size
;
const
int
pooled_width
=
pooled_size
;
const
int
count
=
num_bbox
*
output_dim
*
pooled_height
*
pooled_width
;
const
int
num_classes
=
no_trans
?
1
:
channels_trans
/
2
;
const
int
channels_each_class
=
no_trans
?
output_dim
:
output_dim
/
num_classes
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data
.
type
(),
"deformable_psroi_pool_forward"
,
([
&
]
{
const
scalar_t
*
bottom_data
=
data
.
data
<
scalar_t
>
();
const
scalar_t
*
bottom_rois
=
bbox
.
data
<
scalar_t
>
();
const
scalar_t
*
bottom_trans
=
no_trans
?
NULL
:
trans
.
data
<
scalar_t
>
();
scalar_t
*
top_data
=
out
.
data
<
scalar_t
>
();
scalar_t
*
top_count_data
=
top_count
.
data
<
scalar_t
>
();
DeformablePSROIPoolForwardKernel
<<<
GET_BLOCKS
(
count
),
CUDA_NUM_THREADS
>>>
(
count
,
bottom_data
,
(
scalar_t
)
spatial_scale
,
channels
,
height
,
width
,
pooled_height
,
pooled_width
,
bottom_rois
,
bottom_trans
,
no_trans
,
(
scalar_t
)
trans_std
,
sample_per_part
,
output_dim
,
group_size
,
part_size
,
num_classes
,
channels_each_class
,
top_data
,
top_count_data
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in DeformablePSROIPoolForward: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
void
DeformablePSROIPoolBackwardAcc
(
const
at
::
Tensor
out_grad
,
const
at
::
Tensor
data
,
const
at
::
Tensor
bbox
,
const
at
::
Tensor
trans
,
const
at
::
Tensor
top_count
,
at
::
Tensor
in_grad
,
at
::
Tensor
trans_grad
,
const
int
batch
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
num_bbox
,
const
int
channels_trans
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
)
{
// LOG(INFO) << "DeformablePSROIPoolBackward";
const
int
num_rois
=
num_bbox
;
const
int
pooled_height
=
pooled_size
;
const
int
pooled_width
=
pooled_size
;
const
int
count
=
num_bbox
*
output_dim
*
pooled_height
*
pooled_width
;
const
int
num_classes
=
no_trans
?
1
:
channels_trans
/
2
;
const
int
channels_each_class
=
no_trans
?
output_dim
:
output_dim
/
num_classes
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
out_grad
.
type
(),
"deformable_psroi_pool_backward_acc"
,
([
&
]
{
const
scalar_t
*
top_diff
=
out_grad
.
data
<
scalar_t
>
();
const
scalar_t
*
bottom_data
=
data
.
data
<
scalar_t
>
();
const
scalar_t
*
bottom_rois
=
bbox
.
data
<
scalar_t
>
();
const
scalar_t
*
bottom_trans
=
no_trans
?
NULL
:
trans
.
data
<
scalar_t
>
();
scalar_t
*
bottom_data_diff
=
in_grad
.
data
<
scalar_t
>
();
scalar_t
*
bottom_trans_diff
=
no_trans
?
NULL
:
trans_grad
.
data
<
scalar_t
>
();
const
scalar_t
*
top_count_data
=
top_count
.
data
<
scalar_t
>
();
DeformablePSROIPoolBackwardAccKernel
<<<
GET_BLOCKS
(
count
),
CUDA_NUM_THREADS
>>>
(
count
,
top_diff
,
top_count_data
,
num_rois
,
(
scalar_t
)
spatial_scale
,
channels
,
height
,
width
,
pooled_height
,
pooled_width
,
output_dim
,
bottom_data_diff
,
bottom_trans_diff
,
bottom_data
,
bottom_rois
,
bottom_trans
,
no_trans
,
(
scalar_t
)
trans_std
,
sample_per_part
,
group_size
,
part_size
,
num_classes
,
channels_each_class
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in DeformablePSROIPoolForward: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
\ No newline at end of file
mmdet/ops/dcn/src/modulated_dcn_cuda.cpp
0 → 100644
View file @
ef86c404
#include <torch/torch.h>
#include <cmath>
#include <vector>
// author: Charles Shang
// https://github.com/torch/cunn/blob/master/lib/THCUNN/generic/SpatialConvolutionMM.cu
void
modulated_deformable_im2col_cuda
(
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kenerl_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
data_col
);
void
modulated_deformable_col2im_cuda
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kenerl_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
grad_im
);
void
modulated_deformable_col2im_coord_cuda
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kenerl_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
grad_offset
,
at
::
Tensor
grad_mask
);
void
DeformablePSROIPoolForward
(
const
at
::
Tensor
data
,
const
at
::
Tensor
bbox
,
const
at
::
Tensor
trans
,
at
::
Tensor
out
,
at
::
Tensor
top_count
,
const
int
batch
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
num_bbox
,
const
int
channels_trans
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
);
void
DeformablePSROIPoolBackwardAcc
(
const
at
::
Tensor
out_grad
,
const
at
::
Tensor
data
,
const
at
::
Tensor
bbox
,
const
at
::
Tensor
trans
,
const
at
::
Tensor
top_count
,
at
::
Tensor
in_grad
,
at
::
Tensor
trans_grad
,
const
int
batch
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
num_bbox
,
const
int
channels_trans
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
);
void
modulated_deform_conv_cuda_forward
(
at
::
Tensor
input
,
at
::
Tensor
weight
,
at
::
Tensor
bias
,
at
::
Tensor
ones
,
at
::
Tensor
offset
,
at
::
Tensor
mask
,
at
::
Tensor
output
,
at
::
Tensor
columns
,
int
kernel_h
,
int
kernel_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
)
{
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 8, input, weight, bias, ones, offset, mask, output, columns));
AT_CHECK
(
input
.
is_contiguous
(),
"input tensor has to be contiguous"
);
AT_CHECK
(
weight
.
is_contiguous
(),
"weight tensor has to be contiguous"
);
const
int
batch
=
input
.
size
(
0
);
const
int
channels
=
input
.
size
(
1
);
const
int
height
=
input
.
size
(
2
);
const
int
width
=
input
.
size
(
3
);
const
int
channels_out
=
weight
.
size
(
0
);
const
int
channels_kernel
=
weight
.
size
(
1
);
const
int
kernel_h_
=
weight
.
size
(
2
);
const
int
kernel_w_
=
weight
.
size
(
3
);
if
(
kernel_h_
!=
kernel_h
||
kernel_w_
!=
kernel_w
)
AT_ERROR
(
"Input shape and kernel shape wont match: (%d x %d vs %d x %d)."
,
kernel_h_
,
kernel_w
,
kernel_h_
,
kernel_w_
);
if
(
channels
!=
channels_kernel
)
AT_ERROR
(
"Input shape and kernel channels wont match: (%d vs %d)."
,
channels
,
channels_kernel
);
const
int
height_out
=
(
height
+
2
*
pad_h
-
(
dilation_h
*
(
kernel_h
-
1
)
+
1
))
/
stride_h
+
1
;
const
int
width_out
=
(
width
+
2
*
pad_w
-
(
dilation_w
*
(
kernel_w
-
1
)
+
1
))
/
stride_w
+
1
;
if
(
ones
.
ndimension
()
!=
2
||
ones
.
size
(
0
)
*
ones
.
size
(
1
)
<
height_out
*
width_out
)
{
// Resize plane and fill with ones...
ones
=
at
::
ones
({
height_out
,
width_out
},
input
.
type
());
}
// resize output
output
=
output
.
view
({
batch
,
channels_out
,
height_out
,
width_out
});
// resize temporary columns
columns
=
at
::
zeros
({
channels
*
kernel_h
*
kernel_w
,
1
*
height_out
*
width_out
},
input
.
type
());
for
(
int
b
=
0
;
b
<
batch
;
b
++
)
{
// Do Bias first:
// M,N,K are dims of matrix A and B
// (see http://docs.nvidia.com/cuda/cublas/#cublas-lt-t-gt-gemm)
// (N x 1) (1 x M)
output
[
b
]
=
output
[
b
].
flatten
(
1
).
addmm_
(
bias
.
view
({
-
1
,
1
}),
ones
.
view
({
1
,
-
1
}),
0.0
f
,
1.0
f
).
view_as
(
output
[
b
]);
modulated_deformable_im2col_cuda
(
input
[
b
],
offset
[
b
],
mask
[
b
],
1
,
channels
,
height
,
width
,
height_out
,
width_out
,
kernel_h
,
kernel_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
deformable_group
,
columns
);
//(k * m) x (m * n)
// Y = WC
output
[
b
]
=
output
[
b
].
flatten
(
1
).
addmm_
(
weight
.
flatten
(
1
),
columns
).
view_as
(
output
[
b
]);
}
}
void
modulated_deform_conv_cuda_backward
(
at
::
Tensor
input
,
at
::
Tensor
weight
,
at
::
Tensor
bias
,
at
::
Tensor
ones
,
at
::
Tensor
offset
,
at
::
Tensor
mask
,
at
::
Tensor
columns
,
at
::
Tensor
grad_input
,
at
::
Tensor
grad_weight
,
at
::
Tensor
grad_bias
,
at
::
Tensor
grad_offset
,
at
::
Tensor
grad_mask
,
at
::
Tensor
grad_output
,
int
kernel_h
,
int
kernel_w
,
int
stride_h
,
int
stride_w
,
int
pad_h
,
int
pad_w
,
int
dilation_h
,
int
dilation_w
,
int
deformable_group
)
{
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 13, input, weight, bias, ones, offset, mask, columns,
// grad_input, grad_weight, grad_bias, grad_offset, grad_mask, grad_output));
AT_CHECK
(
input
.
is_contiguous
(),
"input tensor has to be contiguous"
);
AT_CHECK
(
weight
.
is_contiguous
(),
"weight tensor has to be contiguous"
);
const
int
batch
=
input
.
size
(
0
);
const
int
channels
=
input
.
size
(
1
);
const
int
height
=
input
.
size
(
2
);
const
int
width
=
input
.
size
(
3
);
const
int
channels_kernel
=
weight
.
size
(
1
);
const
int
kernel_h_
=
weight
.
size
(
2
);
const
int
kernel_w_
=
weight
.
size
(
3
);
if
(
kernel_h_
!=
kernel_h
||
kernel_w_
!=
kernel_w
)
AT_ERROR
(
"Input shape and kernel shape wont match: (%d x %d vs %d x %d)."
,
kernel_h_
,
kernel_w
,
kernel_h_
,
kernel_w_
);
if
(
channels
!=
channels_kernel
)
AT_ERROR
(
"Input shape and kernel channels wont match: (%d vs %d)."
,
channels
,
channels_kernel
);
const
int
height_out
=
(
height
+
2
*
pad_h
-
(
dilation_h
*
(
kernel_h
-
1
)
+
1
))
/
stride_h
+
1
;
const
int
width_out
=
(
width
+
2
*
pad_w
-
(
dilation_w
*
(
kernel_w
-
1
)
+
1
))
/
stride_w
+
1
;
if
(
ones
.
ndimension
()
!=
2
||
ones
.
size
(
0
)
*
ones
.
size
(
1
)
<
height_out
*
width_out
)
{
// Resize plane and fill with ones...
ones
=
at
::
ones
({
height_out
,
width_out
},
input
.
type
());
}
grad_input
=
grad_input
.
view
({
batch
,
channels
,
height
,
width
});
columns
=
at
::
zeros
({
channels
*
kernel_h
*
kernel_w
,
height_out
*
width_out
},
input
.
type
());
for
(
int
b
=
0
;
b
<
batch
;
b
++
)
{
columns
.
addmm_
(
weight
.
flatten
(
1
).
transpose
(
0
,
1
),
grad_output
[
b
].
flatten
(
1
),
0.0
f
,
1.0
f
);
// gradient w.r.t. input coordinate data
modulated_deformable_col2im_coord_cuda
(
columns
,
input
[
b
],
offset
[
b
],
mask
[
b
],
1
,
channels
,
height
,
width
,
height_out
,
width_out
,
kernel_h
,
kernel_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
deformable_group
,
grad_offset
[
b
],
grad_mask
[
b
]);
// gradient w.r.t. input data
modulated_deformable_col2im_cuda
(
columns
,
offset
[
b
],
mask
[
b
],
1
,
channels
,
height
,
width
,
height_out
,
width_out
,
kernel_h
,
kernel_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
deformable_group
,
grad_input
[
b
]);
// gradient w.r.t. weight, dWeight should accumulate across the batch and group
modulated_deformable_im2col_cuda
(
input
[
b
],
offset
[
b
],
mask
[
b
],
1
,
channels
,
height
,
width
,
height_out
,
width_out
,
kernel_h
,
kernel_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
deformable_group
,
columns
);
grad_weight
=
grad_weight
.
flatten
(
1
).
addmm_
(
grad_output
[
b
].
flatten
(
1
),
columns
.
transpose
(
0
,
1
)).
view_as
(
grad_weight
);
grad_bias
=
grad_bias
.
view
({
-
1
,
1
}).
addmm_
(
grad_output
[
b
].
flatten
(
1
),
ones
.
view
({
-
1
,
1
})).
view
(
-
1
);
}
}
void
deform_psroi_pooling_cuda_forward
(
at
::
Tensor
input
,
at
::
Tensor
bbox
,
at
::
Tensor
trans
,
at
::
Tensor
out
,
at
::
Tensor
top_count
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
)
{
AT_CHECK
(
input
.
is_contiguous
(),
"input tensor has to be contiguous"
);
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 5, input, bbox, trans, out, top_count));
const
int
batch
=
input
.
size
(
0
);
const
int
channels
=
input
.
size
(
1
);
const
int
height
=
input
.
size
(
2
);
const
int
width
=
input
.
size
(
3
);
const
int
channels_trans
=
no_trans
?
2
:
trans
.
size
(
1
);
const
int
num_bbox
=
bbox
.
size
(
0
);
if
(
num_bbox
!=
out
.
size
(
0
))
AT_ERROR
(
"Output shape and bbox number wont match: (%d vs %d)."
,
out
.
size
(
0
),
num_bbox
);
DeformablePSROIPoolForward
(
input
,
bbox
,
trans
,
out
,
top_count
,
batch
,
channels
,
height
,
width
,
num_bbox
,
channels_trans
,
no_trans
,
spatial_scale
,
output_dim
,
group_size
,
pooled_size
,
part_size
,
sample_per_part
,
trans_std
);
}
void
deform_psroi_pooling_cuda_backward
(
at
::
Tensor
out_grad
,
at
::
Tensor
input
,
at
::
Tensor
bbox
,
at
::
Tensor
trans
,
at
::
Tensor
top_count
,
at
::
Tensor
input_grad
,
at
::
Tensor
trans_grad
,
const
int
no_trans
,
const
float
spatial_scale
,
const
int
output_dim
,
const
int
group_size
,
const
int
pooled_size
,
const
int
part_size
,
const
int
sample_per_part
,
const
float
trans_std
)
{
AT_CHECK
(
out_grad
.
is_contiguous
(),
"out_grad tensor has to be contiguous"
);
AT_CHECK
(
input
.
is_contiguous
(),
"input tensor has to be contiguous"
);
// THCAssertSameGPU(THCudaTensor_checkGPU(state, 7, input, bbox, trans, out_grad, top_count,
// input_grad, trans_grad));
const
int
batch
=
input
.
size
(
0
);
const
int
channels
=
input
.
size
(
1
);
const
int
height
=
input
.
size
(
2
);
const
int
width
=
input
.
size
(
3
);
const
int
channels_trans
=
no_trans
?
2
:
trans
.
size
(
1
);
const
int
num_bbox
=
bbox
.
size
(
0
);
if
(
num_bbox
!=
out_grad
.
size
(
0
))
AT_ERROR
(
"Output shape and bbox number wont match: (%d vs %d)."
,
out_grad
.
size
(
0
),
num_bbox
);
DeformablePSROIPoolBackwardAcc
(
out_grad
,
input
,
bbox
,
trans
,
top_count
,
input_grad
,
trans_grad
,
batch
,
channels
,
height
,
width
,
num_bbox
,
channels_trans
,
no_trans
,
spatial_scale
,
output_dim
,
group_size
,
pooled_size
,
part_size
,
sample_per_part
,
trans_std
);
}
PYBIND11_MODULE
(
TORCH_EXTENSION_NAME
,
m
)
{
m
.
def
(
"modulated_deform_conv_cuda_forward"
,
&
modulated_deform_conv_cuda_forward
,
"modulated deform conv forward (CUDA)"
);
m
.
def
(
"modulated_deform_conv_cuda_backward"
,
&
modulated_deform_conv_cuda_backward
,
"modulated deform conv backward (CUDA)"
);
m
.
def
(
"deform_psroi_pooling_cuda_forward"
,
&
deform_psroi_pooling_cuda_forward
,
"deform psroi pooling forward(CUDA)"
);
m
.
def
(
"deform_psroi_pooling_cuda_backward"
,
&
deform_psroi_pooling_cuda_backward
,
"deform psroi pooling backward(CUDA)"
);
}
\ No newline at end of file
mmdet/ops/dcn/src/modulated_deform_im2col_cuda.cu
0 → 100644
View file @
ef86c404
#include <ATen/ATen.h>
#include <THC/THCAtomics.cuh>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using
namespace
at
;
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
const
int
CUDA_NUM_THREADS
=
1024
;
inline
int
GET_BLOCKS
(
const
int
N
)
{
return
(
N
+
CUDA_NUM_THREADS
-
1
)
/
CUDA_NUM_THREADS
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
dmcn_im2col_bilinear
(
const
scalar_t
*
bottom_data
,
const
int
data_width
,
const
int
height
,
const
int
width
,
scalar_t
h
,
scalar_t
w
)
{
int
h_low
=
floor
(
h
);
int
w_low
=
floor
(
w
);
int
h_high
=
h_low
+
1
;
int
w_high
=
w_low
+
1
;
scalar_t
lh
=
h
-
h_low
;
scalar_t
lw
=
w
-
w_low
;
scalar_t
hh
=
1
-
lh
,
hw
=
1
-
lw
;
scalar_t
v1
=
0
;
if
(
h_low
>=
0
&&
w_low
>=
0
)
v1
=
bottom_data
[
h_low
*
data_width
+
w_low
];
scalar_t
v2
=
0
;
if
(
h_low
>=
0
&&
w_high
<=
width
-
1
)
v2
=
bottom_data
[
h_low
*
data_width
+
w_high
];
scalar_t
v3
=
0
;
if
(
h_high
<=
height
-
1
&&
w_low
>=
0
)
v3
=
bottom_data
[
h_high
*
data_width
+
w_low
];
scalar_t
v4
=
0
;
if
(
h_high
<=
height
-
1
&&
w_high
<=
width
-
1
)
v4
=
bottom_data
[
h_high
*
data_width
+
w_high
];
scalar_t
w1
=
hh
*
hw
,
w2
=
hh
*
lw
,
w3
=
lh
*
hw
,
w4
=
lh
*
lw
;
scalar_t
val
=
(
w1
*
v1
+
w2
*
v2
+
w3
*
v3
+
w4
*
v4
);
return
val
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
dmcn_get_gradient_weight
(
scalar_t
argmax_h
,
scalar_t
argmax_w
,
const
int
h
,
const
int
w
,
const
int
height
,
const
int
width
)
{
if
(
argmax_h
<=
-
1
||
argmax_h
>=
height
||
argmax_w
<=
-
1
||
argmax_w
>=
width
)
{
//empty
return
0
;
}
int
argmax_h_low
=
floor
(
argmax_h
);
int
argmax_w_low
=
floor
(
argmax_w
);
int
argmax_h_high
=
argmax_h_low
+
1
;
int
argmax_w_high
=
argmax_w_low
+
1
;
scalar_t
weight
=
0
;
if
(
h
==
argmax_h_low
&&
w
==
argmax_w_low
)
weight
=
(
h
+
1
-
argmax_h
)
*
(
w
+
1
-
argmax_w
);
if
(
h
==
argmax_h_low
&&
w
==
argmax_w_high
)
weight
=
(
h
+
1
-
argmax_h
)
*
(
argmax_w
+
1
-
w
);
if
(
h
==
argmax_h_high
&&
w
==
argmax_w_low
)
weight
=
(
argmax_h
+
1
-
h
)
*
(
w
+
1
-
argmax_w
);
if
(
h
==
argmax_h_high
&&
w
==
argmax_w_high
)
weight
=
(
argmax_h
+
1
-
h
)
*
(
argmax_w
+
1
-
w
);
return
weight
;
}
template
<
typename
scalar_t
>
__device__
scalar_t
dmcn_get_coordinate_weight
(
scalar_t
argmax_h
,
scalar_t
argmax_w
,
const
int
height
,
const
int
width
,
const
scalar_t
*
im_data
,
const
int
data_width
,
const
int
bp_dir
)
{
if
(
argmax_h
<=
-
1
||
argmax_h
>=
height
||
argmax_w
<=
-
1
||
argmax_w
>=
width
)
{
//empty
return
0
;
}
int
argmax_h_low
=
floor
(
argmax_h
);
int
argmax_w_low
=
floor
(
argmax_w
);
int
argmax_h_high
=
argmax_h_low
+
1
;
int
argmax_w_high
=
argmax_w_low
+
1
;
scalar_t
weight
=
0
;
if
(
bp_dir
==
0
)
{
if
(
argmax_h_low
>=
0
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_w_low
+
1
-
argmax_w
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_low
];
if
(
argmax_h_low
>=
0
&&
argmax_w_high
<=
width
-
1
)
weight
+=
-
1
*
(
argmax_w
-
argmax_w_low
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_high
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_low
>=
0
)
weight
+=
(
argmax_w_low
+
1
-
argmax_w
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_low
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_w
-
argmax_w_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_high
];
}
else
if
(
bp_dir
==
1
)
{
if
(
argmax_h_low
>=
0
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_h_low
+
1
-
argmax_h
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_low
];
if
(
argmax_h_low
>=
0
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_h_low
+
1
-
argmax_h
)
*
im_data
[
argmax_h_low
*
data_width
+
argmax_w_high
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_low
>=
0
)
weight
+=
-
1
*
(
argmax_h
-
argmax_h_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_low
];
if
(
argmax_h_high
<=
height
-
1
&&
argmax_w_high
<=
width
-
1
)
weight
+=
(
argmax_h
-
argmax_h_low
)
*
im_data
[
argmax_h_high
*
data_width
+
argmax_w_high
];
}
return
weight
;
}
template
<
typename
scalar_t
>
__global__
void
modulated_deformable_im2col_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_im
,
const
scalar_t
*
data_offset
,
const
scalar_t
*
data_mask
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
num_channels
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
data_col
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
// index index of output matrix
const
int
w_col
=
index
%
width_col
;
const
int
h_col
=
(
index
/
width_col
)
%
height_col
;
const
int
b_col
=
(
index
/
width_col
/
height_col
)
%
batch_size
;
const
int
c_im
=
(
index
/
width_col
/
height_col
)
/
batch_size
;
const
int
c_col
=
c_im
*
kernel_h
*
kernel_w
;
// compute deformable group index
const
int
deformable_group_index
=
c_im
/
channel_per_deformable_group
;
const
int
h_in
=
h_col
*
stride_h
-
pad_h
;
const
int
w_in
=
w_col
*
stride_w
-
pad_w
;
scalar_t
*
data_col_ptr
=
data_col
+
((
c_col
*
batch_size
+
b_col
)
*
height_col
+
h_col
)
*
width_col
+
w_col
;
//const float* data_im_ptr = data_im + ((b_col * num_channels + c_im) * height + h_in) * width + w_in;
const
scalar_t
*
data_im_ptr
=
data_im
+
(
b_col
*
num_channels
+
c_im
)
*
height
*
width
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b_col
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
scalar_t
*
data_mask_ptr
=
data_mask
+
(
b_col
*
deformable_group
+
deformable_group_index
)
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
for
(
int
i
=
0
;
i
<
kernel_h
;
++
i
)
{
for
(
int
j
=
0
;
j
<
kernel_w
;
++
j
)
{
const
int
data_offset_h_ptr
=
((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_col
)
*
width_col
+
w_col
;
const
int
data_offset_w_ptr
=
((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_col
)
*
width_col
+
w_col
;
const
int
data_mask_hw_ptr
=
((
i
*
kernel_w
+
j
)
*
height_col
+
h_col
)
*
width_col
+
w_col
;
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
const
scalar_t
mask
=
data_mask_ptr
[
data_mask_hw_ptr
];
scalar_t
val
=
static_cast
<
scalar_t
>
(
0
);
const
scalar_t
h_im
=
h_in
+
i
*
dilation_h
+
offset_h
;
const
scalar_t
w_im
=
w_in
+
j
*
dilation_w
+
offset_w
;
//if (h_im >= 0 && w_im >= 0 && h_im < height && w_im < width) {
if
(
h_im
>
-
1
&&
w_im
>
-
1
&&
h_im
<
height
&&
w_im
<
width
)
{
//const float map_h = i * dilation_h + offset_h;
//const float map_w = j * dilation_w + offset_w;
//const int cur_height = height - h_in;
//const int cur_width = width - w_in;
//val = dmcn_im2col_bilinear(data_im_ptr, width, cur_height, cur_width, map_h, map_w);
val
=
dmcn_im2col_bilinear
(
data_im_ptr
,
width
,
height
,
width
,
h_im
,
w_im
);
}
*
data_col_ptr
=
val
*
mask
;
data_col_ptr
+=
batch_size
*
height_col
*
width_col
;
//data_col_ptr += height_col * width_col;
}
}
}
}
template
<
typename
scalar_t
>
__global__
void
modulated_deformable_col2im_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_col
,
const
scalar_t
*
data_offset
,
const
scalar_t
*
data_mask
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
grad_im
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
const
int
j
=
(
index
/
width_col
/
height_col
/
batch_size
)
%
kernel_w
;
const
int
i
=
(
index
/
width_col
/
height_col
/
batch_size
/
kernel_w
)
%
kernel_h
;
const
int
c
=
index
/
width_col
/
height_col
/
batch_size
/
kernel_w
/
kernel_h
;
// compute the start and end of the output
const
int
deformable_group_index
=
c
/
channel_per_deformable_group
;
int
w_out
=
index
%
width_col
;
int
h_out
=
(
index
/
width_col
)
%
height_col
;
int
b
=
(
index
/
width_col
/
height_col
)
%
batch_size
;
int
w_in
=
w_out
*
stride_w
-
pad_w
;
int
h_in
=
h_out
*
stride_h
-
pad_h
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
scalar_t
*
data_mask_ptr
=
data_mask
+
(
b
*
deformable_group
+
deformable_group_index
)
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
int
data_offset_h_ptr
=
((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_out
)
*
width_col
+
w_out
;
const
int
data_offset_w_ptr
=
((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_out
)
*
width_col
+
w_out
;
const
int
data_mask_hw_ptr
=
((
i
*
kernel_w
+
j
)
*
height_col
+
h_out
)
*
width_col
+
w_out
;
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
const
scalar_t
mask
=
data_mask_ptr
[
data_mask_hw_ptr
];
const
scalar_t
cur_inv_h_data
=
h_in
+
i
*
dilation_h
+
offset_h
;
const
scalar_t
cur_inv_w_data
=
w_in
+
j
*
dilation_w
+
offset_w
;
const
scalar_t
cur_top_grad
=
data_col
[
index
]
*
mask
;
const
int
cur_h
=
(
int
)
cur_inv_h_data
;
const
int
cur_w
=
(
int
)
cur_inv_w_data
;
for
(
int
dy
=
-
2
;
dy
<=
2
;
dy
++
)
{
for
(
int
dx
=
-
2
;
dx
<=
2
;
dx
++
)
{
if
(
cur_h
+
dy
>=
0
&&
cur_h
+
dy
<
height
&&
cur_w
+
dx
>=
0
&&
cur_w
+
dx
<
width
&&
abs
(
cur_inv_h_data
-
(
cur_h
+
dy
))
<
1
&&
abs
(
cur_inv_w_data
-
(
cur_w
+
dx
))
<
1
)
{
int
cur_bottom_grad_pos
=
((
b
*
channels
+
c
)
*
height
+
cur_h
+
dy
)
*
width
+
cur_w
+
dx
;
scalar_t
weight
=
dmcn_get_gradient_weight
(
cur_inv_h_data
,
cur_inv_w_data
,
cur_h
+
dy
,
cur_w
+
dx
,
height
,
width
);
atomicAdd
(
grad_im
+
cur_bottom_grad_pos
,
weight
*
cur_top_grad
);
}
}
}
}
}
template
<
typename
scalar_t
>
__global__
void
modulated_deformable_col2im_coord_gpu_kernel
(
const
int
n
,
const
scalar_t
*
data_col
,
const
scalar_t
*
data_im
,
const
scalar_t
*
data_offset
,
const
scalar_t
*
data_mask
,
const
int
channels
,
const
int
height
,
const
int
width
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
channel_per_deformable_group
,
const
int
batch_size
,
const
int
offset_channels
,
const
int
deformable_group
,
const
int
height_col
,
const
int
width_col
,
scalar_t
*
grad_offset
,
scalar_t
*
grad_mask
)
{
CUDA_KERNEL_LOOP
(
index
,
n
)
{
scalar_t
val
=
0
,
mval
=
0
;
int
w
=
index
%
width_col
;
int
h
=
(
index
/
width_col
)
%
height_col
;
int
c
=
(
index
/
width_col
/
height_col
)
%
offset_channels
;
int
b
=
(
index
/
width_col
/
height_col
)
/
offset_channels
;
// compute the start and end of the output
const
int
deformable_group_index
=
c
/
(
2
*
kernel_h
*
kernel_w
);
const
int
col_step
=
kernel_h
*
kernel_w
;
int
cnt
=
0
;
const
scalar_t
*
data_col_ptr
=
data_col
+
deformable_group_index
*
channel_per_deformable_group
*
batch_size
*
width_col
*
height_col
;
const
scalar_t
*
data_im_ptr
=
data_im
+
(
b
*
deformable_group
+
deformable_group_index
)
*
channel_per_deformable_group
/
kernel_h
/
kernel_w
*
height
*
width
;
const
scalar_t
*
data_offset_ptr
=
data_offset
+
(
b
*
deformable_group
+
deformable_group_index
)
*
2
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
scalar_t
*
data_mask_ptr
=
data_mask
+
(
b
*
deformable_group
+
deformable_group_index
)
*
kernel_h
*
kernel_w
*
height_col
*
width_col
;
const
int
offset_c
=
c
-
deformable_group_index
*
2
*
kernel_h
*
kernel_w
;
for
(
int
col_c
=
(
offset_c
/
2
);
col_c
<
channel_per_deformable_group
;
col_c
+=
col_step
)
{
const
int
col_pos
=
(((
col_c
*
batch_size
+
b
)
*
height_col
)
+
h
)
*
width_col
+
w
;
const
int
bp_dir
=
offset_c
%
2
;
int
j
=
(
col_pos
/
width_col
/
height_col
/
batch_size
)
%
kernel_w
;
int
i
=
(
col_pos
/
width_col
/
height_col
/
batch_size
/
kernel_w
)
%
kernel_h
;
int
w_out
=
col_pos
%
width_col
;
int
h_out
=
(
col_pos
/
width_col
)
%
height_col
;
int
w_in
=
w_out
*
stride_w
-
pad_w
;
int
h_in
=
h_out
*
stride_h
-
pad_h
;
const
int
data_offset_h_ptr
=
(((
2
*
(
i
*
kernel_w
+
j
))
*
height_col
+
h_out
)
*
width_col
+
w_out
);
const
int
data_offset_w_ptr
=
(((
2
*
(
i
*
kernel_w
+
j
)
+
1
)
*
height_col
+
h_out
)
*
width_col
+
w_out
);
const
int
data_mask_hw_ptr
=
(((
i
*
kernel_w
+
j
)
*
height_col
+
h_out
)
*
width_col
+
w_out
);
const
scalar_t
offset_h
=
data_offset_ptr
[
data_offset_h_ptr
];
const
scalar_t
offset_w
=
data_offset_ptr
[
data_offset_w_ptr
];
const
scalar_t
mask
=
data_mask_ptr
[
data_mask_hw_ptr
];
scalar_t
inv_h
=
h_in
+
i
*
dilation_h
+
offset_h
;
scalar_t
inv_w
=
w_in
+
j
*
dilation_w
+
offset_w
;
if
(
inv_h
<=
-
1
||
inv_w
<=
-
1
||
inv_h
>=
height
||
inv_w
>=
width
)
{
inv_h
=
inv_w
=
-
2
;
}
else
{
mval
+=
data_col_ptr
[
col_pos
]
*
dmcn_im2col_bilinear
(
data_im_ptr
+
cnt
*
height
*
width
,
width
,
height
,
width
,
inv_h
,
inv_w
);
}
const
scalar_t
weight
=
dmcn_get_coordinate_weight
(
inv_h
,
inv_w
,
height
,
width
,
data_im_ptr
+
cnt
*
height
*
width
,
width
,
bp_dir
);
val
+=
weight
*
data_col_ptr
[
col_pos
]
*
mask
;
cnt
+=
1
;
}
// KERNEL_ASSIGN(grad_offset[index], offset_req, val);
grad_offset
[
index
]
=
val
;
if
(
offset_c
%
2
==
0
)
// KERNEL_ASSIGN(grad_mask[(((b * deformable_group + deformable_group_index) * kernel_h * kernel_w + offset_c / 2) * height_col + h) * width_col + w], mask_req, mval);
grad_mask
[(((
b
*
deformable_group
+
deformable_group_index
)
*
kernel_h
*
kernel_w
+
offset_c
/
2
)
*
height_col
+
h
)
*
width_col
+
w
]
=
mval
;
}
}
void
modulated_deformable_im2col_cuda
(
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kenerl_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
data_col
)
{
// num_axes should be smaller than block size
const
int
channel_per_deformable_group
=
channels
/
deformable_group
;
const
int
num_kernels
=
channels
*
batch_size
*
height_col
*
width_col
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_im
.
type
(),
"modulated_deformable_im2col_gpu"
,
([
&
]
{
const
scalar_t
*
data_im_
=
data_im
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
const
scalar_t
*
data_mask_
=
data_mask
.
data
<
scalar_t
>
();
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
modulated_deformable_im2col_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_im_
,
data_offset_
,
data_mask_
,
height_im
,
width_im
,
kernel_h
,
kenerl_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
batch_size
,
channels
,
deformable_group
,
height_col
,
width_col
,
data_col_
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in modulated_deformable_im2col_cuda: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
void
modulated_deformable_col2im_cuda
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
grad_im
){
const
int
channel_per_deformable_group
=
channels
/
deformable_group
;
const
int
num_kernels
=
channels
*
kernel_h
*
kernel_w
*
batch_size
*
height_col
*
width_col
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_col
.
type
(),
"modulated_deformable_col2im_gpu"
,
([
&
]
{
const
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
const
scalar_t
*
data_mask_
=
data_mask
.
data
<
scalar_t
>
();
scalar_t
*
grad_im_
=
grad_im
.
data
<
scalar_t
>
();
modulated_deformable_col2im_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_col_
,
data_offset_
,
data_mask_
,
channels
,
height_im
,
width_im
,
kernel_h
,
kernel_w
,
pad_h
,
pad_h
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
batch_size
,
deformable_group
,
height_col
,
width_col
,
grad_im_
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in modulated_deformable_col2im_cuda: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
void
modulated_deformable_col2im_coord_cuda
(
const
at
::
Tensor
data_col
,
const
at
::
Tensor
data_im
,
const
at
::
Tensor
data_offset
,
const
at
::
Tensor
data_mask
,
const
int
batch_size
,
const
int
channels
,
const
int
height_im
,
const
int
width_im
,
const
int
height_col
,
const
int
width_col
,
const
int
kernel_h
,
const
int
kernel_w
,
const
int
pad_h
,
const
int
pad_w
,
const
int
stride_h
,
const
int
stride_w
,
const
int
dilation_h
,
const
int
dilation_w
,
const
int
deformable_group
,
at
::
Tensor
grad_offset
,
at
::
Tensor
grad_mask
)
{
const
int
num_kernels
=
batch_size
*
height_col
*
width_col
*
2
*
kernel_h
*
kernel_w
*
deformable_group
;
const
int
channel_per_deformable_group
=
channels
*
kernel_h
*
kernel_w
/
deformable_group
;
AT_DISPATCH_FLOATING_TYPES_AND_HALF
(
data_col
.
type
(),
"modulated_deformable_col2im_coord_gpu"
,
([
&
]
{
const
scalar_t
*
data_col_
=
data_col
.
data
<
scalar_t
>
();
const
scalar_t
*
data_im_
=
data_im
.
data
<
scalar_t
>
();
const
scalar_t
*
data_offset_
=
data_offset
.
data
<
scalar_t
>
();
const
scalar_t
*
data_mask_
=
data_mask
.
data
<
scalar_t
>
();
scalar_t
*
grad_offset_
=
grad_offset
.
data
<
scalar_t
>
();
scalar_t
*
grad_mask_
=
grad_mask
.
data
<
scalar_t
>
();
modulated_deformable_col2im_coord_gpu_kernel
<<<
GET_BLOCKS
(
num_kernels
),
CUDA_NUM_THREADS
>>>
(
num_kernels
,
data_col_
,
data_im_
,
data_offset_
,
data_mask_
,
channels
,
height_im
,
width_im
,
kernel_h
,
kernel_w
,
pad_h
,
pad_w
,
stride_h
,
stride_w
,
dilation_h
,
dilation_w
,
channel_per_deformable_group
,
batch_size
,
2
*
kernel_h
*
kernel_w
*
deformable_group
,
deformable_group
,
height_col
,
width_col
,
grad_offset_
,
grad_mask_
);
}));
cudaError_t
err
=
cudaGetLastError
();
if
(
err
!=
cudaSuccess
)
{
printf
(
"error in modulated_deformable_col2im_coord_cuda: %s
\n
"
,
cudaGetErrorString
(
err
));
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment