-

pytorch软件下载安装和使用(图文教程)

PyTorch是一个开源的Python机器学习库,主要应用于人工智能领域,特别是计算机视觉和自然语言处理等深度学习领域。它由Facebook(现更名为Meta Platforms)的人工智能研究团队于2016年发布,并在后续版本中得到了不断的完善和优化。以下是对PyTorch的详细介绍:
pytorch安装
pytorch安装 打开网站 https://pytorch.org/get-started/locally/
选择配置结果为: pip3 install torch torchvision torchaudio
更换pip3源 pip install -i https://mirrors.aliyun.com/pypi/simple/ torch torchvision torchaudio
Matplotlib是一个广泛使用的Python绘图库 pip install -i https://mirrors.aliyun.com/pypi/simple/ matplotlib

pytorch数字识别
使用pytorch库来实现数字识别

下载MNIST和训练并保存训练结果

import torch  
from torch.utils.data import DataLoader  
from torchvision import transforms  
from torchvision.datasets import MNIST  
import matplotlib.pyplot as plt  
  
class Net(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.fc1 = torch.nn.Linear(28*28, 64)
        self.fc2 = torch.nn.Linear(64, 64)
        self.fc3 = torch.nn.Linear(64, 64)
        self.fc4 = torch.nn.Linear(64, 10)
    
    def forward(self, x):
        x = torch.nn.functional.relu(self.fc1(x))
        x = torch.nn.functional.relu(self.fc2(x))
        x = torch.nn.functional.relu(self.fc3(x))
        x = torch.nn.functional.log_softmax(self.fc4(x), dim=1)
        return x


def get_data_loader(is_train):
    to_tensor = transforms.Compose([transforms.ToTensor()])
    data_set = MNIST("", is_train, transform=to_tensor, download=True)
    return DataLoader(data_set, batch_size=15, shuffle=True)


def evaluate(test_data, net):
    n_correct = 0
    n_total = 0
    with torch.no_grad():
        for (x, y) in test_data:
            outputs = net.forward(x.view(-1, 28*28))
            for i, output in enumerate(outputs):
                if torch.argmax(output) == y[i]:
                    n_correct += 1
                n_total += 1
    return n_correct / n_total
  
def main():  
  
    train_data = get_data_loader(is_train=True)  
    test_data = get_data_loader(is_train=False)  
    net = Net()  
      
    # 初始准确率  
    print("initial accuracy:", evaluate(test_data, net))  
    optimizer = torch.optim.Adam(net.parameters(), lr=0.001)  
      
    # 训练模型  
    for epoch in range(2):  #次数
        for (x, y) in train_data:  
            net.zero_grad()  
            output = net.forward(x.view(-1, 28*28))  
            loss = torch.nn.functional.nll_loss(output, y)  
            loss.backward()  
            optimizer.step()  
        print("epoch", epoch, "accuracy:", evaluate(test_data, net))  
      
    # 保存模型  
    torch.save(net.state_dict(), 'model_weights.pth')  
    print("Model saved successfully.")  
      
    # (可选)加载模型  
    # net_loaded = Net()  # 重新实例化模型  
    # net_loaded.load_state_dict(torch.load('model_weights.pth'))  
    # net_loaded.eval()  # 设置模型为评估模式  
      
    # 展示一些预测结果  
    for (n, (x, _)) in enumerate(test_data):  
        if n > 3:  
            break  
        predict = torch.argmax(net.forward(x[0].view(-1, 28*28)))  
        plt.figure(n)  
        plt.imshow(x[0].view(28, 28), cmap='gray')  # 添加cmap='gray'以正确显示图像  
        plt.title("prediction: " + str(int(predict)))  
    plt.show()
  
if __name__ == "__main__":  
    main()
        
使用训练好的来预测图片

import torch  
from torch.utils.data import DataLoader  
from torchvision import transforms  
from torchvision.datasets import MNIST  
import matplotlib.pyplot as plt  
  
class Net(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.fc1 = torch.nn.Linear(28*28, 64)
        self.fc2 = torch.nn.Linear(64, 64)
        self.fc3 = torch.nn.Linear(64, 64)
        self.fc4 = torch.nn.Linear(64, 10)
    
    def forward(self, x):
        x = torch.nn.functional.relu(self.fc1(x))
        x = torch.nn.functional.relu(self.fc2(x))
        x = torch.nn.functional.relu(self.fc3(x))
        x = torch.nn.functional.log_softmax(self.fc4(x), dim=1)
        return x


def get_data_loader(is_train):
    to_tensor = transforms.Compose([transforms.ToTensor()])
    data_set = MNIST("", is_train, transform=to_tensor, download=True)
    return DataLoader(data_set, batch_size=15, shuffle=True)


def evaluate(test_data, net):
    n_correct = 0
    n_total = 0
    with torch.no_grad():
        for (x, y) in test_data:
            outputs = net.forward(x.view(-1, 28*28))
            for i, output in enumerate(outputs):
                if torch.argmax(output) == y[i]:
                    n_correct += 1
                n_total += 1
    return n_correct / n_total
  
def main():   
    test_data = get_data_loader(is_train=False)  
      
    # (可选)加载模型  
    net = Net()  # 重新实例化模型  
    net.load_state_dict(torch.load('model_weights.pth'))  
    net.eval()  # 设置模型为评估模式  
      
    # 展示一些预测结果  
    for (n, (x, _)) in enumerate(test_data):  
        if n > 3:  
            break  
        predict = torch.argmax(net.forward(x[0].view(-1, 28*28)))  
        plt.figure(n)  
        plt.imshow(x[0].view(28, 28), cmap='gray')  # 添加cmap='gray'以正确显示图像  
        plt.title("prediction: " + str(int(predict)))  
 
    plt.show()  
  
if __name__ == "__main__":  
    main()

            
下载MNIST集里的一张图片

from torchvision.datasets import MNIST  

mnist = MNIST(root="",train=True,download=True)

image=mnist[1][0]
image.save('1.png')  
print(image.show())

            
识别本地一张图片转成数字

import torch  
from torch.utils.data import DataLoader  
from torchvision import transforms  
from torchvision.datasets import MNIST  
import matplotlib.pyplot as plt  
from PIL import Image  
import os

class Net(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = torch.nn.Linear(28*28, 64)
        self.fc2 = torch.nn.Linear(64, 64)
        self.fc3 = torch.nn.Linear(64, 64)
        self.fc4 = torch.nn.Linear(64, 10)
    
    def forward(self, x):
        x = torch.nn.functional.relu(self.fc1(x))
        x = torch.nn.functional.relu(self.fc2(x))
        x = torch.nn.functional.relu(self.fc3(x))
        x = torch.nn.functional.log_softmax(self.fc4(x), dim=1)
        return x

def get_data_loader(is_train):
    to_tensor = transforms.Compose([transforms.ToTensor()])
    data_set = MNIST(root="", train=is_train, transform=to_tensor, download=True)
    return DataLoader(data_set, batch_size=15, shuffle=True)

def evaluate(test_data, net):
    n_correct = 0
    n_total = 0
    with torch.no_grad():
        for (x, y) in test_data:
            outputs = net(x.view(-1, 28*28))
            _, predicted = torch.max(outputs, 1)
            n_correct += (predicted == y).sum().item()
            n_total += y.size(0)
    return n_correct / n_total
  
def main():  
    test_data = get_data_loader(is_train=False)  
  
    # 加载模型(假设模型已训练并保存)  
    net = Net()  
    net.load_state_dict(torch.load('model_weights.pth'))  
    net.eval()  
  
    # # 展示一些预测结果  
    # for (n, (x, _)) in enumerate(test_data):  
    #     if n > 3:  
    #         break  
    #     predict = torch.argmax(net(x[0].view(-1, 28*28)), dim=1)  # 使用net()代替net.forward()  
    #     plt.figure(n)  
    #     plt.imshow(x[0].view(28, 28), cmap='gray')  
    #     plt.title("prediction: " + str(int(predict.item())))  # 使用.item()获取标量值  
  
    # plt.show()  
  
    # 预测单独的图像  
    image_path = "1.png"  
    if os.path.exists(image_path):
        image = Image.open(image_path).convert('L')  # 打开图像并转换为灰度
        image = transforms.ToTensor()(image)  # 转换为Tensor
        image = transforms.Resize((28, 28))(image)  # 确保图像大小为28x28
        image = image.unsqueeze(0)  # 增加batch维度
        predict = torch.argmax(net(image.view(-1, 28*28)), dim=1)  
        plt.figure(33)  
        plt.imshow(image.squeeze(0).view(28, 28), cmap='gray')  # 移除batch维度并显示  
        plt.title("prediction: " + str(int(predict.item())))  
        plt.show()
    else:
        print(f"Image file '{image_path}' not found.")

if __name__ == "__main__":  
    main()

在线写数字识别
在线写数字识别 查看案例>>
下载MNIST出错解决
下载MNIST出错解决

cmd >> python >> from torchvision.datasets import MNIST OSError: [WinError 126] 找不到指定的模块。 Error loading "C:\Python\Lib\site-packages\torch\lib\fbgemm.dll" or one of its dependencies.
出现上面错误,下载libomp140.x86_64.dl
C:\python\Lib\site-packages\torch\lib\libomp140.x86_64.dll 下载libomp140.x86_64.dll


指定的 CGI 应用程序由于未返回完整的一组 HTTP 头而产生错误行为。它实际返回的头是“Traceback (most recent call last): File "C:\wwwroot\xiyueta\article\images\python\pytorch\web.py", line 4, in import torch File "C:\python\lib\site-packages\torch\__init__.py", line 148, in raise err OSError: [WinError 126] ÕÒ²»µ½Ö¸¶¨µÄÄ£¿é¡£ Error loading "C:\python\lib\site-packages\torch\lib\fbgemm.dll" or one of its dependencies. ”。
解决:把 libomp140.x86_64.dll 放到 C:\python\lib\site-packages\torch\lib\