PHP前端开发

PyTorch 中的 EMNIST

百变鹏仔 5天前 #Python
文章标签 PyTorch

请我喝杯咖啡☕

*我的帖子解释了 emnist。

emnist()可以使用emnist数据集,如下所示:

*备忘录:

  • 有转换参数(可选-默认:无-类型:可调用)。
  • 有 target_transform 参数(可选-默认:无-类型:可调用)。
  • 有下载参数(可选-默认:false-类型:bool):*备注:
  • 存在图像默认翻转并逆时针旋转90度的错误,因此需要对其进行转换。
  • from torchvision.datasets import emnisttrain_data = emnist(    root="data",    split="byclass")train_data = emnist(    root="data",    split="byclass",    train=true,    transform=none,    target_transform=none,    download=false)test_data = emnist(    root="data",    split="byclass",    train=false)len(train_data), len(test_data)# 697932 116323train_data# dataset emnist#     number of datapoints: 697932#     root location: data#     split: traintrain_data.root# 'data'train_data.split# 'byclass'train_data.train# trueprint(train_data.transform)# noneprint(train_data.target_transform)# nonetrain_data.download# <bound method emnist.download of dataset emnist#     number of datapoints: 697932#     root location: data#     split: train>train_data[0]# (<pil.image.image image mode=l size=28x28>, 35)train_data[1]# (<pil.image.image image mode=l size=28x28>, 36)train_data[2]# (<pil.image.image image mode=l size=28x28>, 6)train_data[3]# (<pil.image.image image mode=l size=28x28>, 3)train_data[4]# (<pil.image.image image mode=l size=28x28>, 22)train_data.classes# ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',#  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',#  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',#  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',#  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    from torchvision.datasets import emnisttrain_data = emnist(    root="data",    split="byclass",    train=true)test_data = emnist(    root="data",    split="byclass",    train=false)import matplotlib.pyplot as pltdef show_images(data):    plt.figure(figsize=(12, 2))    col = 5    for i, (image, label) in enumerate(data, 1):        plt.subplot(1, col, i)        plt.title(label)        plt.imshow(image)        if i == col:            break    plt.show()show_images(data=train_data)show_images(data=test_data)


    from torchvision.datasets import EMNISTfrom torchvision.transforms import v2train_data = EMNIST(    root="data",    split="byclass",    train=True,    transform=v2.Compose([        v2.RandomHorizontalFlip(p=1.0),        v2.RandomRotation(degrees=(90, 90))    ]))test_data = EMNIST(    root="data",    split="byclass",    train=False,    transform=v2.Compose([        v2.RandomHorizontalFlip(p=1.0),        v2.RandomRotation(degrees=(90, 90))    ]))import matplotlib.pyplot as pltdef show_images(data):    plt.figure(figsize=(12, 2))    col = 5    for i, (image, label) in enumerate(data, 1):        plt.subplot(1, col, i)        plt.title(label)        plt.imshow(image)        if i == col:            break    plt.show()show_images(data=train_data)show_images(data=test_data)