def _transform_img_kornia(img, M, dsize, device, flags='bilinear', borderMode='zeros'):
    """Conduct similarity or affine transformation to the image using Kornia.
    
    img: Input image as a PyTorch tensor of shape (C, H, W).
    M: 2x3 transformation matrix as a PyTorch tensor.
    dsize: Target shape (width, height).
    """
   
    # Convert dsize to tensor shape (H, W)
    _dsize = torch.tensor([dsize[1], dsize[0]])  # Kornia expects (H, W)

    # Convert M from numpy.ndarray to PyTorch tensor
    M = torch.from_numpy(M).float().to(device)
    
    if M.shape == (3, 3):
        M = M[:2, :]
    if M.shape == (2, 3):
        M = M.unsqueeze(0)

    # Convert image to floating point tensor if not already
    if img.dtype != torch.float32:
        img = img.float()
    img = img.permute(0, 3, 1, 2).to(device) # Reshape img for Kornia (B, C, H, W)
   
    # Apply the affine transformation
    img_warped = KGT.warp_affine(img, M, _dsize, mode=flags, padding_mode=borderMode)

    return img_warped
Conduct similarity or affine transformation to the image using Kornia.
    
    img: Input image as a PyTorch tensor of shape (C, H, W).
    M: 2x3 transformation matrix as a PyTorch tensor.
    dsize: Target shape (width, height).
    
liveportrait/utils/crop.py