搭建神经网络
import torch
from torch import nn
'''搭建神经网络'''
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Flatten(),
nn.Linear(in_features=64 * 4 * 4, out_features=64),
nn.Linear(in_features=64, out_features=10)
)
def forward(self, x):
x = self.model(x)
return x
# 验证网络模型参数是否正确
if __name__ == '__main__':
model = Model()
input = torch.ones((64, 3, 32, 32))
output = model(input)
print(output.shape)
训练和验证
import torch.optim
import torchvision.datasets
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from model_complete import Model
'''准备数据集'''
train_data = torchvision.datasets.CIFAR10("../dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10("../dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
'''数据集长度'''
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))
'''利用dataloader加载数据集'''
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
'''创建网络模型'''
model = Model()
'''损失函数'''
loss_fn = nn.CrossEntropyLoss()
'''优化器'''
# learning_rate = 0.01
# 1e-2=1*(10)^(-2)=0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(params=model.parameters(), lr=learning_rate)
'''设置训练网络的一些参数'''
total_train_step = 0 # 记录训练的次数
total_test_step = 0 # 记录测试的次数
epoch = 10 # 训练的轮数
'''添加tensorboard'''
writer = SummaryWriter("../logs_train")
for i in range(epoch):
print("---------第{}轮训练开始!---------".format(i + 1))
'''训练步骤开始'''
for data in train_dataloader:
imgs, targets = data
output = model(imgs)
loss = loss_fn(output, targets) # 计算损失
# 优化器优化模型
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_step = total_train_step + 1
if total_train_step % 100 == 0:
print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
writer.add_scalar("train_loss", loss.item(), total_train_step)
'''测试步骤开始'''
total_test_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
outputs = model(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test_loss + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
print("整体测试集上的Loss:{}".format(total_test_loss))
print("整体测试集上的正确率:{}".format(total_accuracy / test_data_size))
writer.add_scalar("test_loss", total_test_loss, total_test_step)
writer.add_scalar("test_accuracy", total_accuracy / test_data_size, total_test_step)
total_test_step = total_test_step + 1
torch.save(model, "model_{}.pth".format(i))
print("模型已保存!")
writer.close()
保存模型测试
从网络上下载一个狗狗的图片测试下模型。
利用Google Colab云服务器gpu训练并保存模型。
CIFAR10数据集对应的类别。
模型预测。
import os
import torch
import torchvision
from PIL import Image
from torch import nn
'''搭建神经网络'''
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2),
nn.Flatten(),
nn.Linear(in_features=64 * 4 * 4, out_features=64),
nn.Linear(in_features=64, out_features=10)
)
def forward(self, x):
x = self.model(x)
return x
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
'truck')
# 注意gpu上训练的模型要映射到cpu上运行。
model = torch.load("model_29_gpu.pth", map_location=torch.device('cpu'))
# print(model)
image_path = "/Users/huang/Desktop/code/learn_torch/imgs"
img_list = os.listdir(image_path)
print(img_list)
for img in img_list:
img_item_path = os.path.join(image_path, img)
print(img_item_path)
image = Image.open(img_item_path)
image = image.convert("RGB")
transform = torchvision.transforms.Compose(
[torchvision.transforms.Resize((32, 32)), torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():
output = model(image)
# print(output)
print("预测结果为:" + classes[output.argmax(1).item()])
预测结果确实为5,是狗狗!
再来一次,验证一个飞机试试。
也验证成功了!!!开心~