一、model
import torch
from torch import nn
from torchsummary import summary
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.c1 = nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5,padding=2)
self.sig = nn.Sigmoid()
self.s2 = nn.AvgPool2d(kernel_size=2,stride=2)
self.c3 = nn.Conv2d(in_channels=6,out_channels=16,kernel_size=5)
self.s4 = nn.AvgPool2d(kernel_size=2,stride=2)
self.flatten = nn.Flatten()
self.f5 = nn.Linear(400,120)
self.f6 = nn.Linear(120,84)
self.f7 = nn.Linear(84,10)
#模型前向传播
def forward(self,x):
x = self.c1(x)
x = self.sig(x)
x = self.s2(x)
x = self.c3(x)
x = self.sig(x)
x = self.s4(x) #池化
x = self.flatten(x)
x = self.f5(x)
x = self.f6(x)
x = self.f7(x)
return x
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = LeNet().to(device)
print(summary(model,(1,28,28)))
二、model_train
import copy
import time
from torchvision.datasets import FashionMNIST
import numpy as np
from torchvision import transforms
import torch.utils.data as Data
import matplotlib.pyplot as plt
from model import LeNet
import torch
import pandas as pd
def train_val_data_process():
# 数据预处理
train_data = FashionMNIST(root='./data',
train=True,
download=True,
transform=transforms.Compose([transforms.Resize(size=28),transforms.ToTensor()]))
# 数据集划分
train_data,val_data = Data.random_split(train_data,[round(0.8*len(train_data)),round(0.2*len(train_data))])
train_dataloader = Data.DataLoader(dataset=train_data,
batch_size=32,
shuffle=True, #数据打乱
num_workers=2)
val_dataloader = Data.DataLoader(dataset=val_data,
batch_size=32,
shuffle=True,
num_workers=2)
#返回数据集
return train_dataloader,val_dataloader
# 训练模型
def train_model_process(model,train_dataloader,val_dataloader,num_epochs):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#定义优化器,
optimizer = torch.optim.Adam(model.parameters(),lr=0.001)
#定义损失函数(交叉熵损失函数)
criterion = torch.nn.CrossEntropyLoss()
model = model.to(device)
#复制当前模型的参数
best_model_wts = copy.deepcopy(model.state_dict())
#初始化参数
best_acc = 0.0
#训练集与验证集损失函数列表
train_loss_all = []
val_loss_all = []
#训练集与验证集准确率列表
train_acc_all = []
val_acc_all = []
since = time.time()
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# 每个epoch都有训练集训练过程和验证集评估过程
train_loss = 0.0
train_corrects = 0
val_loss = 0.0
val_corrects = 0
# 训练集与验证集样本数量
train_num = 0
val_num = 0
for step,(b_x,b_y) in enumerate(train_dataloader):
#将特征和标签数据放入GPU中
b_x = b_x.to(device)
b_y = b_y.to(device)
#设置模型为训练模式
model.train()
#前向传播,输入为一个batch的特征数据,输出为预测的标签
output = model(b_x)
#查看每一行中最大值所在的位置,即预测的类别
pre_lab = torch.argmax(output,dim=1)
# 计算损失函数
loss = criterion(output,b_y)
#将梯度置零,因为PyTorch中梯度是累加的
optimizer.zero_grad()
#反向传播,计算梯度
loss.backward()
#根据网络反向传播的梯度更新网络参数,达到降低loss的目的
optimizer.step()
#计算训练集的损失
train_loss = train_loss + loss.item() * b_x.size(0)
train_corrects = train_corrects + torch.sum(pre_lab == b_y.data)
train_num = train_num + b_x.size(0)
for step,(b_x,b_y) in enumerate(val_dataloader):
# 将特征和标签数据放入验证设备中
b_x = b_x.to(device)
b_y = b_y.to(device)
# 设置模型为评估模式
model.eval()
# 前向传播,输入为一个batch的特征数据,输出为预测的标签
output = model(b_x)
#查看每一行中最大值所在的位置,即预测的类别
pre_lab = torch.argmax(output,dim=1)
# 计算损失函数
loss = criterion(output,b_y)
# 计算验证集的损失
val_loss = val_loss + loss.item() * b_x.size(0)
val_corrects = val_corrects + torch.sum(pre_lab == b_y.data)
val_num = val_num + b_x.size(0)
# 计算训练集与测试集每个epoch的损失和准确率
train_loss_all.append(train_loss / train_num)
train_acc_all.append(train_corrects.double() / train_num)
val_loss_all.append(val_loss / val_num)
val_acc_all.append(val_corrects.double() / val_num)
# 打印训练集和验证集准确率
print('Train Loss: {:.4f} Acc: {:.4f}'.format(train_loss_all[-1], train_acc_all[-1]))
print('Val Loss: {:.4f} Acc: {:.4f}'.format(val_loss_all[-1], val_acc_all[-1]))
if val_acc_all[-1] > best_acc: # 保存准确率最高的模型参数
best_acc = val_acc_all[-1]
#保存当前的最高准确度
best_model_wts = copy.deepcopy(model.state_dict())
#训练耗时
time_use = time.time() - since
print("训练和验证耗费的时间{:.0f}m{:.0f}s".format(time_use / 60,time_use % 60))
#选择最优的模型参数
#加载最高准确率的模型参数
# model.load_state_dict(best_model_wts)
# torch.save(model.load_state_dict(best_model_wts), 'best_model.pth')
torch.save(best_model_wts, 'best_model.pth')
train_porcess = pd.DataFrame(data = {
'epoch':range(num_epochs),
'train_loss_all':train_loss_all,
'train_acc_all':train_acc_all,
'val_loss_all':val_loss_all,
'val_acc_all':val_acc_all
})
return train_porcess
def matplot_acc_loss(train_process):
plt.figure(figsize=(12, 4))
plt.subplot(1,2,1)
plt.plot(train_process["epoch"],train_process["train_loss_all"],'ro-',label="train_loss")
plt.plot(train_process["epoch"],train_process["val_loss_all"],'bo-',label="val_loss")
plt.legend()
plt.xlabel("epoch")
plt.ylabel("loss")
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_process["epoch"], train_process["train_acc_all"], 'ro-', label="train_loss")
plt.plot(train_process["epoch"], train_process["val_acc_all"], 'bo-', label="val_loss")
plt.legend()
plt.xlabel("epoch")
plt.ylabel("acc")
plt.show()
if __name__=='__main__':
#模型实例化
LeNet = LeNet()
train_dataloader,val_dataloader = train_val_data_process()
train_porcess = train_model_process(LeNet, train_dataloader,val_dataloader,num_epochs=10)
matplot_acc_loss(train_porcess)
三、model_test
import torch
import torch.utils.data as Data
from torchvision import transforms
from torchvision.datasets import FashionMNIST
from model import LeNet
import matplotlib.pyplot as plt
import numpy as np
import os
import time
def test_data_process():
# 数据预处理
test_data = FashionMNIST(root='./data',
train=False,
download=True,
transform=transforms.Compose([transforms.Resize(size=28), transforms.ToTensor()]))
test_dataloader = Data.DataLoader(dataset=test_data,
batch_size=1,
shuffle=True, # 数据打乱
num_workers=2)
return test_dataloader
#测试过程
def test_model_process(model,test_dataloader):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#将模型放入设备中
model = model.to(device)
#初始化参数
test_corrects = 0
test_num = 0
#只进行前向传播,不计算梯度,节省内存,提高速度
with torch.no_grad():
for test_data_x,test_data_y in test_dataloader:
test_data_x = test_data_x.to(device)
test_data_y = test_data_y.to(device)
#设置模型为评估模式
model.eval()
#前向传播,输入为测试数据集,输出为每个样本的预测值
test_output = model(test_data_x)
#获取预测值中最大值对应的索引,即预测的类别
pre_lab = torch.argmax(test_output, 1) #预测标签
test_corrects += torch.sum(pre_lab == test_data_y.data) #预测正确的数量
test_num = test_num + test_data_y.size(0) #测试样本数量
#计算测试集的准确率
test_acc = test_corrects.double().item() / test_num
#打印测试集的准确率
print('Test Accuracy: {:.4f}'.format(test_acc))
if __name__ == '__main__':
model = LeNet()
#加载训练好的模型
model.load_state_dict(torch.load('best_model.pth'))
test_dataloader = test_data_process() #加载测试数据
# test_model_process(model,test_dataloader) #测试模型
#模型推理
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = model.to(device)
#将模型设置为评估模式
with torch.no_grad():
for b_x,b_y in test_dataloader:
b_x = b_x.to(device)
b_y = b_y.to(device)
#模型设为验证模型
model.eval()
output = model(b_x)
#获取预测值中最大值对应的索引,即预测的类别
pre_lab = torch.argmax(output, dim = 1) #预测标签
result = pre_lab.item()
label = b_y.item()
#打印预测结果
print('预测结果:',result,"--------","真实值:",label)