This is a PyTorch version of RoIAlign.
This implementation is based on crop_and_resize
and supports both forward and backward on CPU and GPU.
The crop_and_resize function is ported from tensorflow,
and has the same interface with tensorflow version, except the input feature map
should be in NCHW order in PyTorch.
They also have the same output value (error < 1e-5) for both forward and backward as we expected,
see the comparision in test.py.
Note:
Document of crop_and_resize can be found here.
And RoIAlign is a wrap of crop_and_resize
that uses boxes with unnormalized (x1, y1, x2, y2) as input
(while crop_and_resize use normalized (y1, x1, y2, x2) as input).
See more details about the difference of
RoIAlign and crop_and_resize in tensorpack.
IMPORTANT: check your GPU's capability and change the -arch flag in line 7 of make.sh accordingly to your specs.
$CUDA_PATH/bin/nvcc -c -o crop_and_resize_kernel.cu.o crop_and_resize_kernel.cu -x cu -Xcompiler -fPIC -arch=sm_52
E.g. On my laptop I have a Geforce GTX 960M which has capability 5.0 so I changed sm_52 with sm_50 and it worked.
-
Build and test
chmod a+x make.sh ./make.sh python test.py
-
Use RoIAlign or crop_and_resize
from roi_align.roi_align import RoIAlign # RoIAlign module from roi_align.roi_align import CropAndResize # crop_and_resize module # input data image = to_varabile(image_data, requires_grad=True, is_cuda=is_cuda) boxes = to_varabile(boxes_data, requires_grad=False, is_cuda=is_cuda) box_index = to_varabile(box_index_data, requires_grad=False, is_cuda=is_cuda) # RoIAlign layer roi_align = RoIAlign(crop_height, crop_width) crops = roi_align(image, boxes, box_index)
-
Issue1: gradcheck and difference of
RoIAlignandcrop_and_resize.
Check crop_and_resize_example.py to see how crop_and_resize works