Torch.nn.functional.affine_grid : affine matrix that has rotation, translation, and scaling


I am trying to use torch.nn.functional.affine_grid to generate a grid that will be passed to torch.nn.functional.grid_sample.


+(1) Small square image of size (K,K)

+(2) Rotated Box defined by (x_center, y_center, w_box, h_box, angle) where x_center, y_center is in the coordinate system of the input image described below. w_box is the width of the box and h_box is the height of the smaller image. Angle is rotation applied to the box in degrees.

+(3) Input image of size (w_input, h_input).

The problem I am solving is to paste (1) into (3) such that (1) is rotated/scaled/translated to fit the rotated box defined in (2).

Any pixel that falls outside of the input should be discarded. What is the 2x3 affine matrix that I should use to generate the affine grid? Thanks.