准备数据

本教程的上一阶段中,我们在计算机上安装了 PyTorch。 现在,我们将使用它来设置代码,其中包含用于构建模型的数据。

在 Visual Studio 内打开新项目。

  1. 打开 Visual Studio 并选择 create a new project

Create new Visual Studio project

  1. 在搜索栏中,键入 Python然后选择 Python Application 作为项目模板。

Create new Python app

  1. 在配置窗口中:
  • 为项目命名。 在这里,我们将其称为 PyTorchTraining
  • 选择项目的位置。
  • 如果使用的是 VS 2019,请确保选中 Create directory for solution
  • 如果使用的是 VS 2017,请确保已选中 Place solution and project in the same directory

Configure your new Python app

create 创建项目。

创建 Python 解释器

现在,需要定义新的 Python 解释器。 这必须包括最近安装的 PyTorch 包。

  1. 导航到解释器选择,然后选择 Add environment

Python interpreter selection

  1. Add environment 窗口中,选择 Existing environment,然后选择 Anaconda3 (3.6, 64-bit)。 其中包括 PyTorch 包。

Configure a new Python environment

若要测试新的 Python 解释器和 PyTorch 包,请在 PyTorchTraining.py 文件中输入以下代码:

from __future__ import print_function 

import torch 

x=torch.rand(2, 3) 

print(x) 

输出应为类似于以下内容的随机 5x3 张量。

Test your new Python interpreter

注意

想要了解更多内容? 请访问 PyTorch 官方网站

加载数据集

你将使用 PyTorch torchvision 类加载数据。

Torchvision 库包括若干常用的数据集(如 Imagenet、CIFAR10、MNIST 等)、模型体系结构和常见的计算机视觉图像转换。 这使得在 PyTorch 中加载数据非常简单。

CIFAR10

在这里,我们将使用 CIFAR10 数据集来构建和训练图像分类模型。 CIFAR10 是广泛用于机器学习研究的数据集。 该数据集由 50,000 张训练图像和 10,000 张测试图像组成。 这些图像的大小均为 3x32x32,这意味着它们是大小为 32x32 像素的 3 通道彩色图像。

图像分为 10 个类别:“飞机”(0)、“汽车”(1)、“鸟”(2)、“猫”(3)、“鹿”(4)、“狗”(5)、“青蛙”(6)、“马”(7)、“船”(8)、“卡车”(9)。

你将按照三个步骤在 PyTorch 中加载和读取 CIFAR10 数据集:

  • 定义要应用于图像的转换:若要训练模型,需要将图像转换为归一化范围为 [-1,1] 的张量。
  • 创建可用数据集的实例并加载数据集:要加载数据,请使用 torch.utils.data.Dataset 类 - 用于表示数据集的抽象类。 数据集仅在第一次运行代码时下载到本地。
  • 使用 DataLoader 访问数据。 要访问数据并将数据放入内存,请使用 torch.utils.data.DataLoader 类。 PyTorch 中的 DataLoader 会包装数据集并提供对基础数据的访问权限。 该包装器将按照定义的批大小保存多批图像。

你将对训练集和测试集重复这三个步骤。

  1. 在 Visual Studio 中打开 PyTorchTraining.py file,并添加以下代码。 这将处理 CIFAR10 数据集中的训练数据集和测试数据集的上述三个步骤。
from torchvision.datasets import CIFAR10
from torchvision.transforms import transforms
from torch.utils.data import DataLoader

# Loading and normalizing the data.
# Define transformations for the training and test sets
transformations = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# CIFAR10 dataset consists of 50K training images. We define the batch size of 10 to load 5,000 batches of images.
batch_size = 10
number_of_labels = 10 

# Create an instance for training. 
# When we run this code for the first time, the CIFAR10 train dataset will be downloaded locally. 
train_set =CIFAR10(root="./data",train=True,transform=transformations,download=True)

# Create a loader for the training set which will read the data within batch size and put into memory.
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=0)
print("The number of images in a training set is: ", len(train_loader)*batch_size)

# Create an instance for testing, note that train is set to False.
# When we run this code for the first time, the CIFAR10 test dataset will be downloaded locally. 
test_set = CIFAR10(root="./data", train=False, transform=transformations, download=True)

# Create a loader for the test set which will read the data within batch size and put into memory. 
# Note that each shuffle is set to false for the test loader.
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=0)
print("The number of images in a test set is: ", len(test_loader)*batch_size)

print("The number of batches per epoch is: ", len(train_loader))
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

第一次运行此代码时,CIFAR10 数据集将下载到你的设备。

CIFAR10 dataset download

后续步骤

数据准备就绪后,即可训练 PyTorch 模型