nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

视频笔记: 小学生都会的Pytorch #219

Open nonocast opened 2 years ago

nonocast commented 2 years ago

Pytorch 环境配置

hello.py

#!/usr/bin/env python3

import torch

print(torch.__version__)
print(torch.cuda.is_available())

x = torch.rand(5, 3)
print(x)

Outputs

~ ./hello.py
1.10.2
False
tensor([[0.0426, 0.9572, 0.9991],
        [0.4146, 0.5080, 0.4674],
        [0.1504, 0.1075, 0.0906],
        [0.4917, 0.0539, 0.6044],
        [0.0308, 0.1147, 0.4134]])

Tensor (张量), 在CS中可以理解为多维数组, Tensors are similar to NumPy’s ndarrays, 就是之前在OpenCV中保存图像的结构。

Quickstart

第二节课主要就是围绕Pytorch官网的Quickstart展开讲解,个人觉得还是非常有价值的。

总结来说,

还是附一下代码

#!/usr/bin/env python3

# Learn the Basics — PyTorch Tutorials 1.10.1+cu102 documentation
# https://pytorch.org/tutorials/beginner/basics/intro.html
# We’ll use the FashionMNIST dataset to train a neural network that predicts if an input image belongs to one of the following classes: T-shirt/top, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, or Ankle boot.

# fashion-mnist
# Fashion-MNIST is a dataset of Zalando's article images—consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. 
# https://github.com/zalandoresearch/fashion-mnist/blob/master/README.zh-CN.md

import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt

# Download training data from open datasets.
training_data = datasets.FashionMNIST(
  root="./data", 
  train=True, 
  download=True, 
  transform = ToTensor()
) 

# Download test data from open datasets.
test_data = datasets.FashionMNIST(
  root="./data", 
  train=False, 
  download=True, 
  transform=ToTensor()
)

batch_size = 128

# Create data loaders.
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)

for X, y in test_dataloader:
  # PEP 498 Literal String Interpolation since 3.6
  # text = 'world'
  # print(f'Hello, {text}')
  # ---
  # N: Batch, C: Channel (Gray:1, RGB:3), H: Height, W: Width
  # 64, 1, 28, 28
  # ---
  # X: images
  # y: labels
  print(f"Shape of X [N, C, H, W]: {X.shape}")
  print(f"Shape of y: {y.shape} {y.dtype}")
  break

# 定义网络模型
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        # 碾平,将数据碾平为一维
        self.flatten = nn.Flatten()
        # 定义linear_relu_stack,由以下众多层构成
        self.linear_relu_stack = nn.Sequential(
            # 全连接层
            nn.Linear(28*28, 512),
            # ReLU激活函数
            nn.ReLU(),
            # 全连接层
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
            nn.ReLU()
        )
    # x为传入数据
    def forward(self, x):
        # x先经过碾平变为1维
        x = self.flatten(x)
        # 随后x经过linear_relu_stack
        logits = self.linear_relu_stack(x)
        # 输出logits
        return logits

device = "cpu"
model = NeuralNetwork().to(device)
print(model)

# 定义损失函数,计算相差多少,交叉熵
loss_fn = nn.CrossEntropyLoss()

# 定义优化器,用来训练时候优化模型参数,随机梯度下降法
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3) # 初始学习率

# 定义训练函数,需要
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    # 从数据加载器中读取batch(一次读取多少张,即批次数),X(图片数据),y(图片真实标签)。
    for batch, (X, y) in enumerate(dataloader):
        # 将数据存到显卡
        X, y = X.to(device), y.to(device)

        # 得到预测的结果pred
        pred = model(X)

        # 计算预测的误差
        loss = loss_fn(pred, y)

        # 反向传播,更新模型参数
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # 每训练100次,输出一次当前信息
        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")

# test即推理过程
def test(dataloader, model):
    size = len(dataloader.dataset)
    # 将模型转为验证模式
    model.eval()
    # 初始化test_loss 和 correct, 用来统计每次的误差
    test_loss, correct = 0, 0
    # 测试时模型参数不用更新,所以no_gard()
    with torch.no_grad():
        # 加载数据加载器,得到里面的X(图片数据)和y(真实标签)
        for X, y in dataloader:
            # 将数据转到GPU
            X, y = X.to(device), y.to(device)
            # 将图片传入到模型当中就,得到预测的值pred
            pred = model(X)
            # 计算预测值pred和真实值y的差距
            test_loss += loss_fn(pred, y).item()
            # 统计预测正确的个数
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= size
    correct /= size
    print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")

# 训练次数
# Epoch 5: 45%
# Epoch 20: 60%
# Epoch 30: 66%
epochs = 30
for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train(train_dataloader, model, loss_fn, optimizer)
    test(test_dataloader, model)
print("Done!")

# 保存训练好的模型
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")

# 读取训练好的模型,加载训练好的参数
model = NeuralNetwork()
model.load_state_dict(torch.load("model.pth"))

# 定义所有类别
classes = [
    "T-shirt/top",
    "Trouser",
    "Pullover",
    "Dress",
    "Coat",
    "Sandal",
    "Shirt",
    "Sneaker",
    "Bag",
    "Ankle boot",
]

# 模型进入验证阶段
model.eval()

x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
    pred = model(x)
    predicted, actual = classes[pred[0].argmax(0)], classes[y]
    print(f'Predicted: "{predicted}", Actual: "{actual}"')

参考视频教程:

参考阅读

alyang666 commented 2 years ago

小学生都会我却不太会 我是傻逼