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

### 问题描述

```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])
]),
}
```

## 推荐答案

transforms操作在每个批处理生成时都应用于您的原始图像.因此，您的数据集保持不变，仅复制批处理图像并转换了所有迭代.

• 随机调整提供的图像并随机裁剪以获取(224, 224) patch
• 使用50/50 Chance
• 申请或不随机水平翻转
• 将其转换为Tensor
• 给定提供的平均值和偏差值
• ，将结果标准化

• 将图像大小调整到(256, 256)
• 中心裁剪大小的图像以获得(224, 224)补丁
• 将其转换为Tensor
• 给定提供的平均值和偏差值
• ，将结果标准化

(即.

## 其他推荐答案

```class experimental_dataset(Dataset):

def __init__(self, data, transform):
self.data = data
self.transform = transform

def __len__(self):
return len(self.data.shape[0])

def __getitem__(self, idx):
item = self.data[idx]
item = self.transform(item)
return item

transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])

x = torch.rand(8, 1, 2, 2)
print(x)

dataset = experimental_dataset(x,transform)

for item in dataset:
print(item)
```

)

```tensor([[[[0.1872, 0.5518],
[0.5733, 0.6593]]],

[[[0.6570, 0.6487],
[0.4415, 0.5883]]],

[[[0.5682, 0.3294],
[0.9346, 0.1243]]],

[[[0.1829, 0.5607],
[0.3661, 0.6277]]],

[[[0.1201, 0.1574],
[0.4224, 0.6146]]],

[[[0.9301, 0.3369],
[0.9210, 0.9616]]],

[[[0.8567, 0.2297],
[0.1789, 0.8954]]],

[[[0.0068, 0.8932],
[0.9971, 0.3548]]]])
```

```tensor([[[0.1843, 0.5490],
[0.5725, 0.6588]]])
tensor([[[0.6549, 0.6471],
[0.4392, 0.5882]]])
tensor([[[0.5647, 0.3255],
[0.9333, 0.1216]]])
tensor([[[0.5569, 0.1804],
[0.6275, 0.3647]]])
tensor([[[0.1569, 0.1176],
[0.6118, 0.4196]]])
tensor([[[0.9294, 0.3333],
[0.9176, 0.9608]]])
tensor([[[0.8549, 0.2275],
[0.1765, 0.8941]]])
tensor([[[0.8902, 0.0039],
[0.3529, 0.9961]]])
```