Lenet5网络基本架构包括7层网络结构(不含输入层),其中包括2个卷积层、2个降采样层(池化层)、2个全连接层和输出层。
源文件如下图:
程序1:使用pycharm在anaconda创建的虚拟环境PyTorch中实现LeNet-5网络结构
import torch
import torch.nn as nn
import torch.nn.functional as F
#these is good
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.relu = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.relu = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
#
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
# #x = x.view(-1, 16 * 5 * 5)
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
output = F.log_softmax(x, dim=1)
return output
# class LeNet5(nn.Module):
# def __init__(self):
# super(LeNet5, self).__init__()
# self.features = nn.Sequential(
# nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0),
# nn.ReLU(),
# nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
# nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0),
# nn.ReLU(),
# nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
# )
#
# self.classifier = nn.Sequential(
# nn.Linear(16 * 5 * 5, 120),
# nn.ReLU(),
# nn.Linear(120, 84),
# nn.ReLU(),
# nn.Linear(84, 10)
# )
#
#
#
# def forward(self, x):
# x = self.features(x)
# x = torch.flatten(x, 1)
# logits = self.classifier(x)
# probas = F.log_softmax(logits, dim=1)
# return probas
#原文链接:https: // blog.csdn.net / red_stone1 / article / details / 122314811
运行结果:
C:\Users\Administrator\.conda\envs\pytorch\python.exe "E:\deeplearnig\LeNet(1)\LeNet5\LeNet5.py" 进程已结束,退出代码为 0
程序2:创建自定义的数据集类 MnistDataSets
,用于加载MNIST数据集
from PIL import Image
from torch.utils.data import Dataset
class MnistDataSets(Dataset):
def __init__(self, txt_path, transform=None, target_transform=None):
print(txt_path)
fh = open(txt_path, 'r')
imgs = []
for line in fh:
line = line.rstrip()
words = line.split()
imgs.append((words[0], int(words[1])))
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
fn, label = self.imgs[index]
# img = Image.open(fn).convert('RGB')
img = Image.open(fn)
if self.transform is not None:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.imgs)
运行结果:
C:\Users\Administrator\.conda\envs\pytorch\python.exe "E:\deeplearnig\LeNet(1)\LeNet5\LeNet5.py" 进程已结束,退出代码为 0
程序3:完整的训练和测试LeNet-5模型的流程
其中注意要在源代码基础上修改路径,TrainData中包含10个文件夹,命名为0-9,每个文件夹中图像分别对应0-9的手写数字图像。
E:/deeplearnig/LeNet(1)/datasets/datasets/Mnist/train
├── 0
│ ├── image1.jpg
│ ├── image2.jpg
│ └── ...
├── 1
│ ├── image1.jpg
│ ├── image2.jpg
│ └── ...
├── 2
│ ├── image1.jpg
│ ├── image2.jpg
│ └── ...
└── 9
├── image1.jpg
├── image2.jpg
└── ...
每个数字文件夹内包含了相应数字的手写数字图像文件。使用 torchvision.datasets.ImageFolder
来加载数据集,它自动根据文件夹的名称来为每个样本赋予类别标签 。这样,train_data
和 test_data
才是训练集和测试集的 ImageFolder
数据集对象,可以直接传入 DataLoader
进行批量加载
train_data = datasets.ImageFolder('E:/deeplearnig/LeNet(1)/datasets/datasets/Mnist/train', transform=pipline_train)
test_data = datasets.ImageFolder('E:/deeplearnig/LeNet(1)/datasets/datasets/Mnist/test', transform=pipline_test)
如果出现以下错误,就需要在代码的开头添加 torchvision.datasets
的导入语句
导入语句为
import torchvision.datasets as datasets
第一次运行出现如下错误结果
表明输入张量的通道数与模型期望的输入通道数不匹配。LeNet-5模型的第一层卷积层 conv1
期望输入的通道数为1,但是我们输入张量的通道数为3。
这个问题是由于数据预处理过程中的错误导致的。在代码中,数据预处理的步骤包括将图像转换为张量并进行归一化。在这些转换操作中,我们没有正确指定图像的通道数,导致最终的张量具有3个通道而不是1个通道。
做出如下修改:将数据预处理的部分的图像转换为单通道(灰度图像)而不是多通道图像。修改后的数据预处理如下:
pipline_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=1), # 将图像转换为单通道灰度图像
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
pipline_test = transforms.Compose([
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=1), # 将图像转换为单通道灰度图像
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
总训练代码为:
import os
import time
# import torch
import torch.nn as nn
# from PIL import Image
import matplotlib.pyplot as plt
import torch.optim as optim
from torch.utils.data import Dataset
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from LeNet5 import *
from MnistDataSets import *
# from torchvision import datasets
pipline_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=1), # 将图像转换为单通道灰度图像
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
pipline_test = transforms.Compose([
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=1), # 将图像转换为单通道灰度图像
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# train_data = datasets.ImageFolder('../datasets/Mnist/train', transform=pipline_train)
# test_data = datasets.ImageFolder('../datasets/Mnist/test', transform=pipline_test)
train_data = datasets.ImageFolder('E:/deeplearnig/LeNet(1)/datasets/Mnist/train', transform=pipline_train)
test_data = datasets.ImageFolder('E:/deeplearnig/LeNet(1)/datasets/Mnist/test', transform=pipline_test)
# train_data 和test_data包含多有的训练与测试数据,调用DataLoader批量加载
trainloader = torch.utils.data.DataLoader(dataset=train_data, batch_size=8, shuffle=True)
testloader = torch.utils.data.DataLoader(dataset=test_data, batch_size=4, shuffle=False)
#创建模型,部署gpu
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = LeNet5().to(device)
#损失交叉熵损失
criterion = nn.CrossEntropyLoss()
#定义优化器
optimizer = optim.Adam(model.parameters(), lr=0.001)
def train_runner(model, device, trainloader, optimizer, epoch):
# 训练模型, 启用 BatchNormalization 和 Dropout, 将BatchNormalization和Dropout置为True
model.train()
total = 0
correct = 0.0
# enumerate迭代已加载的数据集,同时获取数据和数据下标
for i, (inputs, labels) in enumerate(trainloader):
# 把模型部署到device上
inputs, labels = inputs.to(device), labels.to(device)
# 初始化梯度
optimizer.zero_grad()
# 向前传播
outputs = model(inputs)
# 计算损失 # 多分类情况通常使用cross_entropy(交叉熵损失函数), 而对于二分类问题, 通常使用sigmod
#loss = F.cross_entropy(outputs, labels)
loss = criterion(outputs,labels)
# 获取最大概率的预测结果
# dim=1表示返回每一行的最大值对应的列下标
predict = outputs.argmax(dim=1)
total += labels.size(0)
correct += (predict == labels).sum().item()
# 反向传播
loss.backward()
# 更新参数
optimizer.step()
# Loss.append(loss.item())
# Accuracy.append(correct / total)
#if (i + 1) % 100 == 0:
# loss.item()表示当前loss的数值
print(
"Train Epoch{} \t Loss: {:.3f}, accuracy: {:.3f}%".format(epoch, loss.item(), 100 * (correct / total)))
return loss.item(), correct / total
def test_runner(model, device, testloader):
#模型验证, 必须要写, 否则只要有输入数据, 即使不训练, 它也会改变权值
#因为调用eval()将不启用 BatchNormalization 和 Dropout, BatchNormalization和Dropout置为False
model.eval()
#统计模型正确率, 设置初始值
correct = 0.0
test_loss = 0.0
total = 0
#torch.no_grad将不会计算梯度, 也不会进行反向传播
with torch.no_grad():
for data, label in testloader:
data, label = data.to(device), label.to(device)
output = model(data)
test_loss += F.cross_entropy(output, label).item()
predict = output.argmax(dim=1)
#计算正确数量
total += label.size(0)
correct += (predict == label).sum().item()
#计算损失值
print("test_avarage_loss: {:.3f}, accuracy: {:.3f}%".format(test_loss/total, 100*(correct/total)))
# 调用
epoch =80
Loss = []
Accuracy = []
for epoch in range(1, epoch + 1):
print("start_time", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
loss, acc = train_runner(model, device, trainloader, optimizer, epoch)
Loss.append(loss)
Accuracy.append(acc)
test_runner(model, device, testloader)
print("end_time: ", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '\n')
print('Finished Training')
savePath = "../models/"
if not os.path.exists(savePath):
os.mkdir(savePath)
torch.save(model, savePath+'mnist_leNet5.pth') #保存模型
plt.subplot(2, 1, 1)
plt.plot(Loss)
plt.title('Loss')
plt.subplot(2, 1, 2)
plt.plot(Accuracy)
plt.title('Accuracy')
plt.show()
训练过程(以代码段形式呈现用以区分):
C:\Users\Administrator\.conda\envs\pytorch\python.exe "E:\deeplearnig\LeNet(1)\LeNet5\Train_LeNet5.py"
start_time 2024-04-20 16:00:28
Train Epoch1 Loss: 0.001, accuracy: 92.705%
test_avarage_loss: 0.028, accuracy: 96.490%
end_time: 2024-04-20 16:03:42
start_time 2024-04-20 16:03:42
Train Epoch2 Loss: 0.001, accuracy: 96.790%
test_avarage_loss: 0.017, accuracy: 97.910%
end_time: 2024-04-20 16:04:10
start_time 2024-04-20 16:04:10
Train Epoch3 Loss: 0.006, accuracy: 97.437%
test_avarage_loss: 0.016, accuracy: 98.060%
end_time: 2024-04-20 16:04:38
start_time 2024-04-20 16:04:38
Train Epoch4 Loss: 0.000, accuracy: 97.737%
test_avarage_loss: 0.022, accuracy: 97.420%
end_time: 2024-04-20 16:05:07
start_time 2024-04-20 16:05:07
Train Epoch5 Loss: 0.184, accuracy: 97.938%
test_avarage_loss: 0.019, accuracy: 97.910%
end_time: 2024-04-20 16:05:36
start_time 2024-04-20 16:05:36
Train Epoch6 Loss: 0.712, accuracy: 98.137%
test_avarage_loss: 0.018, accuracy: 97.850%
end_time: 2024-04-20 16:06:04
start_time 2024-04-20 16:06:04
Train Epoch7 Loss: 0.219, accuracy: 98.262%
test_avarage_loss: 0.021, accuracy: 97.570%
end_time: 2024-04-20 16:06:33
start_time 2024-04-20 16:06:33
Train Epoch8 Loss: 0.958, accuracy: 98.328%
test_avarage_loss: 0.024, accuracy: 97.380%
end_time: 2024-04-20 16:07:02
start_time 2024-04-20 16:07:02
Train Epoch9 Loss: 0.002, accuracy: 98.403%
test_avarage_loss: 0.026, accuracy: 97.210%
end_time: 2024-04-20 16:07:30
start_time 2024-04-20 16:07:30
Train Epoch10 Loss: 0.034, accuracy: 98.445%
test_avarage_loss: 0.017, accuracy: 98.010%
end_time: 2024-04-20 16:07:58
start_time 2024-04-20 16:07:58
Train Epoch11 Loss: 0.000, accuracy: 98.460%
test_avarage_loss: 0.022, accuracy: 97.700%
end_time: 2024-04-20 16:08:26
start_time 2024-04-20 16:08:26
Train Epoch12 Loss: 0.005, accuracy: 98.613%
test_avarage_loss: 0.024, accuracy: 97.900%
end_time: 2024-04-20 16:08:55
start_time 2024-04-20 16:08:55
Train Epoch13 Loss: 0.004, accuracy: 98.600%
test_avarage_loss: 0.029, accuracy: 97.610%
end_time: 2024-04-20 16:09:23
start_time 2024-04-20 16:09:23
Train Epoch14 Loss: 0.000, accuracy: 98.690%
test_avarage_loss: 0.022, accuracy: 97.970%
end_time: 2024-04-20 16:09:52
start_time 2024-04-20 16:09:52
Train Epoch15 Loss: 0.108, accuracy: 98.663%
test_avarage_loss: 0.021, accuracy: 98.070%
end_time: 2024-04-20 16:10:21
start_time 2024-04-20 16:10:21
Train Epoch16 Loss: 0.001, accuracy: 98.692%
test_avarage_loss: 0.021, accuracy: 98.000%
end_time: 2024-04-20 16:10:50
start_time 2024-04-20 16:10:50
Train Epoch17 Loss: 0.016, accuracy: 98.820%
test_avarage_loss: 0.023, accuracy: 97.860%
end_time: 2024-04-20 16:11:20
start_time 2024-04-20 16:11:20
Train Epoch18 Loss: 0.000, accuracy: 98.723%
test_avarage_loss: 0.022, accuracy: 98.080%
end_time: 2024-04-20 16:11:49
start_time 2024-04-20 16:11:49
Train Epoch19 Loss: 0.001, accuracy: 98.732%
test_avarage_loss: 0.023, accuracy: 98.120%
end_time: 2024-04-20 16:12:18
start_time 2024-04-20 16:12:18
Train Epoch20 Loss: 0.000, accuracy: 98.760%
test_avarage_loss: 0.029, accuracy: 97.670%
end_time: 2024-04-20 16:12:47
start_time 2024-04-20 16:12:47
Train Epoch21 Loss: 0.000, accuracy: 98.840%
test_avarage_loss: 0.025, accuracy: 98.150%
end_time: 2024-04-20 16:13:15
start_time 2024-04-20 16:13:15
Train Epoch22 Loss: 0.000, accuracy: 98.767%
test_avarage_loss: 0.024, accuracy: 97.830%
end_time: 2024-04-20 16:13:44
start_time 2024-04-20 16:13:44
Train Epoch23 Loss: 0.000, accuracy: 98.768%
test_avarage_loss: 0.027, accuracy: 97.650%
end_time: 2024-04-20 16:14:26
start_time 2024-04-20 16:14:26
Train Epoch24 Loss: 0.091, accuracy: 98.767%
test_avarage_loss: 0.026, accuracy: 98.010%
end_time: 2024-04-20 16:15:17
start_time 2024-04-20 16:15:17
Train Epoch25 Loss: 0.000, accuracy: 98.868%
test_avarage_loss: 0.026, accuracy: 97.840%
end_time: 2024-04-20 16:15:55
start_time 2024-04-20 16:15:55
Train Epoch26 Loss: 0.000, accuracy: 98.817%
test_avarage_loss: 0.027, accuracy: 98.270%
end_time: 2024-04-20 16:16:23
start_time 2024-04-20 16:16:23
Train Epoch27 Loss: 0.104, accuracy: 98.895%
test_avarage_loss: 0.028, accuracy: 97.730%
end_time: 2024-04-20 16:16:51
start_time 2024-04-20 16:16:51
Train Epoch28 Loss: 0.003, accuracy: 98.907%
test_avarage_loss: 0.030, accuracy: 97.860%
end_time: 2024-04-20 16:17:20
start_time 2024-04-20 16:17:20
Train Epoch29 Loss: 0.000, accuracy: 98.843%
test_avarage_loss: 0.024, accuracy: 98.140%
end_time: 2024-04-20 16:17:48
start_time 2024-04-20 16:17:48
Train Epoch30 Loss: 0.302, accuracy: 98.868%
test_avarage_loss: 0.032, accuracy: 97.720%
end_time: 2024-04-20 16:18:16
start_time 2024-04-20 16:18:16
Train Epoch31 Loss: 0.007, accuracy: 98.892%
test_avarage_loss: 0.029, accuracy: 97.800%
end_time: 2024-04-20 16:18:43
start_time 2024-04-20 16:18:43
Train Epoch32 Loss: 0.006, accuracy: 98.843%
test_avarage_loss: 0.027, accuracy: 98.120%
end_time: 2024-04-20 16:19:11
start_time 2024-04-20 16:19:11
Train Epoch33 Loss: 0.019, accuracy: 98.927%
test_avarage_loss: 0.044, accuracy: 97.250%
end_time: 2024-04-20 16:19:38
start_time 2024-04-20 16:19:38
Train Epoch34 Loss: 0.164, accuracy: 98.910%
test_avarage_loss: 0.028, accuracy: 98.040%
end_time: 2024-04-20 16:20:06
start_time 2024-04-20 16:20:06
Train Epoch35 Loss: 0.031, accuracy: 98.932%
test_avarage_loss: 0.026, accuracy: 97.990%
end_time: 2024-04-20 16:20:34
start_time 2024-04-20 16:20:34
Train Epoch36 Loss: 0.326, accuracy: 98.927%
test_avarage_loss: 0.025, accuracy: 98.130%
end_time: 2024-04-20 16:21:02
start_time 2024-04-20 16:21:02
Train Epoch37 Loss: 0.008, accuracy: 98.953%
test_avarage_loss: 0.029, accuracy: 97.750%
end_time: 2024-04-20 16:21:30
start_time 2024-04-20 16:21:30
Train Epoch38 Loss: 0.115, accuracy: 99.008%
test_avarage_loss: 0.027, accuracy: 98.010%
end_time: 2024-04-20 16:21:57
start_time 2024-04-20 16:21:57
Train Epoch39 Loss: 0.000, accuracy: 98.920%
test_avarage_loss: 0.028, accuracy: 98.320%
end_time: 2024-04-20 16:22:25
start_time 2024-04-20 16:22:25
Train Epoch40 Loss: 0.000, accuracy: 99.078%
test_avarage_loss: 0.057, accuracy: 97.800%
end_time: 2024-04-20 16:22:53
start_time 2024-04-20 16:22:53
Train Epoch41 Loss: 0.000, accuracy: 98.977%
test_avarage_loss: 0.033, accuracy: 98.040%
end_time: 2024-04-20 16:23:24
start_time 2024-04-20 16:23:24
Train Epoch42 Loss: 0.000, accuracy: 98.972%
test_avarage_loss: 0.023, accuracy: 98.090%
end_time: 2024-04-20 16:23:53
start_time 2024-04-20 16:23:53
Train Epoch43 Loss: 0.000, accuracy: 99.010%
test_avarage_loss: 0.025, accuracy: 98.160%
end_time: 2024-04-20 16:24:22
start_time 2024-04-20 16:24:22
Train Epoch44 Loss: 0.000, accuracy: 99.058%
test_avarage_loss: 0.026, accuracy: 98.150%
end_time: 2024-04-20 16:24:49
start_time 2024-04-20 16:24:49
Train Epoch45 Loss: 0.001, accuracy: 99.005%
test_avarage_loss: 0.034, accuracy: 97.950%
end_time: 2024-04-20 16:25:17
start_time 2024-04-20 16:25:17
Train Epoch46 Loss: 0.003, accuracy: 99.058%
test_avarage_loss: 0.030, accuracy: 97.950%
end_time: 2024-04-20 16:25:45
start_time 2024-04-20 16:25:45
Train Epoch47 Loss: 0.000, accuracy: 99.052%
test_avarage_loss: 0.039, accuracy: 97.370%
end_time: 2024-04-20 16:26:13
start_time 2024-04-20 16:26:13
Train Epoch48 Loss: 0.000, accuracy: 99.057%
test_avarage_loss: 0.030, accuracy: 97.950%
end_time: 2024-04-20 16:26:42
start_time 2024-04-20 16:26:42
Train Epoch49 Loss: 0.000, accuracy: 99.108%
test_avarage_loss: 0.028, accuracy: 98.400%
end_time: 2024-04-20 16:27:10
start_time 2024-04-20 16:27:10
Train Epoch50 Loss: 0.000, accuracy: 99.097%
test_avarage_loss: 0.040, accuracy: 97.730%
end_time: 2024-04-20 16:27:38
start_time 2024-04-20 16:27:38
Train Epoch51 Loss: 0.000, accuracy: 99.025%
test_avarage_loss: 0.043, accuracy: 97.290%
end_time: 2024-04-20 16:28:06
start_time 2024-04-20 16:28:06
Train Epoch52 Loss: 0.000, accuracy: 99.080%
test_avarage_loss: 0.024, accuracy: 98.350%
end_time: 2024-04-20 16:28:34
start_time 2024-04-20 16:28:34
Train Epoch53 Loss: 0.000, accuracy: 99.057%
test_avarage_loss: 0.059, accuracy: 97.830%
end_time: 2024-04-20 16:29:03
start_time 2024-04-20 16:29:03
Train Epoch54 Loss: 0.000, accuracy: 99.153%
test_avarage_loss: 0.073, accuracy: 97.060%
end_time: 2024-04-20 16:29:32
start_time 2024-04-20 16:29:32
Train Epoch55 Loss: 0.003, accuracy: 99.123%
test_avarage_loss: 0.041, accuracy: 97.890%
end_time: 2024-04-20 16:30:00
start_time 2024-04-20 16:30:00
Train Epoch56 Loss: 0.008, accuracy: 99.132%
test_avarage_loss: 0.039, accuracy: 97.850%
end_time: 2024-04-20 16:30:29
start_time 2024-04-20 16:30:29
Train Epoch57 Loss: 0.000, accuracy: 99.145%
test_avarage_loss: 0.040, accuracy: 97.790%
end_time: 2024-04-20 16:30:57
start_time 2024-04-20 16:30:57
Train Epoch58 Loss: 0.000, accuracy: 99.093%
test_avarage_loss: 0.044, accuracy: 97.800%
end_time: 2024-04-20 16:31:24
start_time 2024-04-20 16:31:24
Train Epoch59 Loss: 0.000, accuracy: 99.115%
test_avarage_loss: 0.030, accuracy: 98.330%
end_time: 2024-04-20 16:31:52
start_time 2024-04-20 16:31:52
Train Epoch60 Loss: 0.010, accuracy: 99.152%
test_avarage_loss: 0.033, accuracy: 97.860%
end_time: 2024-04-20 16:32:21
start_time 2024-04-20 16:32:21
Train Epoch61 Loss: 0.012, accuracy: 99.143%
test_avarage_loss: 0.034, accuracy: 98.020%
end_time: 2024-04-20 16:32:50
start_time 2024-04-20 16:32:50
Train Epoch62 Loss: 0.000, accuracy: 99.112%
test_avarage_loss: 0.028, accuracy: 97.680%
end_time: 2024-04-20 16:33:18
start_time 2024-04-20 16:33:18
Train Epoch63 Loss: 0.000, accuracy: 99.180%
test_avarage_loss: 0.032, accuracy: 97.880%
end_time: 2024-04-20 16:33:46
start_time 2024-04-20 16:33:46
Train Epoch64 Loss: 0.001, accuracy: 99.225%
test_avarage_loss: 0.030, accuracy: 97.910%
end_time: 2024-04-20 16:34:14
start_time 2024-04-20 16:34:14
Train Epoch65 Loss: 0.000, accuracy: 99.173%
test_avarage_loss: 0.053, accuracy: 96.950%
end_time: 2024-04-20 16:34:42
start_time 2024-04-20 16:34:42
Train Epoch66 Loss: 0.000, accuracy: 99.053%
test_avarage_loss: 0.040, accuracy: 97.840%
end_time: 2024-04-20 16:35:10
start_time 2024-04-20 16:35:10
Train Epoch67 Loss: 0.030, accuracy: 99.200%
test_avarage_loss: 0.036, accuracy: 98.180%
end_time: 2024-04-20 16:35:38
start_time 2024-04-20 16:35:38
Train Epoch68 Loss: 0.000, accuracy: 99.225%
test_avarage_loss: 0.040, accuracy: 98.200%
end_time: 2024-04-20 16:36:06
start_time 2024-04-20 16:36:06
Train Epoch69 Loss: 1.754, accuracy: 99.218%
test_avarage_loss: 0.098, accuracy: 97.680%
end_time: 2024-04-20 16:36:34
start_time 2024-04-20 16:36:34
Train Epoch70 Loss: 0.000, accuracy: 99.215%
test_avarage_loss: 0.029, accuracy: 98.330%
end_time: 2024-04-20 16:37:03
start_time 2024-04-20 16:37:03
Train Epoch71 Loss: 0.000, accuracy: 99.217%
test_avarage_loss: 0.031, accuracy: 98.270%
end_time: 2024-04-20 16:37:31
start_time 2024-04-20 16:37:31
Train Epoch72 Loss: 0.000, accuracy: 99.240%
test_avarage_loss: 0.037, accuracy: 97.650%
end_time: 2024-04-20 16:37:59
start_time 2024-04-20 16:37:59
Train Epoch73 Loss: 0.000, accuracy: 99.243%
test_avarage_loss: 0.045, accuracy: 98.070%
end_time: 2024-04-20 16:38:29
start_time 2024-04-20 16:38:29
Train Epoch74 Loss: 0.000, accuracy: 99.092%
test_avarage_loss: 0.032, accuracy: 98.100%
end_time: 2024-04-20 16:39:20
start_time 2024-04-20 16:39:20
Train Epoch75 Loss: 0.290, accuracy: 99.215%
test_avarage_loss: 0.045, accuracy: 97.790%
end_time: 2024-04-20 16:39:47
start_time 2024-04-20 16:39:47
Train Epoch76 Loss: 0.002, accuracy: 99.112%
test_avarage_loss: 0.038, accuracy: 97.970%
end_time: 2024-04-20 16:40:15
start_time 2024-04-20 16:40:15
Train Epoch77 Loss: 0.000, accuracy: 99.252%
test_avarage_loss: 0.058, accuracy: 97.430%
end_time: 2024-04-20 16:40:43
start_time 2024-04-20 16:40:43
Train Epoch78 Loss: 0.002, accuracy: 99.225%
test_avarage_loss: 0.049, accuracy: 98.020%
end_time: 2024-04-20 16:41:10
start_time 2024-04-20 16:41:10
Train Epoch79 Loss: 0.000, accuracy: 99.175%
test_avarage_loss: 0.031, accuracy: 98.170%
end_time: 2024-04-20 16:41:38
start_time 2024-04-20 16:41:38
Train Epoch80 Loss: 0.000, accuracy: 99.100%
test_avarage_loss: 0.032, accuracy: 98.030%
end_time: 2024-04-20 16:42:07
Finished Training
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
……
进程已结束,退出代码为 0
图像:
程序4:交互式应用程序,用户选择一张手写数字图片,并使用已经训练好的LeNet-5模型对其进行预测。预测结果将显示在图片上方,并且该图片将以灰度图像的形式显示出来(元代码基础上添加 transforms.Grayscale(num_output_channels=1)
来将图像转换为灰度图像),总代码如下:
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
# import cv2
# import numpy as np
import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as plt
from PIL import Image
if __name__ == '__main__':
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = torch.load('../models/mnist_leNet5.pth') # 加载模型
model = model.to(device)
model.eval() # 把模型转为test模式
# 读取要预测的图片
root= tk.Tk()
root.withdraw()
fpath = filedialog.askopenfilename()
root.update()
img = Image.open(fpath) # 读取图像
#img = Image.open("../datasets/Mnist/test/3/3_78.jpg") # 读取图像
# 导入图片,图片扩展后为[1,1,32,32]
trans = transforms.Compose(
[
# 将图片尺寸resize到32x32
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=1), # 将图像转换为单通道灰度图像
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
img_org = img
img = trans(img)
img = img.to(device)
img = img.unsqueeze(0) # 图片扩展多一维,因为输入到保存的模型中是4维的[batch_size,通道,长,宽],而普通图片只有三维,[通道,长,宽]
# 预测
output = model(img)
prob = F.softmax(output, dim=1) # prob是10个分类的概率
print("概率:", prob)
#value, predicted = torch.max(output.data, 1)
predict = output.argmax(dim=1)
print("预测类别:", predict.item())
plt.imshow(img_org, cmap="gray") # 显示图片
plt.text(0.1, 0.1,predict.item(),fontsize=22, bbox=dict(facecolor='red', alpha=0.5))
plt.axis('off') # 不显示坐标轴
plt.show()
plt.cla()
# img.show()
识别图片结果:
运行结果段(以代码段形式呈现用以区分):
C:\Users\Administrator\.conda\envs\pytorch\python.exe "E:\deeplearnig\LeNet(1)\LeNet5\Test_LeNet5.py"
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
概率: tensor([[0.0000e+00, 1.0000e+00, 1.4637e-09, 0.0000e+00, 1.5891e-16, 5.8417e-31,
0.0000e+00, 3.4601e-07, 4.8586e-28, 1.7669e-27]], device='cuda:0',
grad_fn=<SoftmaxBackward0>)
预测类别: 1
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
进程已结束,退出代码为 0