PyTorch 中的数据增强 [英] Data Augmentation in PyTorch

查看:26
本文介绍了PyTorch 中的数据增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PyTorch 中执行的数据增强有点困惑.现在,据我所知,当我们执行数据增强时,我们会保留原始数据集,然后添加它的其他版本(翻转、裁剪等).但这在 PyTorch 中似乎不会发生.据我了解,当我们在 PyTorch 中使用 data.transforms 时,它会一一应用它们.例如:

I am a little bit confused about the data augmentation performed in PyTorch. Now, as far as I know, when we are performing data augmentation, we are KEEPING our original dataset, and then adding other versions of it (Flipping, Cropping...etc). But that doesn't seem like happening in PyTorch. As far as I understood from the references, when we use data.transforms in PyTorch, then it applies them one by one. So for example:

data_transforms = {
    'train': transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

在这里,对于训练,我们首先随机裁剪图像并将其调整为形状(224,224).然后我们将这些 (224,224) 图像水平翻转.因此,我们的数据集现在只包含水平翻转的图像,因此在这种情况下我们的原始图像丢失了.

Here , for the training, we are first randomly cropping the image and resizing it to shape (224,224). Then we are taking these (224,224) images and horizontally flipping them. Therefore, our dataset is now containing ONLY the horizontally flipped images, so our original images are lost in this case.

我说得对吗?这种理解是否正确?如果不是,那么我们在上面的代码中(取自官方文档)在哪里告诉 PyTorch 保留原始图像并将它们调整为预期的形状 (224,224)?

Am I right? Is this understanding correct? If not, then where do we tell PyTorch in this code above (taken from Official Documentation) to keep the original images and resize them to the expected shape (224,224)?

谢谢

推荐答案

transforms 操作会在每次批量生成时应用于原始图像.所以你的数据集保持不变,每次迭代只复制和转换批量图像.

The transforms operations are applied to your original images at every batch generation. So your dataset is left unchanged, only the batch images are copied and transformed every iteration.

混淆可能来自这样一个事实,就像在您的示例中一样,transforms 通常用于数据准备(调整大小/裁剪到预期维度、规范化值等)和数据增强(随机调整大小/裁剪,随机翻转图像等).

The confusion may come from the fact that often, like in your example, transforms are used both for data preparation (resizing/cropping to expected dimensions, normalizing values, etc.) and for data augmentation (randomizing the resizing/cropping, randomly flipping the images, etc.).

你的 data_transforms['train'] 的作用是:

  • 随机调整提供的图像大小并随机裁剪以获得(224, 224)补丁
  • 对该补丁应用或不应用随机水平翻转,有 50/50 的几率
  • 将其转换为 Tensor
  • 根据您提供的均值和偏差值对结果 Tensor 进行标准化
  • Randomly resize the provided image and randomly crop it to obtain a (224, 224) patch
  • Apply or not a random horizontal flip to this patch, with a 50/50 chance
  • Convert it to a Tensor
  • Normalize the resulting Tensor, given the mean and deviation values you provided

你的 data_transforms['val'] 的作用是:

  • 将图像调整为 (256, 256)
  • 中心裁剪调整后的图像以获得(224, 224)补丁
  • 将其转换为 Tensor
  • 根据您提供的均值和偏差值对结果 Tensor 进行标准化
  • Resize your image to (256, 256)
  • Center crop the resized image to obtain a (224, 224) patch
  • Convert it to a Tensor
  • Normalize the resulting Tensor, given the mean and deviation values you provided

(即,将训练数据的随机调整大小/裁剪替换为验证数据的固定操作,以获得可靠的验证结果)

(i.e. the random resizing/cropping for the training data is replaced by a fixed operation for the validation one, to have reliable validation results)

如果您不希望训练图像以 50/50 的几率水平翻转,只需删除 transforms.RandomHorizo​​ntalFlip() 行.

If you don't want your training images to be horizontally flipped with a 50/50 chance, just remove the transforms.RandomHorizontalFlip() line.

同样,如果您希望图像始终居中裁剪,请将 transforms.RandomResizedCrop 替换为 transforms.Resizetransforms.CenterCrop,就像对 data_transforms['val'] 所做的一样.

Similarly, if you want your images to always be center-cropped, replace transforms.RandomResizedCrop by transforms.Resize and transforms.CenterCrop, as done for data_transforms['val'].

这篇关于PyTorch 中的数据增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆