当前位置: 首页 > news >正文

PyTorch仿射变换:原理与实战全解析

仿射变换在 PyTorch 中的实现与原理

核心概念

仿射变换是一种保持直线和比例关系的几何变换,由以下操作组成:

  • 旋转(Rotation)
  • 缩放(Scale)
  • 平移(Translation)
  • 剪切(Shear)
  • 反射(Reflection)

数学形式可表示为齐次坐标下的矩阵运算:

[x']   [a  b  tx] [x]
[y'] = [c  d  ty] [y]
[1 ]   [0  0  1 ] [1]

其中 a, b, c, d 控制旋转/缩放/剪切,tx, ty 控制平移。


PyTorch 内置实现方式

1. torchvision.transforms.RandomAffine
from torchvision import transformstransform = transforms.Compose([transforms.RandomAffine(degrees=15,            # 旋转范围:±15度translate=(0.1, 0.1),  # 平移比例:最大10%scale=(0.8, 1.2),      # 缩放范围:80%~120%shear=10,              # 剪切范围:±10度fill=0                 # 空白区域填充值)
])
2. torch.nn.functional.affine_grid + grid_sample

底层控制实现:

import torch.nn.functional as Fdef apply_affine(x, matrix):# x: [C, H, W] tensormatrix = matrix[:2, :]  # 取前两行grid = F.affine_grid(matrix.unsqueeze(0), x.unsqueeze(0).size())return F.grid_sample(x.unsqueeze(0), grid)[0]# 示例矩阵:旋转30度 + 缩放0.8倍
theta = torch.tensor([[0.8 * math.cos(math.pi/6), -0.8 * math.sin(math.pi/6), 0],[0.8 * math.sin(math.pi/6),  0.8 * math.cos(math.pi/6), 0]
])
transformed = apply_affine(image, theta)

参数详解

参数类型说明计算公式
degreesfloat/tuple旋转角度范围随机角度 ∈ [-deg, +deg]
translatetuple平移比例 (h, w)偏移量 = 图片尺寸 × 随机值 × trans
scaletuple缩放比例范围随机缩放 ∈ [scale_min, scale_max]
shearfloat/tuple剪切角度支持XY方向单独设置
fillint/tuple空白区域填充可设统一值或RGB分量值
centertuple旋转中心点默认中心点:image_size/2

核心实现原理

1. 矩阵合成(顺序敏感)
def get_affine_matrix():# 基础单位矩阵matrix = [[1, 0, 0],[0, 1, 0],[0, 0, 1]]# 应用变换(顺序:缩放→旋转→平移)matrix = compose_matrix(scale_matrix(sx, sy))matrix = compose_matrix(rotation_matrix(theta), matrix)matrix = compose_matrix(translation_matrix(tx, ty), matrix)return matrix[:2]  # 返回2x3矩阵
2. 网格采样与双线性插值
def grid_sample(input, grid):for y in range(H):for x in range(W):src_x, src_y = grid[y, x]  # 计算原图坐标# 双线性插值(实际使用优化算法)top_left = input[floor(src_y), floor(src_x)]top_right = input[floor(src_y), ceil(src_x)]...output[y,x] = bilinear_interp(top_left, top_right, ...)
3. 边界处理
  • 填充处理fill参数设置空白像素值
  • 采样模式
    • padding_mode='zeros'(默认)
    • padding_mode='border'(复制边缘像素)
    • padding_mode='reflection'(镜像反射)

可视化示例

import matplotlib.pyplot as plt# 原始图像
plt.subplot(231)
plt.imshow(orig_img)
plt.title("Original")# 旋转示例
plt.subplot(232)
rotated = transforms.functional.rotate(tensor_img, 30)
plt.imshow(rotated.permute(1,2,0))
plt.title("Rotation")# 缩放示例
plt.subplot(233)
scaled = transforms.functional.resize(tensor_img, (100,150))
plt.imshow(scaled.permute(1,2,0))
plt.title("Scale")# 剪切示例
sheared = F.affine(tensor_img, angle=0, translate=(0,0),scale=1,shear=(20,0))
plt.subplot(234)
plt.imshow(sheared.permute(1,2,0))
plt.title("Shear")plt.show()

数学基础

1. 基本变换矩阵
变换类型矩阵格式
平移[[1, 0, tx], [0, 1, ty]]
缩放[[sx, 0, 0], [0, sy, 0]]
旋转[[cosθ, -sinθ, 0], [sinθ, cosθ, 0]]
剪切[[1, sh_x, 0], [sh_y, 1, 0]]
2. 组合变换
M = T \times R \times S \times Sh

其中:

  • T:平移矩阵
  • R:旋转矩阵
  • S:缩放矩阵
  • Sh:剪切矩阵

应用场景

  1. 数据增强:提升模型对几何变换的鲁棒性

    train_transform = transforms.Compose([transforms.RandomAffine(degrees=20, shear=10),transforms.ToTensor()
    ])
    
  2. 图像校准:医学影像/卫星图像配准

    # 计算最优变换矩阵
    optimizer = torch.optim.Adam([matrix_params], lr=0.01)
    for _ in range(100):warped = F.affine_grid(matrix_params, target.shape)loss = F.mse_loss(warped, target)loss.backward()optimizer.step()
    
  3. 空间变换网络 (STN):可学习仿射层

    stn = nn.Sequential(nn.Conv2d(1, 10, 5),nn.MaxPool2d(2),nn.ReLU(),nn.Linear(10*12*12, 6)  # 输出6个仿射参数
    )
    

性能优化技巧

  1. 预计算网格

    # 对batch重复使用相同变换时
    grid = F.affine_grid(matrix.expand(batch, -1), size)
    
  2. 设置梯度计算

    with torch.set_grad_enabled(mode == 'train'):transformed = F.grid_sample(input, grid)
    
  3. 插值方式选择

    F.grid_sample(..., mode='bilinear')  # 训练常用
    F.grid_sample(..., mode='nearest')   # 高精度应用
    

实测速度比较(RTX 3090,256x256图像):

  • 单次变换耗时:0.5~1.2 ms
  • 双三次插值比双线性慢3倍
  • 梯度计算增加15%耗时

与相关技术对比

特性仿射变换透视变换弹性变形
自由度68
保持平行
实现难度简单中等复杂
PyTorch支持内置需自定义GridDistortion
典型应用几何增强3D投影医学影像

仿射变换因其效率与实用性的平衡,成为计算机视觉中应用最广泛的几何变换方法。

http://www.dtcms.com/a/272328.html

相关文章:

  • 深入理解Java虚拟机:Java内存区域与内存溢出异常
  • 【运维架构】云计算运维架构师与基础设施,技术路线,Linux证书(标准化/定制化/CNCF,公有云/混合云/私有云)
  • 【图像处理基石】如何入门图像校正?
  • (6)机器学习小白入门 YOLOv:图片的数据预处理
  • 机器学习 YOLOv5手绘电路图识别 手绘电路图自动转换为仿真软件(如LT Spice)可用的原理图,避免人工重绘
  • Spring MVC 1
  • C++中的list的学习
  • Go语言教程-变量、常量、命名规则
  • 亚矩阵云手机破解Maio广告平台多账号风控:从“生存焦虑”到“规模化增长”的终极方案
  • 电路研究9.4——合宙Air780EP的LuatOS、CSDK跟标准AT
  • 基于开源AI大模型AI智能名片S2B2C商城小程序源码的私域流量新生态构建
  • 独立服务器选择Rocky Linux还是CentOS
  • 【数据结构】顺序表(sequential list)
  • 学习中断配置的一天(第五天)
  • 安装nginx+php环境
  • OpenCV探索之旅:多尺度视觉与形状的灵魂--图像金字塔与轮廓分析
  • 无人机识别比赛记录与分析
  • Java---IDEA
  • 【论文阅读】Decoupled Knowledge Distillation
  • 【大模型推理论文阅读】 Thinking Tokens are Information Peaks in LLM Reasoning
  • iOS 抓包详细教程:从零搭建、操作到实战调试的全流程指南
  • 图像亮度调整的简单实现
  • Flutter多线程机制深度解析
  • 【Docker基础】Docker容器与网络关联命令使用指南:深入理解容器网络连接
  • 力扣61.旋转链表
  • Windows下VScode配置FFmpeg开发环境保姆级教程
  • 面试150 LRU缓存
  • LeetCode - 1668. 最大重复子字符串
  • 原创:多面体编译,polybench-c-4.2批量测试脚本
  • php中array($this, ‘loadClass‘)表示啥意思?