This is how I implement RoIAlign using affine_grid and grid_sample.

  
      github.com
  
  
    ruotianluo/pytorch-faster-rcnn/blob/master/lib/nets/network.py#L91 140



def _proposal_layer(self, rpn_cls_prob, rpn_bbox_pred):
  rois, rpn_scores = proposal_layer(\
                                  rpn_cls_prob, rpn_bbox_pred, self._im_info, self._mode,
                                   self._feat_stride, self._anchors, self._num_anchors)


  return rois, rpn_scores


def _roi_pool_layer(self, bottom, rois):
  return RoIPoolFunction(cfg.POOLING_SIZE, cfg.POOLING_SIZE, 1. / 16.)(bottom, rois)


def _crop_pool_layer(self, bottom, rois, max_pool=True):
  # implement it using stn
  # box to affine
  # input (x1,y1,x2,y2)
  """
  [  x2-x1             x1 + x2 - W + 1  ]
  [  -----      0      ---------------  ]
  [  W - 1                  W - 1       ]
  [                                     ]
  [           y2-y1    y1 + y2 - H + 1  ]