搭建神经网络

  1. import torch
  2. from torch import nn
  3. '''搭建神经网络'''
  4. class Model(nn.Module):
  5. def __init__(self):
  6. super(Model, self).__init__()
  7. self.model = nn.Sequential(
  8. nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
  9. nn.MaxPool2d(kernel_size=2),
  10. nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
  11. nn.MaxPool2d(kernel_size=2),
  12. nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
  13. nn.MaxPool2d(kernel_size=2),
  14. nn.Flatten(),
  15. nn.Linear(in_features=64 * 4 * 4, out_features=64),
  16. nn.Linear(in_features=64, out_features=10)
  17. )
  18. def forward(self, x):
  19. x = self.model(x)
  20. return x
  21. # 验证网络模型参数是否正确
  22. if __name__ == '__main__':
  23. model = Model()
  24. input = torch.ones((64, 3, 32, 32))
  25. output = model(input)
  26. print(output.shape)

训练和验证

  1. import torch.optim
  2. import torchvision.datasets
  3. from torch import nn
  4. from torch.utils.data import DataLoader
  5. from torch.utils.tensorboard import SummaryWriter
  6. from model_complete import Model
  7. '''准备数据集'''
  8. train_data = torchvision.datasets.CIFAR10("../dataset", train=True, transform=torchvision.transforms.ToTensor(),
  9. download=True)
  10. test_data = torchvision.datasets.CIFAR10("../dataset", train=False, transform=torchvision.transforms.ToTensor(),
  11. download=True)
  12. '''数据集长度'''
  13. train_data_size = len(train_data)
  14. test_data_size = len(test_data)
  15. print("训练数据集的长度为:{}".format(train_data_size))
  16. print("测试数据集的长度为:{}".format(test_data_size))
  17. '''利用dataloader加载数据集'''
  18. train_dataloader = DataLoader(train_data, batch_size=64)
  19. test_dataloader = DataLoader(test_data, batch_size=64)
  20. '''创建网络模型'''
  21. model = Model()
  22. '''损失函数'''
  23. loss_fn = nn.CrossEntropyLoss()
  24. '''优化器'''
  25. # learning_rate = 0.01
  26. # 1e-2=1*(10)^(-2)=0.01
  27. learning_rate = 1e-2
  28. optimizer = torch.optim.SGD(params=model.parameters(), lr=learning_rate)
  29. '''设置训练网络的一些参数'''
  30. total_train_step = 0 # 记录训练的次数
  31. total_test_step = 0 # 记录测试的次数
  32. epoch = 10 # 训练的轮数
  33. '''添加tensorboard'''
  34. writer = SummaryWriter("../logs_train")
  35. for i in range(epoch):
  36. print("---------第{}轮训练开始!---------".format(i + 1))
  37. '''训练步骤开始'''
  38. for data in train_dataloader:
  39. imgs, targets = data
  40. output = model(imgs)
  41. loss = loss_fn(output, targets) # 计算损失
  42. # 优化器优化模型
  43. optimizer.zero_grad()
  44. loss.backward()
  45. optimizer.step()
  46. total_train_step = total_train_step + 1
  47. if total_train_step % 100 == 0:
  48. print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
  49. writer.add_scalar("train_loss", loss.item(), total_train_step)
  50. '''测试步骤开始'''
  51. total_test_loss = 0
  52. total_accuracy = 0
  53. with torch.no_grad():
  54. for data in test_dataloader:
  55. imgs, targets = data
  56. outputs = model(imgs)
  57. loss = loss_fn(outputs, targets)
  58. total_test_loss = total_test_loss + loss.item()
  59. accuracy = (outputs.argmax(1) == targets).sum()
  60. total_accuracy = total_accuracy + accuracy
  61. print("整体测试集上的Loss:{}".format(total_test_loss))
  62. print("整体测试集上的正确率:{}".format(total_accuracy / test_data_size))
  63. writer.add_scalar("test_loss", total_test_loss, total_test_step)
  64. writer.add_scalar("test_accuracy", total_accuracy / test_data_size, total_test_step)
  65. total_test_step = total_test_step + 1
  66. torch.save(model, "model_{}.pth".format(i))
  67. print("模型已保存!")
  68. writer.close()

保存模型测试

从网络上下载一个狗狗的图片测试下模型。
image.png
利用Google Colab云服务器gpu训练并保存模型。
image.png
CIFAR10数据集对应的类别。
image.png
模型预测。

  1. import os
  2. import torch
  3. import torchvision
  4. from PIL import Image
  5. from torch import nn
  6. '''搭建神经网络'''
  7. class Model(nn.Module):
  8. def __init__(self):
  9. super(Model, self).__init__()
  10. self.model = nn.Sequential(
  11. nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
  12. nn.MaxPool2d(kernel_size=2),
  13. nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
  14. nn.MaxPool2d(kernel_size=2),
  15. nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
  16. nn.MaxPool2d(kernel_size=2),
  17. nn.Flatten(),
  18. nn.Linear(in_features=64 * 4 * 4, out_features=64),
  19. nn.Linear(in_features=64, out_features=10)
  20. )
  21. def forward(self, x):
  22. x = self.model(x)
  23. return x
  24. classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
  25. 'truck')
  26. # 注意gpu上训练的模型要映射到cpu上运行。
  27. model = torch.load("model_29_gpu.pth", map_location=torch.device('cpu'))
  28. # print(model)
  29. image_path = "/Users/huang/Desktop/code/learn_torch/imgs"
  30. img_list = os.listdir(image_path)
  31. print(img_list)
  32. for img in img_list:
  33. img_item_path = os.path.join(image_path, img)
  34. print(img_item_path)
  35. image = Image.open(img_item_path)
  36. image = image.convert("RGB")
  37. transform = torchvision.transforms.Compose(
  38. [torchvision.transforms.Resize((32, 32)), torchvision.transforms.ToTensor()])
  39. image = transform(image)
  40. print(image.shape)
  41. image = torch.reshape(image, (1, 3, 32, 32))
  42. model.eval()
  43. with torch.no_grad():
  44. output = model(image)
  45. # print(output)
  46. print("预测结果为:" + classes[output.argmax(1).item()])

预测结果确实为5,是狗狗!
image.png
再来一次,验证一个飞机试试。
image.png
也验证成功了!!!开心~
image.png