PyTorch-Tutorials【pytorch官方教程中英文详解】- 4 Transforms

该篇博客介绍了如何使用PyTorch中的Transforms对数据进行预处理,包括将FashionMNIST数据集的特征转换为Tensor并归一化,将标签转换为one-hot编码。ToTensor用于将PIL图像转换为FloatTensor,Lambda则用于定义自定义转换,这里用于创建one-hot编码张量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇文章PyTorch-Tutorials【pytorch官方教程中英文详解】- 3 Datasets&DataLoaders介绍了pytorch中读取数据集的Datasets&DataLoaders类,下面进一步看看如何对数据进行转换。

原文链接:Transforms — PyTorch Tutorials 1.10.1+cu102 documentation

Data does not always come in its final processed form that is required for training machine learning algorithms. We use transforms to perform some manipulation of the data and make it suitable for training.

【数据并不总是以训练机器学习算法所需的最终处理形式出现。我们使用转换来对数据执行一些操作,并使其适合于训练。】

All TorchVision datasets have two parameters -transform to modify the features and target_transform to modify the labels - that accept callables containing the transformation logic. The torchvision.transforms module offers several commonly-used transforms out of the box.

【所有的TorchVision数据集都有两个参数——transform用来修改特征,target_transform用来修改标签——它们接受包含转换逻辑的可调用对象。torchvision.transforms模块提供了几个常用的开箱即用转换。】

The FashionMNIST features are in PIL Image format, and the labels are integers. For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors. To make these transformations, we use ToTensor and Lambda.

【FashionMNIST特征采用PIL Image格式,标签为整数。在训练中,我们需要特征是标准化张量,标签是one-hot编码张量。为了做这些变换,我们使用ToTensor和Lambda。】

import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda

ds = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor(),
    target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)

代码的输出结果:之前没有下载过数据集会输出downloading进度,但是因为之前已经下载过,我运行之后没有任何反应。

1 ToTensor()

ToTensor converts a PIL image or NumPy ndarray into a FloatTensor. and scales the image’s pixel intensity values in the range [0., 1.]

ToTensor将PIL图像或NumPy ndarray转换为FloatTensor,并缩放图像的像素强度值在范围[0,1)内。】

2 Lambda Transforms

Lambda transforms apply any user-defined lambda function. Here, we define a function to turn the integer into a one-hot encoded tensor. It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls scatter_ which assigns a value=1 on the index as given by the label y.

【λ转换应用任何用户定义的Lambda函数。在这里,我们定义了一个函数来将整数转换为一个热编码张量。它首先创建一个0张量,大小为10(我们的数据集中标签的数量),然后调用scatter_,它为标签y给定的索引赋值=1。】

3 Further Reading

说明:记录学习笔记,如果错误欢迎指正!写文章不易,转载请联系我。

### 关于IA-YOLO在PyTorch中的实现 IA-YOLO是一种改进版的目标检测算法,其具体实现在公开资源中较少提及。不过,基于YOLO系列和其他目标检测框架的发展趋势以及PyTorch的强大灵活性[^2],可以推测IA-YOLO的实现会遵循类似的模式。 #### 实现要点 为了在PyTorch中构建IA-YOLO模型,开发者通常需要掌握卷积神经网络的工作原理,包括但不限于残差块、跳跃连接和上采样的概念[^3]。这些组件对于设计高效的特征提取器至关重要。此外,理解边界框回归、交并比(IoU)计算方法及其优化技术如非极大值抑制(NMS),也是成功部署此类模型的关键要素之一。 当考虑批量处理图片时,保持输入尺寸的一致性有助于提高GPU利用率从而加快训练过程[^4]。因此,在实际编码过程中应当注意这一点。 #### 资源推荐 虽然特定针对IA-YOLO的教学材料较为稀缺,但可以从更广泛的角度出发寻找相关联的学习路径: - **官方文档**:访问[PyTorch官方网站](https://pytorch.org/tutorials/)获取最新的API说明和技术指南[^1]。 - **社区贡献**:GitHub平台上存在大量由个人或团队维护的开源项目,其中不乏高质量的目标检测案例研究。通过搜索`IA-YOLO pytorch`关键词可能会发现有价值的参考资料。 - **实践教程**:类似于《Getting Things Done With PyTorch》这样的书籍提供了丰富的实战经验分享,尽管不是专门讨论IA-YOLO,但对于熟悉整个工作流程非常有帮助[^5]。 ```python import torch from torchvision import models, transforms # 这里仅展示基础导入语句,具体的IA-YOLO模块需根据实际情况调整 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值