Hi, i have solved it.Code is below.

import cv2

import torch.nn.functional as F

import skimage.transform as trans

import numpy as np

def convert_image_np(inp):

“”“Convert a Tensor to numpy image.”""

inp = inp.numpy().transpose((1, 2, 0))

inp = (inp*255).astype(np.uint8)

return inp

def param2theta(param, w, h):

param = np.linalg.inv(param)

theta = np.zeros([2,3])

theta[0,0] = param[0,0]

theta[0,1] = param[0,1]*h/w

theta[0,2] = param[0,2]*2/w + param[0,0] + param[0,1] - 1

theta[1,0] = param[1,0]*w/h

theta[1,1] = param[1,1]

theta[1,2] = param[1,2]*2/h + param[1,0] + param[1,1] - 1

return theta

tr = trans.estimate_transform(‘affine’, src=src, dst=dst)

M = tr.params[0:2,:]

img = cv2.warpAffine(image,M,(w,h))

theta = param2theta(tr.params, w, h)

image = Variable(image).unsqueeze(0).cuda()

theta = Variable(theta).unsqueeze(0).cuda()

grid = F.affine_grid(theta,image.size())

img_ = F.grid_sample(image,grid)

img_ = convert_image_np(img_.data.cpu().squeeze(0))

The visualizations of img and img_ should be same. Hope it can help you!