向 MNIST 数据集添加随机位置方差

问题描述 投票:0回答:1

我尝试在 MNIST 集上训练自动编码器,其中的数字应该应用随机翻译。 使用火炬变换,我可以调整大小和翻译,但这没有达到预期的效果(数字被翻译到框架之外)。这里有人知道变换或其他一些方法可以让我随机翻译一个较小的数字吗?

我尝试使用以下代码手动执行此操作:

image = dataset[0][0][0]
background = np.zeros((56,56))
topLeft = (random.randint(0,27), random.randint(0,27))
background[topLeft[0]:topLeft[0]+28, topLeft[1]:topLeft[1]+28] = image[0][0]

但我无法在实际的 MNIST 集上进行此转换。 任何帮助将不胜感激。

pytorch transform mnist pytorch-lightning
1个回答
0
投票

我已经用仿射变换完成了

from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt

import torch
from torchvision.transforms import v2

plt.rcParams["savefig.bbox"] = 'tight'


torch.manual_seed(0)

# you can download the assets and the
# helpers from https://github.com/pytorch/vision/tree/main/gallery/
from helpers import plot
orig_img = Image.open(Path('gallery/assets/astronaut.jpg'))

affine_transfomer = v2.RandomAffine(degrees=0,scale=(0.5,0.5))
affine_imgs = [affine_transfomer(orig_img) for _ in range(4)]
plot([orig_img] + affine_imgs)

除此之外,您还可以使用 56x56 调整大小方法

我希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.